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:
|
|
|
|
* 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
|
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
|
2017-02-26 02:42:27 +01:00
|
|
|
import json
|
2017-02-26 08:07:58 +01:00
|
|
|
import time
|
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")
|
2017-09-29 14:31:42 +02:00
|
|
|
self._city = self.parameter("location", "")
|
2017-02-26 08:07:58 +01:00
|
|
|
self._interval = int(self.parameter("interval", "15"))
|
|
|
|
self._unit = self.parameter("unit", "metric")
|
|
|
|
self._nextcheck = 0
|
2017-04-01 16:53:39 +02:00
|
|
|
self._valid = False
|
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())
|
|
|
|
|
|
|
|
def city(self):
|
|
|
|
self._city = re.sub('[_-]', ' ', self._city)
|
|
|
|
return u"{} ".format(self._city)
|
|
|
|
|
|
|
|
def output(self, widget):
|
2017-04-01 16:53:39 +02:00
|
|
|
if not self._valid:
|
|
|
|
return u"?"
|
2017-09-29 14:31:42 +02:00
|
|
|
return self.city() + self.temperature()
|
2017-02-26 02:42:27 +01:00
|
|
|
|
2017-06-13 04:26:33 +02:00
|
|
|
def state( self, widget ):
|
|
|
|
if self._valid:
|
|
|
|
if "thunderstorm" in self._weather:
|
|
|
|
return [ 'thunder' ]
|
|
|
|
elif "drizzle" in self._weather:
|
|
|
|
return [ 'rain' ]
|
|
|
|
elif "rain" in self._weather:
|
|
|
|
return [ 'rain' ]
|
|
|
|
elif "snow" in self._weather:
|
|
|
|
return [ 'snow' ]
|
|
|
|
elif "sleet" in self._weather:
|
|
|
|
return [ 'sleet' ]
|
|
|
|
elif "clear" in self._weather:
|
|
|
|
return [ 'clear' ]
|
|
|
|
elif "cloud" in self._weather:
|
|
|
|
return [ 'clouds' ]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
2017-02-26 02:42:27 +01:00
|
|
|
def update(self, widgets):
|
2017-02-26 08:07:58 +01:00
|
|
|
timestamp = int(time.time())
|
|
|
|
if self._nextcheck < int(time.time()):
|
2017-04-01 15:23:32 +02:00
|
|
|
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(",")
|
2017-09-29 14:31:42 +02:00
|
|
|
self._city = location["city"]
|
2017-04-01 15:23:32 +02:00
|
|
|
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'])
|
2017-06-13 04:26:33 +02:00
|
|
|
self._weather = weather['weather'][0]['main'].lower()
|
2017-04-01 16:53:39 +02:00
|
|
|
self._valid = True
|
2017-04-01 15:23:32 +02:00
|
|
|
except RequestException:
|
2017-04-01 16:53:39 +02:00
|
|
|
self._valid = False
|
2017-06-10 13:59:44 +02:00
|
|
|
except Exception:
|
|
|
|
self._valid = False
|
2017-02-26 02:42:27 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|