From 6971fa553b1232b8faa61cb9545ac0ead78782f6 Mon Sep 17 00:00:00 2001 From: emkael Date: Sat, 24 Feb 2018 00:54:16 +0100 Subject: Lots of lots of messages are being logged --- jfr_playoff/matchinfo.py | 84 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 76 insertions(+), 8 deletions(-) (limited to 'jfr_playoff/matchinfo.py') diff --git a/jfr_playoff/matchinfo.py b/jfr_playoff/matchinfo.py index 997e685..db49b07 100644 --- a/jfr_playoff/matchinfo.py +++ b/jfr_playoff/matchinfo.py @@ -5,6 +5,7 @@ import jfr_playoff.sql as p_sql from jfr_playoff.dto import Match, Team from jfr_playoff.remote import RemoteUrl as p_remote from jfr_playoff.tournamentinfo import TournamentInfo +from jfr_playoff.logger import PlayoffLogger class MatchInfo: @@ -36,10 +37,15 @@ class MatchInfo: def __fetch_match_link(self): if 'link' in self.config: self.info.link = self.config['link'] + PlayoffLogger.get('matchinfo').info( + 'match #%d link pre-defined: %s', self.info.id, self.info.link) elif ('round' in self.config) and ('database' in self.config): event_info = TournamentInfo(self.config, self.database) self.info.link = event_info.get_results_link( 'runda%d.html' % (self.config['round'])) + PlayoffLogger.get('matchinfo').info( + 'match #%d link fetched: %s', self.info.id, self.info.link) + PlayoffLogger.get('matchinfo').info('match #%d link empty', self.info.id) def __get_predefined_scores(self): teams = [Team(), Team()] @@ -62,6 +68,9 @@ class MatchInfo: if i == 2: break scores_fetched = True + PlayoffLogger.get('matchinfo').info( + 'pre-defined scores for match #%d: %s', + self.info.id, teams) return scores_fetched, teams_fetched, teams def __get_db_teams(self, teams, fetch_scores): @@ -77,6 +86,8 @@ class MatchInfo: teams[0].score += row[2] else: teams[1].score -= row[2] + PlayoffLogger.get('matchinfo').info( + 'db scores for match #%d: %s', self.info.id, teams) return teams def __find_table_row(self, url): @@ -84,7 +95,13 @@ class MatchInfo: for row in html_content.select('tr tr'): for cell in row.select('td.t1'): if cell.text.strip() == str(self.config['table']): + PlayoffLogger.get('matchinfo.html').debug( + 'HTML row for table %d found: %s', + self.config['table'], row) return row + PlayoffLogger.get('matchinfo.html').debug( + 'HTML row for table %d not found', + self.config['table']) return None def __get_html_teams(self, teams, fetch_score): @@ -104,6 +121,9 @@ class MatchInfo: for i in range(0, 2): teams[i].name = team_names[i] teams[i].score = scores[i] + PlayoffLogger.get('matchinfo').info( + 'HTML scores for match #%d: %s', + self.info.id, teams) return teams def __get_config_teams(self, teams): @@ -133,11 +153,16 @@ class MatchInfo: else '??' for team in match_teams]) else: teams[i].name = '' + PlayoffLogger.get('matchinfo').info( + 'config scores for match #%d: %s', + self.info.id, teams) return teams def __fetch_teams_with_scores(self): (scores_fetched, teams_fetched, self.info.teams) = self.__get_predefined_scores() if scores_fetched: + PlayoffLogger.get('matchinfo').info( + 'pre-defined scores for match #%d fetched', self.info.id) if 'running' in self.config: self.info.running = int(self.config['running']) else: @@ -151,10 +176,16 @@ class MatchInfo: raise KeyError('database not configured') self.info.teams = self.__get_db_teams( self.info.teams, not scores_fetched) - except (IOError, TypeError, IndexError, KeyError): + except (IOError, TypeError, IndexError, KeyError) as e: + PlayoffLogger.get('matchinfo').warning( + 'fetching DB scores for match #%d failed: %s(%s)', + self.info.id, type(e).__name__, e.message) self.info.teams = self.__get_html_teams( self.info.teams, not scores_fetched) - except (TypeError, IndexError, KeyError, IOError, ValueError): + except (TypeError, IndexError, KeyError, IOError, ValueError) as e: + PlayoffLogger.get('matchinfo').warning( + 'fetching HTML scores for match #%d failed: %s(%s)', + self.info.id, type(e).__name__, e.message) self.info.teams = self.__get_config_teams(self.info.teams) def __get_db_board_count(self): @@ -170,6 +201,9 @@ class MatchInfo: boards_played = max(int(row[1]), 0) if boards_to_play > 0: boards_played += int(towels[0]) + PlayoffLogger.get('matchinfo').info( + 'DB board count for match #%d: %d/%d', + self.info.id, boards_played, boards_to_play) return boards_played, boards_to_play def __has_segment_link(self, cell): @@ -196,8 +230,14 @@ class MatchInfo: played_boards = len([ row for row in board_rows if len( ''.join([cell.text.strip() for cell in row.select('td.bdc')])) > 0]) + PlayoffLogger.get('matchinfo').info( + 'HTML played boards count for segment: %d/%d', + played_boards, board_count) return board_count, played_boards >= board_count - except IOError: + except IOError as e: + PlayoffLogger.get('matchinfo').info( + 'cannot fetch HTML played boards count for segment: %s(%s)', + self.info.id, type(e).__name__, e.message) return 0, False return 0, False @@ -212,6 +252,8 @@ class MatchInfo: towels = [cell for cell in cells if self.__has_towel_image(cell)] if len(segments) == 0: if len(towels) > 0: + PlayoffLogger.get('matchinfo').info( + 'HTML board count for match #%d: all towels', self.info.id) return 1, 1 # entire match is toweled, so mark as finished else: raise ValueError('segments not found') @@ -226,8 +268,14 @@ class MatchInfo: finished_segments.append(segment) if boards_in_segment is None and boards > 0: boards_in_segment = boards + PlayoffLogger.get('matchinfo').info( + 'HTML board count for match #%d, found: %d finished segments, %d towels, %d boards per segment and %d boards in running segment', + self.info.id, len(finished_segments), len(towels), boards_in_segment, running_boards) total_boards = (len(segments) + len(towels) + len(running_segments)) * boards_in_segment played_boards = (len(towels) + len(finished_segments)) * boards_in_segment + running_boards + PlayoffLogger.get('matchinfo').info( + 'HTML board count for match #%d: %d/%d', + self.info.id, played_boards, total_boards) return played_boards, total_boards def __fetch_board_count(self): @@ -237,10 +285,16 @@ class MatchInfo: if self.database is None: raise KeyError('database not configured') boards_played, boards_to_play = self.__get_db_board_count() - except (IOError, TypeError, IndexError, KeyError): + except (IOError, TypeError, IndexError, KeyError) as e: + PlayoffLogger.get('matchinfo').warning( + 'fetching board count from DB for match #%d failed: %s(%s)', + self.info.id, type(e).__name__, e.message) try: boards_played, boards_to_play = self.__get_html_board_count() - except (TypeError, IndexError, KeyError, IOError, ValueError): + except (TypeError, IndexError, KeyError, IOError, ValueError) as e: + PlayoffLogger.get('matchinfo').warning( + 'fetching board count from HTML for match #%d failed: %s(%s)', + self.info.id, type(e).__name__, e.message) pass if boards_played > 0: self.info.running = -1 \ @@ -260,6 +314,9 @@ class MatchInfo: current_segment = int( self.database.fetch( self.config['database'], p_sql.CURRENT_SEGMENT, ())[0]) + PlayoffLogger.get('matchinfo').info( + 'fetched running segment from DB for match #%d: %d', + self.info.id, current_segment) return '%s%st%d-%d.html' % ( prefix, round_no, self.config['table'], current_segment) @@ -270,6 +327,9 @@ class MatchInfo: running_link = row.select('td.bdcg a[href]') if len(running_link) == 0: raise ValueError('running link not found') + PlayoffLogger.get('matchinfo').info( + 'fetched running link from HTML for match #%d: %s', + self.info.id, running_link) return urljoin(self.info.link, running_link[0]['href']) def __determine_running_link(self): @@ -282,11 +342,16 @@ class MatchInfo: raise KeyError('database not configured') self.info.link = self.__get_db_running_link( link_match.group(1), link_match.group(2)) - except (IOError, TypeError, IndexError, KeyError): + except (IOError, TypeError, IndexError, KeyError) as e: + PlayoffLogger.get('matchinfo').warning( + 'cannot determine running link from DB for match #%d: %s(%s)', + self.info.id, type(e).__name__, e.message) try: self.info.link = self.__get_html_running_link() - except (TypeError, IndexError, KeyError, IOError, ValueError): - pass + except (TypeError, IndexError, KeyError, IOError, ValueError) as e: + PlayoffLogger.get('matchinfo').warning( + 'cannot determine running link from HTML for match #%d: %s(%s)', + self.info.id, type(e).__name__, e.message) def set_phase_link(self, phase_link): if self.info.link is None: @@ -294,6 +359,9 @@ class MatchInfo: else: if self.info.link != '#': self.info.link = urljoin(phase_link, self.info.link) + PlayoffLogger.get('matchinfo').info( + 'applying phase link %s to match #%d: %s', + phase_link, self.info.id, self.info.link) def get_info(self): self.__fetch_teams_with_scores() -- cgit v1.2.3 From 8c500a75a6eafd65b332d0f8346d9059d5293cbb Mon Sep 17 00:00:00 2001 From: emkael Date: Sat, 24 Feb 2018 11:04:11 +0100 Subject: Extracting text representation of an exception instead of explicit .message property --- jfr_playoff/matchinfo.py | 14 +++++++------- jfr_playoff/settings.py | 2 +- jfr_playoff/tournamentinfo.py | 12 ++++++------ 3 files changed, 14 insertions(+), 14 deletions(-) (limited to 'jfr_playoff/matchinfo.py') diff --git a/jfr_playoff/matchinfo.py b/jfr_playoff/matchinfo.py index db49b07..8132fe2 100644 --- a/jfr_playoff/matchinfo.py +++ b/jfr_playoff/matchinfo.py @@ -179,13 +179,13 @@ class MatchInfo: except (IOError, TypeError, IndexError, KeyError) as e: PlayoffLogger.get('matchinfo').warning( 'fetching DB scores for match #%d failed: %s(%s)', - self.info.id, type(e).__name__, e.message) + self.info.id, type(e).__name__, str(e)) self.info.teams = self.__get_html_teams( self.info.teams, not scores_fetched) except (TypeError, IndexError, KeyError, IOError, ValueError) as e: PlayoffLogger.get('matchinfo').warning( 'fetching HTML scores for match #%d failed: %s(%s)', - self.info.id, type(e).__name__, e.message) + self.info.id, type(e).__name__, str(e)) self.info.teams = self.__get_config_teams(self.info.teams) def __get_db_board_count(self): @@ -237,7 +237,7 @@ class MatchInfo: except IOError as e: PlayoffLogger.get('matchinfo').info( 'cannot fetch HTML played boards count for segment: %s(%s)', - self.info.id, type(e).__name__, e.message) + self.info.id, type(e).__name__, str(e)) return 0, False return 0, False @@ -288,13 +288,13 @@ class MatchInfo: except (IOError, TypeError, IndexError, KeyError) as e: PlayoffLogger.get('matchinfo').warning( 'fetching board count from DB for match #%d failed: %s(%s)', - self.info.id, type(e).__name__, e.message) + self.info.id, type(e).__name__, str(e)) try: boards_played, boards_to_play = self.__get_html_board_count() except (TypeError, IndexError, KeyError, IOError, ValueError) as e: PlayoffLogger.get('matchinfo').warning( 'fetching board count from HTML for match #%d failed: %s(%s)', - self.info.id, type(e).__name__, e.message) + self.info.id, type(e).__name__, str(e)) pass if boards_played > 0: self.info.running = -1 \ @@ -345,13 +345,13 @@ class MatchInfo: except (IOError, TypeError, IndexError, KeyError) as e: PlayoffLogger.get('matchinfo').warning( 'cannot determine running link from DB for match #%d: %s(%s)', - self.info.id, type(e).__name__, e.message) + self.info.id, type(e).__name__, str(e)) try: self.info.link = self.__get_html_running_link() except (TypeError, IndexError, KeyError, IOError, ValueError) as e: PlayoffLogger.get('matchinfo').warning( 'cannot determine running link from HTML for match #%d: %s(%s)', - self.info.id, type(e).__name__, e.message) + self.info.id, type(e).__name__, str(e)) def set_phase_link(self, phase_link): if self.info.link is None: diff --git a/jfr_playoff/settings.py b/jfr_playoff/settings.py index 181e19f..58de66c 100644 --- a/jfr_playoff/settings.py +++ b/jfr_playoff/settings.py @@ -35,7 +35,7 @@ class PlayoffSettings(object): except Exception as e: PlayoffLogger.get('settings').warning( 'unable to merge remote config %s: %s(%s)', - remote_url, type(e).__name__, e.message) + remote_url, type(e).__name__, str(e)) return base_config def load(self): diff --git a/jfr_playoff/tournamentinfo.py b/jfr_playoff/tournamentinfo.py index 24440b3..a646d4e 100644 --- a/jfr_playoff/tournamentinfo.py +++ b/jfr_playoff/tournamentinfo.py @@ -162,13 +162,13 @@ class TournamentInfo: except (IOError, TypeError, IndexError, KeyError) as e: PlayoffLogger.get('tournamentinfo').warning( 'cannot determine tournament results from DB: %s(%s)', - type(e).__name__, e.message) + type(e).__name__, str(e)) try: teams = self.__get_html_results() except (TypeError, IndexError, KeyError, IOError, ValueError) as e: PlayoffLogger.get('tournamentinfo').warning( 'cannot determine tournament results from HTML: %s(%s)', - type(e).__name__, e.message) + type(e).__name__, str(e)) if self.is_finished() and 'final_positions' in self.settings: PlayoffLogger.get('tournamentinfo').info( 'setting final positions from tournament results: %s', @@ -184,13 +184,13 @@ class TournamentInfo: except (IOError, TypeError, IndexError, KeyError) as e: PlayoffLogger.get('tournamentinfo').warning( 'cannot determine tournament finished status from DB: %s(%s)', - type(e).__name__, e.message) + type(e).__name__, str(e)) try: return self.__get_html_finished() except (TypeError, IndexError, KeyError, IOError, ValueError) as e: PlayoffLogger.get('tournamentinfo').warning( 'cannot determine tournament finished status from HTML: %s(%s)', - type(e).__name__, e.message) + type(e).__name__, str(e)) PlayoffLogger.get('tournamentinfo').info( 'assuming tournament is finished') return True @@ -201,11 +201,11 @@ class TournamentInfo: except (IOError, TypeError, IndexError, KeyError) as e: PlayoffLogger.get('tournamentinfo').warning( 'cannot determine tournament link from DB: %s(%s)', - type(e).__name__, e.message) + type(e).__name__, str(e)) try: return self.__get_html_link(suffix) except (KeyError, ValueError): PlayoffLogger.get('tournamentinfo').warning( 'cannot determine tournament link from HTML: %s(%s)', - type(e).__name__, e.message) + type(e).__name__, str(e)) return None -- cgit v1.2.3 From 2b906f5038565f718e589cd70c24f4614fd0f4cf Mon Sep 17 00:00:00 2001 From: emkael Date: Thu, 9 Aug 2018 20:17:08 +0200 Subject: Scores and team names for single-segment matches --- jfr_playoff/matchinfo.py | 52 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 14 deletions(-) (limited to 'jfr_playoff/matchinfo.py') diff --git a/jfr_playoff/matchinfo.py b/jfr_playoff/matchinfo.py index 93b997a..2aeb2c1 100644 --- a/jfr_playoff/matchinfo.py +++ b/jfr_playoff/matchinfo.py @@ -110,11 +110,31 @@ class MatchInfo: row = self.__find_table_row(self.info.link) if row is None: raise ValueError('table row not found') - score_cell = row.select('td.bdc')[-1] - scores = [ - float(text) for text - in score_cell.contents - if isinstance(text, unicode)] + try: + scores = [ + float(text) for text + in row.select('td.bdc')[-1].contents + if isinstance(text, unicode)] + except ValueError: + try: + # running single-segment + scores = [ + float(text.strip()) for text + in row.select('td.bdcg a')[-1].contents + if isinstance(text, unicode)] + except IndexError: + # static single-segment + scores = [ + float(text.strip()) for text + in row.select('td.bdc a')[-1].contents + if isinstance(text, unicode)] + # carry-over + carry_over = [ + float(text.strip()) if len(text.strip()) > 0 else 0.0 for text + in row.select('td.bdc')[0].contents + if isinstance(text, unicode)] + for i in range(0, 2): + scores[i] += carry_over[i] team_names = [[text for text in link.contents if isinstance(text, unicode)][0].strip(u'\xa0') for link in row.select('a[onmouseover]')] @@ -247,17 +267,21 @@ class MatchInfo: row = self.__find_table_row(self.info.link) if row is None: raise ValueError('table row not found') - cells = row.select('td.bdc') - segments = [cell for cell in cells if self.__has_segment_link(cell)] - towels = [cell for cell in cells if self.__has_towel_image(cell)] - if len(segments) == 0: - if len(towels) > 0: - PlayoffLogger.get('matchinfo').info( - 'HTML board count for match #%d: all towels', self.info.id) - return 1, 1 # entire match is toweled, so mark as finished + for selector in ['td.bdc', 'td.bdcg']: + cells = row.select(selector) + segments = [cell for cell in cells if self.__has_segment_link(cell)] + towels = [cell for cell in cells if self.__has_towel_image(cell)] + if len(segments) == 0: + if len(towels) > 0: + PlayoffLogger.get('matchinfo').info( + 'HTML board count for match #%d: all towels', self.info.id) + return 1, 1 # entire match is toweled, so mark as finished else: - raise ValueError('segments not found') + break + if len(segments) == 0: + raise ValueError('segments not found') running_segments = row.select('td.bdca') + # FIXME: running single-segment match board count running_boards = sum([self.__get_html_running_boards(segment) for segment in running_segments]) finished_segments = [] boards_in_segment = None -- cgit v1.2.3 From 528bbb28fafdef9aa107149fa5f2a90dccfa00f9 Mon Sep 17 00:00:00 2001 From: emkael Date: Fri, 28 Sep 2018 14:30:19 +0200 Subject: Comments on the code for single-segment match team/score fetching --- jfr_playoff/matchinfo.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'jfr_playoff/matchinfo.py') diff --git a/jfr_playoff/matchinfo.py b/jfr_playoff/matchinfo.py index 2aeb2c1..b40d6d1 100644 --- a/jfr_playoff/matchinfo.py +++ b/jfr_playoff/matchinfo.py @@ -116,6 +116,7 @@ class MatchInfo: in row.select('td.bdc')[-1].contents if isinstance(text, unicode)] except ValueError: + # single-segment match try: # running single-segment scores = [ @@ -272,11 +273,14 @@ class MatchInfo: segments = [cell for cell in cells if self.__has_segment_link(cell)] towels = [cell for cell in cells if self.__has_towel_image(cell)] if len(segments) == 0: + # in single-segment match, there are no td.bdc cells with segment links + # but maybe it's a multi-segment match with towels if len(towels) > 0: PlayoffLogger.get('matchinfo').info( 'HTML board count for match #%d: all towels', self.info.id) return 1, 1 # entire match is toweled, so mark as finished else: + # not a single-segment match, no need to look for td.bdcg cells break if len(segments) == 0: raise ValueError('segments not found') -- cgit v1.2.3 From b1ce81da1478715b1d3a8dd6883c5655db0a8f68 Mon Sep 17 00:00:00 2001 From: emkael Date: Fri, 28 Sep 2018 14:30:55 +0200 Subject: Fixing score fetch for a single-segment match with a towel --- jfr_playoff/matchinfo.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'jfr_playoff/matchinfo.py') diff --git a/jfr_playoff/matchinfo.py b/jfr_playoff/matchinfo.py index b40d6d1..f726282 100644 --- a/jfr_playoff/matchinfo.py +++ b/jfr_playoff/matchinfo.py @@ -124,11 +124,15 @@ class MatchInfo: in row.select('td.bdcg a')[-1].contents if isinstance(text, unicode)] except IndexError: - # static single-segment - scores = [ - float(text.strip()) for text - in row.select('td.bdc a')[-1].contents - if isinstance(text, unicode)] + try: + # static single-segment + scores = [ + float(text.strip()) for text + in row.select('td.bdc a')[-1].contents + if isinstance(text, unicode)] + except IndexError: + # toweled single-segment + scores = [0.0, 0.0] # carry-over carry_over = [ float(text.strip()) if len(text.strip()) > 0 else 0.0 for text -- cgit v1.2.3 From cb1edf3fb102f347adde2a1c8aec205c24cd36be Mon Sep 17 00:00:00 2001 From: emkael Date: Fri, 28 Sep 2018 14:31:35 +0200 Subject: Fixing carry-over fetch for a single-segment match if there's none --- jfr_playoff/matchinfo.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'jfr_playoff/matchinfo.py') diff --git a/jfr_playoff/matchinfo.py b/jfr_playoff/matchinfo.py index f726282..d968a34 100644 --- a/jfr_playoff/matchinfo.py +++ b/jfr_playoff/matchinfo.py @@ -138,6 +138,9 @@ class MatchInfo: float(text.strip()) if len(text.strip()) > 0 else 0.0 for text in row.select('td.bdc')[0].contents if isinstance(text, unicode)] + if len(carry_over) < 2: + # no carry-over, possibly no carry-over cells or empty + carry_over = [0.0, 0.0] for i in range(0, 2): scores[i] += carry_over[i] team_names = [[text for text in link.contents -- cgit v1.2.3 From e23ef3b9dc245d134e4b0105e2dca7a1833f1ba5 Mon Sep 17 00:00:00 2001 From: emkael Date: Fri, 28 Sep 2018 14:32:23 +0200 Subject: Fixing overall bourd count fetch for a single-segment match --- jfr_playoff/matchinfo.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'jfr_playoff/matchinfo.py') diff --git a/jfr_playoff/matchinfo.py b/jfr_playoff/matchinfo.py index d968a34..900c210 100644 --- a/jfr_playoff/matchinfo.py +++ b/jfr_playoff/matchinfo.py @@ -292,7 +292,6 @@ class MatchInfo: if len(segments) == 0: raise ValueError('segments not found') running_segments = row.select('td.bdca') - # FIXME: running single-segment match board count running_boards = sum([self.__get_html_running_boards(segment) for segment in running_segments]) finished_segments = [] boards_in_segment = None @@ -303,10 +302,14 @@ class MatchInfo: finished_segments.append(segment) if boards_in_segment is None and boards > 0: boards_in_segment = boards - PlayoffLogger.get('matchinfo').info( - 'HTML board count for match #%d, found: %d finished segments, %d towels, %d boards per segment and %d boards in running segment', - self.info.id, len(finished_segments), len(towels), boards_in_segment, running_boards) - total_boards = (len(segments) + len(towels) + len(running_segments)) * boards_in_segment + if 'bdcg' in segments[0]['class']: + # only a single-segment match will yield td.bdcg cells with segment scores + total_boards = boards_in_segment + else: + PlayoffLogger.get('matchinfo').info( + 'HTML board count for match #%d, found: %d finished segments, %d towels, %d boards per segment and %d boards in running segment', + self.info.id, len(finished_segments), len(towels), boards_in_segment, running_boards) + total_boards = (len(segments) + len(towels) + len(running_segments)) * boards_in_segment played_boards = (len(towels) + len(finished_segments)) * boards_in_segment + running_boards PlayoffLogger.get('matchinfo').info( 'HTML board count for match #%d: %d/%d', -- cgit v1.2.3 From 1ab6233374e1c25e3f9f58db98b547125cbef452 Mon Sep 17 00:00:00 2001 From: emkael Date: Fri, 28 Sep 2018 15:46:28 +0200 Subject: Revert running segment link if there are no scores in the segment (possibly "Don't send scores") Part of #7 is implented by this change --- jfr_playoff/matchinfo.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) (limited to 'jfr_playoff/matchinfo.py') diff --git a/jfr_playoff/matchinfo.py b/jfr_playoff/matchinfo.py index 900c210..dd2ee30 100644 --- a/jfr_playoff/matchinfo.py +++ b/jfr_playoff/matchinfo.py @@ -245,6 +245,15 @@ class MatchInfo: def __get_html_running_boards(self, cell): return int(cell.contents[-1].strip()) + def __get_html_segment_board_count(self, segment_url): + segment_content = p_remote.fetch(segment_url) + board_rows = [row for row in segment_content.find_all('tr') if len(row.select('td.bdcc a.zb')) > 0] + board_count = len(board_rows) + played_boards = len([ + row for row in board_rows if len( + ''.join([cell.text.strip() for cell in row.select('td.bdc')])) > 0]) + return played_boards, board_count + def __get_finished_info(self, cell): segment_link = cell.select('a[href]') if len(segment_link) > 0: @@ -252,12 +261,7 @@ class MatchInfo: r'\.htm$', '.html', urljoin(self.info.link, segment_link[0]['href'])) try: - segment_content = p_remote.fetch(segment_url) - board_rows = [row for row in segment_content.find_all('tr') if len(row.select('td.bdcc a.zb')) > 0] - board_count = len(board_rows) - played_boards = len([ - row for row in board_rows if len( - ''.join([cell.text.strip() for cell in row.select('td.bdc')])) > 0]) + played_boards, board_count = self.__get_html_segment_board_count(segment_url) PlayoffLogger.get('matchinfo').info( 'HTML played boards count for segment: %d/%d', played_boards, board_count) @@ -373,6 +377,7 @@ class MatchInfo: def __determine_running_link(self): if self.info.link is None: return + match_link = self.info.link link_match = re.match(r'^(.*)runda(\d+)\.html$', self.info.link) if link_match: try: @@ -390,6 +395,21 @@ class MatchInfo: PlayoffLogger.get('matchinfo').warning( 'cannot determine running link from HTML for match #%d: %s(%s)', self.info.id, type(e).__name__, str(e)) + if self.info.link != match_link: + # we've detected a running segment link + # we should check if the segment's uploaded live + try: + boards_played, board_count = self.__get_html_segment_board_count(re.sub('\.htm$', '.html', self.info.link)) + except IOError as e: + PlayoffLogger.get('matchinfo').warning( + 'cannot determine running link (%s) board count for match #%d: %s(%s)', + self.info.link, self.info.id, type(e).__name__, str(e)) + boards_played = 0 + if not boards_played: + PlayoffLogger.get('matchinfo').warning( + 'running link (%s) for match #%d is not live, reverting to match link (%s)', + self.info.link, self.info.id, match_link) + self.info.link = match_link def set_phase_link(self, phase_link): if self.info.link is None: -- cgit v1.2.3 From 72e52cf8c572f262d022acaa1eb1aa65bccb099e Mon Sep 17 00:00:00 2001 From: emkael Date: Fri, 28 Sep 2018 17:37:16 +0200 Subject: Refactoring of some config dictionary default value fetches --- jfr_playoff/data.py | 37 +++++++++++++------------------------ jfr_playoff/generator.py | 5 ++--- jfr_playoff/matchinfo.py | 5 +---- jfr_playoff/tournamentinfo.py | 7 ++++--- 4 files changed, 20 insertions(+), 34 deletions(-) (limited to 'jfr_playoff/matchinfo.py') diff --git a/jfr_playoff/data.py b/jfr_playoff/data.py index 9f9a0c2..6adf00c 100644 --- a/jfr_playoff/data.py +++ b/jfr_playoff/data.py @@ -36,18 +36,16 @@ class PlayoffData(object): def generate_phases(self): self.grid = [] for phase in self.phases: - phase_count = len(phase['matches']) - if 'dummies' in phase: - phase_count += len(phase['dummies']) + dummies = phase.get('dummies', []) + phase_count = len(phase['matches']) + len(dummies) phase_object = Phase() phase_object.title = phase['title'] - phase_object.link = phase['link'] if 'link' in phase else None + phase_object.link = phase.get('link', None) phase_object.matches = [None] * phase_count phase_pos = 0 for match in phase['matches']: - if 'dummies' in phase: - while phase_pos in phase['dummies']: - phase_pos += 1 + while phase_pos in dummies: + phase_pos += 1 phase_object.matches[phase_pos] = match['id'] phase_pos += 1 PlayoffLogger.get('data').info('phase object: %s', phase_object) @@ -73,8 +71,7 @@ class PlayoffData(object): def get_swiss_link(self, event): event_info = TournamentInfo(event, self.database) swiss_link = event_info.get_results_link() - if ('relative_path' in event) and ( - event['relative_path'] is not None): + if event.get('relative_path', None): swiss_link = '%s/%s' % (event['relative_path'], swiss_link) PlayoffLogger.get('data').info('swiss link: %s', swiss_link) return swiss_link @@ -94,16 +91,8 @@ class PlayoffData(object): event['ties'] = teams event_info = TournamentInfo(event, self.database) if event_info.is_finished(): - swiss_position = ( - event['swiss_position'] - if 'swiss_position' in event - else 1 - ) - position_limit = ( - event['position_to'] - if 'position_to' in event - else 9999 - ) + swiss_position = event.get('swiss_position', 1) + position_limit = event.get('position_to', 9999) place = 1 swiss_results = event_info.get_tournament_results() for team in swiss_results: @@ -156,7 +145,7 @@ class PlayoffData(object): swiss_info = [{ 'link': self.get_swiss_link(event), 'position': event['position'], - 'label': event['label'] if 'label' in event else None, + 'label': event.get('label', None), 'finished': TournamentInfo(event, self.database).is_finished() } for event in self.swiss] PlayoffLogger.get('data').info('swiss info: %s', swiss_info) @@ -166,10 +155,10 @@ class PlayoffData(object): dimensions = ( len(self.phases), max([ - len(phase['matches']) + len(phase['dummies']) - if 'dummies' in phase - else len(phase['matches']) - for phase in self.phases])) + len(phase['matches']) + len(phase.get('dummies', [])) + for phase in self.phases + ]) + ) PlayoffLogger.get('data').info('grid dimensions: %s', dimensions) return dimensions diff --git a/jfr_playoff/generator.py b/jfr_playoff/generator.py index 5dfed13..d358d86 100644 --- a/jfr_playoff/generator.py +++ b/jfr_playoff/generator.py @@ -57,8 +57,7 @@ class PlayoffGenerator(object): team_label = ' / '.join([ self.data.get_shortname(name) for name in team.name.split('
')]) - label_max_length = self.page['label_length_limit'] \ - if 'label_length_limit' in self.page else 0 + label_max_length = self.page.get('label_length_limit', 0) if label_max_length: team_label = team_label[:label_max_length] + (team_label[label_max_length:] and '(...)') team_html = self.p_temp.get( @@ -199,7 +198,7 @@ class PlayoffGenerator(object): info = [] for event in self.data.get_swiss_info(): event_label = self.p_temp.get('SWISS_DEFAULT_LABEL', event['position']) - if 'label' in event and event['label'] is not None: + if event.get('label', None): event_label = event['label'] info.append((self.p_temp.get('SWISS_LINK', event['link'], event_label) \ diff --git a/jfr_playoff/matchinfo.py b/jfr_playoff/matchinfo.py index dd2ee30..ab742d4 100644 --- a/jfr_playoff/matchinfo.py +++ b/jfr_playoff/matchinfo.py @@ -191,10 +191,7 @@ class MatchInfo: if scores_fetched: PlayoffLogger.get('matchinfo').info( 'pre-defined scores for match #%d fetched', self.info.id) - if 'running' in self.config: - self.info.running = int(self.config['running']) - else: - self.info.running = -1 + self.info.running = int(self.config.get('running', -1)) if not teams_fetched: try: try: diff --git a/jfr_playoff/tournamentinfo.py b/jfr_playoff/tournamentinfo.py index a646d4e..45a7752 100644 --- a/jfr_playoff/tournamentinfo.py +++ b/jfr_playoff/tournamentinfo.py @@ -169,11 +169,12 @@ class TournamentInfo: PlayoffLogger.get('tournamentinfo').warning( 'cannot determine tournament results from HTML: %s(%s)', type(e).__name__, str(e)) - if self.is_finished() and 'final_positions' in self.settings: + if self.is_finished(): + final_positions = self.settings.get('final_positions', []) PlayoffLogger.get('tournamentinfo').info( 'setting final positions from tournament results: %s', - self.settings['final_positions']) - for position in self.settings['final_positions']: + final_positions) + for position in final_positions: if len(teams) >= position: teams[position-1].append(position) return teams -- cgit v1.2.3