diff --git a/i3pystatus/weather.py b/i3pystatus/weather.py index ad22e50..dea2d29 100644 --- a/i3pystatus/weather.py +++ b/i3pystatus/weather.py @@ -1,11 +1,15 @@ from i3pystatus import IntervalModule -import pywapi from i3pystatus.core.util import internet, require +from urllib.request import urlopen +import re +import xml.etree.ElementTree as ElementTree + +WEATHER_COM_URL = 'http://wxdata.weather.com/wxdata/weather/local/%s?unit=%s&cc=*' class Weather(IntervalModule): """ - This module gets the weather from weather.com using pywapi module + This module gets the weather from weather.com. First, you need to get the code for the location from the www.weather.com .. rubric:: Available formatters @@ -14,7 +18,6 @@ class Weather(IntervalModule): * {current_wind} * {humidity} - Requires pywapi from PyPI. """ interval = 20 @@ -27,6 +30,7 @@ class Weather(IntervalModule): ) required = ("location_code",) + location_code = None units = "metric" format = "{current_temp}" colorize = False @@ -40,9 +44,34 @@ class Weather(IntervalModule): "default": ("", None), } + def fetch_weather(self): + '''Fetches the current weather from wxdata.weather.com service.''' + unit = '' if self.units == 'imperial' or self.units == '' else 'm' + url = WEATHER_COM_URL % (self.location_code, unit) + with urlopen(url) as handler: + try: + content_type = dict(handler.getheaders())['Content-Type'] + charset = re.search(r'charset=(.*)', content_type).group(1) + except AttributeError: + charset = 'utf-8' + xml = handler.read().decode(charset) + doc = ElementTree.XML(xml) + return dict( + current_conditions=dict( + text=doc.findtext('cc/t'), + temperature=doc.findtext('cc/tmp'), + humidity=doc.findtext('cc/hmid'), + wind=dict(text=doc.findtext('cc/wind/t'), speed=doc.findtext('cc/wind/s')) + ), + units=dict( + temperature=doc.findtext('head/ut'), + speed=doc.findtext('head/us'), + ), + ) + @require(internet) def run(self): - result = pywapi.get_weather_from_weather_com(self.location_code, self.units) + result = self.fetch_weather() conditions = result["current_conditions"] temperature = conditions["temperature"] humidity = conditions["humidity"] @@ -61,6 +90,10 @@ class Weather(IntervalModule): color = color self.output = { - "full_text": self.format.format(current_temp=current_temp, current_wind=current_wind, humidity=humidity), + "full_text": self.format.format( + current_temp=current_temp, + current_wind=current_wind, + humidity=humidity, + ), "color": color }