beginning of attempted refactor

This commit is contained in:
Holly 2021-03-09 15:08:25 +00:00
parent 68c8f66740
commit 306bf1870b
1 changed files with 45 additions and 20 deletions

View File

@ -36,9 +36,45 @@ async def startup():
await t await t
def get_cards(card_names): def get_cards(card_names):
async def download_card_image(session, c):
url = c.image_uris(0, 'normal')
async with session.get(url) as r:
return io.BytesIO(r.read())
def get_text_representation(c):
# I genuinely think this is the best way to check whether a card has
# power/toughness, considering how Scrython is implemented.
# Can't even check for Creature type because of stuff like Vehicles.
try:
c.power()
has_pt = True
except KeyError:
has_pt = False
ret = c.name() # All cards have a name.
# Some cards (lands, [[Wheel of Fate]], whatever) don't have mana costs.
# Add it if it's there.
if c.mana_cost():
ret += f' - {c.mana_cost()}'
# All cards have a type line.
ret += '\n' + c.type_line()
# Funnily enough, not all cards have oracle text.
# It feels like they should, but that's ignoring vanilla creatures.
if c.oracle_text():
ret += f'\n\n{c.oracle_text()}'
# Finally, power/toughness.
if has_pt:
ret += f'\n\n{c.power()}/{c.toughness()}'
return ret
cards = [] cards = []
found = [] found = []
images = []
for name in card_names: for name in card_names:
try: try:
@ -49,25 +85,14 @@ def get_cards(card_names):
cards.append(f'No card named "{name}" was found.') cards.append(f'No card named "{name}" was found.')
if 1 <= len(found) <= 4: if 1 <= len(found) <= 4:
r = requests.get(c.image_uris(0, 'normal'), stream=True) # download card images
async with aiohttp.ClientSession() as session:
images = list(await asyncio.gather(
*(download_card_image(session, c) for c in found)
))
has_pt = False else:
try: images = None
c.power()
has_pt = True
except KeyError:
pass
description = c.name()
if c.mana_cost():
description += f' - {c.mana_cost()}'
description += '\n' + c.type_line()
if c.oracle_text():
description += f'\n\n{c.oracle_text()}'
if has_pt:
description += f'\n\n{c.power()}/{c.toughness()}'
images.append((io.BytesIO(r.content), description))
return cards, images return cards, images