Added teams from fg for new players
This commit is contained in:
parent
cadd1fcbe9
commit
31a2fe8804
@ -416,6 +416,39 @@ BLANK_RESULTS = {
|
|||||||
}
|
}
|
||||||
TESTING = False
|
TESTING = False
|
||||||
YES = ['y', 'yes', 'yeet', 'please', 'yeah']
|
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):
|
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)]
|
params=[('inc_dex', False), ('cardset_id', cardset_id), ('short_output', True)]
|
||||||
)
|
)
|
||||||
if p_query['count'] == 0:
|
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'])
|
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'
|
api_url = f'https://statsapi.mlb.com/api/v1/people/{mlbam_playerid}?hydrate=currentTeam'
|
||||||
logging.info(f'Calling {api_url}')
|
logging.info(f'Calling {api_url}')
|
||||||
p_data = {'mlbclub': None, 'franchise': None}
|
p_data = {'mlbclub': None, 'franchise': None}
|
||||||
club_list = [
|
# club_list = [
|
||||||
'Arizona Diamondbacks',
|
# 'Arizona Diamondbacks',
|
||||||
'Atlanta Braves',
|
# 'Atlanta Braves',
|
||||||
'Baltimore Orioles',
|
# 'Baltimore Orioles',
|
||||||
'Boston Red Sox',
|
# 'Boston Red Sox',
|
||||||
'Chicago Cubs',
|
# 'Chicago Cubs',
|
||||||
'Chicago White Sox',
|
# 'Chicago White Sox',
|
||||||
'Cincinnati Reds',
|
# 'Cincinnati Reds',
|
||||||
'Cleveland Guardians',
|
# 'Cleveland Guardians',
|
||||||
'Colorado Rockies',
|
# 'Colorado Rockies',
|
||||||
'Detroit Tigers',
|
# 'Detroit Tigers',
|
||||||
'Houston Astros',
|
# 'Houston Astros',
|
||||||
'Kansas City Royals',
|
# 'Kansas City Royals',
|
||||||
'Los Angeles Angels',
|
# 'Los Angeles Angels',
|
||||||
'Los Angeles Dodgers',
|
# 'Los Angeles Dodgers',
|
||||||
'Miami Marlins',
|
# 'Miami Marlins',
|
||||||
'Milwaukee Brewers',
|
# 'Milwaukee Brewers',
|
||||||
'Minnesota Twins',
|
# 'Minnesota Twins',
|
||||||
'New York Mets',
|
# 'New York Mets',
|
||||||
'New York Yankees',
|
# 'New York Yankees',
|
||||||
'Oakland Athletics',
|
# 'Oakland Athletics',
|
||||||
'Philadelphia Phillies',
|
# 'Philadelphia Phillies',
|
||||||
'Pittsburgh Pirates',
|
# 'Pittsburgh Pirates',
|
||||||
'San Diego Padres',
|
# 'San Diego Padres',
|
||||||
'Seattle Mariners',
|
# 'Seattle Mariners',
|
||||||
'San Francisco Giants',
|
# 'San Francisco Giants',
|
||||||
'St Louis Cardinals',
|
# 'St Louis Cardinals',
|
||||||
'Tampa Bay Rays',
|
# 'Tampa Bay Rays',
|
||||||
'Texas Rangers',
|
# 'Texas Rangers',
|
||||||
'Toronto Blue Jays',
|
# 'Toronto Blue Jays',
|
||||||
'Washington Nationals'
|
# 'Washington Nationals'
|
||||||
]
|
# ]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
resp = requests.get(api_url, timeout=2)
|
resp = requests.get(api_url, timeout=2)
|
||||||
@ -986,7 +1024,7 @@ def mlbteam_and_franchise(mlbam_playerid):
|
|||||||
data = resp.json()
|
data = resp.json()
|
||||||
data = data['people'][0]
|
data = data['people'][0]
|
||||||
logging.debug(f'data: {data}')
|
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['mlbclub'] = data['currentTeam']['name']
|
||||||
p_data['franchise'] = data['currentTeam']['name']
|
p_data['franchise'] = data['currentTeam']['name']
|
||||||
else:
|
else:
|
||||||
|
|||||||
@ -18,7 +18,7 @@ import sys
|
|||||||
|
|
||||||
from creation_helpers import pd_players_df, get_batting_stats, pd_battingcards_df, pd_battingcardratings_df, \
|
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_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 db_calls import db_get, db_put, db_post, db_patch
|
||||||
from typing import Literal
|
from typing import Literal
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
@ -153,6 +153,9 @@ async def main(args):
|
|||||||
.query('key_mlbam == key_mlbam')
|
.query('key_mlbam == key_mlbam')
|
||||||
.set_index('key_bbref', drop=False))
|
.set_index('key_bbref', drop=False))
|
||||||
print(f'Matched mlbam to pd players.')
|
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 = []
|
new_players = []
|
||||||
|
|
||||||
@ -164,8 +167,8 @@ async def main(args):
|
|||||||
'cost': 99999,
|
'cost': 99999,
|
||||||
'image': f'{CARD_BASE_URL}/{df_data["player_id"]}/battingcard'
|
'image': f'{CARD_BASE_URL}/{df_data["player_id"]}/battingcard'
|
||||||
f'{urllib.parse.quote("?d=")}{release_directory}',
|
f'{urllib.parse.quote("?d=")}{release_directory}',
|
||||||
'mlbclub': 'None',
|
'mlbclub': CLUB_LIST[df_data['Tm_vL']],
|
||||||
'franchise': 'None',
|
'franchise': CLUB_LIST[df_data['Tm_vL']],
|
||||||
'cardset_id': cardset['id'],
|
'cardset_id': cardset['id'],
|
||||||
'set_num': int(float(df_data['key_fangraphs'])),
|
'set_num': int(float(df_data['key_fangraphs'])),
|
||||||
'rarity_id': 99,
|
'rarity_id': 99,
|
||||||
@ -176,16 +179,13 @@ async def main(args):
|
|||||||
'strat_code': int(float(df_data['key_mlbam']))
|
'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...')
|
print(f'Creating {len(new_players)} new players...')
|
||||||
for x in new_players:
|
for x in new_players:
|
||||||
this_player = await db_post('players', payload=x)
|
this_player = await db_post('players', payload=x)
|
||||||
player_data.at[x['bbref_id'], 'player_id'] = this_player['player_id']
|
final_batting.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'], '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
|
del ids_and_names, all_batting, pd_players
|
||||||
print(f'Player IDs linked to batting stats.\n{len(final_batting.values)} players remain\n')
|
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...')
|
print(f'Calculating fielding lines now...')
|
||||||
offense_stats.apply(create_positions, axis=1)
|
offense_stats.apply(create_positions, axis=1)
|
||||||
print(f'Fielding is complete.\n\nPosting positions now...')
|
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)
|
resp = await db_put('cardpositions', payload={'positions': position_payload}, timeout=30)
|
||||||
print(f'Response: {resp}\n')
|
print(f'Response: {resp}\n')
|
||||||
|
|
||||||
@ -433,6 +433,16 @@ async def main(args):
|
|||||||
player_updates = {} # { <player_id> : [ (param pairs) ] }
|
player_updates = {} # { <player_id> : [ (param pairs) ] }
|
||||||
rarity_group = player_data.query('rarity == new_rarity_id').groupby('rarity')
|
rarity_group = player_data.query('rarity == new_rarity_id').groupby('rarity')
|
||||||
average_ops = rarity_group['total_OPS'].mean().to_dict()
|
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()
|
# cost_groups = rarity_group['cost'].mean()
|
||||||
|
|
||||||
def get_player_updates(df_data):
|
def get_player_updates(df_data):
|
||||||
@ -444,7 +454,10 @@ async def main(args):
|
|||||||
5: 10,
|
5: 10,
|
||||||
99: 2400
|
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':
|
if 'is_liveseries' in arg_data and arg_data['is_liveseries'].lower() == 'true':
|
||||||
team_data = mlbteam_and_franchise(int(float(df_data['key_mlbam'])))
|
team_data = mlbteam_and_franchise(int(float(df_data['key_mlbam'])))
|
||||||
@ -555,6 +568,7 @@ async def main(args):
|
|||||||
for x in player_updates:
|
for x in player_updates:
|
||||||
await db_patch('players', object_id=x, params=player_updates[x])
|
await db_patch('players', object_id=x, params=player_updates[x])
|
||||||
|
|
||||||
|
del player_updates
|
||||||
print(f'Batter updates are complete')
|
print(f'Batter updates are complete')
|
||||||
start_time_two = datetime.datetime.now()
|
start_time_two = datetime.datetime.now()
|
||||||
run_time = start_time_two - start_time
|
run_time = start_time_two - start_time
|
||||||
@ -576,6 +590,11 @@ async def main(args):
|
|||||||
.set_index('key_bbref', drop=False))
|
.set_index('key_bbref', drop=False))
|
||||||
print(f'Matched mlbam to pd players.')
|
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 = []
|
new_players = []
|
||||||
|
|
||||||
def create_pitchers(df_data):
|
def create_pitchers(df_data):
|
||||||
@ -586,8 +605,8 @@ async def main(args):
|
|||||||
'cost': 99999,
|
'cost': 99999,
|
||||||
'image': f'{CARD_BASE_URL}/{df_data["player_id"]}/'
|
'image': f'{CARD_BASE_URL}/{df_data["player_id"]}/'
|
||||||
f'pitchingcard{urllib.parse.quote("?d=")}{release_directory}',
|
f'pitchingcard{urllib.parse.quote("?d=")}{release_directory}',
|
||||||
'mlbclub': 'None',
|
'mlbclub': CLUB_LIST[df_data['Tm_vL']],
|
||||||
'franchise': 'None',
|
'franchise': CLUB_LIST[df_data['Tm_vL']],
|
||||||
'cardset_id': cardset['id'],
|
'cardset_id': cardset['id'],
|
||||||
'set_num': int(float(df_data['key_fangraphs'])),
|
'set_num': int(float(df_data['key_fangraphs'])),
|
||||||
'rarity_id': 99,
|
'rarity_id': 99,
|
||||||
@ -598,17 +617,12 @@ async def main(args):
|
|||||||
'strat_code': int(float(df_data['key_mlbam']))
|
'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...')
|
print(f'Creating {len(new_players)} new players...')
|
||||||
for x in new_players:
|
for x in new_players:
|
||||||
this_player = await db_post('players', payload=x)
|
this_player = await db_post('players', payload=x)
|
||||||
player_data.at[x['bbref_id'], 'player_id'] = this_player['player_id']
|
final_pitching.at[x['bbref_id'], 'player_id'] = this_player['player_id']
|
||||||
player_data.at[x['bbref_id'], 'p_name'] = this_player['p_name']
|
final_pitching.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')
|
|
||||||
del ids_and_names, all_pitching, p_data, step_pitching
|
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')
|
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))
|
.set_index('key_bbref', drop=False))
|
||||||
|
|
||||||
player_updates = {} # { <player_id> : [ (param pairs) ] }
|
player_updates = {} # { <player_id> : [ (param pairs) ] }
|
||||||
rarity_group = player_data.query('rarity == new_rarity_id').groupby('rarity')
|
sp_rarity_group = player_data.query('rarity == new_rarity_id and starter_rating >= 4').groupby('rarity')
|
||||||
average_ops = rarity_group['total_OPS'].mean().to_dict()
|
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()
|
# 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):
|
def get_player_updates(df_data):
|
||||||
base_costs = {
|
base_costs = {
|
||||||
@ -792,7 +833,17 @@ async def main(args):
|
|||||||
5: 10,
|
5: 10,
|
||||||
99: 2400
|
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':
|
if 'is_liveseries' in arg_data and arg_data['is_liveseries'].lower() == 'true':
|
||||||
team_data = mlbteam_and_franchise(int(float(df_data['key_mlbam'])))
|
team_data = mlbteam_and_franchise(int(float(df_data['key_mlbam'])))
|
||||||
@ -810,7 +861,7 @@ async def main(args):
|
|||||||
params.extend([
|
params.extend([
|
||||||
('cost',
|
('cost',
|
||||||
round(base_costs[df_data['new_rarity_id']] * df_data['total_OPS'] /
|
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'])
|
('rarity_id', df_data['new_rarity_id'])
|
||||||
])
|
])
|
||||||
|
|
||||||
@ -898,11 +949,6 @@ async def main(args):
|
|||||||
|
|
||||||
player_data.apply(get_player_updates, axis=1)
|
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..')
|
print(f'Running player position updates..')
|
||||||
all_pos = await pd_positions_df(cardset['id'])
|
all_pos = await pd_positions_df(cardset['id'])
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user