From 49af9c14d62cab8d0da4f34b7dcebdd8d7535f58 Mon Sep 17 00:00:00 2001 From: Pavle Portic Date: Sun, 26 Feb 2017 02:42:27 +0100 Subject: [PATCH 1/2] [modules/weather] Add rudimentary weather module --- bumblebee/modules/weather.py | 45 ++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 bumblebee/modules/weather.py diff --git a/bumblebee/modules/weather.py b/bumblebee/modules/weather.py new file mode 100644 index 0000000..4d1726c --- /dev/null +++ b/bumblebee/modules/weather.py @@ -0,0 +1,45 @@ +# pylint: disable=C0111,R0903 + +"""Displays the temperature on the current location based on the ip + +Requires the following python packages: + * urllib + +""" + +import bumblebee.input +import bumblebee.output +import bumblebee.engine +import json +from urllib.request import urlopen + +class Module(bumblebee.engine.Module): + def __init__(self, engine, config): + super(Module, self).__init__(engine, config, + bumblebee.output.Widget(full_text=self.temperature) + ) + self._timer = 0 + self._temperature = 0 + + def temperature(self, widget): + return "{}°C".format(self._temperature) + + def update(self, widgets): + if self._timer == 0: + location_url = 'http://ipinfo.io/json' + location = json.loads(urlopen(location_url).read()) + # city = location['city'] + # country = location['country'] + coord = location['loc'].split(',') + # weather_url = 'http://api.openweathermap.org/data/2.5/weather?q={city},{country}'.format(city=city, country=country) + weather_url = 'http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid=9c672428ac92772c6437191a43a5b13f&units=metric'.format(lat=coord[0], lon=coord[1]) + weather = json.loads(urlopen(weather_url).read()) + self._temperature = weather['main']['temp'] + self._timer += 1 + return + if self._timer < 300: + self._timer += 1 + return + self._timer = 0 + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 From feb5719ecc8dff3e0cde5e2f74d1e1358ad703a2 Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Sun, 26 Feb 2017 08:07:58 +0100 Subject: [PATCH 2/2] [modules/weather] Minor reworks * Use app-specific API key for bumblebee-status * Add some parameters (location, unit, update interval) * Make interval calculation based on time, not number of calls --- bumblebee/modules/weather.py | 48 +++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/bumblebee/modules/weather.py b/bumblebee/modules/weather.py index 4d1726c..51d55c4 100644 --- a/bumblebee/modules/weather.py +++ b/bumblebee/modules/weather.py @@ -1,3 +1,4 @@ +# -*- coding: UTF-8 -*- # pylint: disable=C0111,R0903 """Displays the temperature on the current location based on the ip @@ -5,13 +6,18 @@ Requires the following python packages: * urllib +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 """ import bumblebee.input import bumblebee.output import bumblebee.engine import json -from urllib.request import urlopen +import time +from urllib import urlopen class Module(bumblebee.engine.Module): def __init__(self, engine, config): @@ -20,26 +26,40 @@ class Module(bumblebee.engine.Module): ) self._timer = 0 self._temperature = 0 + self._apikey = self.parameter("apikey", "af7bfe22287c652d032a3064ffa44088") + self._location = self.parameter("location", "auto") + self._interval = int(self.parameter("interval", "15")) + self._unit = self.parameter("unit", "metric") + self._nextcheck = 0 + + def _unit_suffix(self): + if self._unit == "metric": + return "C" + if self._unit == "kelvin": + return "K" + if self._unit == "imperial": + return "F" + return "" def temperature(self, widget): - return "{}°C".format(self._temperature) + return u"{}°{}".format(self._temperature, self._unit_suffix()) def update(self, widgets): - if self._timer == 0: - location_url = 'http://ipinfo.io/json' - location = json.loads(urlopen(location_url).read()) - # city = location['city'] - # country = location['country'] - coord = location['loc'].split(',') - # weather_url = 'http://api.openweathermap.org/data/2.5/weather?q={city},{country}'.format(city=city, country=country) - weather_url = 'http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid=9c672428ac92772c6437191a43a5b13f&units=metric'.format(lat=coord[0], lon=coord[1]) + 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(urlopen(location_url).read()) + 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(urlopen(weather_url).read()) self._temperature = weather['main']['temp'] self._timer += 1 return - if self._timer < 300: - self._timer += 1 - return - self._timer = 0 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4