Many fixes to the dashboard.

This commit is contained in:
Remy Moll
2021-01-03 00:33:24 +01:00
parent 8c14b546d9
commit 069c83e796
8 changed files with 270 additions and 180 deletions

View File

@@ -2,29 +2,60 @@ import requests
import bot.api.keys
import datetime
def show_weather(location):
url = "https://api.openweathermap.org/data/2.5/onecall?"
data = {"lat" : location[0], "lon" : location[1], "exclude" : "minutely,hourly", "appid" : bot.api.keys.weather_api, "units" : "metric"}
today = datetime.datetime.today().weekday()
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
class WeatherFetch():
def __init__(self):
self.last_fetch = datetime.datetime.fromtimestamp(0)
self.last_weather = ""
try:
weather = requests.get(url,params=data).json()
print("WEATHER API CALLED")
categories = {"Clouds": ":cloud:", "Rain": ":cloud_with_rain:", "Thunderstorm": "thunder_cloud_rain", "Drizzle": ":droplet:", "Snow": ":cloud_snow:", "Clear": ":sun:", "Mist": "Mist", "Smoke": "Smoke", "Haze": "Haze", "Dust": "Dust", "Fog": "Fog", "Sand": "Sand", "Dust": "Dust", "Ash": "Ash", "Squall": "Squall", "Tornado": "Tornado",}
self.url = "https://api.openweathermap.org/data/2.5/onecall?"
self.key = bot.api.keys.weather_api
now = weather["current"]
message = "<b>Now:</b> " + categories[now["weather"][0]["main"]] + "\n"
message += ":thermometer: " + str(int(now["temp"])) + "°\n\n"
def show_weather(self, location):
delta = datetime.datetime.now() - self.last_fetch
if delta.total_seconds()/60 > 60 or "\n" not in self.last_weather: # 1 hour passed:
weather_days = weather["daily"]
for i, day in enumerate(weather_days):
if i == 0:
message += "<b>" + "Today" + ":</b> " + categories[day["weather"][0]["main"]] + "\n"
else:
message += "<b>" + days[(today + i + 1) % 7] + ":</b> " + categories[day["weather"][0]["main"]] + "\n"
message += ":thermometer: :fast_down_button: " + str(int(day["temp"]["min"])) + "° , :thermometer: :fast_up_button: " + str(int(day["temp"]["max"])) + "°\n\n"
except:
return "Query failed, it's my fault, I'm sorry :sad:"
return message
data = {"lat" : location[0], "lon" : location[1], "exclude" : "minutely,hourly", "appid" : self.key, "units" : "metric"}
# today = datetime.datetime.today().weekday()
# days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
try:
weather = requests.get(self.url,params=data).json()
# categories = {"Clouds": ":cloud:", "Rain": ":cloud_with_rain:", "Thunderstorm": "thunder_cloud_rain", "Drizzle": ":droplet:", "Snow": ":cloud_snow:", "Clear": ":sun:", "Mist": "Mist", "Smoke": "Smoke", "Haze": "Haze", "Dust": "Dust", "Fog": "Fog", "Sand": "Sand", "Dust": "Dust", "Ash": "Ash", "Squall": "Squall", "Tornado": "Tornado",}
now = weather["current"]
ret_weather = []
ret_weather.append({
"short" : now["weather"][0]["main"],
"temps" : [int(now["temp"])]
})
weather_days = weather["daily"]
for i, day in enumerate(weather_days):
ret_weather.append({
"short" : day["weather"][0]["main"],
"temps" : [int(day["temp"]["min"]),int(day["temp"]["max"])]
})
except:
ret_weather = []
# now = weather["current"]
# message = "<b>Now:</b> " + categories[now["weather"][0]["main"]] + "\n"
# message += ":thermometer: " + str(int(now["temp"])) + "°\n\n"
# weather_days = weather["daily"]
# for i, day in enumerate(weather_days):
# if i == 0:
# message += "<b>" + "Today" + ":</b> " + categories[day["weather"][0]["main"]] + "\n"
# else:
# message += "<b>" + days[(today + i + 1) % 7] + ":</b> " + categories[day["weather"][0]["main"]] + "\n"
# message += ":thermometer: :fast_down_button: " + str(int(day["temp"]["min"])) + "° , :thermometer: :fast_up_button: " + str(int(day["temp"]["max"])) + "°\n\n"
# except:
# message = "Query failed, it's my fault, I'm sorry :sad:"
self.last_weather = ret_weather
self.last_fetch = datetime.datetime.now()
else:
ret_weather = self.last_weather
return ret_weather

View File

@@ -60,6 +60,7 @@ class BotFramework():
}
self.telegram = telegram.TelegramIO(self.persistence)
self.weather = weather.WeatherFetch()
def react_chats(self):
"""Checks unanswered messages and answers them"""

View File

@@ -1,6 +1,5 @@
from bot.api import telegram, google, weather, reddit
import datetime
from bot.api import telegram, google, weather, reddit
import requests
import time
@@ -90,8 +89,22 @@ class ChatBot(FW.BotFramework):
city = locations[loc.lower().replace("ü","u")]
else:
return "Couldn't find city, it might be added later though."
categories = {"Clouds": ":cloud:", "Rain": ":cloud_with_rain:", "Thunderstorm": "thunder_cloud_rain", "Drizzle": ":droplet:", "Snow": ":cloud_snow:", "Clear": ":sun:", "Mist": "Mist", "Smoke": "Smoke", "Haze": "Haze", "Dust": "Dust", "Fog": "Fog", "Sand": "Sand", "Dust": "Dust", "Ash": "Ash", "Squall": "Squall", "Tornado": "Tornado",}
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
today = datetime.datetime.today().weekday()
weather = self.weather.show_weather(city)
now = weather.pop(0)
message = "<b>Now:</b> " + categories[now["short"]] + "\n"
message += ":thermometer: " + str(now["temps"][0]) + "°\n\n"
message = weather.show_weather(city)
for i, day in enumerate(weather):
if i == 0:
message += "<b>" + "Today" + ":</b> " + categories[day["short"]] + "\n"
else:
message += "<b>" + days[(today + i + 1) % 7] + ":</b> " + categories[day["short"]] + "\n"
message += ":thermometer: :fast_down_button: " + str(day["temps"][0]) + "° , :thermometer: :fast_up_button: " + str(day["temps"][1]) + "°\n\n"
return message