paper-dynasty-card-creation/check_cards.py
2024-12-22 15:46:52 -06:00

126 lines
3.8 KiB
Python

import asyncio
import datetime
import sys
from creation_helpers import get_args
from db_calls import db_get, url_get
from exceptions import logger
CARDSET_NAME = '1998 Live'
START_ID = None # Integer to only start pulling cards at player_id START_ID
TEST_COUNT = 1 # integer to stop after TEST_COUNT calls
HTML_CARDS = False # boolean to only check and not generate cards
SKIP_ARMS = False
SKIP_BATS = False
async def main(args):
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:
pass
elif 'batting' in x['image'] and SKIP_BATS:
pass
elif START_ID is not None and START_ID > x['player_id']:
pass
elif 'sombaseball' in x['image']:
errors.append((x, f'Bad card url: {x["image"]}'))
else:
count += 1
if count % 20 == 0:
print(f'Card #{count + 1} being pulled is {x["p_name"]}...')
elif TEST_COUNT is not None and TEST_COUNT < count:
print(f'Done test run')
break
if HTML_CARDS:
card_url = f'{x["image"]}&html=true'
timeout = 2
else:
card_url = x['image']
timeout = 6
try:
logger.info(f'calling the card url')
resp = await url_get(card_url, timeout=timeout)
except ConnectionError as e:
if cxn_error:
raise e
cxn_error = True
errors.append((x, e))
except ValueError as e:
errors.append((x, e))
else:
if x['image2'] is not None:
if HTML_CARDS:
card_url = f'{x["image2"]}&html=true'
else:
card_url = x['image2']
if 'sombaseball' in x['image2']:
errors.append((x, f'Bad card url: {x["image"]}'))
else:
try:
resp = await url_get(card_url, timeout=6)
except ConnectionError as e:
if cxn_error:
raise e
cxn_error = True
errors.append((x, e))
except ValueError as e:
errors.append((x, e))
else:
successes.append(x)
else:
successes.append(x)
if len(errors) > 0:
logger.error(f'All Errors:')
for x in errors:
logger.error(f'ID {x[0]["player_id"]} {x[0]["p_name"]} - Error: {x[1]}')
if len(successes) > 0:
logger.debug(f'All Successes:')
for x in successes:
logger.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:]))