109 lines
3.7 KiB
Python
109 lines
3.7 KiB
Python
import asyncio
|
|
import datetime
|
|
import logging
|
|
import sys
|
|
import urllib.parse
|
|
|
|
from creation_helpers import get_args
|
|
from db_calls import db_get, url_get, db_post, 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 - check-cards - %(levelname)s - %(message)s',
|
|
level=log_level
|
|
)
|
|
CARD_BASE_URL = f'{DB_URL}/v2/players'
|
|
|
|
|
|
async def main(args):
|
|
arg_data = get_args(args)
|
|
|
|
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
|
|
|
|
p_query = await db_get(
|
|
'players',
|
|
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')
|
|
all_players = p_query['players']
|
|
del p_query
|
|
|
|
errors = []
|
|
successes = []
|
|
cxn_error = False
|
|
count = -1
|
|
start_time = datetime.datetime.now()
|
|
|
|
for x in all_players:
|
|
if 'pitching' in x['image'] and 'skip_arms' in arg_data and arg_data['skip_arms'].lower() == 'true':
|
|
pass
|
|
elif 'batting' in x['image'] and 'skip_bats' in arg_data and arg_data['skip_bats'].lower() == 'true':
|
|
pass
|
|
elif 'start_id' in arg_data and int(arg_data['start_id']) > x['player_id']:
|
|
pass
|
|
# elif 'sombaseball' in x['image']:
|
|
# errors.append((x, f'Bad card url: {x["image"]}'))
|
|
elif 'sombaseball' in x['image']:
|
|
print(f'Found old image URL for {x["p_name"]} - setting card type by pos_1: {x["pos_1"]}')
|
|
release_dir = f'{start_time.year}-{start_time.month}-{start_time.day}'
|
|
if x['pos_1'] in ['SP', 'RP', 'CP', 'P']:
|
|
image_url = (f'{CARD_BASE_URL}/{x["player_id"]}/pitchingcard'
|
|
f'{urllib.parse.quote("?d=")}{release_dir}')
|
|
else:
|
|
image_url = (f'{CARD_BASE_URL}/{x["player_id"]}/battingcard'
|
|
f'{urllib.parse.quote("?d=")}{release_dir}')
|
|
this_player = await db_patch(
|
|
'players',
|
|
object_id=x["player_id"],
|
|
params=[('image', image_url)]
|
|
)
|
|
else:
|
|
count += 1
|
|
if count % 20 == 0:
|
|
print(f'Card #{count + 1} being updated is {x["p_name"]}...')
|
|
elif 'test_count' in arg_data and int(arg_data['test_count']) < count:
|
|
print(f'Done test run')
|
|
break
|
|
|
|
try:
|
|
resp = await db_post(f'players/{x["player_id"]}/image-reset')
|
|
|
|
except ConnectionError as e:
|
|
if cxn_error:
|
|
raise e
|
|
|
|
cxn_error = True
|
|
errors.append((x, e))
|
|
|
|
except ValueError as e:
|
|
errors.append((x, e))
|
|
|
|
if len(errors) > 0:
|
|
logging.error(f'All Errors:')
|
|
for x in errors:
|
|
logging.error(f'ID {x[0]["player_id"]} {x[0]["p_name"]} - Error: {x[1]}')
|
|
|
|
if len(successes) > 0:
|
|
logging.debug(f'All Successes:')
|
|
for x in successes:
|
|
logging.info(f'ID {x["player_id"]} {x["p_name"]}')
|
|
|
|
p_run_time = datetime.datetime.now() - start_time
|
|
print(f'\nAll done!\nErrors: {len(errors)}\nSuccesses: {len(successes)}')
|
|
print(f'Total runtime: {p_run_time.total_seconds()} seconds')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
asyncio.run(main(sys.argv[1:]))
|