diff --git a/CHANGELOG.md b/CHANGELOG.md index 83e032001001a92bb2f2f80f6176c71fc3835410..88e1417258e38598b9a4c8a2bffa1d5920d3b460 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # CHANGELOG: +## 19 aout 2019 +**Ajout:** +- script bot.py quasi complet + +--- + ## 17 aout 2019 - Script useless/quizz.py complet diff --git a/bot.py b/bot.py old mode 100644 new mode 100755 index 4973fdbc28bec6a24737c93898565814ad045d60..24051d4271ec71674c761d258e8966837ed6fc5c --- a/bot.py +++ b/bot.py @@ -1,17 +1,86 @@ #!/usr/bin/python3 +# https://fbchat.readthedocs.io/en/latest/examples.html -import fbchat +from fbchat import log, Client from getpass import getpass +from fbchat.models import * +import useless +import usefull -username = str(input("Username: ")) -client = fbchat.Client(username, getpass()) - -no_of_friends = int(input("Number of friends: ")) -for i in range(no_of_friends): - name = str(input("Name: ")) - friends = client.getUsers(name) # return a list of names - friend = friends[0] - msg = str(input("Message: ")) - sent = client.send(friend.uid, msg) - if sent: - print("Message sent successfully!") + +class Bot(Client): + def onMessage(self, author_id, message_object, thread_id, thread_type, **kwargs): + self.markAsDelivered(thread_id, message_object.uid) + self.markAsRead(thread_id) + + log.info("{} from {} in {}".format(message_object, thread_id, thread_type.name)) + + to_send = Bot.tests(message_object.text, self.fetchThreadInfo(thread_id)[thread_id].name) + if (author_id != self.uid or 'A.N.U.B.I.S. :' not in message_object.text) and to_send is not None: + self.send(Message('A.N.U.B.I.S. :' + to_send), thread_id=thread_id, thread_type=thread_type) + + @classmethod + def tests(self, texte, channel=''): + if texte == '!help': + help_text = 'Ce que je sais faire :\n \ + !meteo <ville> ; <pays> ; <dans_n_heure(facultatif)>\n \ + !wikipedia <truc_a_chercher>\n \ + !useless \n \ + !chuck \n\ + !nordpress \n \ + !quizz [start|new|reset|scoreboard] , not yet implemented\n \ + !haddock \n \ + !philo \n \ + !pipo\n \ + !kohlanta <nom>\n \ + !rateau \n \ + !maps <nom_du_lieu> <destination(facultatif)>, not yet fully fonctionnal\n \ + !about -> vas-y test-moi !\n' + return help_text + if texte == '!useless': + return useless.useless() + if texte == '!nordpress': + return useless.nordpresse() + if texte == '!chuck': + return useless.chuck() + if texte == '!philo': + return useless.genererSujet() + if texte == '!haddock': + return useless.haddock() + if texte == '!kohlanta': + texte = texte.replace('!kohlanta ', '') + return useless.kohlanta(texte, channel) + if texte == '!pipo': + return useless.pipo() + if texte == '!rateau': + return useless.getRateau() + if '!meteo' in texte: + try: + texte = texte.replace('!meteo ', '').split(' ; ') + if len(texte) == 2: + return usefull.meteo_de(texte[0], texte[1]) + else: + return usefull.meteo_de(texte[0], texte[1], dans_n_heures=texte[2]) + except: + return 'Ousp, c\'est cassé...' + if '!wikipedia' in texte: + try: + texte = texte.replace('!wikipedia ', '') + return usefull.wiki(texte) + except: + return 'Ousp, j\'ai glissé chef...' + if '!maps' in texte: + try: + usefull.maps(texte.replace('!maps ', '')) + except: + return 'Houston, on a un problème...' + if texte == '!about': + return 'Hello, je suis A.N.U.B.I.S. A Not Usefull But Intelligent System, créé par le talentueux LawiK aka Wikle aka Cyborg aka Loïc Dub que je vénère !' + return None + + +if __name__ == '__main__': + + username = str(input("Username: ")) + client = Bot(username, getpass()) + client.listen() diff --git a/requirements.txt b/requirements.txt index 28da7cbad14b287b73e0fb9dd089fe0646e9121f..625bdc8fb826ac8617b36d8f5626776e3dcda808 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,18 +1,3 @@ requests fbchat -alembic -cffi -Flask -itsdangerous -Jinja2 -Mako -MarkupSafe -pycparser -python-editor -six -Werkzeug -cycler -pyparsing -pytz -repoze.lru -Routes \ No newline at end of file +wikipedia \ No newline at end of file diff --git a/routes/__init__.py b/routes/__init__.py deleted file mode 100644 index 17512b574d8dbebc633855ea572459a8aa739b2d..0000000000000000000000000000000000000000 --- a/routes/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -from flask import Blueprint -routes = Blueprint('routes', __name__) - -from .auth import * -from .respond import * \ No newline at end of file diff --git a/routes/auth.py b/routes/auth.py deleted file mode 100644 index 1c5856e9755f88f8247badffcb963caf5a122bf6..0000000000000000000000000000000000000000 --- a/routes/auth.py +++ /dev/null @@ -1,14 +0,0 @@ -from flask import Flask, request, jsonify, Response -import json -from . import routes -import requests -from pprint import pprint -VERIFY_TOKEN = 'kameronkales' - - -@routes.route("/auth", methods=['GET']) -def handle_verification(): - if request.args['hub.verify_token'] == VERIFY_TOKEN: - return request.args['hub.challenge'] - else: - return "Invalid verification token" diff --git a/routes/respond.py b/routes/respond.py deleted file mode 100644 index 95fcf706a6b9c79247ee7ccc4604f540269fa640..0000000000000000000000000000000000000000 --- a/routes/respond.py +++ /dev/null @@ -1,26 +0,0 @@ -from flask import Flask, request, jsonify, Response -import json -from . import routes -import requests -from pprint import pprint -VERIFY_TOKEN = 'kameronkales' -ACCESS_TOKEN = 'EAAGZCfOZCdjZBwBANkL6JEMJ8BQodNR76c2HLpteN63F3phr4b9sqVjeGsapiCS1ZC5MzGJxXZA4dpLk6OHUZA0KJp1ZAnYaEijZBllkvbpnFCejFyCxj9y8nrCmjASxWTspIZAhBuEnWh3UBTOyJxGSDiZBAIxeKZBHiUeLcTBuZCkrHwZDZD' - - -def reply(user_id, msg): - data = { - "recipient": {"id": user_id}, - "message": {"text": msg} - } - url = "https://graph.facebook.com/v2.6/me/messages?access_token={}".format(ACCESS_TOKEN) - resp = requests.post(url, json=data) - print(resp.content) - - -@routes.route("/auth", methods=['POST']) -def handle_incoming_messages(): - data = request.json - sender = data['entry'][0]['messaging'][0]['sender']['id'] - message = data['entry'][0]['messaging'][0]['message']['text'] - reply(sender, message) - return "ok" diff --git a/server.py b/server.py deleted file mode 100644 index 3e74038b5c59585b50f0f5de25c261ce2cb015ef..0000000000000000000000000000000000000000 --- a/server.py +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/python3 - -from flask import Flask, request, jsonify, Response -import json -from routes import * - -app = Flask(__name__) - -app.register_blueprint(routes) - - -@app.route('/') -def hello_world(): - return 'Hello, World!' - - -if __name__ == "__main__": - app.run(debug=True, port=5000) diff --git a/usefull/__init__.py b/usefull/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..9c76126002f478a22b33baf9e5757208da1b118a --- /dev/null +++ b/usefull/__init__.py @@ -0,0 +1,4 @@ +#!/usr/bin/python3 +from .meteo import * +from .wiki import * +from .maps import * \ No newline at end of file diff --git a/usefull/__pycache__/__init__.cpython-37.pyc b/usefull/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..23f74f4439da180b01bc249f30ad42568e257e52 Binary files /dev/null and b/usefull/__pycache__/__init__.cpython-37.pyc differ diff --git a/usefull/__pycache__/maps.cpython-37.pyc b/usefull/__pycache__/maps.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..7ff05c644fec870b581acd61f5ae8a0a5e39cb07 Binary files /dev/null and b/usefull/__pycache__/maps.cpython-37.pyc differ diff --git a/usefull/__pycache__/meteo.cpython-37.pyc b/usefull/__pycache__/meteo.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6f2d537a5f008a8449ff869176f2522b999d9710 Binary files /dev/null and b/usefull/__pycache__/meteo.cpython-37.pyc differ diff --git a/usefull/__pycache__/wiki.cpython-37.pyc b/usefull/__pycache__/wiki.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4ee2698199c2ea3020085524db681d31012c29b6 Binary files /dev/null and b/usefull/__pycache__/wiki.cpython-37.pyc differ diff --git a/usefull/maps.py b/usefull/maps.py new file mode 100644 index 0000000000000000000000000000000000000000..f6ac3f2d0f24193176951f41f3cde0652f684bc5 --- /dev/null +++ b/usefull/maps.py @@ -0,0 +1,15 @@ +#!/usr/bin/python3 +''' +affiche une page maps avec un lieu rechercher + +TODO : affiche un itinéraire sur maps +''' + + +def maps(location, dest=None): + location = ("+").join(location.split(" ")) + return "https://www.google.nl/maps/place/" + location + "/& >/dev/null" + + +if __name__ == '__main__': + lieu, dest = input('entrez un lieu/une destination>').split('/') diff --git a/usefull/meteo.py b/usefull/meteo.py new file mode 100644 index 0000000000000000000000000000000000000000..fbe9808de108c492b6fcf520c8fa56f5782075b8 --- /dev/null +++ b/usefull/meteo.py @@ -0,0 +1,174 @@ +#!/usr/bin/python3 +# donne la meteo +import requests + + +def var_name(var): + """return the name of the variable var""" + for name, value in globals().items(): + if value is var and name != "i": + return name + return '?????' + + +def printv(var): + ''' + return the name of the variable var associated with the value of the var itself + exemple: + toto_is_toto = 'tata';printv(toto) returns the string : 'toto is toto : tata' + ''' + return str(var_name(var).replace("_", " ") + ' : ' + var) + + +def meteo_de(villes="petite-ile", pay="RE", all_data=True, dans_n_heures=None): + global compris, ville, pays, lattitude, longitude, température,\ + température_minimum, température_max, humidité, pression,\ + vitesse_du_vent, direction_du_vent, nuages, \ + description, volume_pluie_des_3_dernière_heures, \ + volume_neige_des_3_dernière_heures + if dans_n_heures is not None: + return forecast(villes, pay, all_data, dans_n_heures=dans_n_heures) + + pay = pay.upper()[:2] + api = "c23d0a0ffe081307fb6fbba06125aef3" + adresseid = "http://api.openweathermap.org/data/2.5/weather?q=" + requette = requests.get("{}{},{}&appid={}".format(adresseid, "%20".join(villes.split(" ")), pay, api)).json() + try: + vitesse_du_vent = str(requette["wind"]["speed"]) + " m/s" + température = str(int(requette["main"]["temp"]) - 273) + "°" + température_max = "%d °C" % (int(requette["main"]["temp_max"]) - 273) + humidité = str(requette["main"]["humidity"]) + "%" + température_minimum = "%d °C" % (int(requette["main"]["temp_min"] - 273)) + direction_du_vent = str(requette["wind"]["deg"]) + "°" + longitude = str(requette["coord"]["lon"]) + lattitude = str(requette["coord"]["lat"]) + pression = str(requette["main"]["pressure"]) + "hPa" + pays = str(requette["sys"]["country"]) + ville = str(requette["name"]) + nuages = str(requette["clouds"]["all"]) + "%" + description = str(requette["weather"][0]["description"]) + except Exception as e: + return e + + try: + volume_pluie_des_3_dernière_heures = str(requette["rain"]["3h"]) + a1 = True + except: + a1 = False + try: + volume_neige_des_3_dernière_heures = str(requette["snow"]["3h"]) + a2 = True + except: + a2 = False + + if all_data: + liste = [pays, + ville, + lattitude, longitude, + température, + température_minimum, température_max, + humidité, pression, vitesse_du_vent, + direction_du_vent, + nuages, description] + else: + liste = [pays, + ville, + # lattitude, longitude, + température, + # température_minimum, température_max, + humidité, pression, vitesse_du_vent, + # direction_du_vent, + nuages, description] + + result = ", ".join([printv(i) for i in liste]) + if a1: + result += ' ' + printv(volume_pluie_des_3_dernière_heures) + if a2: + result += ' ' + printv(volume_neige_des_3_dernière_heures) + return result + '.' + + +def forecast(villes="petite-ile", pay="RE", all_data=True, dans_n_heures=None): + global compris, ville, pays, lattitude, longitude, température,\ + temp_min, temp_max, humidité, pression,\ + vent, direction, nuages, \ + description, volume_pluie_des_3_dernière_heures, \ + volume_neige_des_3_dernière_heures, date + + pay = pay.upper()[:2] + api = "c23d0a0ffe081307fb6fbba06125aef3" + adresseid = "http://api.openweathermap.org/data/2.5/forecast?q=" + requette = requests.get("{}{},{}&appid={}".format(adresseid, "%20".join(villes.split(" ")), pay, api)).json() + result = '' + result_dans_n_heure = '' + try: + pays = str(requette["city"]["country"]) + ville = str(requette['city']["name"]) + longitude = str(requette['city']["coord"]["lon"]) + lattitude = str(requette['city']["coord"]["lat"]) + except Exception as e: + return e + + for i in range(len(requette['list'])): + try: + vent = str(requette['list'][i]["wind"]["speed"]) + " m/s" + température = str(int(requette['list'][i]['main']["temp"]) - 273) + "°" + temp_max = "%d °C" % (int(requette['list'][i]["main"]["temp_max"]) - 273) + humidité = str(requette['list'][i]["main"]["humidity"]) + "%" + temp_min = "%d °C" % (int(requette['list'][i]["main"]["temp_min"] - 273)) + direction = str(requette['list'][i]["wind"]["deg"]) + "°" + pression = str(requette['list'][i]["main"]["pressure"]) + "hPa" + nuages = str(requette['list'][i]["clouds"]["all"]) + "%" + description = str(requette['list'][i]["weather"][0]["description"]) + date = str(requette['list'][i]["dt_txt"]) + except Exception as e: + return e + + try: + volume_pluie_des_3_dernière_heures = str(requette['list'][i]["rain"]["3h"]) + a1 = True + except: + a1 = False + try: + volume_neige_des_3_dernière_heures = str(requette['list'][i]["snow"]["3h"]) + a2 = True + except: + a2 = False + + if all_data: + liste = [date, température, + temp_min, temp_max, + humidité, pression, vent, + direction, + nuages, description] + else: + liste = [date, température, + # temp_min, temp_max, + humidité, pression, vent, + # direction, + nuages, description] + + result += ", ".join([printv(i) for i in liste]) + if dans_n_heures // 3 == i: + result_dans_n_heure = ", ".join([printv(i) for i in liste]) + if a1: + result += ' ' + printv(volume_pluie_des_3_dernière_heures) + if dans_n_heures // 3 == i: + result_dans_n_heure += ' ' + printv(volume_pluie_des_3_dernière_heures) + if a2: + result += ' ' + printv(volume_neige_des_3_dernière_heures) + if dans_n_heures // 3 == i: + result_dans_n_heure += ' ' + printv(volume_neige_des_3_dernière_heures) + result += '\n\n' + if dans_n_heures is not None: + return result_dans_n_heure + return result + + +if __name__ == '__main__': + import sys + if len(sys.argv) == 3: + print(meteo_de(sys.argv[1], sys.argv[2])) + else: + print(meteo_de()) + print(forecast(dans_n_heures=24)) diff --git a/usefull/wiki.py b/usefull/wiki.py new file mode 100644 index 0000000000000000000000000000000000000000..22f1d3d5faf72ca8f42c20349c8cbcb58ee20a71 --- /dev/null +++ b/usefull/wiki.py @@ -0,0 +1,22 @@ +#!/usr/bin/python3 +# wikipedia + +import wikipedia + + +def wiki(texte, lang='fr'): + global topics + wikipedia.set_lang(lang) + topics = [] + try: + page = wikipedia.page(texte) + texte1 = page.summary + return texte1.encode().decode('utf-8').encode('cp850', 'ignore').decode('cp850') + except: + topics = wikipedia.search(texte) + texte1 = "Vous voulez dire : " + ", ".join(topics) + return texte1.encode().decode('utf-8').encode('cp850', 'ignore').decode('cp850') + + +if __name__ == '__main__': + print(wiki(input('vas-y donne un truc a chercher sr le wiki ? : '))) diff --git a/useless/__init__.py b/useless/__init__.py index da54f6aac813c3b48f1ac316af2be239f0caa1c6..f9df4fc916d4abbcfe9cb4db7ec2b63a68fe4fff 100644 --- a/useless/__init__.py +++ b/useless/__init__.py @@ -7,4 +7,5 @@ from .nordpresse import nordpresse from .philotron import genererSujet from .pipotron import pipo from .haddock import haddock -from .quizz import quizz +from .quizz import Quizz +from .kohlanta import * \ No newline at end of file diff --git a/useless/__pycache__/__init__.cpython-37.pyc b/useless/__pycache__/__init__.cpython-37.pyc index cf412b6c085ef2c5780461e4d76434a47e1bd2be..85db6bfc32a7e089403b2ee93b06ad2d82d0f9d1 100644 Binary files a/useless/__pycache__/__init__.cpython-37.pyc and b/useless/__pycache__/__init__.cpython-37.pyc differ diff --git a/useless/__pycache__/chuck.cpython-37.pyc b/useless/__pycache__/chuck.cpython-37.pyc index 46c212a0f1d915544fc769c3ebc5e87d3b17d219..44dc14e38bf4787d8a763881c918ac3d469b5e99 100644 Binary files a/useless/__pycache__/chuck.cpython-37.pyc and b/useless/__pycache__/chuck.cpython-37.pyc differ diff --git a/useless/__pycache__/haddock.cpython-37.pyc b/useless/__pycache__/haddock.cpython-37.pyc index 52a059dad3d953fafa9bbdf6e857dee7502eec79..312d204fd81cd59f6d2828c8386298088b437db0 100644 Binary files a/useless/__pycache__/haddock.cpython-37.pyc and b/useless/__pycache__/haddock.cpython-37.pyc differ diff --git a/useless/__pycache__/kohlanta.cpython-37.pyc b/useless/__pycache__/kohlanta.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..31f47bebe9f9c059a858d4630d0f359f0cfaf028 Binary files /dev/null and b/useless/__pycache__/kohlanta.cpython-37.pyc differ diff --git a/useless/__pycache__/nordpresse.cpython-37.pyc b/useless/__pycache__/nordpresse.cpython-37.pyc index 1a3b83f68f812837bde4de32d3146b6646f0f4a6..841e4898779aa0b5a638a95b177e731a1188bfb7 100644 Binary files a/useless/__pycache__/nordpresse.cpython-37.pyc and b/useless/__pycache__/nordpresse.cpython-37.pyc differ diff --git a/useless/__pycache__/philotron.cpython-37.pyc b/useless/__pycache__/philotron.cpython-37.pyc index b9bee385255e11d32884df8d1fb9bfdf620df497..47a52a610a0cc4efc1f31bf1e956cf93d46010bf 100644 Binary files a/useless/__pycache__/philotron.cpython-37.pyc and b/useless/__pycache__/philotron.cpython-37.pyc differ diff --git a/useless/__pycache__/pipotron.cpython-37.pyc b/useless/__pycache__/pipotron.cpython-37.pyc index b8a6c842328015cbffbc0380873cd10a3de433cf..77eeb132529797dd8b8545d4f5ed41438ebf8a78 100644 Binary files a/useless/__pycache__/pipotron.cpython-37.pyc and b/useless/__pycache__/pipotron.cpython-37.pyc differ diff --git a/useless/__pycache__/quizz.cpython-37.pyc b/useless/__pycache__/quizz.cpython-37.pyc index 6be4cc40a3225f6e3b69648fda439ec0f3b0ac70..35910c75677a4fcc860ecef6f6e0a8e3858c2472 100644 Binary files a/useless/__pycache__/quizz.cpython-37.pyc and b/useless/__pycache__/quizz.cpython-37.pyc differ diff --git a/useless/__pycache__/rateau.cpython-37.pyc b/useless/__pycache__/rateau.cpython-37.pyc index 9c21e82af81a396ed606abeb11499e53c49d981b..a94143de6d3cc72bb344bb86914e529a32e92797 100644 Binary files a/useless/__pycache__/rateau.cpython-37.pyc and b/useless/__pycache__/rateau.cpython-37.pyc differ diff --git a/useless/__pycache__/useless.cpython-37.pyc b/useless/__pycache__/useless.cpython-37.pyc index 491705d56e0fb86d2223ccc403cefdca977b942b..6ed1f3edad9eb53064c0a34223c48739b4cc3230 100644 Binary files a/useless/__pycache__/useless.cpython-37.pyc and b/useless/__pycache__/useless.cpython-37.pyc differ