James McCool
Refactor contest data handling in app.py and load_contest_file.py for improved clarity and functionality
996f8cb
| import pandas as pd | |
| import requests | |
| def grab_contest_data(sport, contest_file): | |
| 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) | |
| 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 |