Skip to content

Commit

Permalink
Wunderground: fix API key lookup (#858)
Browse files Browse the repository at this point in the history
  • Loading branch information
terminalmage authored Feb 6, 2024
1 parent 5348220 commit 28ed5c1
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions i3pystatus/weather/wunderground.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,15 @@ def get_api_key(self):
except Exception as exc:
self.logger.exception(f'Failed to load {url}')
else:
try:
return re.search(r'apiKey=([0-9a-f]+)', page_source).group(1)
except AttributeError:
self.logger.debug('Scanning page source for embedded API keys')
for key_match in re.finditer(r'apiKey=([0-9a-f]+)', page_source):
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.api_key = None

@require(internet)
def api_request(self, url, headers=None):
Expand All @@ -128,15 +133,17 @@ def check_weather(self):
Query the desired station and return the weather data
'''
# 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:
self.data['update_error'] = self.update_error
return

self.data['update_error'] = ''
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):
self.logger.error(
'Failed to retrieve observation data from API response. '
Expand Down

0 comments on commit 28ed5c1

Please sign in to comment.