Catch RequestException when fetching weather

This commit is contained in:
Michal Siedlaczek 2017-04-01 09:23:32 -04:00
parent f0bacdecd8
commit d826e250c8

View file

@ -20,6 +20,7 @@ import json
import time import time
try: try:
import requests import requests
from requests.exceptions import RequestException
except ImportError: except ImportError:
pass pass
@ -50,6 +51,7 @@ class Module(bumblebee.engine.Module):
def update(self, widgets): def update(self, widgets):
timestamp = int(time.time()) timestamp = int(time.time())
if self._nextcheck < int(time.time()): if self._nextcheck < int(time.time()):
try:
self._nextcheck = int(time.time()) + self._interval*60 self._nextcheck = int(time.time()) + self._interval*60
weather_url = "http://api.openweathermap.org/data/2.5/weather?appid={}".format(self._apikey) weather_url = "http://api.openweathermap.org/data/2.5/weather?appid={}".format(self._apikey)
weather_url = "{}&units={}".format(weather_url, self._unit) weather_url = "{}&units={}".format(weather_url, self._unit)
@ -62,5 +64,7 @@ class Module(bumblebee.engine.Module):
weather_url = "{url}&q={city}".format(url=weather_url, city=self._location) weather_url = "{url}&q={city}".format(url=weather_url, city=self._location)
weather = json.loads(requests.get(weather_url).text) weather = json.loads(requests.get(weather_url).text)
self._temperature = int(weather['main']['temp']) self._temperature = int(weather['main']['temp'])
except RequestException:
pass
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4