File size: 4,082 Bytes
0841c51
 
 
e13ecee
0841c51
 
 
 
 
 
 
 
 
 
7bd0e86
0841c51
7bd0e86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9916481
 
 
 
 
 
73a4cc3
 
032d464
0841c51
 
 
 
 
 
 
 
 
 
 
 
a8cf688
 
0841c51
 
 
 
 
 
 
a8cf688
73a4cc3
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
92
import pandas as pd
import requests

def grab_contest_data(sport, contest_name, contest_id_map, contest_date):

    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(':')
        
        # For MLB, we need to handle the specific position order
        if len(player_ids) == 9:  # 9-player lineup
            # Map positions to the correct order for a 9-player lineup
            position_map = {
                0: '1B ',
                1: ' 2B ',
                2: ' 3B ',
                3: ' C ',
                4: ' OF ',
                5: ' OF ',
                6: ' P ',
                7: ' P ',
                8: ' SS '
            }
            # Use the mapped positions instead of the full position list
            actual_positions = [position_map[i] for i in range(len(player_ids))]
        else:
            # For other cases, use the standard position list
            actual_positions = positions[:len(player_ids)]

        # Combine positions and 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))
        
        # 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'],
            '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