Use Scryfall to look up unmatched cards

This commit is contained in:
Correl Roush 2021-07-05 23:54:39 -04:00
parent 04b85aa280
commit 996adbf376
2 changed files with 25 additions and 0 deletions

View file

@ -9,6 +9,7 @@ python = "^3.9"
tornado = "^6.1"
aiosqlite = "^0.17.0"
click = "^8.0.1"
httpx = "^0.18.2"
[tool.poetry.dev-dependencies]
pytest = "^5.2"

View file

@ -3,11 +3,35 @@ import logging
import typing
import aiosqlite
import httpx
import tutor.database
import tutor.models
async def find_by_scryfall_id(
settings: dict, scryfall_id: str
) -> typing.List[tutor.models.Card]:
async with aiosqlite.connect(settings["database"]) as db:
found = await tutor.database.search(db, scryfall_id=row["Scryfall ID"])
if not found:
# Scryfall IDs could be pointing to a card in another language, which
# won't be in our database. Let's get the card info from Scryfall and
# search with that.
async with httpx.AsyncClient() as client:
response = client.get(f"https://api.scryfall.com/card/{scryfall_id}")
if response.status_code != httpx.codes.OK:
logging.error("Error response from Scryfall: {response}")
return []
info = response.json()
found = await tutor.database.search(
db,
collector_number=info["collector_number"],
set_code=info["set"].upper(),
)
return found
async def load(settings: dict, filename: str) -> typing.List[tutor.models.Card]:
"""Load cards from a CSV file.