Update EPL scores backend for offseason

During the offseason, the context URL which provides params used to
locate information on a given game redirects to the EPL mainpage.
This commit properly catches these cases, and also adds some helpful
logging to the ScoresBackend api_request() function.
This commit is contained in:
Erik Johnson 2016-06-10 00:05:33 -05:00
parent b4e21dfd9e
commit 1f8e11fd6d
2 changed files with 65 additions and 37 deletions

View File

@ -45,11 +45,29 @@ class ScoresBackend(SettingsBase):
try:
with urlopen(url) as content:
try:
if content.url != url:
self.logger.debug('Request to %s was redirected to %s',
url, content.url)
content_type = dict(content.getheaders())['Content-Type']
mime_type = content_type.split(';')[0].lower()
if 'json' not in mime_type:
self.logger.debug('Response from %s is not JSON',
content.url)
return {}
charset = re.search(r'charset=(.*)', content_type).group(1)
except AttributeError:
charset = 'utf-8'
response = json.loads(content.read().decode(charset))
response_json = content.read().decode(charset).strip()
if not response_json:
self.logger.debug('JSON response from %s was blank', url)
return {}
try:
response = json.loads(response_json)
except json.decoder.JSONDecodeError as exc:
self.logger.error('Error loading JSON: %s', exc)
self.logger.debug('JSON text that failed to load: %s',
response_json)
return {}
self.logger.log(5, 'API response: %s', response)
return response
except HTTPError as exc:

View File

@ -213,6 +213,12 @@ class EPL(ScoresBackend):
def get_context(self):
response = self.api_request(self.context_url)
if not response:
# There is no context data, but we still need a date to use in
# __init__.py to log that there are no games for the given date.
# Fall back to the parent class' function to set a date.
super(EPL, self).get_api_date()
return False
context_tuple = namedtuple(
'Context',
('competition', 'date', 'game_week', 'match_day', 'season')
@ -224,6 +230,7 @@ class EPL(ScoresBackend):
'matchDayId', 'seasonId')
]
)
return True
def get_team_stats(self):
ret = {}
@ -259,7 +266,9 @@ class EPL(ScoresBackend):
return '?\''
def check_scores(self):
self.get_context()
if not self.get_context():
data = team_game_map = {}
else:
self.get_api_date()
url = self.api_url % (self.context.competition,
@ -272,6 +281,7 @@ class EPL(ScoresBackend):
break
else:
game_list = []
self.logger.debug('game_list = %s', game_list)
team_stats = self.get_team_stats()