def parse_boxscores(predictor, teams, skip_save_to_mongodb): games_list = [] match_info = [] prediction_stats = [] stdev_dict = find_stdev_for_every_stat(teams) today = datetime.today() today_string = '%s-%s-%s' % (today.month, today.day, today.year) for game in Boxscores(today).games[today_string]: # Skip the games that are not between two DI teams since stats are not # saved for those teams. if game['non_di']: continue for sim in range(NUM_SIMS): home = Team(game['home_name'], game['home_abbr']) away = Team(game['away_name'], game['away_abbr']) title = '%s at %s' % (away.name, home.name) game_info = GameInfo(home, away, title) games_list.append(game_info) match_stats = get_match_stats(game, stdev_dict) prediction_stats.append(match_stats) home_name = game['home_name'] away_name = game['away_name'] if game['home_rank']: home_name = '(%s) %s' % (game['home_rank'], home_name) if game['away_rank']: away_name = '(%s) %s' % (game['away_rank'], away_name) g = MatchInfo(away_name, home_name, game['away_abbr'], game['home_abbr'], game['top_25'], None, match_stats) match_info.append(g) predictions = make_predictions(prediction_stats, games_list, match_info, predictor) save_predictions(predictions, skip_save_to_mongodb)
def get_scores(year, month ,day): home_teams = [] away_teams = [] home_scores = [] away_scores = [] games = Boxscores(datetime(year, month, day)) all_games = games.games[str(month)+'-'+str(day)+'-'+str(year)] for game in all_games: home_teams.append(game['home_name']) away_teams.append(game['away_name']) home_scores.append(game['home_score']) away_scores.append(game['away_score']) name = str(year)+"_"+str(month)+"_"+str(day)+"_scores.csv" with open(name, 'w', newline='') as file: writer = csv.writer(file) writer.writerow(["home_team","away_team","home_score","away_score"]) for i,team in enumerate(home_teams): writer.writerow([home_teams[i],away_teams[i],home_scores[i],away_scores[i]]) path = os.getcwd() os.rename(path+"\\"+name, path+"\\"+"scores\\"+name) return home_teams,away_teams,home_scores,away_scores
def get_yesterdays_scores(): today = datetime.today() - timedelta(days=1) date = datetime.today().strftime('%Y%m%d') games_today = Boxscores(today).games[today.strftime('%-m-%-d-%Y')] output = [] for g in games_today: if g['away_score'] == 'None': print('Not all scores available yet') break else: try: output.append({ 'date': date, 'away': team_alt_names[g['away_name']], 'home': team_alt_names[g['home_name']], "away_score": g['away_score'], "home_score": g['home_score'] }) except: print('Cant find team name in matchup: {} v. {}'.format( g['away_name'], g['home_name'])) outcome = pd.DataFrame(data=output) outcome["key"] = outcome["date"] + outcome["home"] outcome = outcome[[ 'key', 'date', 'away', 'home', "away_score", "home_score" ]] td.append_df_to_excel('Historic_Scores', outcome)
def find_todays_games(teams): games_list = [] today = datetime.today() today_string = '%s-%s-%s' % (today.month, today.day, today.year) for game in Boxscores(today).games[today_string]: # Skip the games that are not between two DI teams since stats are not # saved for those teams. if game['non_di']: continue games_list.append(populate_game_info(teams, game)) return games_list
def iterate_files(files): total_games = 0 total_correct = 0 for filename in files: date = get_date(filename) saved_data = get_saved_prediction(filename) games = Boxscores(date).games[filename.replace('.json', '')] saved_data, num_games, num_correct = parse_boxscore(games, saved_data) total_games += num_games total_correct += num_correct save_json(saved_data, 'predictions/%s' % filename) print('=' * 80) print(' Accuracy: %s%%' % round(100.0 * float(total_correct) / \ float(total_games), 2)) print('=' * 80)
class TestNCAABBoxscores: @patch('requests.get', side_effect=mock_pyquery) def setup_method(self, *args, **kwargs): flexmock(Boxscores) \ .should_receive('_find_games') \ .and_return(None) self.boxscores = Boxscores(None) def test_boxscore_with_no_score_returns_none(self): mock_html = pq("""<table class="teams"> <tbody> <tr class="loser"> <td><a href="/cbb/schools/south-dakota/2019.html">South Dakota</a></td> <td class="right"></td> <td class="right gamelink"> </td> </tr> <tr class="loser"> <td><a href="/cbb/schools/kansas/2019.html">Kansas</a>\ <span class='pollrank'> (1) </span></td> <td class="right"></td> <td class="right"> </td> </tr> </tbody> </table>""") games = self.boxscores._extract_game_info([mock_html]) assert games == [ { 'home_name': 'Kansas', 'home_abbr': 'kansas', 'away_name': 'South Dakota', 'away_abbr': 'south-dakota', 'boxscore': '', 'non_di': False, 'top_25': True, 'home_score': None, 'home_rank': 1, 'away_score': None, 'away_rank': None, 'winning_name': None, 'winning_abbr': None, 'losing_name': None, 'losing_abbr': None } ]
def main(): min_date = date(2021, 11, 9) max_date = date(2022, 3, 14) print("Fetching Games") boxs = Boxscores(min_date, max_date) print("Writing Games") with open('../../data/gamelist_20220314.pkl', 'wb') as fh: pickle.dump(boxs.games, fh) print("Fetching Teams") teams = Teams() print("Writing Teams") with open('../../data/teamlist_20220314.pkl', 'wb') as fh: pickle.dump(teams, fh)
def test_boxscores_search(self, *args, **kwargs): expected = { 'boxscores': [ { 'home_name': 'Buffalo', 'away_abbr': 'canisius', 'home_abbr': 'buffalo', 'boxscore': '2017-11-11-13-buffalo', 'away_name': 'Canisius', 'non_di': False }, { 'home_name': 'Fairfield', 'away_abbr': 'pennsylvania', 'home_abbr': 'fairfield', 'boxscore': '2017-11-11-13-fairfield', 'away_name': 'Penn', 'non_di': False }, { 'home_name': 'Florida Gulf Coast', 'away_abbr': 'illinois-state', 'home_abbr': 'florida-gulf-coast', 'boxscore': '2017-11-11-13-florida-gulf-coast', 'away_name': 'Illinois State', 'non_di': False }, { 'home_name': 'Bradley', 'away_abbr': 'iupui', 'home_abbr': 'bradley', 'boxscore': '2017-11-11-14-bradley', 'away_name': 'IUPUI', 'non_di': False }, { 'home_name': 'Ohio', 'away_abbr': 'alabama-am', 'home_abbr': 'ohio', 'boxscore': '2017-11-11-14-ohio', 'away_name': 'Alabama A&M', 'non_di': False }, { 'home_name': 'Quinnipiac', 'away_abbr': 'dartmouth', 'home_abbr': 'quinnipiac', 'boxscore': '2017-11-11-14-quinnipiac', 'away_name': 'Dartmouth', 'non_di': False }, { 'home_name': 'Western Michigan', 'away_abbr': 'Siena Heights', 'home_abbr': 'western-michigan', 'boxscore': '2017-11-11-14-western-michigan', 'away_name': 'Siena Heights', 'non_di': True }, { 'home_name': 'Appalachian State', 'away_abbr': 'Toccoa Falls', 'home_abbr': 'appalachian-state', 'boxscore': '2017-11-11-15-appalachian-state', 'away_name': 'Toccoa Falls', 'non_di': True }, { 'home_name': 'Brown', 'away_abbr': 'Johnson & Wales (RI)', 'home_abbr': 'brown', 'boxscore': '2017-11-11-15-brown', 'away_name': 'Johnson & Wales (RI)', 'non_di': True }, { 'home_name': 'Drake', 'away_abbr': 'Coe', 'home_abbr': 'drake', 'boxscore': '2017-11-11-15-drake', 'away_name': 'Coe', 'non_di': True }, { 'home_name': 'La Salle', 'away_abbr': 'saint-peters', 'home_abbr': 'la-salle', 'boxscore': '2017-11-11-15-la-salle', 'away_name': 'St. Peter\'s', 'non_di': False }, { 'home_name': 'Longwood', 'away_abbr': 'stephen-f-austin', 'home_abbr': 'longwood', 'boxscore': '2017-11-11-15-longwood', 'away_name': 'Stephen F. Austin', 'non_di': False }, { 'home_name': 'Little Rock', 'away_abbr': 'Ouachita', 'home_abbr': 'arkansas-little-rock', 'boxscore': '2017-11-11-16-arkansas-little-rock', 'away_name': 'Ouachita', 'non_di': True }, { 'home_name': 'UCSB', 'away_abbr': 'north-dakota-state', 'home_abbr': 'california-santa-barbara', 'boxscore': '2017-11-11-16-california-santa-barbara', 'away_name': 'North Dakota State', 'non_di': False }, { 'home_name': 'DePaul', 'away_abbr': 'notre-dame', 'home_abbr': 'depaul', 'boxscore': '2017-11-11-16-depaul', 'away_name': 'Notre Dame', 'non_di': False }, { 'home_name': 'Duquesne', 'away_abbr': 'st-francis-ny', 'home_abbr': 'duquesne', 'boxscore': '2017-11-11-17-duquesne', 'away_name': 'St. Francis (NY)', 'non_di': False }, { 'home_name': 'NJIT', 'away_abbr': 'wagner', 'home_abbr': 'njit', 'boxscore': '2017-11-11-18-njit', 'away_name': 'Wagner', 'non_di': False }, { 'home_name': 'Akron', 'away_abbr': 'cleveland-state', 'home_abbr': 'akron', 'boxscore': '2017-11-11-19-akron', 'away_name': 'Cleveland State', 'non_di': False }, { 'home_name': 'Duke', 'away_abbr': 'utah-valley', 'home_abbr': 'duke', 'boxscore': '2017-11-11-19-duke', 'away_name': 'Utah Valley', 'non_di': False }, { 'home_name': 'Elon', 'away_abbr': 'Peace', 'home_abbr': 'elon', 'boxscore': '2017-11-11-19-elon', 'away_name': 'Peace', 'non_di': True }, { 'home_name': 'Marist', 'away_abbr': 'lehigh', 'home_abbr': 'marist', 'boxscore': '2017-11-11-19-marist', 'away_name': 'Lehigh', 'non_di': False }, { 'home_name': 'Michigan', 'away_abbr': 'north-florida', 'home_abbr': 'michigan', 'boxscore': '2017-11-11-19-michigan', 'away_name': 'North Florida', 'non_di': False }, { 'home_name': 'North Carolina-Wilmington', 'away_abbr': 'North Carolina Wesleyan', 'home_abbr': 'north-carolina-wilmington', 'boxscore': '2017-11-11-19-north-carolina-wilmington', 'away_name': 'North Carolina Wesleyan', 'non_di': True }, { 'home_name': 'Presbyterian', 'away_abbr': 'Johnson University', 'home_abbr': 'presbyterian', 'boxscore': '2017-11-11-19-presbyterian', 'away_name': 'Johnson University', 'non_di': True }, { 'home_name': 'Sam Houston State', 'away_abbr': 'Texas-Tyler', 'home_abbr': 'sam-houston-state', 'boxscore': '2017-11-11-19-sam-houston-state', 'away_name': 'Texas-Tyler', 'non_di': True }, { 'home_name': 'South Dakota', 'away_abbr': 'Mayville State', 'home_abbr': 'south-dakota', 'boxscore': '2017-11-11-19-south-dakota', 'away_name': 'Mayville State', 'non_di': True }, { 'home_name': 'Toledo', 'away_abbr': 'saint-josephs', 'home_abbr': 'toledo', 'boxscore': '2017-11-11-19-toledo', 'away_name': 'St. Joseph\'s', 'non_di': False }, { 'home_name': 'Nebraska', 'away_abbr': 'eastern-illinois', 'home_abbr': 'nebraska', 'boxscore': '2017-11-11-20-nebraska', 'away_name': 'Eastern Illinois', 'non_di': False }, { 'home_name': 'Saint Mary\'s (CA)', 'away_abbr': 'saint-francis-pa', 'home_abbr': 'saint-marys-ca', 'boxscore': '2017-11-11-20-saint-marys-ca', 'away_name': 'Saint Francis (PA)', 'non_di': False }, { 'home_name': 'Texas-Arlington', 'away_abbr': 'loyola-marymount', 'home_abbr': 'texas-arlington', 'boxscore': '2017-11-11-20-texas-arlington', 'away_name': 'Loyola Marymount', 'non_di': False }, { 'home_name': 'Western Illinois', 'away_abbr': 'Saint Mary\'s (MN)', 'home_abbr': 'western-illinois', 'boxscore': '2017-11-11-20-western-illinois', 'away_name': 'Saint Mary\'s (MN)', 'non_di': True }, { 'home_name': 'BYU', 'away_abbr': 'mississippi-valley-state', 'home_abbr': 'brigham-young', 'boxscore': '2017-11-11-21-brigham-young', 'away_name': 'Mississippi Valley State', 'non_di': False }, { 'home_name': 'Kent State', 'away_abbr': 'youngstown-state', 'home_abbr': 'kent-state', 'boxscore': '2017-11-11-21-kent-state', 'away_name': 'Youngstown State', 'non_di': False }, { 'home_name': 'New Mexico', 'away_abbr': 'Northern New Mexico', 'home_abbr': 'new-mexico', 'boxscore': '2017-11-11-21-new-mexico', 'away_name': 'Northern New Mexico', 'non_di': True }, { 'home_name': 'UNLV', 'away_abbr': 'florida-am', 'home_abbr': 'nevada-las-vegas', 'boxscore': '2017-11-11-22-nevada-las-vegas', 'away_name': 'Florida A&M', 'non_di': False }, { 'home_name': 'Portland', 'away_abbr': 'portland-state', 'home_abbr': 'portland', 'boxscore': '2017-11-11-22-portland', 'away_name': 'Portland State', 'non_di': False }, ] } result = Boxscores(datetime(2017, 11, 11)).games assert result == expected
def projections(): home = request.args.get('home') away = request.args.get('away') if home is None and away is None: today = dt.today() today_start = today.replace(hour=0, minute=0, second=0, microsecond=0) today_end = today_start + timedelta(1) games = db.session.query(Game).filter(Game.date > today_start, Game.date < today_end).all() all_games = db.session.query(Game).all() if len(all_games) > 9500: db.session.query(Game).delete() db.session.commit() # If today's games have not been retrieved yet then do so from sports # And get the projected score from the data science API if len(games) == 0: games_today = Boxscores(dt.today()) games_today = games_today.games rankings = Rankings() rankings = rankings.current for date in games_today.keys(): for game in games_today[date]: home_team = db.session.query(Team).filter( Team.name == game["home_name"]).first() away_team = db.session.query(Team).filter( Team.name == game["away_name"]).first() if home_team is None: home_team = Team(id=game["home_abbr"], name=game["home_name"]) db.session.add(home_team) try: db.session.commit() home_team = db.session.query(Team).filter( Team.name == game["home_name"]).first() except: db.session.rollback() elif away_team is None: away_team = Team(id=game["away_abbr"], name=game["away_name"]) db.session.add(away_team) try: db.session.commit() away_team = db.session.query(Team).filter( Team.name == game["away_name"]).first() except: db.session.rollback() print("000ooof") if home_team and away_team: data = { "year": dt.today().year, "month": dt.today().month, "day": dt.today().day, "home_name": home_team.name, "home_rank": float(rankings.get(home_team.id.lower(), 0)), "away_name": away_team.name, "away_rank": float(rankings.get(away_team.id.lower(), 0)) } res = requests.post("http://ncaab.herokuapp.com/", data=json.dumps(data)) res = res.json() home_projection = res["home_score"] away_projection = res["away_score"] new_game = Game(date=date, home=home_team.id, away=away_team.id, home_projection=home_projection, away_projection=away_projection) db.session.add(new_game) games.append(new_game) # TODO: Test this out... # Update yesterday's games with the scores # yesterday_start = today_start - timedelta(1) # games_yesterday = Boxscores(yesterday_start, today_start) # games_yesterday = games_yesterday.games # for date in games_yesterday.keys(): # for game in games_yesterday[date]: # home_team = db.session.query(Team).filter( # Team.name == game["home_name"]).first() # away_team = db.session.query(Team).filter( # Team.name == game["away_name"]).first() # print(home_team, away_team) # applicable_game = db.session.query(Game).filter( # Game.date > yesterday_start, Game.date < today_start, Game.home == home_team.id, Game.away == away_team.id).first() # print(applicable_game) # applicable_game.home_score = game["home_score"] # applicable_game.away_score = game["away_score"] try: db.session.commit() except: db.session.rollback() games_without_names = [g.serialize() for g in games] games_with_names = [] for game in games_without_names: home_team = db.session.query(Team).filter( Team.id == game["home_id"]).first() away_team = db.session.query(Team).filter( Team.id == game["away_id"]).first() print("oof") game["home_name"] = home_team.name game["away_name"] = away_team.name games_with_names.append(game) return jsonify(games_with_names) elif home is None or away is None: return ( "'home' and 'away' are necessary query parameters when not using the 'all' query parameter" ) else: rankings = Rankings() rankings = rankings.current print(home) print(away) home_team = db.session.query(Team).filter(Team.id == home).first() away_team = db.session.query(Team).filter(Team.id == away).first() print("ooooof") data = { "year": dt.today().year, "month": dt.today().month, "day": dt.today().day, "home_name": home_team.name, "home_rank": float(rankings.get(home_team.id.lower(), 0)), "away_name": away_team.name, "away_rank": float(rankings.get(away_team.id.lower(), 0)) } res = requests.post("http://ncaab.herokuapp.com/", data=json.dumps(data)) res = res.json() home_projection = res["home_score"] away_projection = res["away_score"] print("ummmmm") return jsonify({ 'home_id': home_team.id, 'away_id': away_team.id, 'home_name': home_team.name, 'away_name': away_team.name, 'home_projection': home_projection, 'away_projection': away_projection })
datetime.date(2014, 3, 21), datetime.date(2013, 3, 21), datetime.date(2013, 3, 22), datetime.date(2012, 3, 15), datetime.date(2012, 3, 16), datetime.date(2011, 3, 17), datetime.date(2011, 3, 18), datetime.date(2010, 3, 18), datetime.date(2010, 3, 19), datetime.date(2009, 3, 19), datetime.date(2009, 3, 20) ] sSum = np.zeros(16) sCount = np.zeros(16) for dDate in dates_firstRound: gameList = Boxscores(dDate).games for day in gameList: for game in gameList[day]: hRank = game['home_rank'] vRank = game['away_rank'] hTeam = game['home_name'] vTeam = game['away_name'] hScore = game['home_score'] vScore = game['away_score'] if not (hScore is None or vScore is None): hDiff = hScore - vScore vDiff = -hDiff else: continue if not (hRank is None or vRank is None): sSum[hRank - 1] += hDiff
def setup_method(self, *args, **kwargs): flexmock(Boxscores) \ .should_receive('_find_games') \ .and_return(None) self.boxscores = Boxscores(None)
def ncaab_boxscores(game_date: Date): date_key = f"{game_date.month}-{game_date.day}-{game_date.year}" return Boxscores(game_date).games[date_key]
def get_todays_games(): todaysGames = [] teamData = get_team_data() today = datetime.today() games_today = Boxscores(today).games[today.strftime('%-m-%-d-%Y')] for game in games_today: try: awayName = team_alt_names[game['away_name']] homeName = team_alt_names[game['home_name']] awayInfo = teamData[awayName] homeInfo = teamData[homeName] ag = awayInfo.games_played hg = homeInfo.games_played away = ballgame( awayName, awayInfo.points / ag, awayInfo.field_goals / ag, awayInfo.field_goal_attempts / ag, awayInfo.three_point_field_goals / ag, awayInfo.three_point_field_goal_attempts / ag, awayInfo.two_point_field_goals / ag, awayInfo.two_point_field_goal_attempts / ag, awayInfo.free_throws / ag, awayInfo.free_throw_attempts / ag, awayInfo.offensive_rebounds / ag, awayInfo.defensive_rebounds / ag, awayInfo.assists / ag, awayInfo.turnovers / ag, awayInfo.steals / ag, awayInfo.field_goal_percentage, awayInfo.free_throw_percentage, homeInfo.opp_points / hg, homeInfo.opp_field_goals / hg, homeInfo.opp_field_goal_attempts / hg, homeInfo.opp_three_point_field_goal_attempts / hg, homeInfo.opp_three_point_field_goals / hg, homeInfo.opp_two_point_field_goal_attempts / hg, homeInfo.opp_free_throws / hg, homeInfo.opp_free_throw_attempts / hg, homeInfo.opp_defensive_rebounds / hg, homeInfo.opp_assists / hg, homeInfo.opp_turnovers / hg, homeInfo.opp_steals / hg, homeInfo.opp_blocks / hg, homeInfo.opp_personal_fouls / hg, homeInfo.opp_field_goal_percentage, homeInfo.opp_three_point_field_goal_percentage, homeInfo.opp_free_throw_percentage, ) home = ballgame( homeName, homeInfo.points / hg, homeInfo.field_goals / hg, homeInfo.field_goal_attempts / hg, homeInfo.three_point_field_goals / hg, homeInfo.three_point_field_goal_attempts / hg, homeInfo.two_point_field_goals / hg, homeInfo.two_point_field_goal_attempts / hg, homeInfo.free_throws / hg, homeInfo.free_throw_attempts / hg, homeInfo.offensive_rebounds / hg, homeInfo.defensive_rebounds / hg, homeInfo.assists / hg, homeInfo.turnovers / hg, homeInfo.steals / hg, homeInfo.field_goal_percentage, homeInfo.free_throw_percentage, awayInfo.opp_points / ag, awayInfo.opp_field_goals / ag, awayInfo.opp_field_goal_attempts / ag, awayInfo.opp_three_point_field_goal_attempts / ag, awayInfo.opp_three_point_field_goals / ag, awayInfo.opp_two_point_field_goal_attempts / ag, awayInfo.opp_free_throws / ag, awayInfo.opp_free_throw_attempts / ag, awayInfo.opp_defensive_rebounds / ag, awayInfo.opp_assists / ag, awayInfo.opp_turnovers / ag, awayInfo.opp_steals / ag, awayInfo.opp_blocks / ag, awayInfo.opp_personal_fouls / ag, awayInfo.opp_field_goal_percentage, awayInfo.opp_three_point_field_goal_percentage, awayInfo.opp_free_throw_percentage, ) todaysGames.append([away, home]) except: print(game['away_name'], game['home_name']) return todaysGames