def setup_method(self, *args, **kwargs): flexmock(Boxscores) \ .should_receive('_get_team_details') \ .and_return((None, None, None, None, None, None)) flexmock(Boxscores) \ .should_receive('_find_games') \ .and_return(None) self.boxscores = Boxscores(None, None)
class TestNFLBoxscores: @patch('requests.get', side_effect=mock_pyquery) def setup_method(self, *args, **kwargs): flexmock(Boxscores) \ .should_receive('_get_team_details') \ .and_return((None, None, None, None, None, None)) flexmock(Boxscores) \ .should_receive('_find_games') \ .and_return(None) self.boxscores = Boxscores(None, None) def test_improper_loser_boxscore_format_skips_game(self): mock_html = pq("""<table class="teams"> <tbody> <tr class="date"><td colspan=3>Dec 9, 2018</td></tr> <tr class="winner"> <td><a href="/teams/nyj/2018.htm">New York Jets</a></td> <td class="right">27</td> <td class="right gamelink"> <a href="/boxscores/201812090buf.htm">Final</a> </td> </tr> <tr class="loser"> <td class="right">23</td> <td class="right"> </td> </tr> </tbody> </table>""") games = self.boxscores._extract_game_info([mock_html]) assert len(games) == 0 def test_improper_winner_boxscore_format_skips_game(self): mock_html = pq("""<table class="teams"> <tbody> <tr class="date"><td colspan=3>Dec 9, 2018</td></tr> <tr class="winner"> <td class="right">27</td> <td class="right gamelink"> </td> </tr> <tr class="loser"> <td><a href="/teams/buf/2018.htm">Buffalo Bills</a></td> <td class="right">23</td> <td class="right"> </td> </tr> </tbody> </table>""") games = self.boxscores._extract_game_info([mock_html]) assert len(games) == 0
def create_boxscore_dfs(season, start, end): b_games = [] player_dict = dict() player_stats = [] box_id = [] box_keys = {} box_games = Boxscores(start, season, end).games for k, v in box_games.items(): for box in v: box_keys[box['boxscore']] = k.split('-')[0] box_id.append(box['boxscore']) #box_id = [ids['boxscore'] for v in box_games.values() for ids in v] for ids in box_id: box = Boxscore(ids) for p in box.home_players: if p not in player_dict.keys(): player_dict[p.name] = p.player_id temp_hplayer_df = p.dataframe temp_hplayer_df['box_id'] = ids temp_hplayer_df['team'] = box.home_abbreviation temp_hplayer_df['name'] = p.name player_stats.append(temp_hplayer_df) for p in box.away_players: if p not in player_dict.keys(): player_dict[p.name] = p.player_id temp_aplayer_df = p.dataframe temp_aplayer_df['box_id'] = ids temp_aplayer_df['team'] = box.away_abbreviation temp_aplayer_df['name'] = p.name player_stats.append(temp_aplayer_df) b_games.append(box.dataframe) boxscore_stats_df = pd.concat(player_stats) box_df = pd.concat(b_games) # Temp cut out to try and work it into the first nested loop #box_keys ={} #for k,v in box_games.items(): # for box in v: # box_keys[box['boxscore']] = k.split('-')[0] # creates in the boxscore_stats_df a column for what week the game was played boxscore_stats_df['week'] = boxscore_stats_df['box_id'].map( lambda x: box_keys[x]) # Creates a column for the week the game was played box_df = box_df.reset_index().rename(columns={'index': 'box_id'}) box_df['week'] = box_df['box_id'].map(lambda x: box_keys[x]) box_df = box_df.set_index('box_id') return boxscore_stats_df, box_df, player_dict
def get_game_stats_for_season(self): week_list = list(range(1, 18)) nfl_game_stats = pd.DataFrame() year = 2019 for week in week_list: week_str = str(week) year_str = str(year) date = week_str + "-" + year_str week_results = Boxscores(week, year) week_stats_df = pd.DataFrame() for game in week_results.games[date]: game_id = game['boxscore'] game_stats = Boxscore(game_id) game_results = pd.DataFrame(game, index=[0]) away_team_stats, home_team_stats = self.game_stats_cleanup(game_results, game_stats) away_team_stats['week'] = week home_team_stats['week'] = week week_stats_df = pd.concat([week_stats_df, away_team_stats]) week_stats_df = pd.concat([week_stats_df, home_team_stats]) nfl_game_stats = pd.concat([nfl_game_stats, week_stats_df]) return nfl_game_stats
def get_games_data(season): games_data = [] weeks = Boxscores(start_week, season, end_week) for key in weeks.games: for game in weeks.games[key]: print("Fetching {} data".format(game['boxscore'])) games_data.append(Boxscore(game['boxscore']).dataframe) return pd.concat(games_data)
def lastweek(): games_today = Boxscores(12, 2019) stef = games_today._boxscores week = "12" year = "2019" numberOfGames = len(stef[week + '-' + year]) winners = [] for i in range(numberOfGames): f = stef['12-2019'][i]['winning_name'] winners.append(f) lens = len(winners) return render_template('lastweek.html', lens=lens, winners=winners, usersLeauges=currentuser.usersCurrentLeauges)
def makepicks(team): global currentuser error = None gamesThisWeek = Boxscores(14, 2019) Libary = gamesThisWeek._boxscores week = "14" year = "2019" numberOfGames = len(Libary[week + '-' + year]) games = [] global currentuser for i in range(numberOfGames): home = Libary['14-2019'][i]['home_name'] away = Libary['14-2019'][i]['away_name'] hmm = {'Home': home, 'Away': away} games.append(hmm) alreadyPicked = [] full_path = os.path.realpath(__file__) directory = os.path.dirname(full_path) + "/picks" if request.method == 'POST': with open(directory + "/" + team + ".csv", 'r') as csv_file: for row2 in csv_file: info = row2.split(',') WhoPicked = info[0] alreadyPicked.append(WhoPicked) if currentuser.username in alreadyPicked: error = "You have already made your picks this week" return render_template( 'main.html', usersLeauges=currentuser.usersCurrentLeauges, error=error) else: with open('picks/%s.csv' % team, 'a') as picksOut: picksOut.write(currentuser.username + ',') for x in range(1, 15): picksOut.write(request.form['row-%s' % str(x)] + ',') return redirect(url_for('home')) return render_template('makepicks.html', games=games, error=error, usersLeauges=currentuser.usersCurrentLeauges, team=team)
def get_nfl_schedule(self): week_list = list(range(1, 18)) nfl_schedule = pd.DataFrame() year = 2019 for week in week_list: week_str = str(week) year_str = str(year) date = week_str + "-" + year_str week_results = Boxscores(week, year) week_results_df = pd.DataFrame() for game in week_results.games[date]: game_results = pd.DataFrame(game, index=[0])[ ['away_name', 'away_abbr', 'home_name', 'home_abbr', 'winning_name', 'winning_abbr']] game_results['week'] = week week_results_df = pd.concat([week_results_df, game_results]) nfl_schedule = pd.concat([nfl_schedule, week_results_df]).reset_index().drop(columns='index') return nfl_schedule
from sportsreference.nfl.boxscore import Boxscores games_today = Boxscores(1, 2019) # Prints a dictionary of all matchups for week 1 of 2017 print(games_today.games)
def test_boxscores_search_string_representation_multi_week( self, *args, **kwargs): result = Boxscores(7, 2017, 8) assert result.__repr__() == 'NFL games for weeks 7, 8'
def test_boxscores_search_multiple_weeks(self, *args, **kwargs): expected = { '7-2017': [{ 'boxscore': '201710190rai', 'away_name': 'Kansas City Chiefs', 'away_abbr': 'kan', 'away_score': 30, 'home_name': 'Oakland Raiders', 'home_abbr': 'rai', 'home_score': 31, 'winning_name': 'Oakland Raiders', 'winning_abbr': 'rai', 'losing_name': 'Kansas City Chiefs', 'losing_abbr': 'kan' }, { 'boxscore': '201710220chi', 'away_name': 'Carolina Panthers', 'away_abbr': 'car', 'away_score': 3, 'home_name': 'Chicago Bears', 'home_abbr': 'chi', 'home_score': 17, 'winning_name': 'Chicago Bears', 'winning_abbr': 'chi', 'losing_name': 'Carolina Panthers', 'losing_abbr': 'car' }, { 'boxscore': '201710220buf', 'away_name': 'Tampa Bay Buccaneers', 'away_abbr': 'tam', 'away_score': 27, 'home_name': 'Buffalo Bills', 'home_abbr': 'buf', 'home_score': 30, 'winning_name': 'Buffalo Bills', 'winning_abbr': 'buf', 'losing_name': 'Tampa Bay Buccaneers', 'losing_abbr': 'tam' }, { 'boxscore': '201710220ram', 'away_name': 'Arizona Cardinals', 'away_abbr': 'crd', 'away_score': 0, 'home_name': 'Los Angeles Rams', 'home_abbr': 'ram', 'home_score': 33, 'winning_name': 'Los Angeles Rams', 'winning_abbr': 'ram', 'losing_name': 'Arizona Cardinals', 'losing_abbr': 'crd' }, { 'boxscore': '201710220min', 'away_name': 'Baltimore Ravens', 'away_abbr': 'rav', 'away_score': 16, 'home_name': 'Minnesota Vikings', 'home_abbr': 'min', 'home_score': 24, 'winning_name': 'Minnesota Vikings', 'winning_abbr': 'min', 'losing_name': 'Baltimore Ravens', 'losing_abbr': 'rav' }, { 'boxscore': '201710220mia', 'away_name': 'New York Jets', 'away_abbr': 'nyj', 'away_score': 28, 'home_name': 'Miami Dolphins', 'home_abbr': 'mia', 'home_score': 31, 'winning_name': 'Miami Dolphins', 'winning_abbr': 'mia', 'losing_name': 'New York Jets', 'losing_abbr': 'nyj' }, { 'boxscore': '201710220gnb', 'away_name': 'New Orleans Saints', 'away_abbr': 'nor', 'away_score': 26, 'home_name': 'Green Bay Packers', 'home_abbr': 'gnb', 'home_score': 17, 'winning_name': 'New Orleans Saints', 'winning_abbr': 'nor', 'losing_name': 'Green Bay Packers', 'losing_abbr': 'gnb' }, { 'boxscore': '201710220clt', 'away_name': 'Jacksonville Jaguars', 'away_abbr': 'jax', 'away_score': 27, 'home_name': 'Indianapolis Colts', 'home_abbr': 'clt', 'home_score': 0, 'winning_name': 'Jacksonville Jaguars', 'winning_abbr': 'jax', 'losing_name': 'Indianapolis Colts', 'losing_abbr': 'clt' }, { 'boxscore': '201710220cle', 'away_name': 'Tennessee Titans', 'away_abbr': 'oti', 'away_score': 12, 'home_name': 'Cleveland Browns', 'home_abbr': 'cle', 'home_score': 9, 'winning_name': 'Tennessee Titans', 'winning_abbr': 'oti', 'losing_name': 'Cleveland Browns', 'losing_abbr': 'cle' }, { 'boxscore': '201710220sfo', 'away_name': 'Dallas Cowboys', 'away_abbr': 'dal', 'away_score': 40, 'home_name': 'San Francisco 49ers', 'home_abbr': 'sfo', 'home_score': 10, 'winning_name': 'Dallas Cowboys', 'winning_abbr': 'dal', 'losing_name': 'San Francisco 49ers', 'losing_abbr': 'sfo' }, { 'boxscore': '201710220sdg', 'away_name': 'Denver Broncos', 'away_abbr': 'den', 'away_score': 0, 'home_name': 'Los Angeles Chargers', 'home_abbr': 'sdg', 'home_score': 21, 'winning_name': 'Los Angeles Chargers', 'winning_abbr': 'sdg', 'losing_name': 'Denver Broncos', 'losing_abbr': 'den' }, { 'boxscore': '201710220pit', 'away_name': 'Cincinnati Bengals', 'away_abbr': 'cin', 'away_score': 14, 'home_name': 'Pittsburgh Steelers', 'home_abbr': 'pit', 'home_score': 29, 'winning_name': 'Pittsburgh Steelers', 'winning_abbr': 'pit', 'losing_name': 'Cincinnati Bengals', 'losing_abbr': 'cin' }, { 'boxscore': '201710220nyg', 'away_name': 'Seattle Seahawks', 'away_abbr': 'sea', 'away_score': 24, 'home_name': 'New York Giants', 'home_abbr': 'nyg', 'home_score': 7, 'winning_name': 'Seattle Seahawks', 'winning_abbr': 'sea', 'losing_name': 'New York Giants', 'losing_abbr': 'nyg' }, { 'boxscore': '201710220nwe', 'away_name': 'Atlanta Falcons', 'away_abbr': 'atl', 'away_score': 7, 'home_name': 'New England Patriots', 'home_abbr': 'nwe', 'home_score': 23, 'winning_name': 'New England Patriots', 'winning_abbr': 'nwe', 'losing_name': 'Atlanta Falcons', 'losing_abbr': 'atl' }, { 'boxscore': '201710230phi', 'away_name': 'Washington Redskins', 'away_abbr': 'was', 'away_score': 24, 'home_name': 'Philadelphia Eagles', 'home_abbr': 'phi', 'home_score': 34, 'winning_name': 'Philadelphia Eagles', 'winning_abbr': 'phi', 'losing_name': 'Washington Redskins', 'losing_abbr': 'was' }], '8-2017': [{ 'boxscore': '201710260rav', 'away_name': 'Miami Dolphins', 'away_abbr': 'mia', 'away_score': 0, 'home_name': 'Baltimore Ravens', 'home_abbr': 'rav', 'home_score': 40, 'winning_name': 'Baltimore Ravens', 'winning_abbr': 'rav', 'losing_name': 'Miami Dolphins', 'losing_abbr': 'mia' }, { 'boxscore': '201710290cle', 'away_name': 'Minnesota Vikings', 'away_abbr': 'min', 'away_score': 33, 'home_name': 'Cleveland Browns', 'home_abbr': 'cle', 'home_score': 16, 'winning_name': 'Minnesota Vikings', 'winning_abbr': 'min', 'losing_name': 'Cleveland Browns', 'losing_abbr': 'cle' }, { 'boxscore': '201710290buf', 'away_name': 'Oakland Raiders', 'away_abbr': 'rai', 'away_score': 14, 'home_name': 'Buffalo Bills', 'home_abbr': 'buf', 'home_score': 34, 'winning_name': 'Buffalo Bills', 'winning_abbr': 'buf', 'losing_name': 'Oakland Raiders', 'losing_abbr': 'rai' }, { 'boxscore': '201710290tam', 'away_name': 'Carolina Panthers', 'away_abbr': 'car', 'away_score': 17, 'home_name': 'Tampa Bay Buccaneers', 'home_abbr': 'tam', 'home_score': 3, 'winning_name': 'Carolina Panthers', 'winning_abbr': 'car', 'losing_name': 'Tampa Bay Buccaneers', 'losing_abbr': 'tam' }, { 'boxscore': '201710290phi', 'away_name': 'San Francisco 49ers', 'away_abbr': 'sfo', 'away_score': 10, 'home_name': 'Philadelphia Eagles', 'home_abbr': 'phi', 'home_score': 33, 'winning_name': 'Philadelphia Eagles', 'winning_abbr': 'phi', 'losing_name': 'San Francisco 49ers', 'losing_abbr': 'sfo' }, { 'boxscore': '201710290nyj', 'away_name': 'Atlanta Falcons', 'away_abbr': 'atl', 'away_score': 25, 'home_name': 'New York Jets', 'home_abbr': 'nyj', 'home_score': 20, 'winning_name': 'Atlanta Falcons', 'winning_abbr': 'atl', 'losing_name': 'New York Jets', 'losing_abbr': 'nyj' }, { 'boxscore': '201710290nwe', 'away_name': 'Los Angeles Chargers', 'away_abbr': 'sdg', 'away_score': 13, 'home_name': 'New England Patriots', 'home_abbr': 'nwe', 'home_score': 21, 'winning_name': 'New England Patriots', 'winning_abbr': 'nwe', 'losing_name': 'Los Angeles Chargers', 'losing_abbr': 'sdg' }, { 'boxscore': '201710290nor', 'away_name': 'Chicago Bears', 'away_abbr': 'chi', 'away_score': 12, 'home_name': 'New Orleans Saints', 'home_abbr': 'nor', 'home_score': 20, 'winning_name': 'New Orleans Saints', 'winning_abbr': 'nor', 'losing_name': 'Chicago Bears', 'losing_abbr': 'chi' }, { 'boxscore': '201710290cin', 'away_name': 'Indianapolis Colts', 'away_abbr': 'clt', 'away_score': 23, 'home_name': 'Cincinnati Bengals', 'home_abbr': 'cin', 'home_score': 24, 'winning_name': 'Cincinnati Bengals', 'winning_abbr': 'cin', 'losing_name': 'Indianapolis Colts', 'losing_abbr': 'clt' }, { 'boxscore': '201710290sea', 'away_name': 'Houston Texans', 'away_abbr': 'htx', 'away_score': 38, 'home_name': 'Seattle Seahawks', 'home_abbr': 'sea', 'home_score': 41, 'winning_name': 'Seattle Seahawks', 'winning_abbr': 'sea', 'losing_name': 'Houston Texans', 'losing_abbr': 'htx' }, { 'boxscore': '201710290was', 'away_name': 'Dallas Cowboys', 'away_abbr': 'dal', 'away_score': 33, 'home_name': 'Washington Redskins', 'home_abbr': 'was', 'home_score': 19, 'winning_name': 'Dallas Cowboys', 'winning_abbr': 'dal', 'losing_name': 'Washington Redskins', 'losing_abbr': 'was' }, { 'boxscore': '201710290det', 'away_name': 'Pittsburgh Steelers', 'away_abbr': 'pit', 'away_score': 20, 'home_name': 'Detroit Lions', 'home_abbr': 'det', 'home_score': 15, 'winning_name': 'Pittsburgh Steelers', 'winning_abbr': 'pit', 'losing_name': 'Detroit Lions', 'losing_abbr': 'det' }, { 'boxscore': '201710300kan', 'away_name': 'Denver Broncos', 'away_abbr': 'den', 'away_score': 19, 'home_name': 'Kansas City Chiefs', 'home_abbr': 'kan', 'home_score': 29, 'winning_name': 'Kansas City Chiefs', 'winning_abbr': 'kan', 'losing_name': 'Denver Broncos', 'losing_abbr': 'den' }] } result = Boxscores(7, 2017, 8).games assert result == expected
def test_boxscores_search_invalid_end(self, *args, **kwargs): result = Boxscores(7, 2017, 5).games assert result == self.expected
def test_boxscores_search(self, *args, **kwargs): result = Boxscores(7, 2017).games assert result == self.expected
def get_game_codes(week, year): p = re.compile("'(\\d{4}\\d+\\w+)'") selected_week = Boxscores(int(week), year) return p.findall(str(selected_week.games.values()))
def calculate_elo(self, team_dict, abbrev_dict, start_year, end_year, end_week): endw = 22 week = 1 excel_name = 'NFLelo' + start_year + '-' + end_year + 'week' + end_week + '.xlsx' wb = xlsxwriter.Workbook(excel_name) start_year = int(start_year) end_year = int(end_year) # CONSTANT K FOR ELO ALGO k = 30 p = re.compile("'(\\d{4}\\d+\\w+)'") team_objects = {} self.output_text.delete(1.0, "end-1c") for name, abbrev in team_dict.items(): new_team = teamobj.NflTeam(name, abbrev) team_objects.update({abbrev: new_team}) for year in range(start_year, end_year + 1): # Initialize new excel sheet sheet = wb.add_worksheet(str(year)) sheet.set_column(0, 0, 24) sheet.set_column(1, 1, 10) sheet.write(0, 0, "Team") sheet.write(0, 1, "Elo Rating") sheet.write(0, 2, "Wins") sheet.write(0, 3, "Losses") if year > start_year: team_objects = elo_regression(team_objects) # will iterate through weeks 1-21 if year == end_year: endw = int(end_week) + 1 for week in range(1, endw): print("----- YEAR ", year, " | WEEK:", week, "-----") selected_week = Boxscores(week, year) game_codes = p.findall(str(selected_week.games.values())) for game in game_codes: box = Boxscore(game) winner = team_objects[box.winning_abbr] loser = team_objects[box.losing_abbr] # elo prob_winner = self.probability(loser.elo, winner.elo) prob_loser = self.probability(winner.elo, loser.elo) winner.elo = winner.elo + k * (1 - prob_winner) loser.elo = loser.elo + k * (0 - prob_loser) welo = round(winner.elo, 4) lelo = round(loser.elo, 4) print(winner.name, str(welo)) print(loser.name, str(lelo)) # update elo at end of year to regress 1/3 to mean if year == end_year and week == 21: team_objects = elo_regression(team_objects) n = 1 excel_dict = OrderedDict(sorted(team_objects.items(), key=lambda x: x[1].elo, reverse=True)) for abv, tobj in excel_dict.items(): for name, ab in team_dict.items(): if ab == abv: sheet.write(n, 0, name) sheet.write(n, 1, tobj.elo) # SportsReference Library Team Lookups is Currently Broken as of 11/25/19 # for team in Teams(year): # if team.abbreviation == abv: # sheet.write(n, 2, team.wins) # sheet.write(n, 3, team.losses) n = n + 1 # SportsReference Library Team Lookups is Currently Broken as of 11/25/19 # rank = 1 # newdict = OrderedDict(sorted(team_objects.items(), key=lambda x: x[1].elo, reverse=True)) # for abv, tobj in newdict.items(): # name = abbrev_dict[abv] # self.output_text.insert("end-1c", '{:4s}{:24s}{:9s}\n'.format(str(rank) + '.', name, str(tobj.elo))) # rank = rank + 1 # Close Excel Workbook wb.close()
def test_boxscores_search(self, *args, **kwargs): expected = { 'boxscores': [ {'home_name': 'Oakland Raiders', 'home_abbr': 'rai', 'boxscore': '201710190rai', 'away_name': 'Kansas City Chiefs', 'away_abbr': 'kan'}, {'home_name': 'Chicago Bears', 'home_abbr': 'chi', 'boxscore': '201710220chi', 'away_name': 'Carolina Panthers', 'away_abbr': 'car'}, {'home_name': 'Buffalo Bills', 'home_abbr': 'buf', 'boxscore': '201710220buf', 'away_name': 'Tampa Bay Buccaneers', 'away_abbr': 'tam'}, {'home_name': 'Los Angeles Rams', 'home_abbr': 'ram', 'boxscore': '201710220ram', 'away_name': 'Arizona Cardinals', 'away_abbr': 'crd'}, {'home_name': 'Minnesota Vikings', 'home_abbr': 'min', 'boxscore': '201710220min', 'away_name': 'Baltimore Ravens', 'away_abbr': 'rav'}, {'home_name': 'Miami Dolphins', 'home_abbr': 'mia', 'boxscore': '201710220mia', 'away_name': 'New York Jets', 'away_abbr': 'nyj'}, {'home_name': 'Green Bay Packers', 'home_abbr': 'gnb', 'boxscore': '201710220gnb', 'away_name': 'New Orleans Saints', 'away_abbr': 'nor'}, {'home_name': 'Indianapolis Colts', 'home_abbr': 'clt', 'boxscore': '201710220clt', 'away_name': 'Jacksonville Jaguars', 'away_abbr': 'jax'}, {'home_name': 'Cleveland Browns', 'home_abbr': 'cle', 'boxscore': '201710220cle', 'away_name': 'Tennessee Titans', 'away_abbr': 'oti'}, {'home_name': 'San Francisco 49ers', 'home_abbr': 'sfo', 'boxscore': '201710220sfo', 'away_name': 'Dallas Cowboys', 'away_abbr': 'dal'}, {'home_name': 'Los Angeles Chargers', 'home_abbr': 'sdg', 'boxscore': '201710220sdg', 'away_name': 'Denver Broncos', 'away_abbr': 'den'}, {'home_name': 'Pittsburgh Steelers', 'home_abbr': 'pit', 'boxscore': '201710220pit', 'away_name': 'Cincinnati Bengals', 'away_abbr': 'cin'}, {'home_name': 'New York Giants', 'home_abbr': 'nyg', 'boxscore': '201710220nyg', 'away_name': 'Seattle Seahawks', 'away_abbr': 'sea'}, {'home_name': 'New England Patriots', 'home_abbr': 'nwe', 'boxscore': '201710220nwe', 'away_name': 'Atlanta Falcons', 'away_abbr': 'atl'}, {'home_name': 'Philadelphia Eagles', 'home_abbr': 'phi', 'boxscore': '201710230phi', 'away_name': 'Washington Redskins', 'away_abbr': 'was'}, ] } result = Boxscores(7, 2017).games assert result == expected
with open(path + '.csv', 'a', newline='') as file: wr = csv.writer(file, dialect='excel') wr.writerow(item_list) #Stores old directory and changes current first_directory = os.getcwd() change_directory('/Database/') game_list = ['Year', 'Week', 'Home', 'Home Score', 'Away', 'Away Score', 'ID'] database('Boxscore-Database', game_list) for year in range(2010, 2020): for week in range(1, 18): game_list[0] = year game_list[1] = week games = Boxscores(week, year) for f_key, f_value in games.games.items(): for current_dict in f_value: for s_key, s_value in current_dict.items(): if s_key == 'boxscore': game_list[6] = s_value elif s_key == 'home_name': game_list[2] = s_value elif s_key == 'home_score': game_list[3] = s_value elif s_key == 'away_name': game_list[4] = s_value elif s_key == 'away_score': game_list[5] = s_value if game_list[2] == 'San Diego Chargers':
from sportsreference.nfl.boxscore import Boxscores games_today = Boxscores(18, 2018) # Prints a dictionary of all matchups for week 1 of 2017 print(games_today.games) #games_today.game stef = games_today._boxscores # print(games_today._boxscores['18-2018']['winning_name']) print(type(stef)) f = stef['18-2018'][0]['winning_name'] print(stef.get(0)) #g = f[0] #win = g['winning_name'] bad = 6
def teamThree(): PickCheckerList = [] full_path = os.path.realpath(__file__) directory = os.path.dirname(full_path) + "/picks" with open(directory + '/%s.csv' % currentuser.usersCurrentLeauges[0], 'r') as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') for row in csv_reader: ListOfWinners = [] if len(row) != 0: name = row[0] RedoRow = row RedoRow.pop(0) for item in RedoRow: if item != '': ListOfWinners.append(item) Done = PickChecker(name, ListOfWinners) PickCheckerList.append(Done) games_today = Boxscores(9, 2019) stef = games_today._boxscores week = "9" year = "2019" numberOfGames = len(stef[week + '-' + year]) winners = [] ListOfLeadboardRow = [] for i in range(numberOfGames): f = stef['9-2019'][i]['winning_name'] winners.append(f) directory = os.path.dirname(full_path) + "/leagues" with open(directory + '/%s.csv' % currentuser.usersCurrentLeauges[0], 'r') as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') for row in csv_reader: row = LeadboardRow(row[0], int(row[1])) ListOfLeadboardRow.append(row) for item in PickCheckerList: count = 0 for WinnerPicked in item.picks: if WinnerPicked in winners: count = count + 1 for LeadboardRowCheck in ListOfLeadboardRow: if LeadboardRowCheck.username == item.username: LeadboardRowCheck.score = int(LeadboardRowCheck.score) + count directory = os.path.dirname(full_path) + "/leagues" with open(directory + '/%s.csv' % currentuser.usersCurrentLeauges[2], 'w') as csv_file: for item in ListOfLeadboardRow: csv_file.write(str(item.username) + "," + str(item.score) + "\n") directory = os.path.dirname(full_path) + "/picks" empty = False with open(directory + '/%s.csv' % currentuser.usersCurrentLeauges[2], 'a+') as csv_file: emptycheck = csv_file.read(1) if not emptycheck: empty = True if empty == False: with open(directory + '/%s.csv' % currentuser.usersCurrentLeauges[2], 'w+') as csv_file: NeedThisForIndent = 5 ListOfLeadboardRow.sort(key=lambda x: x.score, reverse=True) return render_template('teamThree.html', Leaders=ListOfLeadboardRow, usersLeauges=currentuser.usersCurrentLeauges)