Skip to content

Commit

Permalink
weathercom.py: Honour units parameter. (enkore#582)
Browse files Browse the repository at this point in the history
Potential fix for enkore#577
  • Loading branch information
facetoe committed Jun 15, 2017
1 parent c5757fc commit 4c0bba5
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions i3pystatus/weather/weathercom.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from i3pystatus.core.util import internet, require
from i3pystatus.weather import WeatherBackend

from datetime import datetime
from urllib.request import urlopen
from html.parser import HTMLParser
import json
import re
from datetime import datetime
from html.parser import HTMLParser
from urllib.request import urlopen

WEATHER_URL = 'https://weather.com/weather/today/l/%s'
from i3pystatus.core.util import internet, require
from i3pystatus.weather import WeatherBackend


class WeathercomHTMLParser(HTMLParser):
Expand All @@ -16,13 +14,12 @@ class WeathercomHTMLParser(HTMLParser):
through some other source at runtime and added as <script> elements to the
page source.
'''
def __init__(self, logger, location_code):

def __init__(self, logger):
self.logger = logger
self.location_code = location_code
super(WeathercomHTMLParser, self).__init__()

def get_weather_data(self):
url = WEATHER_URL % self.location_code
def get_weather_data(self, url):
self.logger.debug('Making request to %s to retrieve weather data', url)
self.weather_data = None
with urlopen(url) as content:
Expand Down Expand Up @@ -164,6 +161,8 @@ class Weathercom(WeatherBackend):
units = 'metric'
update_error = '!'

url_template = 'https://weather.com/{locale}/weather/today/l/{location_code}'

# This will be set in the init based on the passed location code
forecast_url = None

Expand All @@ -173,8 +172,13 @@ def init(self):
# Ensure that the location code is a string, in the event that a
# ZIP code (or other all-numeric code) is passed as a non-string.
self.location_code = str(self.location_code)
self.forecast_url = WEATHER_URL % self.location_code
self.parser = WeathercomHTMLParser(self.logger, self.location_code)

# Setting the locale to en-AU returns units in metric. Leaving it blank
# causes weather.com to return the default, which is imperial.
self.locale = 'en-AU' if self.units == 'metric' else ''

self.forecast_url = self.url_template.format(**vars(self))
self.parser = WeathercomHTMLParser(self.logger)

def check_response(self, response):
# Errors for weather.com API manifest in HTTP error codes, not in the
Expand All @@ -186,6 +190,10 @@ def check_weather(self):
'''
Fetches the current weather from wxdata.weather.com service.
'''

if self.units not in ('imperial', 'metric'):
raise Exception("units must be one of (imperial, metric)!")

if self.location_code is None:
self.logger.error(
'A location_code is required to check Weather.com. See the '
Expand All @@ -196,7 +204,7 @@ def check_weather(self):
self.data['update_error'] = ''
try:

self.parser.get_weather_data()
self.parser.get_weather_data(self.forecast_url)
if self.parser.weather_data is None:
self.logger.error(
'Failed to read weather data from page. Run module with '
Expand Down

0 comments on commit 4c0bba5

Please sign in to comment.