File size: 4,337 Bytes
0841c51
 
 
c1c18f8
0841c51
f147e97
 
 
 
59e1d02
0841c51
f147e97
 
0841c51
 
 
 
4a0abde
 
0841c51
4a0abde
 
 
 
 
 
 
 
c375de1
 
 
 
 
 
 
3c6a773
c375de1
 
73a4cc3
 
032d464
0841c51
9cc55fb
 
 
 
 
 
0841c51
 
 
 
 
 
 
 
 
a8cf688
 
0841c51
 
 
 
 
 
 
a8cf688
c375de1
0841c51
 
 
 
 
 
 
 
 
 
 
 
 
e1f40de
0841c51
 
a8cfbf7
0841c51
cbe1c19
0841c51
 
 
ab18789
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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."""
        # Remove the leading colon and split by the remaining colons
        # Only split on colons that are between numbers
        player_ids = [pid for pid in lineup_hash.lstrip(':').split(':') if pid.isdigit() or pid == '-1']
        
        # Get the actual positions needed for this lineup
        actual_positions = positions[:len(player_ids)]
        
        # Check if the number of IDs matches the number of positions
        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

        # Combine positions and player IDs, only for valid player IDs
        combined_parts = []
        for pos, pid in zip(actual_positions, player_ids):
            if pid == '-1':  # Handle empty slots
                combined_parts.append(pos + '')
            else:
                combined_parts.append(pos + pid_map.get(pid, pid))
        
        # # Just combine positions and PIDs without name mapping
        # combined_parts = [pos + pid for pos, pid in zip(actual_positions, player_ids)]
        
        # Join them into a single string
        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