Commit d9fa575a authored by Otthorn's avatar Otthorn 🤔

added weather module and help about it

parent cba241d6
......@@ -8,3 +8,4 @@
- [ ] fonction !music pour changer la musique à la med
- [ ] fonction qui renvoie les menus pour les commandes de nourritures à la med
- [ ] module pour les citations
- [ ] module qui intégarit avec la BDD de la Med
......@@ -5,7 +5,7 @@ from matrix_bot_api.mregex_handler import MRegexHandler
from matrix_bot_api.mcommand_handler import MCommandHandler
from giphy_api import gif_search
from weather_api import query_weather
from parse_env import parse_env
# Parse the global variables from the env
......@@ -63,21 +63,26 @@ def help_callback(room, event):
<li><em>!d<strong>N</strong></em> donne le résultat d'un dé à <strong>N</strong> faces;</li>\n\
<li><em>!echo</em> <strong>X</strong> renvoie un message contenant <strong>X</strong>;</li>\n\
<li><em>!weather</em> <strong>L</strong> renvoie la météo pour l'emplacement <strong>L</strong>;</li>\n\
<li><em>!gif</em> <strong>X</strong> renvoie un gif correspondant à la recherche <strong>X</strong>.</li>\n</ul>\n")
elif args[1] == ("d" or "dice" or "die" or "dés" or "dé" or "dN" or "!dN"):
elif args[1] in ("d", "dice", "die", "dés", "dé", "dN", "!dN"):
room.send_html("<em>!d<strong>N</strong></em> est une commande pour lancer un dé à <strong>N</strong> face(s). <strong>N</strong> entre 1 et 1000.<br /><em>Exemple</em> : <code>!d20</code> pour lancer un dé à 20 faces.")
elif args[1] == ("gif" or "!gif"):
elif args[1] in ("gif", "!gif"):
room.send_html("<em>!gif</em> <strong>X</strong> permet d'afficher un gif correspondant à la recherche <strong>X</strong>.<br /><em>Exemple</em> : <code>!gif suhsi</code> renvera un gif avec des suhsi !")
elif args[1] == ("echo" or "!echo"):
elif args[1] in ("echo", "!echo"):
room.send_html("<em>!echo</em> <strong>X</strong> renvoie un message contenant <strong>X</strong>.<br /><em>Exemple</em> : <code>!echo Med</code> renverra le message <code>Med</code>.")
elif args[1] in ("meteo", "weather", "!weather"):
room.send_html("<em>!weather</em> <strong>L</strong> renvoie la météo pour l'emplacement <strong>L</strong>. On peut spécifier soit <code>ville</code> soit <code>ville, pays</code>.<br /><em>Exemple</em> : <code>!weather Cachan</code> ou bien <code>!weather newcastle, gb</code>.")
......@@ -86,7 +91,7 @@ def gif_callback(room, event):
args = event['content']['body'].split()
query = " ".join(args[1:])
print("gif called with query={} in room={}".format(query, room.display_name))
print("GIF called with query={} in room={}".format(query, room.display_name))
data, mime = gif_search(query)
mxc_url = bot.upload_to_server(data, mime)
......@@ -94,6 +99,18 @@ def gif_callback(room, event):
room.send_image(mxc_url, query, mimetype=mime)
def weather_callback(room, event):
args = event['content']['body'].split()
query = " ".join(args[1:])
print("WEATHER called with query={} in room={}".format(query, room.display_name))
summary = query_weather(query)
def main():
# Create an instance of the MatrixBotAPI
......@@ -116,6 +133,9 @@ def main():
gif_handler = MCommandHandler("gif", gif_callback)
weather_handler = MCommandHandler("weather", weather_callback)
# Start polling
Traceback (most recent call last):
File "", line 125, in <module>
File "", line 122, in main
OSError: [Errno 9] Bad file descriptor
import requests
from parse_env import parse_env
appid = parse_env("weather.env")['appid']
def query_weather(query, api_key=appid):
url = "{}&q={}".format(appid, query)
json_data = requests.get(url).json()
# check if we joined the api correctly
code = json_data['cod']
if str(code) != "200": # str(code) par l'api renvoie parfois des int et parfois des str
return "Internal error " + code
# import all of the variables
weather_main = json_data['weather'][0]['main']
weather_description = json_data['weather'][0]['description']
temp = kelvin_to_celsius(json_data['main']['temp'])
pressure = json_data['main']['pressure']
humidity = json_data['main']['humidity']
temp_min = kelvin_to_celsius(json_data['main']['temp_min'])
temp_max = kelvin_to_celsius(json_data['main']['temp_max'])
wind_speed = json_data['wind']['speed']
clouds = json_data['clouds']['all']
country = json_data['sys']['country']
name = json_data['name']
summary = """ {}, {} : {}
{:.1f}°C ({:.1f}°C - {:.1f}°C), wind {} m/s, clouds {}%, {} hPa""".format(name, country,
weather_description, temp, temp_min, temp_max, wind_speed, clouds, pressure)
return summary
def kelvin_to_celsius(T):
return float(T) - 273
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment