[modules/various] Use new interval mechanism

Use generic interval mechanism in most of the modules that use slow
updates.

Only exception: getcrypto, as the interval is specified in seconds there
and I want to retain backwards-compatibility.

fixes #220
This commit is contained in:
Tobias Witek 2018-01-07 20:27:11 +01:00
parent 776be11137
commit 3638aa2420
7 changed files with 55 additions and 79 deletions

View file

@ -7,7 +7,6 @@ Requires the following python packages:
* requests
Parameters:
* weather.interval: Interval (in minutes) for updating weather information
* weather.location: Set location (ISO 3166 country code), defaults to 'auto' for getting location from http://ipinfo.io
* weather.unit: metric (default), kelvin, imperial
* weather.apikey: API key from http://api.openweathermap.org
@ -18,7 +17,6 @@ import bumblebee.output
import bumblebee.engine
import re
import json
import time
try:
import requests
from requests.exceptions import RequestException
@ -34,10 +32,9 @@ class Module(bumblebee.engine.Module):
self._apikey = self.parameter("apikey", "af7bfe22287c652d032a3064ffa44088")
self._location = self.parameter("location", "auto")
self._city = self.parameter("location", "")
self._interval = int(self.parameter("interval", "15"))
self._unit = self.parameter("unit", "metric")
self._nextcheck = 0
self._valid = False
self.interval(15)
def _unit_suffix(self):
if self._unit == "metric":
@ -82,27 +79,24 @@ class Module(bumblebee.engine.Module):
return []
def update(self, widgets):
timestamp = int(time.time())
if self._nextcheck < timestamp:
try:
self._nextcheck = timestamp + 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(",")
self._city = location["city"]
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'])
self._weather = weather['weather'][0]['main'].lower()
self._valid = True
except RequestException:
self._valid = False
except Exception:
self._valid = False
try:
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(",")
self._city = location["city"]
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'])
self._weather = weather['weather'][0]['main'].lower()
self._valid = True
except RequestException:
self._valid = False
except Exception:
self._valid = False
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4