Fixing weather api issues

This commit is contained in:
Remy Moll 2020-12-10 11:20:00 +01:00
parent 7debff33ab
commit af839dd842
7 changed files with 53 additions and 31 deletions

View File

@ -10,23 +10,21 @@ def show_weather(location):
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",}
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:
return "Query failed, it's my fault, I'm sorry :sad:"
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"]
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"
return message

View File

@ -237,11 +237,17 @@ class ChatBot():
def bot_print_log(self, *args):
"""Shows an error-log, mostly of bad api-requests"""
"""Shows an error-log, mostly of bad api-requests. Usage
log clear - clears log
log system - shows python output"""
if "clear" in args:
self.persistence.write("log",[])
self.telegram.send_message("Log cleared")
return
return "Log cleared"
elif "system" in args:
send_text = self.persistence.read_ext_file("log.txt")
return send_text
send_text = ""
for event in self.persistence.read("log"):
send_text += event + "\n"

View File

@ -30,6 +30,7 @@ digits = {
"0" : [[1,1,1],[1,0,1],[1,0,1],[1,0,1],[1,1,1]],
"-" : [[0,0,0],[0,0,0],[1,1,1],[0,0,0],[0,0,0]],
"-1" : [[0,0,1],[0,0,1],[1,1,1],[0,0,1],[0,0,1]],
"error" : [[1,0,1],[1,0,1],[0,1,0],[1,0,1],[1,0,1]],
}
##place of numbers, invariant
@ -52,6 +53,8 @@ def time_converter(top="", bottom=""):
if ("-" in top and len(top) > 2) or ("-" in bottom and len(bottom) > 2):
time_split = 4*["-"]
elif "error" in top and "error" in bottom:
time_split = 4*["error"]
else:
time_split = [i for i in top] + [i for i in bottom]
@ -102,6 +105,7 @@ weather_categories = {
"Ash": "Ash",
"Squal": "Squal",
"Tornado": "Tornado",
"error" : "moon"
}
def weather_converter(name):

View File

@ -28,17 +28,21 @@ class ClockWrapper(wrapper.Wrapper):
# fetch new weather every 3 hours (hard coded)
prev_weather_time = datetime.datetime.now()
weather = self.others[0].bot_show_weather("zurich")
if not (sad in weather):
l1 = weather[weather.find("</b>")+5:weather.find("\n")].replace (":","")
# current weather situation (icon): we pick the first line, remove the start string, remove :: indicating an emoji
l1 = weather[weather.find("</b>")+5:weather.find("\n")].replace (":","")
# current weather situation (icon): we pick the first line, remove the start string, remove :: indicating an emoji
temps_today = weather.splitlines()[4]
low = temps_today[temps_today.find("button")+8:temps_today.find("°")]
temps_today = temps_today[temps_today.find("°") + 1:]
high = temps_today[temps_today.find("button")+8:temps_today.find("°")]
self.weather["weather"] = l1
self.weather["high"] = high
self.weather["low"] = low
temps_today = weather.splitlines()[4]
low = temps_today[temps_today.find("button")+8:temps_today.find("°")]
temps_today = temps_today[temps_today.find("°") + 1:]
high = temps_today[temps_today.find("button")+8:temps_today.find("°")]
self.weather["weather"] = l1
self.weather["high"] = high
self.weather["low"] = low
else:
self.weather["weather"] = "error"
self.weather["high"] = "error"
self.weather["low"] = "error"
if mins_elapsed % 5 == 0:
if self.weather["show"] == "weather":

View File

@ -2,4 +2,4 @@
sleep 30
cd /home/pi/AIO
sudo python3 launcher.py
sudo python3 launcher.py > persistence/log.txt

0
persistence/log.txt Normal file
View File

View File

@ -71,3 +71,13 @@ class Variables():
pre = self.read(name)
pre.append(value)
self.write(name, pre)
def read_ext_file(self, path):
"""returns content of given file"""
if not os.path.exists(path):
return "File does not exist"
file = os.open(path,"r")
content = os.read(file)
os.close(file)
return content