126 lines
4.7 KiB
Python
126 lines
4.7 KiB
Python
import asyncio
|
|
import datetime
|
|
import logging
|
|
import sys
|
|
|
|
import pandas as pd
|
|
|
|
from creation_helpers import get_args, pd_players_df, pd_positions_df
|
|
from db_calls import db_get, db_patch, DB_URL
|
|
|
|
date = f'{datetime.datetime.now().year}-{datetime.datetime.now().month}-{datetime.datetime.now().day}'
|
|
log_level = logging.INFO
|
|
logging.basicConfig(
|
|
filename=f'logs/{date}.log',
|
|
format='%(asctime)s - card-creation - %(levelname)s - %(message)s',
|
|
level=log_level
|
|
)
|
|
CARD_BASE_URL = f'{DB_URL}/v2/players'
|
|
|
|
|
|
async def main(args):
|
|
arg_data = get_args(args)
|
|
post_players = False if 'post_players' not in arg_data or arg_data['post_players'].lower() == 'false' else True
|
|
|
|
# cardset_name = input(f'What is the name of this Cardset? ')
|
|
cardset_name = arg_data['cardset_name']
|
|
print(f'Searching for cardset: {cardset_name}')
|
|
c_query = await db_get('cardsets', params=[('name', cardset_name)])
|
|
|
|
if c_query['count'] == 0:
|
|
print(f'I do not see a cardset named {cardset_name}')
|
|
return
|
|
cardset = c_query['cardsets'][0]
|
|
del c_query
|
|
|
|
print(f'Pulling {cardset["name"]} players now...')
|
|
base_df = await pd_players_df(cardset['id'])
|
|
all_players = base_df.set_index('bbref_id', drop=False)
|
|
|
|
print(f'Pulling {cardset["name"]} card positions now...')
|
|
all_pos = await pd_positions_df(cardset['id'])
|
|
|
|
print(f'Running player position updates..')
|
|
p_query = await db_get('pitchingcards', params=[('cardset_id', cardset['id'])])
|
|
if p_query['count'] == 0:
|
|
raise ValueError(f'No pitching cards returned for cardset {cardset["id"]}')
|
|
|
|
for x in p_query['cards']:
|
|
x['player_id'] = x['player']['player_id']
|
|
x['bbref_id'] = x['player']['bbref_id']
|
|
del x['player']
|
|
|
|
raw_df = pd.DataFrame(p_query['cards'])
|
|
pit_pos_df = raw_df.set_index('bbref_id', drop=False)
|
|
|
|
player_updates = {}
|
|
|
|
def set_all_positions(df_data):
|
|
def pos_match(old_pos, new_pos):
|
|
if old_pos is None and new_pos == 'False':
|
|
return True
|
|
elif old_pos is None:
|
|
return False
|
|
else:
|
|
return old_pos.lower() == new_pos.lower()
|
|
|
|
pos_series = all_pos.query(f'player_id == {df_data["player_id"]}')['position']
|
|
this_player = all_players.loc[df_data['bbref_id']]
|
|
pos_updates = []
|
|
count = 1
|
|
for this_pos in pos_series:
|
|
if this_pos == 'P':
|
|
this_pitcher = pit_pos_df.loc[df_data['bbref_id']]
|
|
if this_pitcher['starter_rating'] > 3:
|
|
if not pos_match(this_player[f'pos_{count}'], 'SP'):
|
|
pos_updates.append((f'pos_{count}', 'SP'))
|
|
count += 1
|
|
if this_pitcher['relief_rating'] > 1 or not pd.isna(this_pitcher['closer_rating']):
|
|
if not pos_match(this_player[f'pos_{count}'], 'RP'):
|
|
pos_updates.append((f'pos_{count}', 'RP'))
|
|
count += 1
|
|
else:
|
|
if not pos_match(this_player[f'pos_{count}'], 'RP'):
|
|
pos_updates.append((f'pos_{count}', 'RP'))
|
|
count += 1
|
|
|
|
if not pd.isna(this_pitcher['closer_rating']):
|
|
if not pos_match(this_player[f'pos_{count}'], 'CP'):
|
|
pos_updates.append((f'pos_{count}', 'CP'))
|
|
count += 1
|
|
else:
|
|
if not pos_match(this_player[f'pos_{count}'], this_pos):
|
|
pos_updates.append((f'pos_{count}', this_pos))
|
|
count += 1
|
|
|
|
if count == 1:
|
|
if not pos_match(this_player[f'pos_{count}'], 'DH'):
|
|
pos_updates.append(('pos_1', 'DH'))
|
|
count += 1
|
|
|
|
while count < 9:
|
|
if not pos_match(this_player[f'pos_{count}'], 'False'):
|
|
pos_updates.append((f'pos_{count}', 'False'))
|
|
count += 1
|
|
|
|
if len(pos_updates) > 0:
|
|
if df_data.player_id not in player_updates.keys():
|
|
player_updates[df_data.player_id] = pos_updates
|
|
else:
|
|
player_updates[df_data.player_id].extend(pos_updates)
|
|
|
|
all_players.apply(set_all_positions, axis=1)
|
|
|
|
print(f'Sending {len(player_updates)} player updates to PD database...')
|
|
if post_players:
|
|
for x in player_updates:
|
|
await db_patch('players', object_id=x, params=player_updates[x])
|
|
else:
|
|
update_string = "\n".join(player_updates)
|
|
logging.info(f'All player updates:{update_string}')
|
|
print(f'Printed player updates to log since \'post_players\' argument was not true')
|
|
print(f'Player updates are complete\n')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
asyncio.run(main(sys.argv[1:])) |