Wunderground: fix API key lookup (#858)

This commit is contained in:
Erik Johnson 2024-02-05 20:48:02 -06:00 committed by GitHub
parent 534822077a
commit 28ed5c1c30
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -109,10 +109,15 @@ class Wunderground(WeatherBackend):
except Exception as exc: except Exception as exc:
self.logger.exception(f'Failed to load {url}') self.logger.exception(f'Failed to load {url}')
else: else:
try: self.logger.debug('Scanning page source for embedded API keys')
return re.search(r'apiKey=([0-9a-f]+)', page_source).group(1) for key_match in re.finditer(r'apiKey=([0-9a-f]+)', page_source):
except AttributeError: self.api_key = key_match.group(1)
self.logger.debug(f'Checking if {self.api_key} works')
if self.api_request(self.observation_url.format(**vars(self))):
break
else:
self.logger.error('Failed to find API key in mainpage source') self.logger.error('Failed to find API key in mainpage source')
self.api_key = None
@require(internet) @require(internet)
def api_request(self, url, headers=None): def api_request(self, url, headers=None):
@ -128,7 +133,7 @@ class Wunderground(WeatherBackend):
Query the desired station and return the weather data Query the desired station and return the weather data
''' '''
# Get the API key from the page source # Get the API key from the page source
self.api_key = self.get_api_key() self.get_api_key()
if self.api_key is None: if self.api_key is None:
self.data['update_error'] = self.update_error self.data['update_error'] = self.update_error
return return
@ -136,7 +141,9 @@ class Wunderground(WeatherBackend):
self.data['update_error'] = '' self.data['update_error'] = ''
try: try:
try: try:
observation = self.api_request(self.observation_url.format(**vars(self)))['observations'][0] observation = self.api_request(
self.observation_url.format(**vars(self))
)['observations'][0]
except (IndexError, KeyError): except (IndexError, KeyError):
self.logger.error( self.logger.error(
'Failed to retrieve observation data from API response. ' 'Failed to retrieve observation data from API response. '