Added teams from fg for new players
This commit is contained in:
parent
cadd1fcbe9
commit
31a2fe8804
@ -416,6 +416,39 @@ BLANK_RESULTS = {
|
||||
}
|
||||
TESTING = False
|
||||
YES = ['y', 'yes', 'yeet', 'please', 'yeah']
|
||||
CLUB_LIST = {
|
||||
'ARI': 'Arizona Diamondbacks',
|
||||
'ATL': 'Atlanta Braves',
|
||||
'BAL': 'Baltimore Orioles',
|
||||
'BOS': 'Boston Red Sox',
|
||||
'CHC': 'Chicago Cubs',
|
||||
'CHW': 'Chicago White Sox',
|
||||
'CIN': 'Cincinnati Reds',
|
||||
'CLE': 'Cleveland Guardians',
|
||||
'COL': 'Colorado Rockies',
|
||||
'DET': 'Detroit Tigers',
|
||||
'HOU': 'Houston Astros',
|
||||
'KCR': 'Kansas City Royals',
|
||||
'LAA': 'Los Angeles Angels',
|
||||
'LAD': 'Los Angeles Dodgers',
|
||||
'MIA': 'Miami Marlins',
|
||||
'MIL': 'Milwaukee Brewers',
|
||||
'MIN': 'Minnesota Twins',
|
||||
'NYM': 'New York Mets',
|
||||
'NYY': 'New York Yankees',
|
||||
'OAK': 'Oakland Athletics',
|
||||
'PHI': 'Philadelphia Phillies',
|
||||
'PIT': 'Pittsburgh Pirates',
|
||||
'SDP': 'San Diego Padres',
|
||||
'SEA': 'Seattle Mariners',
|
||||
'SFG': 'San Francisco Giants',
|
||||
'STL': 'St Louis Cardinals',
|
||||
'TBR': 'Tampa Bay Rays',
|
||||
'TEX': 'Texas Rangers',
|
||||
'TOR': 'Toronto Blue Jays',
|
||||
'WSN': 'Washington Nationals',
|
||||
'TOT': 'None'
|
||||
}
|
||||
|
||||
|
||||
def get_args(args):
|
||||
@ -441,7 +474,12 @@ async def pd_players_df(cardset_id: int):
|
||||
params=[('inc_dex', False), ('cardset_id', cardset_id), ('short_output', True)]
|
||||
)
|
||||
if p_query['count'] == 0:
|
||||
raise ValueError(f'No players returned from Paper Dynasty API')
|
||||
return pd.DataFrame({
|
||||
'player_id': [], 'p_name': [], 'cost': [], 'image': [], 'image2': [], 'mlbclub': [], 'franchise': [],
|
||||
'cardset': [], 'set_num': [], 'rarity': [], 'pos_1': [], 'pos_2': [], 'pos_3': [], 'pos_4': [], 'pos_5': [],
|
||||
'pos_6': [], 'pos_7': [], 'pos_8': [], 'headshot': [], 'vanity_card': [], 'strat_code': [], 'bbref_id': [],
|
||||
'fangr_id': [], 'description': [], 'quantity': [], 'mlbplayer': []
|
||||
})
|
||||
return pd.DataFrame(p_query['players'])
|
||||
|
||||
|
||||
@ -943,38 +981,38 @@ def mlbteam_and_franchise(mlbam_playerid):
|
||||
api_url = f'https://statsapi.mlb.com/api/v1/people/{mlbam_playerid}?hydrate=currentTeam'
|
||||
logging.info(f'Calling {api_url}')
|
||||
p_data = {'mlbclub': None, 'franchise': None}
|
||||
club_list = [
|
||||
'Arizona Diamondbacks',
|
||||
'Atlanta Braves',
|
||||
'Baltimore Orioles',
|
||||
'Boston Red Sox',
|
||||
'Chicago Cubs',
|
||||
'Chicago White Sox',
|
||||
'Cincinnati Reds',
|
||||
'Cleveland Guardians',
|
||||
'Colorado Rockies',
|
||||
'Detroit Tigers',
|
||||
'Houston Astros',
|
||||
'Kansas City Royals',
|
||||
'Los Angeles Angels',
|
||||
'Los Angeles Dodgers',
|
||||
'Miami Marlins',
|
||||
'Milwaukee Brewers',
|
||||
'Minnesota Twins',
|
||||
'New York Mets',
|
||||
'New York Yankees',
|
||||
'Oakland Athletics',
|
||||
'Philadelphia Phillies',
|
||||
'Pittsburgh Pirates',
|
||||
'San Diego Padres',
|
||||
'Seattle Mariners',
|
||||
'San Francisco Giants',
|
||||
'St Louis Cardinals',
|
||||
'Tampa Bay Rays',
|
||||
'Texas Rangers',
|
||||
'Toronto Blue Jays',
|
||||
'Washington Nationals'
|
||||
]
|
||||
# club_list = [
|
||||
# 'Arizona Diamondbacks',
|
||||
# 'Atlanta Braves',
|
||||
# 'Baltimore Orioles',
|
||||
# 'Boston Red Sox',
|
||||
# 'Chicago Cubs',
|
||||
# 'Chicago White Sox',
|
||||
# 'Cincinnati Reds',
|
||||
# 'Cleveland Guardians',
|
||||
# 'Colorado Rockies',
|
||||
# 'Detroit Tigers',
|
||||
# 'Houston Astros',
|
||||
# 'Kansas City Royals',
|
||||
# 'Los Angeles Angels',
|
||||
# 'Los Angeles Dodgers',
|
||||
# 'Miami Marlins',
|
||||
# 'Milwaukee Brewers',
|
||||
# 'Minnesota Twins',
|
||||
# 'New York Mets',
|
||||
# 'New York Yankees',
|
||||
# 'Oakland Athletics',
|
||||
# 'Philadelphia Phillies',
|
||||
# 'Pittsburgh Pirates',
|
||||
# 'San Diego Padres',
|
||||
# 'Seattle Mariners',
|
||||
# 'San Francisco Giants',
|
||||
# 'St Louis Cardinals',
|
||||
# 'Tampa Bay Rays',
|
||||
# 'Texas Rangers',
|
||||
# 'Toronto Blue Jays',
|
||||
# 'Washington Nationals'
|
||||
# ]
|
||||
|
||||
try:
|
||||
resp = requests.get(api_url, timeout=2)
|
||||
@ -986,7 +1024,7 @@ def mlbteam_and_franchise(mlbam_playerid):
|
||||
data = resp.json()
|
||||
data = data['people'][0]
|
||||
logging.debug(f'data: {data}')
|
||||
if data['currentTeam']['name'] in club_list:
|
||||
if data['currentTeam']['name'] in CLUB_LIST.values():
|
||||
p_data['mlbclub'] = data['currentTeam']['name']
|
||||
p_data['franchise'] = data['currentTeam']['name']
|
||||
else:
|
||||
|
||||
@ -18,7 +18,7 @@ import sys
|
||||
|
||||
from creation_helpers import pd_players_df, get_batting_stats, pd_battingcards_df, pd_battingcardratings_df, \
|
||||
get_pitching_stats, get_all_pybaseball_ids, pd_pitchingcards_df, pd_pitchingcardratings_df, pd_positions_df, \
|
||||
get_args, mlbteam_and_franchise
|
||||
get_args, mlbteam_and_franchise, CLUB_LIST
|
||||
from db_calls import db_get, db_put, db_post, db_patch
|
||||
from typing import Literal
|
||||
from bs4 import BeautifulSoup
|
||||
@ -153,6 +153,9 @@ async def main(args):
|
||||
.query('key_mlbam == key_mlbam')
|
||||
.set_index('key_bbref', drop=False))
|
||||
print(f'Matched mlbam to pd players.')
|
||||
final_batting = pd.merge(
|
||||
player_data, all_batting, left_on='key_fangraphs', right_on='playerId', sort=False
|
||||
).set_index('key_bbref', drop=False)
|
||||
|
||||
new_players = []
|
||||
|
||||
@ -164,8 +167,8 @@ async def main(args):
|
||||
'cost': 99999,
|
||||
'image': f'{CARD_BASE_URL}/{df_data["player_id"]}/battingcard'
|
||||
f'{urllib.parse.quote("?d=")}{release_directory}',
|
||||
'mlbclub': 'None',
|
||||
'franchise': 'None',
|
||||
'mlbclub': CLUB_LIST[df_data['Tm_vL']],
|
||||
'franchise': CLUB_LIST[df_data['Tm_vL']],
|
||||
'cardset_id': cardset['id'],
|
||||
'set_num': int(float(df_data['key_fangraphs'])),
|
||||
'rarity_id': 99,
|
||||
@ -176,16 +179,13 @@ async def main(args):
|
||||
'strat_code': int(float(df_data['key_mlbam']))
|
||||
})
|
||||
|
||||
player_data[player_data['player_id'].isnull()].apply(create_batters, axis=1)
|
||||
final_batting[final_batting['player_id'].isnull()].apply(create_batters, axis=1)
|
||||
print(f'Creating {len(new_players)} new players...')
|
||||
for x in new_players:
|
||||
this_player = await db_post('players', payload=x)
|
||||
player_data.at[x['bbref_id'], 'player_id'] = this_player['player_id']
|
||||
player_data.at[x['bbref_id'], 'p_name'] = this_player['p_name']
|
||||
final_batting.at[x['bbref_id'], 'player_id'] = this_player['player_id']
|
||||
final_batting.at[x['bbref_id'], 'p_name'] = this_player['p_name']
|
||||
|
||||
final_batting = pd.merge(
|
||||
player_data, all_batting, left_on='key_fangraphs', right_on='playerId', sort=False
|
||||
).set_index('key_bbref', drop=False)
|
||||
del ids_and_names, all_batting, pd_players
|
||||
print(f'Player IDs linked to batting stats.\n{len(final_batting.values)} players remain\n')
|
||||
|
||||
@ -378,7 +378,7 @@ async def main(args):
|
||||
print(f'Calculating fielding lines now...')
|
||||
offense_stats.apply(create_positions, axis=1)
|
||||
print(f'Fielding is complete.\n\nPosting positions now...')
|
||||
if 'post_updates' not in arg_data or arg_data['post_updates'].lower() == 'true':
|
||||
if 'post_batters' not in arg_data or arg_data['post_batters'].lower() == 'true':
|
||||
resp = await db_put('cardpositions', payload={'positions': position_payload}, timeout=30)
|
||||
print(f'Response: {resp}\n')
|
||||
|
||||
@ -433,6 +433,16 @@ async def main(args):
|
||||
player_updates = {} # { <player_id> : [ (param pairs) ] }
|
||||
rarity_group = player_data.query('rarity == new_rarity_id').groupby('rarity')
|
||||
average_ops = rarity_group['total_OPS'].mean().to_dict()
|
||||
if 1 not in average_ops:
|
||||
average_ops[1] = 1.066
|
||||
if 2 not in average_ops:
|
||||
average_ops[2] = 0.938
|
||||
if 3 not in average_ops:
|
||||
average_ops[3] = 0.844
|
||||
if 4 not in average_ops:
|
||||
average_ops[4] = 0.752
|
||||
if 5 not in average_ops:
|
||||
average_ops[5] = 0.612
|
||||
# cost_groups = rarity_group['cost'].mean()
|
||||
|
||||
def get_player_updates(df_data):
|
||||
@ -444,7 +454,10 @@ async def main(args):
|
||||
5: 10,
|
||||
99: 2400
|
||||
}
|
||||
params = [('description', f'{player_description}')]
|
||||
params = []
|
||||
|
||||
if df_data['description'] != player_description:
|
||||
params = [('description', f'{player_description}')]
|
||||
|
||||
if 'is_liveseries' in arg_data and arg_data['is_liveseries'].lower() == 'true':
|
||||
team_data = mlbteam_and_franchise(int(float(df_data['key_mlbam'])))
|
||||
@ -555,6 +568,7 @@ async def main(args):
|
||||
for x in player_updates:
|
||||
await db_patch('players', object_id=x, params=player_updates[x])
|
||||
|
||||
del player_updates
|
||||
print(f'Batter updates are complete')
|
||||
start_time_two = datetime.datetime.now()
|
||||
run_time = start_time_two - start_time
|
||||
@ -576,6 +590,11 @@ async def main(args):
|
||||
.set_index('key_bbref', drop=False))
|
||||
print(f'Matched mlbam to pd players.')
|
||||
|
||||
step_pitching = pd.merge(
|
||||
player_data, all_pitching, left_on='key_fangraphs', right_on='playerId', sort=False
|
||||
).set_index('key_bbref', drop=False)
|
||||
final_pitching = step_pitching.join(df_p, rsuffix='_r')
|
||||
|
||||
new_players = []
|
||||
|
||||
def create_pitchers(df_data):
|
||||
@ -586,8 +605,8 @@ async def main(args):
|
||||
'cost': 99999,
|
||||
'image': f'{CARD_BASE_URL}/{df_data["player_id"]}/'
|
||||
f'pitchingcard{urllib.parse.quote("?d=")}{release_directory}',
|
||||
'mlbclub': 'None',
|
||||
'franchise': 'None',
|
||||
'mlbclub': CLUB_LIST[df_data['Tm_vL']],
|
||||
'franchise': CLUB_LIST[df_data['Tm_vL']],
|
||||
'cardset_id': cardset['id'],
|
||||
'set_num': int(float(df_data['key_fangraphs'])),
|
||||
'rarity_id': 99,
|
||||
@ -598,17 +617,12 @@ async def main(args):
|
||||
'strat_code': int(float(df_data['key_mlbam']))
|
||||
})
|
||||
|
||||
player_data[player_data['player_id'].isnull()].apply(create_pitchers, axis=1)
|
||||
final_pitching[final_pitching['player_id'].isnull()].apply(create_pitchers, axis=1)
|
||||
print(f'Creating {len(new_players)} new players...')
|
||||
for x in new_players:
|
||||
this_player = await db_post('players', payload=x)
|
||||
player_data.at[x['bbref_id'], 'player_id'] = this_player['player_id']
|
||||
player_data.at[x['bbref_id'], 'p_name'] = this_player['p_name']
|
||||
|
||||
step_pitching = pd.merge(
|
||||
player_data, all_pitching, left_on='key_fangraphs', right_on='playerId', sort=False
|
||||
).set_index('key_bbref', drop=False)
|
||||
final_pitching = step_pitching.join(df_p, rsuffix='_r')
|
||||
final_pitching.at[x['bbref_id'], 'player_id'] = this_player['player_id']
|
||||
final_pitching.at[x['bbref_id'], 'p_name'] = this_player['p_name']
|
||||
del ids_and_names, all_pitching, p_data, step_pitching
|
||||
print(f'Player IDs linked to pitching stats.\n{len(final_pitching.values)} players remain\n')
|
||||
|
||||
@ -779,9 +793,36 @@ async def main(args):
|
||||
.set_index('key_bbref', drop=False))
|
||||
|
||||
player_updates = {} # { <player_id> : [ (param pairs) ] }
|
||||
rarity_group = player_data.query('rarity == new_rarity_id').groupby('rarity')
|
||||
average_ops = rarity_group['total_OPS'].mean().to_dict()
|
||||
sp_rarity_group = player_data.query('rarity == new_rarity_id and starter_rating >= 4').groupby('rarity')
|
||||
sp_average_ops = sp_rarity_group['total_OPS'].mean().to_dict()
|
||||
rp_rarity_group = player_data.query('rarity == new_rarity_id and starter_rating < 4').groupby('rarity')
|
||||
rp_average_ops = rp_rarity_group['total_OPS'].mean().to_dict()
|
||||
# cost_groups = rarity_group['cost'].mean()
|
||||
if 99 not in sp_average_ops:
|
||||
sp_average_ops[99] = 0.388
|
||||
if 1 not in sp_average_ops:
|
||||
sp_average_ops[1] = 0.445
|
||||
if 2 not in sp_average_ops:
|
||||
sp_average_ops[2] = 0.504
|
||||
if 3 not in sp_average_ops:
|
||||
sp_average_ops[3] = 0.568
|
||||
if 4 not in sp_average_ops:
|
||||
sp_average_ops[4] = 0.634
|
||||
if 5 not in sp_average_ops:
|
||||
sp_average_ops[5] = 0.737
|
||||
|
||||
if 99 not in rp_average_ops:
|
||||
rp_average_ops[99] = 0.282
|
||||
if 1 not in rp_average_ops:
|
||||
rp_average_ops[1] = 0.375
|
||||
if 2 not in rp_average_ops:
|
||||
rp_average_ops[2] = 0.442
|
||||
if 3 not in rp_average_ops:
|
||||
rp_average_ops[3] = 0.516
|
||||
if 4 not in rp_average_ops:
|
||||
rp_average_ops[4] = 0.591
|
||||
if 5 not in rp_average_ops:
|
||||
rp_average_ops[5] = 0.702
|
||||
|
||||
def get_player_updates(df_data):
|
||||
base_costs = {
|
||||
@ -792,7 +833,17 @@ async def main(args):
|
||||
5: 10,
|
||||
99: 2400
|
||||
}
|
||||
params = [('description', f'{player_description}')]
|
||||
|
||||
def avg_ops(rarity_id, starter_rating):
|
||||
if starter_rating >= 4:
|
||||
return sp_average_ops[rarity_id]
|
||||
else:
|
||||
return rp_average_ops[rarity_id]
|
||||
|
||||
params = []
|
||||
|
||||
if df_data['description'] != player_description:
|
||||
params = [('description', f'{player_description}')]
|
||||
|
||||
if 'is_liveseries' in arg_data and arg_data['is_liveseries'].lower() == 'true':
|
||||
team_data = mlbteam_and_franchise(int(float(df_data['key_mlbam'])))
|
||||
@ -810,7 +861,7 @@ async def main(args):
|
||||
params.extend([
|
||||
('cost',
|
||||
round(base_costs[df_data['new_rarity_id']] * df_data['total_OPS'] /
|
||||
average_ops[df_data['new_rarity_id']])),
|
||||
avg_ops(df_data['new_rarity_id'], df_data['starter_rating']))),
|
||||
('rarity_id', df_data['new_rarity_id'])
|
||||
])
|
||||
|
||||
@ -898,11 +949,6 @@ async def main(args):
|
||||
|
||||
player_data.apply(get_player_updates, axis=1)
|
||||
|
||||
# print(f'Sending {len(player_updates)} player updates to PD database...')
|
||||
# if 'post_pitchers' not in arg_data or arg_data['post_pitchers'].lower() == 'true':
|
||||
# for x in player_updates:
|
||||
# await db_patch('players', object_id=x, params=player_updates[x])
|
||||
|
||||
print(f'Running player position updates..')
|
||||
all_pos = await pd_positions_df(cardset['id'])
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user