2020-03-15 14:05:13 +01:00
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
2020-04-02 22:10:18 +02:00
|
|
|
"""Displays the temperature on the current location based on the ip
|
2020-03-15 14:05:13 +01:00
|
|
|
|
|
|
|
Requires the following python packages:
|
|
|
|
* requests
|
|
|
|
|
|
|
|
Parameters:
|
2020-04-15 13:26:08 +02:00
|
|
|
* weather.location: Set location, defaults to 'auto' for getting location automatically from a web service
|
2020-05-06 12:57:38 +02:00
|
|
|
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.
|
2020-03-15 14:05:13 +01:00
|
|
|
* weather.unit: metric (default), kelvin, imperial
|
|
|
|
* weather.showcity: If set to true, show location information, otherwise hide it (defaults to true)
|
|
|
|
* weather.showminmax: If set to true, show the minimum and maximum temperature, otherwise hide it (defaults to false)
|
|
|
|
* weather.apikey: API key from http://api.openweathermap.org
|
2020-05-08 20:58:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
contributed by `TheEdgeOfRage <https://github.com/TheEdgeOfRage>`_ - many thanks!
|
2020-04-02 22:10:18 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.input
|
|
|
|
|
|
|
|
import util.format
|
2020-04-15 13:26:08 +02:00
|
|
|
import util.location
|
2020-03-15 14:05:13 +01:00
|
|
|
|
|
|
|
import re
|
|
|
|
|
2020-04-04 07:47:53 +02:00
|
|
|
import requests
|
|
|
|
from requests.exceptions import RequestException
|
2020-03-15 14:05:13 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-02 22:10:18 +02:00
|
|
|
class Module(core.module.Module):
|
|
|
|
@core.decorators.every(minutes=15)
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.output))
|
2020-04-02 22:10:18 +02:00
|
|
|
|
|
|
|
self.__temperature = 0
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__apikey = self.parameter("apikey", "af7bfe22287c652d032a3064ffa44088")
|
|
|
|
self.__location = util.format.aslist(self.parameter("location", "auto"))
|
2020-04-02 22:10:18 +02:00
|
|
|
|
|
|
|
self.__index = 0
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__showcity = util.format.asbool(self.parameter("showcity", True))
|
|
|
|
self.__showminmax = util.format.asbool(self.parameter("showminmax", False))
|
|
|
|
self.__unit = self.parameter("unit", "metric")
|
2020-04-02 22:10:18 +02:00
|
|
|
self.__valid = False
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
core.input.register(
|
|
|
|
self, button=core.input.LEFT_MOUSE, cmd=self.__next_location
|
|
|
|
)
|
|
|
|
core.input.register(
|
|
|
|
self, button=core.input.RIGHT_MOUSE, cmd=self.__prev_location
|
|
|
|
)
|
2020-04-02 22:10:18 +02:00
|
|
|
|
|
|
|
def __next_location(self, event):
|
|
|
|
self.__index = (self.__index + 1) % len(self.__location)
|
2020-04-04 07:47:53 +02:00
|
|
|
self.update()
|
2020-03-15 14:05:13 +01:00
|
|
|
|
2020-04-02 22:10:18 +02:00
|
|
|
def __prev_location(self, event):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__index = (
|
|
|
|
len(self.__location) - 1 if self.__index <= 0 else self.__index - 1
|
|
|
|
)
|
2020-04-04 07:47:53 +02:00
|
|
|
self.update()
|
2020-03-15 14:05:13 +01:00
|
|
|
|
|
|
|
def temperature(self):
|
2020-04-04 07:47:53 +02:00
|
|
|
return util.format.astemperature(self.__temperature, self.__unit)
|
2020-03-15 14:05:13 +01:00
|
|
|
|
|
|
|
def tempmin(self):
|
2020-04-04 07:47:53 +02:00
|
|
|
return util.format.astemperature(self.__tempmin, self.__unit)
|
2020-03-15 14:05:13 +01:00
|
|
|
|
|
|
|
def tempmax(self):
|
2020-04-04 07:47:53 +02:00
|
|
|
return util.format.astemperature(self.__tempmax, self.__unit)
|
2020-03-15 14:05:13 +01:00
|
|
|
|
|
|
|
def city(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
city = re.sub("[_-]", " ", self.__city)
|
|
|
|
return "{} ".format(city)
|
2020-03-15 14:05:13 +01:00
|
|
|
|
|
|
|
def output(self, widget):
|
2020-04-02 22:10:18 +02:00
|
|
|
if not self.__valid:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "?"
|
2020-04-02 22:10:18 +02:00
|
|
|
if self.__showminmax:
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__showcity = False
|
|
|
|
return (
|
|
|
|
self.city()
|
|
|
|
+ self.temperature()
|
|
|
|
+ " Hi:"
|
|
|
|
+ self.tempmax()
|
|
|
|
+ " Lo:"
|
|
|
|
+ self.tempmin()
|
|
|
|
)
|
2020-04-02 22:10:18 +02:00
|
|
|
elif self.__showcity:
|
2020-03-15 14:05:13 +01:00
|
|
|
return self.city() + self.temperature()
|
|
|
|
else:
|
|
|
|
return self.temperature()
|
|
|
|
|
|
|
|
def state(self, widget):
|
2020-04-02 22:10:18 +02:00
|
|
|
if self.__valid:
|
2020-05-03 11:15:52 +02:00
|
|
|
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"]
|
2020-03-15 14:05:13 +01:00
|
|
|
|
|
|
|
return []
|
|
|
|
|
2020-04-02 22:10:18 +02:00
|
|
|
def update(self):
|
2020-03-15 14:05:13 +01:00
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
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[self.__index] == "auto":
|
2020-04-15 13:26:08 +02:00
|
|
|
coord = util.location.coordinates()
|
2020-05-03 11:15:52 +02:00
|
|
|
weather_url = "{url}&lat={lat}&lon={lon}".format(
|
|
|
|
url=weather_url, lat=coord[0], lon=coord[1]
|
|
|
|
)
|
2020-04-02 22:10:18 +02:00
|
|
|
elif self.__location[self.__index].isdigit():
|
2020-05-03 11:15:52 +02:00
|
|
|
weather_url = "{url}&id={id}".format(
|
|
|
|
url=weather_url, id=self.__location[self.__index]
|
|
|
|
)
|
2020-03-15 14:05:13 +01:00
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
weather_url = "{url}&q={city}".format(
|
|
|
|
url=weather_url, city=self.__location[self.__index]
|
|
|
|
)
|
2020-03-15 14:05:13 +01:00
|
|
|
weather = requests.get(weather_url).json()
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__city = weather["name"]
|
|
|
|
self.__temperature = int(weather["main"]["temp"])
|
|
|
|
self.__tempmin = int(weather["main"]["temp_min"])
|
|
|
|
self.__tempmax = int(weather["main"]["temp_max"])
|
|
|
|
self.__weather = weather["weather"][0]["main"].lower()
|
2020-04-02 22:10:18 +02:00
|
|
|
self.__valid = True
|
2020-03-15 14:05:13 +01:00
|
|
|
except Exception:
|
2020-04-02 22:10:18 +02:00
|
|
|
self.__valid = False
|
2020-03-15 14:05:13 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-03-15 14:05:13 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|