def test_match_fields(self): ''' Checks the fields of matches ''' session = database.get_session() match = MatchRepository(session).get_by_id_site('IXDncKQQ') self.assertEqual(match[MatchNotation.HOME_TEAM], 'Fluminense') self.assertEqual(match[MatchNotation.AWAY_TEAM], 'Guarani') self.assertEqual(match[MatchNotation.HOME_GOALS], 1) self.assertEqual(match[MatchNotation.AWAY_GOALS], 0) self.assertEqual( match[MatchNotation.RESULTS][OddsTypeNotation.MONEY_LINE], OddsNotation.MoneyLine.HOME) matchModel = Match(match) self.assertEqual(matchModel.get_money_line_result(), OddsNotation.MoneyLine.HOME) self.assertEqual(matchModel.get_both_teams_to_score_result(), OddsNotation.BothTeamsToScore.NO) match = MatchRepository(session).get_by_id_site('l0MZkxJ0') self.assertEqual(match[MatchNotation.HOME_TEAM], 'Goias') self.assertEqual(match[MatchNotation.AWAY_TEAM], 'Corinthians') self.assertEqual(match[MatchNotation.HOME_GOALS], 1) self.assertEqual(match[MatchNotation.AWAY_GOALS], 1) self.assertEqual( match[MatchNotation.RESULTS][OddsTypeNotation.MONEY_LINE], OddsNotation.MoneyLine.DRAW) matchModel = Match(match) self.assertEqual(matchModel.get_money_line_result(), OddsNotation.MoneyLine.DRAW) self.assertEqual(matchModel.get_both_teams_to_score_result(), OddsNotation.BothTeamsToScore.YES) match = MatchRepository(session).get_by_id_site('zJAzNd9i') self.assertEqual(match[MatchNotation.HOME_TEAM], 'Portuguesa') self.assertEqual(match[MatchNotation.AWAY_TEAM], 'Sao Paulo') self.assertEqual(match[MatchNotation.HOME_GOALS], 2) self.assertEqual(match[MatchNotation.AWAY_GOALS], 3) self.assertEqual( match[MatchNotation.RESULTS][OddsTypeNotation.MONEY_LINE], OddsNotation.MoneyLine.AWAY) matchModel = Match(match) self.assertEqual(matchModel.get_money_line_result(), OddsNotation.MoneyLine.AWAY) self.assertEqual(matchModel.get_both_teams_to_score_result(), OddsNotation.BothTeamsToScore.YES)
def process_favorites(self, season): """ Processes favorites """ for match in SeasonRepository(self.session).get_matches(season): favorites = MatchBusiness.get_favorites(match) if bool(favorites): fields = {MatchNotation.odds_favorites(): favorites} MatchRepository(self.session).update(match, fields)
def process_summary_odds(self, season): """ Process summary odds """ for match in SeasonRepository(self.session).get_matches(season): summary = MatchBusiness.get_resume_odds(match) if bool(summary): fields = {MatchNotation.odds_summary(): summary} MatchRepository(self.session).update(match, fields)
def create_odds(self, season): """ Create Odds """ for match in SeasonRepository(self.session).get_matches(season): odds_list = MatchOddsScraper.extract(match) if bool(odds_list): fields = {MatchNotation.odds_list(): odds_list} MatchRepository(self.session).update(match, fields) logger.debug('Created odds: ' + str(match))
def create_matches(self, season): matches = SeasonScraper.extract_matches(season) for match in matches: try: MatchRepository(self.session).insert(match) except DuplicateKeyError as error: logger.debug(error) pass
def test_performance(self): session = database.get_session() match = MatchRepository(session).get_by_id_site('bTr4iQt3') p_filter = { PerformanceNotation.NEXT_MATCH: match[MatchNotation.ID], PerformanceNotation.TEAM: 'Lazio' } performances = PerformanceRepository(session).list(filter=p_filter) self.assertEqual(performances.count(), 3) for p in performances: print(p) if (p[PerformanceNotation.LOCAL] == PerformanceNotation.Local.OVERALL): self.assertEqual(p[PerformanceNotation.POINTS], 3) self.assertEqual(p[PerformanceNotation.WINS], 0) self.assertEqual(p[PerformanceNotation.DRAWS], 3) self.assertEqual(p[PerformanceNotation.LOSES], 1) self.assertEqual(p[PerformanceNotation.MATCHES_PLAYED], 4) self.assertEqual(p[PerformanceNotation.MATCHES_PLAYED_HOME], 2) self.assertEqual(p[PerformanceNotation.MATCHES_PLAYED_AWAY], 2) self.assertEqual(p[PerformanceNotation.LAST_MATCHES_NUM], 4) self.assertEqual(p[PerformanceNotation.GOALS_FOR], 3) self.assertEqual(p[PerformanceNotation.GOALS_AGAINST], 4) elif p[PerformanceNotation. LOCAL] == PerformanceNotation.Local.HOME: self.assertEqual(p[PerformanceNotation.POINTS], 2) self.assertEqual(p[PerformanceNotation.WINS], 0) self.assertEqual(p[PerformanceNotation.DRAWS], 2) self.assertEqual(p[PerformanceNotation.LOSES], 0) self.assertEqual(p[PerformanceNotation.MATCHES_PLAYED], 4) self.assertEqual(p[PerformanceNotation.MATCHES_PLAYED_HOME], 2) self.assertEqual(p[PerformanceNotation.MATCHES_PLAYED_AWAY], 2) self.assertEqual(p[PerformanceNotation.LAST_MATCHES_NUM], 2) self.assertEqual(p[PerformanceNotation.GOALS_FOR], 2) self.assertEqual(p[PerformanceNotation.GOALS_AGAINST], 2) elif p[PerformanceNotation. LOCAL] == PerformanceNotation.Local.AWAY: self.assertEqual(p[PerformanceNotation.POINTS], 1) self.assertEqual(p[PerformanceNotation.WINS], 0) self.assertEqual(p[PerformanceNotation.DRAWS], 1) self.assertEqual(p[PerformanceNotation.LOSES], 1) self.assertEqual(p[PerformanceNotation.MATCHES_PLAYED], 4) self.assertEqual(p[PerformanceNotation.LAST_MATCHES_NUM], 2) self.assertEqual(p[PerformanceNotation.GOALS_FOR], 1) self.assertEqual(p[PerformanceNotation.GOALS_AGAINST], 2)
def update_rounds(self, season): matches = list(SeasonRepository(self.session).get_matches(season).sort(MatchNotation.DATE)) max_matches_per_round = season[SeasonNotation.NUM_MATCHES_PER_ROUND] matches = MatchBusiness.set_round_groups(matches, max_matches_per_round) for match in matches: fields = {MatchNotation.NUMBER: match[MatchNotation.NUMBER], MatchNotation.ROUND_GROUP: match[MatchNotation.ROUND_GROUP]} MatchRepository(self.session).update(match, fields)
def process_results(self, season): matches = SeasonRepository(self.session).get_matches(season) for match in matches: try: match[MatchNotation.RESULTS] = dict() MatchBusiness.set_result_money_line(match) MatchBusiness.set_result_both_teams_to_score(match) fields_to_update = {MatchNotation.RESULTS: match[MatchNotation.RESULTS]} MatchRepository(self.session).update(match, fields_to_update) except Exception as e: logger.warning(e) pass
def process_hits(self, season): matches = SeasonRepository(self.session).get_matches(season) for match in matches: match[MatchNotation.HITS] = dict() MatchBusiness.process_hits(match, OddsTypeNotation.BOTH_TEAMS_TO_SCORE, OddsFavoritesNotation.FAVORITE, ) MatchBusiness.process_hits(match, OddsTypeNotation.MONEY_LINE, OddsFavoritesNotation.FAVORITE) if bool(match[MatchNotation.HITS]): fields_to_update = {MatchNotation.HITS: match[MatchNotation.HITS]} MatchRepository(self.session).update(match, fields_to_update)
def test_match_odds(self): ''' Checks the odds of matches ''' session = database.get_session() match = MatchRepository(session).get_by_id_site('8KEmQeVQ') matchModel = Match(match) odds_ml = matchModel.get_odds_list(OddsTypeNotation.MONEY_LINE) odds_bts = matchModel.get_odds_list( OddsTypeNotation.BOTH_TEAMS_TO_SCORE) self.assertEqual(len(odds_ml), 25) self.assertEqual(len(odds_bts), 13) ml_summary = matchModel.get_odds_summary(OddsTypeNotation.MONEY_LINE) bts_summary = matchModel.get_odds_summary( OddsTypeNotation.BOTH_TEAMS_TO_SCORE) self.assertIsNotNone(ml_summary) self.assertIsNotNone(bts_summary) ml_favorites = matchModel.get_odds_favorites( OddsTypeNotation.MONEY_LINE) bts_favorites = matchModel.get_odds_favorites( OddsTypeNotation.BOTH_TEAMS_TO_SCORE) self.assertIsNotNone(ml_favorites) self.assertIsNotNone(bts_favorites) self.assertEqual( matchModel.get_favorite_column(OddsTypeNotation.MONEY_LINE), OddsNotation.MoneyLine.HOME) self.assertEqual( matchModel.get_favorite_column( OddsTypeNotation.BOTH_TEAMS_TO_SCORE), OddsNotation.BothTeamsToScore.NO) ml_hits = matchModel.get_hits(OddsTypeNotation.MONEY_LINE) bts_hits = matchModel.get_hits(OddsTypeNotation.BOTH_TEAMS_TO_SCORE) self.assertEqual(ml_hits[OddsFavoritesNotation.FAVORITE], True) self.assertEqual(bts_hits[OddsFavoritesNotation.FAVORITE], False)
def test_first_match(self): session = database.get_session() match = MatchRepository(session).get_by_id_site('OvMLBTBD') p_filter = {PerformanceNotation.NEXT_MATCH: match[MatchNotation.ID]} markov_chains = MarkovChainRepository(session).list(filter=p_filter) self.assertEqual(markov_chains.count(), 6) for mc in markov_chains: self.assertEqual(mc[PerformanceNotation.MATCHES_PLAYED], 0) self.assertEqual(mc[PerformanceNotation.MATCHES_PLAYED_HOME], 0) self.assertEqual(mc[PerformanceNotation.MATCHES_PLAYED_AWAY], 0) self.assertEqual(mc[PerformanceNotation.LAST_MATCHES_NUM], 0) self.assertIn(mc[PerformanceNotation.TEAM],['Lazio','Torino']) chains = mc[MarkovChainNotation.CHAINS] self.assertEqual(len(chains),2)
def test_first_match(self): session = database.get_session() match = MatchRepository(session).get_by_id_site('OvMLBTBD') p_filter = {PerformanceNotation.NEXT_MATCH: match[MatchNotation.ID]} performances = PerformanceRepository(session).list(filter=p_filter) self.assertEqual(performances.count(), 6) for p in performances: self.assertEqual(p[PerformanceNotation.POINTS], 0) self.assertEqual(p[PerformanceNotation.WINS], 0) self.assertEqual(p[PerformanceNotation.DRAWS], 0) self.assertEqual(p[PerformanceNotation.LOSES], 0) self.assertEqual(p[PerformanceNotation.MATCHES_PLAYED], 0) self.assertEqual(p[PerformanceNotation.MATCHES_PLAYED_HOME], 0) self.assertEqual(p[PerformanceNotation.MATCHES_PLAYED_AWAY], 0) self.assertEqual(p[PerformanceNotation.LAST_MATCHES_NUM], 0) self.assertEqual(p[PerformanceNotation.GOALS_FOR], 0) self.assertEqual(p[PerformanceNotation.GOALS_AGAINST], 0) self.assertIn(p[PerformanceNotation.TEAM], ['Lazio', 'Torino'])
def unset_odds(self): fields = {MatchNotation.ODDS: True} MatchRepository(self.session).unset(dict(), fields)
def drop(self): return MatchRepository(self.session).drop()
def create_indexes(self): return MatchRepository(self.session).create_index(MatchNotation.ID_SITE, unique=True)
def test_markov_chain(self): session = database.get_session() match = MatchRepository(session).get_by_id_site('UPfBnP5G') p_filter = {PerformanceNotation.NEXT_MATCH: match[MatchNotation.ID], PerformanceNotation.TEAM: 'Napoli'} markov_chains = MarkovChainRepository(session).list(filter=p_filter) self.assertEqual(markov_chains.count(), 3) for mc in markov_chains: if (mc[MarkovChainNotation.LOCAL] == MarkovChainNotation.Local.OVERALL): scored = mc[MarkovChainNotation.CHAINS][MarkovChainNotation.SCORED] conceded = mc[MarkovChainNotation.CHAINS][MarkovChainNotation.CONCEDED] self.assertEqual(scored['0_0'], 0) self.assertEqual(scored['0_1'], 1) self.assertEqual(scored['1_0'], 0.2857) self.assertEqual(scored['1_1'], 0.7143) self.assertEqual(scored['current_state'], 0) self.assertEqual(conceded['0_0'], 0.75) self.assertEqual(conceded['0_1'], 0.25) self.assertEqual(conceded['1_0'], 0.2) self.assertEqual(conceded['1_1'], 0.8) self.assertEqual(conceded['current_state'], 1) if (mc[MarkovChainNotation.LOCAL] == MarkovChainNotation.Local.HOME): scored = mc[MarkovChainNotation.CHAINS][MarkovChainNotation.SCORED] conceded = mc[MarkovChainNotation.CHAINS][MarkovChainNotation.CONCEDED] self.assertEqual(scored['0_0'], 0) self.assertEqual(scored['0_1'], 1) self.assertEqual(scored['1_0'], 0) self.assertEqual(scored['1_1'], 1) self.assertEqual(scored['current_state'], 1) self.assertEqual(conceded['0_0'], 0.5) self.assertEqual(conceded['0_1'], 0.5) self.assertEqual(conceded['1_0'], 0.5) self.assertEqual(conceded['1_1'], 0.5) self.assertEqual(conceded['current_state'], 1) if (mc[MarkovChainNotation.LOCAL] == MarkovChainNotation.Local.AWAY): scored = mc[MarkovChainNotation.CHAINS][MarkovChainNotation.SCORED] conceded = mc[MarkovChainNotation.CHAINS][MarkovChainNotation.CONCEDED] self.assertEqual(scored['0_0'], 0) self.assertEqual(scored['0_1'], 1) self.assertEqual(scored['1_0'], 0.6667) self.assertEqual(scored['1_1'], 0.3333) self.assertEqual(scored['current_state'], 0) self.assertEqual(conceded['0_0'], 0.5) self.assertEqual(conceded['0_1'], 0.5) self.assertEqual(conceded['1_0'], 0) self.assertEqual(conceded['1_1'], 1) self.assertEqual(conceded['current_state'], 1)
def create_dataframe(self, replace=False): if replace or not DataframeManager.is_file_exists('Total'): filter = {} projection = { MatchNotation.ID: 1, MatchNotation.ID_SITE: 1, MatchNotation.RESULTS: 1, MatchNotation.HOME_TEAM: 1, MatchNotation.AWAY_TEAM: 1, MatchNotation.HOME_GOALS: 1, MatchNotation.AWAY_GOALS: 1, MatchNotation.season_initial_year(): 1, MatchNotation.championship_name(): 1, MatchNotation.odds_summary(): 1, MatchNotation.odds_favorites(): 1, MatchNotation.HITS: 1, MatchNotation.ROUND_GROUP: 1 } matches = MatchRepository(self.session).list(filter, projection) content = list() for m in matches: filter = {PerformanceNotation.NEXT_MATCH: m[MatchNotation.ID]} projection = { PerformanceNotation.ID: 0, PerformanceNotation.NEXT_MATCH: 0, PerformanceNotation.SEASON_ID: 0, PerformanceNotation.LAST_MATCHES_NUM: 0 } performances = PerformanceRepository(self.session).list( filter, projection) perf_home = dict() perf_away = dict() for p in performances: if p[PerformanceNotation.TEAM] == m[ MatchNotation.HOME_TEAM]: if p[PerformanceNotation. LOCAL] == PerformanceNotation.Local.OVERALL: perf_home[PerformanceNotation.Local.OVERALL] = p elif p[PerformanceNotation. LOCAL] == PerformanceNotation.Local.HOME: perf_home[PerformanceNotation.Local.HOME] = p else: perf_home[PerformanceNotation.Local.AWAY] = p else: if p[PerformanceNotation. LOCAL] == PerformanceNotation.Local.OVERALL: perf_away[PerformanceNotation.Local.OVERALL] = p elif p[PerformanceNotation. LOCAL] == PerformanceNotation.Local.HOME: perf_away[PerformanceNotation.Local.HOME] = p else: perf_away[PerformanceNotation.Local.AWAY] = p del p[PerformanceNotation.TEAM] del p[PerformanceNotation.LOCAL] markov_chains = MarkovChainRepository(self.session).list( filter, projection) mc_home = dict() mc_away = dict() for mc in markov_chains: if mc[PerformanceNotation.TEAM] == m[ MatchNotation.HOME_TEAM]: if mc[PerformanceNotation. LOCAL] == PerformanceNotation.Local.OVERALL: mc_home[PerformanceNotation.Local.OVERALL] = mc elif mc[PerformanceNotation. LOCAL] == PerformanceNotation.Local.HOME: mc_home[PerformanceNotation.Local.HOME] = mc else: mc_home[PerformanceNotation.Local.AWAY] = mc else: if mc[PerformanceNotation. LOCAL] == PerformanceNotation.Local.OVERALL: mc_away[PerformanceNotation.Local.OVERALL] = mc elif mc[PerformanceNotation. LOCAL] == PerformanceNotation.Local.HOME: mc_away[PerformanceNotation.Local.HOME] = mc else: mc_away[PerformanceNotation.Local.AWAY] = mc del mc[PerformanceNotation.TEAM] del mc[PerformanceNotation.LOCAL] perf = { 'm': m, 'ph': perf_home, 'pa': perf_away, 'kh': mc_home, 'ka': mc_away } content.append(perf) DataframeManager.create_file('Total', content)
def unset_hits(self): fields = {MatchNotation.HITS: True} MatchRepository(self.session).unset(dict(), fields)