Fixing weather api issues
This commit is contained in:
		| @@ -10,23 +10,21 @@ def show_weather(location): | |||||||
|  |  | ||||||
|     try: |     try: | ||||||
|         weather = requests.get(url,params=data).json() |         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: |     except: | ||||||
|         return "Query failed, it's my fault, I'm sorry :sad:" |         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 |     return message | ||||||
|   | |||||||
							
								
								
									
										12
									
								
								bot/main.py
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								bot/main.py
									
									
									
									
									
								
							| @@ -237,11 +237,17 @@ class ChatBot(): | |||||||
|  |  | ||||||
|  |  | ||||||
|     def bot_print_log(self, *args): |     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: |         if "clear" in args: | ||||||
|             self.persistence.write("log",[]) |             self.persistence.write("log",[]) | ||||||
|             self.telegram.send_message("Log cleared") |             return "Log cleared" | ||||||
|             return |         elif "system" in args: | ||||||
|  |             send_text = self.persistence.read_ext_file("log.txt") | ||||||
|  |             return send_text | ||||||
|  |              | ||||||
|         send_text = "" |         send_text = "" | ||||||
|         for event in self.persistence.read("log"): |         for event in self.persistence.read("log"): | ||||||
|             send_text += event + "\n" |             send_text += event + "\n" | ||||||
|   | |||||||
| @@ -30,6 +30,7 @@ digits = { | |||||||
|     "0" : [[1,1,1],[1,0,1],[1,0,1],[1,0,1],[1,1,1]], |     "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]], |     "-" : [[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]], |     "-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 | ##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): |     if ("-" in top and len(top) > 2) or ("-" in bottom and len(bottom) > 2): | ||||||
|         time_split = 4*["-"] |         time_split = 4*["-"] | ||||||
|  |     elif "error" in top and "error" in bottom: | ||||||
|  |         time_split = 4*["error"] | ||||||
|     else: |     else: | ||||||
|         time_split = [i for i in top] + [i for i in bottom] |         time_split = [i for i in top] + [i for i in bottom] | ||||||
|  |  | ||||||
| @@ -102,6 +105,7 @@ weather_categories = { | |||||||
|     "Ash": "Ash", |     "Ash": "Ash", | ||||||
|     "Squal": "Squal", |     "Squal": "Squal", | ||||||
|     "Tornado": "Tornado", |     "Tornado": "Tornado", | ||||||
|  |     "error" : "moon" | ||||||
| } | } | ||||||
|  |  | ||||||
| def weather_converter(name): | def weather_converter(name): | ||||||
|   | |||||||
| @@ -28,17 +28,21 @@ class ClockWrapper(wrapper.Wrapper): | |||||||
|                     # fetch new weather every 3 hours (hard coded) |                     # fetch new weather every 3 hours (hard coded) | ||||||
|                     prev_weather_time = datetime.datetime.now() |                     prev_weather_time = datetime.datetime.now() | ||||||
|                     weather = self.others[0].bot_show_weather("zurich") |                     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 (":","") |                         temps_today = weather.splitlines()[4] | ||||||
|                     # current weather situation (icon): we pick the first line, remove the start string, remove :: indicating an emoji |                         low = temps_today[temps_today.find("button")+8:temps_today.find("°")] | ||||||
|  |                         temps_today = temps_today[temps_today.find("°") + 1:] | ||||||
|                     temps_today = weather.splitlines()[4] |                         high = temps_today[temps_today.find("button")+8:temps_today.find("°")] | ||||||
|                     low = temps_today[temps_today.find("button")+8:temps_today.find("°")] |                         self.weather["weather"] = l1 | ||||||
|                     temps_today = temps_today[temps_today.find("°") + 1:] |                         self.weather["high"] = high | ||||||
|                     high = temps_today[temps_today.find("button")+8:temps_today.find("°")] |                         self.weather["low"] = low | ||||||
|                     self.weather["weather"] = l1 |                     else: | ||||||
|                     self.weather["high"] = high |                         self.weather["weather"] = "error" | ||||||
|                     self.weather["low"] = low |                         self.weather["high"] = "error" | ||||||
|  |                         self.weather["low"] = "error" | ||||||
|  |  | ||||||
|                 if mins_elapsed % 5 == 0: |                 if mins_elapsed % 5 == 0: | ||||||
|                     if self.weather["show"] == "weather": |                     if self.weather["show"] == "weather": | ||||||
|   | |||||||
| @@ -2,4 +2,4 @@ | |||||||
|  |  | ||||||
| sleep 30 | sleep 30 | ||||||
| cd /home/pi/AIO | cd /home/pi/AIO | ||||||
| sudo python3 launcher.py | sudo python3 launcher.py > persistence/log.txt | ||||||
|   | |||||||
							
								
								
									
										0
									
								
								persistence/log.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								persistence/log.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -71,3 +71,13 @@ class Variables(): | |||||||
|         pre = self.read(name) |         pre = self.read(name) | ||||||
|         pre.append(value) |         pre.append(value) | ||||||
|         self.write(name, pre) |         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 | ||||||
		Reference in New Issue
	
	Block a user
	 Remy Moll
					Remy Moll