| import pandas as pd |
| import requests |
|
|
| def grab_contest_data(sport, contest_name, contest_id_map, contest_date, contest_date2): |
|
|
| if sport == 'GOLF': |
| sport_var = 'pga' |
| else: |
| sport_var = sport.lower() |
| |
| contest_id = contest_id_map[contest_name] |
| raw_url = f'https://dh5nxc6yx3kwy.cloudfront.net/contests/{sport_var}/{contest_date}/{contest_id}/' |
| raw_url2 = f'https://dh5nxc6yx3kwy.cloudfront.net/contests/{sport_var}/{contest_date2}/{contest_id}/' |
|
|
| def format_lineup_string(lineup_hash, positions): |
| """Replaces colons in a lineup hash with sequential positions.""" |
| |
| |
| player_ids = [pid for pid in lineup_hash.lstrip(':').split(':') if pid.isdigit() or pid == '-1'] |
| |
| |
| actual_positions = positions[:len(player_ids)] |
| |
| |
| if len(player_ids) != len(actual_positions): |
| print(f"Warning: Mismatch for hash {lineup_hash}. IDs: {len(player_ids)}, Positions: {len(actual_positions)}") |
| return lineup_hash |
|
|
| |
| combined_parts = [] |
| for pos, pid in zip(actual_positions, player_ids): |
| if pid == '-1': |
| combined_parts.append(pos + '') |
| else: |
| combined_parts.append(pos + pid_map.get(pid, pid)) |
| |
| |
| |
| |
| |
| return "".join(combined_parts) |
|
|
| try: |
| lineups_json = requests.get(raw_url + 'lineups/').json() |
| data_json = requests.get(raw_url + 'data/').json() |
| except: |
| lineups_json = requests.get(raw_url2 + 'lineups/').json() |
| data_json = requests.get(raw_url2 + 'data/').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'], |
| 'salary': player_info['salary'], |
| 'currentTeam': player_info['currentTeam'], |
| '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', 'salary': 'Salary', 'currentTeam': 'Team'}) |
| 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'] = lineups_df['index'].astype(str) + lineups_df['entryNameList'].astype(str) |
| 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 = 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 |