From 5bf3b2b32aa45aa6aecb4262a79a122838e080a3 Mon Sep 17 00:00:00 2001 From: Erik Johnson Date: Thu, 30 Mar 2017 10:36:23 -0500 Subject: [PATCH] Another fix for Weather.com API changes (#559) It appears that the page data loads in one of two ways. I haven't figured out what causes either, but this makes it so that we detect both methods and load the relevant data either way. --- i3pystatus/weather/weathercom.py | 70 ++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 8 deletions(-) diff --git a/i3pystatus/weather/weathercom.py b/i3pystatus/weather/weathercom.py index 838a5f6..3954be0 100644 --- a/i3pystatus/weather/weathercom.py +++ b/i3pystatus/weather/weathercom.py @@ -40,25 +40,79 @@ class WeathercomHTMLParser(HTMLParser): exc_info=True ) + def load_json(self, json_input): + self.logger.debug('Loading the following data as JSON: %s', json_input) + try: + return json.loads(json_string) + except json.decoder.JSONDecodeError as exc: + self.logger.debug('Error loading JSON: %s', exc) + self.logger.debug('String that failed to load: %s', json_input) + return None + def handle_data(self, content): + ''' + Sometimes the weather data is set under an attribute of the "window" + DOM object. Sometimes it appears as part of a javascript function. + Catch either possibility. + ''' + if self.weather_data is not None: + # We've already found weather data, no need to continue parsing + return + content = content.strip().rstrip(';') try: tag_text = self.get_starttag_text().lower() except AttributeError: tag_text = '' if tag_text.startswith('