2017-02-26 08:07:58 +01:00
|
|
|
# -*- coding: UTF-8 -*-
|
2017-02-26 02:42:27 +01:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Displays the temperature on the current location based on the ip
|
|
|
|
|
|
|
|
Requires the following python packages:
|
2017-02-26 08:19:30 +01:00
|
|
|
* requests
|
2017-02-26 02:42:27 +01:00
|
|
|
|
2017-02-26 08:07:58 +01:00
|
|
|
Parameters:
|
2019-05-20 20:10:39 +02:00
|
|
|
* weather.location: Set location, defaults to 'auto' for getting location from http://ipinfo.io
|
|
|
|
If set to a comma-separated list, left-click and right-click can be used to rotate the locations.
|
|
|
|
Locations should be city names or city ids.
|
2017-02-26 08:07:58 +01:00
|
|
|
* weather.unit: metric (default), kelvin, imperial
|
2019-02-11 20:07:10 +01:00
|
|
|
* weather.showcity: If set to true, show location information, otherwise hide it (defaults to true)
|
2019-12-24 13:50:18 +01:00
|
|
|
* weather.showminmax: If set to true, show the minimum and maximum temperature, otherwise hide it (defaults to false)
|
2017-02-26 14:17:26 +01:00
|
|
|
* weather.apikey: API key from http://api.openweathermap.org
|
2017-02-26 02:42:27 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
import bumblebee.input
|
|
|
|
import bumblebee.output
|
|
|
|
import bumblebee.engine
|
2017-09-29 14:31:42 +02:00
|
|
|
import re
|
2018-10-31 16:26:02 +01:00
|
|
|
|
2017-03-26 03:32:11 +02:00
|
|
|
try:
|
|
|
|
import requests
|
2017-04-01 15:23:32 +02:00
|
|
|
from requests.exceptions import RequestException
|
2017-03-26 03:32:11 +02:00
|
|
|
except ImportError:
|
|
|
|
pass
|
2017-02-26 02:42:27 +01:00
|
|
|
|
|
|
|
class Module(bumblebee.engine.Module):
|
|
|
|
def __init__(self, engine, config):
|
|
|
|
super(Module, self).__init__(engine, config,
|
2017-09-29 14:31:42 +02:00
|
|
|
bumblebee.output.Widget(full_text=self.output)
|
2017-02-26 02:42:27 +01:00
|
|
|
)
|
|
|
|
self._temperature = 0
|
2017-02-26 08:07:58 +01:00
|
|
|
self._apikey = self.parameter("apikey", "af7bfe22287c652d032a3064ffa44088")
|
|
|
|
self._location = self.parameter("location", "auto")
|
2019-02-09 13:10:35 +01:00
|
|
|
if "," in self._location:
|
|
|
|
self._location = self._location.split(",")
|
|
|
|
else:
|
|
|
|
self._location = [self._location]
|
|
|
|
self._index = 0
|
2018-11-02 10:04:43 +01:00
|
|
|
self._showcity = bumblebee.util.asbool(self.parameter("showcity", True))
|
2019-12-24 13:50:18 +01:00
|
|
|
self._showminmax = bumblebee.util.asbool(self.parameter("showminmax", False))
|
2017-02-26 08:07:58 +01:00
|
|
|
self._unit = self.parameter("unit", "metric")
|
2017-04-01 16:53:39 +02:00
|
|
|
self._valid = False
|
2019-10-29 20:20:19 +01:00
|
|
|
self.interval_factor(60)
|
2018-01-07 20:27:11 +01:00
|
|
|
self.interval(15)
|
2017-02-26 08:07:58 +01:00
|
|
|
|
2019-02-09 13:10:35 +01:00
|
|
|
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd=self._next_location)
|
|
|
|
engine.input.register_callback(self, button=bumblebee.input.RIGHT_MOUSE, cmd=self._prev_location)
|
|
|
|
|
|
|
|
def _next_location(self, event):
|
|
|
|
self._index = 0 if self._index >= len(self._location) - 1 else self._index + 1
|
|
|
|
self.update(self.widgets())
|
|
|
|
|
|
|
|
def _prev_location(self, event):
|
|
|
|
self._index = len(self._location)-1 if self._index <= 0 else self._index - 1
|
|
|
|
self.update(self.widgets())
|
|
|
|
|
2017-02-26 08:07:58 +01:00
|
|
|
def _unit_suffix(self):
|
|
|
|
if self._unit == "metric":
|
|
|
|
return "C"
|
|
|
|
if self._unit == "kelvin":
|
|
|
|
return "K"
|
|
|
|
if self._unit == "imperial":
|
|
|
|
return "F"
|
|
|
|
return ""
|
2017-02-26 02:42:27 +01:00
|
|
|
|
2017-09-29 14:31:42 +02:00
|
|
|
def temperature(self):
|
|
|
|
return u"{}°{}".format(self._temperature, self._unit_suffix())
|
|
|
|
|
2019-12-24 13:50:18 +01:00
|
|
|
def tempmin(self):
|
|
|
|
return u"{}°{}".format(self._tempmin, self._unit_suffix())
|
|
|
|
|
|
|
|
def tempmax(self):
|
|
|
|
return u"{}°{}".format(self._tempmax, self._unit_suffix())
|
|
|
|
|
2017-09-29 14:31:42 +02:00
|
|
|
def city(self):
|
2019-02-11 20:07:10 +01:00
|
|
|
city = re.sub('[_-]', ' ', self._city)
|
2019-02-09 13:10:35 +01:00
|
|
|
return u"{} ".format(city)
|
2017-09-29 14:31:42 +02:00
|
|
|
|
|
|
|
def output(self, widget):
|
2017-04-01 16:53:39 +02:00
|
|
|
if not self._valid:
|
|
|
|
return u"?"
|
2019-12-24 13:50:18 +01:00
|
|
|
if self._showminmax:
|
|
|
|
self._showcity=False
|
|
|
|
return self.city() + self.temperature() + " Hi:" + self.tempmax() + " Lo:" + self.tempmin()
|
|
|
|
elif self._city:
|
2018-11-01 22:15:00 +01:00
|
|
|
return self.city() + self.temperature()
|
2018-11-02 10:04:43 +01:00
|
|
|
else:
|
|
|
|
return self.temperature()
|
2017-02-26 02:42:27 +01:00
|
|
|
|
2017-10-13 17:06:18 +02:00
|
|
|
def state(self, widget):
|
2017-06-13 04:26:33 +02:00
|
|
|
if self._valid:
|
|
|
|
if "thunderstorm" in self._weather:
|
2017-10-13 17:06:18 +02:00
|
|
|
return ['thunder']
|
2017-06-13 04:26:33 +02:00
|
|
|
elif "drizzle" in self._weather:
|
2017-10-13 17:06:18 +02:00
|
|
|
return ['rain']
|
2017-06-13 04:26:33 +02:00
|
|
|
elif "rain" in self._weather:
|
2017-10-13 17:06:18 +02:00
|
|
|
return ['rain']
|
2017-06-13 04:26:33 +02:00
|
|
|
elif "snow" in self._weather:
|
2017-10-13 17:06:18 +02:00
|
|
|
return ['snow']
|
2017-06-13 04:26:33 +02:00
|
|
|
elif "sleet" in self._weather:
|
2017-10-13 17:06:18 +02:00
|
|
|
return ['sleet']
|
2017-06-13 04:26:33 +02:00
|
|
|
elif "clear" in self._weather:
|
2017-10-13 17:06:18 +02:00
|
|
|
return ['clear']
|
2017-06-13 04:26:33 +02:00
|
|
|
elif "cloud" in self._weather:
|
2017-10-13 17:06:18 +02:00
|
|
|
return ['clouds']
|
2017-06-13 04:26:33 +02:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
2017-02-26 02:42:27 +01:00
|
|
|
def update(self, widgets):
|
2018-01-07 20:27:11 +01:00
|
|
|
try:
|
|
|
|
weather_url = "http://api.openweathermap.org/data/2.5/weather?appid={}".format(self._apikey)
|
|
|
|
weather_url = "{}&units={}".format(weather_url, self._unit)
|
2019-02-09 13:10:35 +01:00
|
|
|
if self._location[self._index] == "auto":
|
2018-01-07 20:27:11 +01:00
|
|
|
location_url = "http://ipinfo.io/json"
|
2018-10-31 16:26:02 +01:00
|
|
|
location = requests.get(location_url).json()
|
2018-01-07 20:27:11 +01:00
|
|
|
coord = location["loc"].split(",")
|
|
|
|
weather_url = "{url}&lat={lat}&lon={lon}".format(url=weather_url, lat=coord[0], lon=coord[1])
|
2019-05-20 20:10:39 +02:00
|
|
|
elif self._location[self._index].isdigit():
|
|
|
|
weather_url = "{url}&id={id}".format(url=weather_url, id=self._location[self._index])
|
2018-01-07 20:27:11 +01:00
|
|
|
else:
|
2019-02-09 13:10:35 +01:00
|
|
|
weather_url = "{url}&q={city}".format(url=weather_url, city=self._location[self._index])
|
2018-10-31 16:26:02 +01:00
|
|
|
weather = requests.get(weather_url).json()
|
2019-05-20 20:10:39 +02:00
|
|
|
self._city = weather['name']
|
2018-01-07 20:27:11 +01:00
|
|
|
self._temperature = int(weather['main']['temp'])
|
2019-12-24 13:50:18 +01:00
|
|
|
self._tempmin = int(weather['main']['temp_min'])
|
|
|
|
self._tempmax = int(weather['main']['temp_max'])
|
2018-01-07 20:27:11 +01:00
|
|
|
self._weather = weather['weather'][0]['main'].lower()
|
|
|
|
self._valid = True
|
|
|
|
except Exception:
|
|
|
|
self._valid = False
|
2017-02-26 02:42:27 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|