From d826e250c80f1f170e376652db81c600aaa03b95 Mon Sep 17 00:00:00 2001 From: Michal Siedlaczek Date: Sat, 1 Apr 2017 09:23:32 -0400 Subject: [PATCH] Catch RequestException when fetching weather --- bumblebee/modules/weather.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/bumblebee/modules/weather.py b/bumblebee/modules/weather.py index 4d0416e..fb472f0 100644 --- a/bumblebee/modules/weather.py +++ b/bumblebee/modules/weather.py @@ -20,6 +20,7 @@ import json import time try: import requests + from requests.exceptions import RequestException except ImportError: pass @@ -50,17 +51,20 @@ class Module(bumblebee.engine.Module): def update(self, widgets): timestamp = int(time.time()) if self._nextcheck < int(time.time()): - self._nextcheck = int(time.time()) + self._interval*60 - weather_url = "http://api.openweathermap.org/data/2.5/weather?appid={}".format(self._apikey) - weather_url = "{}&units={}".format(weather_url, self._unit) - if self._location == "auto": - location_url = "http://ipinfo.io/json" - location = json.loads(requests.get(location_url).text) - coord = location["loc"].split(",") - weather_url = "{url}&lat={lat}&lon={lon}".format(url=weather_url, lat=coord[0], lon=coord[1]) - else: - weather_url = "{url}&q={city}".format(url=weather_url, city=self._location) - weather = json.loads(requests.get(weather_url).text) - self._temperature = int(weather['main']['temp']) + try: + self._nextcheck = int(time.time()) + self._interval*60 + weather_url = "http://api.openweathermap.org/data/2.5/weather?appid={}".format(self._apikey) + weather_url = "{}&units={}".format(weather_url, self._unit) + if self._location == "auto": + location_url = "http://ipinfo.io/json" + location = json.loads(requests.get(location_url).text) + coord = location["loc"].split(",") + weather_url = "{url}&lat={lat}&lon={lon}".format(url=weather_url, lat=coord[0], lon=coord[1]) + else: + weather_url = "{url}&q={city}".format(url=weather_url, city=self._location) + weather = json.loads(requests.get(weather_url).text) + self._temperature = int(weather['main']['temp']) + except RequestException: + pass # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4