James McCool
Refactor grab_contest_data function to adjust EntryId generation and DataFrame structure
cbe1c19
import pandas as pd | |
import requests | |
def grab_contest_data(sport, contest_name, contest_id_map, contest_date_map): | |
contest_date = contest_date_map[contest_name] | |
contest_id = contest_id_map[contest_name] | |
raw_url = f'https://dh5nxc6yx3kwy.cloudfront.net/contests/{sport.lower()}/{contest_date}/{contest_id}/' | |
data_url = raw_url + 'data/' | |
lineups_url = raw_url + 'lineups/' | |
def format_lineup_string(lineup_hash, positions): | |
"""Replaces colons in a lineup hash with sequential positions.""" | |
# Remove the leading colon and split by the remaining colons | |
player_ids = lineup_hash.lstrip(':').split(':') | |
# Check if the number of IDs matches the number of positions | |
if len(player_ids) != len(positions): | |
# Handle potential errors - maybe return the original hash or log a warning | |
print(f"Warning: Mismatch for hash {lineup_hash}. IDs: {len(player_ids)}, Positions: {len(positions)}") | |
return lineup_hash # Or some other error indication | |
# Combine positions and player IDs | |
combined_parts = [pos + pid for pos, pid in zip(positions, player_ids)] | |
# Join them into a single string | |
return "".join(combined_parts) | |
lineups_json = requests.get(lineups_url).json() | |
data_json = requests.get(data_url).json() | |
lineup_data = [] | |
player_data = [] | |
position_inserts = ['1B ', ' 2B ', ' 3B ', ' C ', ' OF ', ' OF ', ' OF ', ' P ', ' P ', ' SS '] | |
for players, player_info in data_json['players'].items(): | |
player_data.append({ | |
'fullName': player_info['fullName'], | |
'playerId': player_info['playerId'], | |
'rosterPosition': player_info['rosterPosition'], | |
'ownership': player_info['ownership'], | |
'actualPoints': player_info['actualPoints'] | |
}) | |
players_df = pd.DataFrame(player_data) | |
players_df = players_df.sort_values(by='ownership', ascending=False).reset_index(drop=True) | |
players_df = players_df.rename(columns={'fullName': 'Player', 'rosterPosition': 'Roster Position', 'ownership': '%Drafted', 'actualPoints': 'FPTS'}) | |
pid_map = dict(zip(players_df['playerId'].astype(str), players_df['Player'])) | |
for lineup_hash, lineup_info in lineups_json['lineups'].items(): | |
lineup_data.append({ | |
'lineupHash': lineup_hash, | |
'points': lineup_info['points'], | |
'entryNameList': lineup_info['entryNameList'][0] | |
}) | |
lineups_df = pd.DataFrame(lineup_data) | |
lineups_df = lineups_df.sort_values(by='points', ascending=False) | |
lineups_df = lineups_df.reset_index() | |
lineups_df['index'] = lineups_df.index + 1 | |
lineups_df['TimeRemaining'] = str(0) | |
lineups_df['EntryId'] = str(lineups_df['index']) + str(lineups_df['entryNameList']) | |
lineups_df['lineupHash'] = ':' + lineups_df['lineupHash'] | |
lineups_df = lineups_df.rename(columns={'index': 'Rank', 'points': 'Points', 'entryNameList': 'EntryName', 'lineupHash': 'Lineup'}) | |
lineups_df['EntryName'] = lineups_df['EntryName'] + ' (1/1)' | |
lineups_df['Lineup'] = lineups_df['Lineup'].apply(lambda x: format_lineup_string(x, position_inserts)) | |
lineups_df['Lineup'] = lineups_df['Lineup'].replace(pid_map, regex=True) | |
lineups_df = lineups_df[['Rank', 'EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup']] | |
total_data = lineups_df.merge(players_df, how='left', left_index=True, right_index=True) | |
return total_data |