From af839dd842c31856367b4dd29332303c139070a6 Mon Sep 17 00:00:00 2001 From: Remy Moll Date: Thu, 10 Dec 2020 11:20:00 +0100 Subject: [PATCH] Fixing weather api issues --- bot/api/weather.py | 32 +++++++++++++++----------------- bot/main.py | 12 +++++++++--- clock/api/converter.py | 4 ++++ clock_wrapper.py | 24 ++++++++++++++---------- launch.sh | 2 +- persistence/log.txt | 0 persistence/rw.py | 10 ++++++++++ 7 files changed, 53 insertions(+), 31 deletions(-) create mode 100644 persistence/log.txt diff --git a/bot/api/weather.py b/bot/api/weather.py index 2e6f90b..c547dce 100644 --- a/bot/api/weather.py +++ b/bot/api/weather.py @@ -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 = "Now: " + 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 += "" + "Today" + ": " + categories[day["weather"][0]["main"]] + "\n" + else: + message += "" + days[(today + i + 1) % 7] + ": " + 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 = "Now: " + 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 += "" + "Today" + ": " + categories[day["weather"][0]["main"]] + "\n" - else: - message += "" + days[(today + i + 1) % 7] + ": " + 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 diff --git a/bot/main.py b/bot/main.py index 0d8808c..11b20c7 100644 --- a/bot/main.py +++ b/bot/main.py @@ -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" diff --git a/clock/api/converter.py b/clock/api/converter.py index d4f56b4..c25f93e 100644 --- a/clock/api/converter.py +++ b/clock/api/converter.py @@ -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): diff --git a/clock_wrapper.py b/clock_wrapper.py index 5c26a87..b206123 100644 --- a/clock_wrapper.py +++ b/clock_wrapper.py @@ -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("")+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("")+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": diff --git a/launch.sh b/launch.sh index 635b0b5..2c9cbe0 100644 --- a/launch.sh +++ b/launch.sh @@ -2,4 +2,4 @@ sleep 30 cd /home/pi/AIO -sudo python3 launcher.py +sudo python3 launcher.py > persistence/log.txt diff --git a/persistence/log.txt b/persistence/log.txt new file mode 100644 index 0000000..e69de29 diff --git a/persistence/rw.py b/persistence/rw.py index faaf993..214d317 100644 --- a/persistence/rw.py +++ b/persistence/rw.py @@ -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 \ No newline at end of file