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('