Merge pull request #399 from terminalmage/scores

Update EPL scores backend for offseason
This commit is contained in:
enkore 2016-07-10 17:36:23 +02:00 committed by GitHub
commit 9e9cb8dffc
2 changed files with 65 additions and 37 deletions

View File

@ -45,11 +45,29 @@ class ScoresBackend(SettingsBase):
try: try:
with urlopen(url) as content: with urlopen(url) as content:
try: 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'] 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) charset = re.search(r'charset=(.*)', content_type).group(1)
except AttributeError: except AttributeError:
charset = 'utf-8' 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) self.logger.log(5, 'API response: %s', response)
return response return response
except HTTPError as exc: except HTTPError as exc:

View File

@ -213,6 +213,12 @@ class EPL(ScoresBackend):
def get_context(self): def get_context(self):
response = self.api_request(self.context_url) 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_tuple = namedtuple(
'Context', 'Context',
('competition', 'date', 'game_week', 'match_day', 'season') ('competition', 'date', 'game_week', 'match_day', 'season')
@ -224,6 +230,7 @@ class EPL(ScoresBackend):
'matchDayId', 'seasonId') 'matchDayId', 'seasonId')
] ]
) )
return True
def get_team_stats(self): def get_team_stats(self):
ret = {} ret = {}
@ -259,7 +266,9 @@ class EPL(ScoresBackend):
return '?\'' return '?\''
def check_scores(self): def check_scores(self):
self.get_context() if not self.get_context():
data = team_game_map = {}
else:
self.get_api_date() self.get_api_date()
url = self.api_url % (self.context.competition, url = self.api_url % (self.context.competition,
@ -272,6 +281,7 @@ class EPL(ScoresBackend):
break break
else: else:
game_list = [] game_list = []
self.logger.debug('game_list = %s', game_list) self.logger.debug('game_list = %s', game_list)
team_stats = self.get_team_stats() team_stats = self.get_team_stats()