Remove pywapi
dependency from weather module
The python-weather-api seems currently unmaintained and provides a thin wrapper around the XML structure output from weather.com
This commit is contained in:
parent
eae741fd6e
commit
e2d07f8262
@ -1,11 +1,15 @@
|
|||||||
from i3pystatus import IntervalModule
|
from i3pystatus import IntervalModule
|
||||||
import pywapi
|
|
||||||
from i3pystatus.core.util import internet, require
|
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):
|
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
|
First, you need to get the code for the location from the www.weather.com
|
||||||
|
|
||||||
.. rubric:: Available formatters
|
.. rubric:: Available formatters
|
||||||
@ -14,7 +18,6 @@ class Weather(IntervalModule):
|
|||||||
* {current_wind}
|
* {current_wind}
|
||||||
* {humidity}
|
* {humidity}
|
||||||
|
|
||||||
Requires pywapi from PyPI.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
interval = 20
|
interval = 20
|
||||||
@ -27,6 +30,7 @@ class Weather(IntervalModule):
|
|||||||
)
|
)
|
||||||
required = ("location_code",)
|
required = ("location_code",)
|
||||||
|
|
||||||
|
location_code = None
|
||||||
units = "metric"
|
units = "metric"
|
||||||
format = "{current_temp}"
|
format = "{current_temp}"
|
||||||
colorize = False
|
colorize = False
|
||||||
@ -40,9 +44,34 @@ class Weather(IntervalModule):
|
|||||||
"default": ("", None),
|
"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)
|
@require(internet)
|
||||||
def run(self):
|
def run(self):
|
||||||
result = pywapi.get_weather_from_weather_com(self.location_code, self.units)
|
result = self.fetch_weather()
|
||||||
conditions = result["current_conditions"]
|
conditions = result["current_conditions"]
|
||||||
temperature = conditions["temperature"]
|
temperature = conditions["temperature"]
|
||||||
humidity = conditions["humidity"]
|
humidity = conditions["humidity"]
|
||||||
@ -61,6 +90,10 @@ class Weather(IntervalModule):
|
|||||||
color = color
|
color = color
|
||||||
|
|
||||||
self.output = {
|
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
|
"color": color
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user