Skip to content

Commit

Permalink
Lookup card name if oracle id is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
ldeluigi committed Mar 16, 2023
1 parent 461c8d2 commit f6414ce
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
6 changes: 3 additions & 3 deletions backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@
'level': 'DEBUG',
'handlers': ['console'],
},
'django.db.backends': {
'level': 'DEBUG',
},
# 'django.db.backends': {
# 'level': 'DEBUG',
# },
}
}
41 changes: 29 additions & 12 deletions backend/spellbook/management/commands/update_cards.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import traceback
import datetime
import uuid
from django.core.management.base import BaseCommand
from django.utils import timezone
from spellbook.models import Job, Card
Expand All @@ -10,24 +11,40 @@
class Command(BaseCommand):
help = 'Updates cards using Scryfall bulk data'

def log_job(self, job, message, style=lambda x: x):
self.stdout.write(style(message))
job.message += message + '\n'
job.save()

def handle(self, *args, **options):
job = Job.start('update_cards_data', timezone.timedelta(minutes=2))
if job is None:
self.stdout.write(self.style.ERROR('Job already running'))
return
job.save()
try:
self.stdout.write('Fetching scryfall dataset...')
scryfall_db = {card_object['oracle_id']: card_object for _, card_object in scryfall().items()}
self.stdout.write('Fetching scryfall dataset...done')
self.stdout.write('Updating cards...')
cards_to_update = Card.objects.filter(oracle_id__isnull=False)
updated_count = cards_to_update.count()
self.log_job(job, 'Fetching scryfall dataset...')
scryfall_name_db = scryfall()
scryfall_db = {card_object['oracle_id']: card_object for card_object in scryfall_name_db.values()}
self.log_job(job, 'Fetching scryfall dataset...done')
self.log_job(job, 'Updating cards...')
cards_to_update = Card.objects.all()
updated_count = 0
for card in cards_to_update:
updated = False
if card.oracle_id is None:
self.log_job(job, f'Card {card.name} lacks an oracle_id: attempting to find it by name...')
card_name = card.name.lower().strip(' \t\n\r')
if card_name in scryfall_name_db:
card.oracle_id = uuid.UUID(hex=scryfall_name_db[card_name]['oracle_id'])
updated = True
self.log_job(job, f'Card {card.name} found in scryfall dataset, oracle_id set to {card.oracle_id}')
else:
self.log_job(job, f'Card {card.name} not found in scryfall dataset', self.style.WARNING)
continue
oracle_id = str(card.oracle_id)
if oracle_id in scryfall_db:
card_in_db = scryfall_db[oracle_id]
updated = False
card_name = card_in_db['name']
if card.name != card_name:
card.name = card_in_db['name']
Expand All @@ -48,18 +65,18 @@ def handle(self, *args, **options):
updated = True
if updated:
card.save()
updated_count += 1
else:
self.stdout.write(self.style.WARNING(f'Card {card.name} not found in Scryfall, using oracle id {oracle_id}'))
self.log_job(job, f'Card {card.name} with oracle id {oracle_id} not found in scryfall dataset', self.style.WARNING)
job.termination = timezone.now()
job.status = Job.Status.SUCCESS
job.message = f'Successfully updated {updated_count} cards'
self.log_job(job, 'Updating cards...done', self.style.SUCCESS)
self.log_job(job, f'Successfully updated {updated_count} cards' if updated_count > 0 else 'Everything is up to date', self.style.SUCCESS)
job.save()
self.stdout.write('Updating cards...done')
self.stdout.write(self.style.SUCCESS(job.message))
except Exception as e:
self.stdout.write(self.style.ERROR(traceback.format_exc()))
message = f'Error while updating cards: {e}'
job.termination = timezone.now()
job.status = Job.Status.ERROR
job.status = Job.Status.FAILURE
job.message = message
job.save()
18 changes: 18 additions & 0 deletions backend/spellbook/migrations/0017_alter_job_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.5 on 2023-03-16 19:05

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('spellbook', '0016_card_spoiler_variant_spoiler_alter_variant_legal'),
]

operations = [
migrations.AlterField(
model_name='job',
name='message',
field=models.TextField(blank=True, default=''),
),
]
2 changes: 1 addition & 1 deletion backend/spellbook/models/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Status(models.TextChoices):
expected_termination = models.DateTimeField(blank=False)
termination = models.DateTimeField(blank=True, null=True)
status = models.CharField(choices=Status.choices, default=Status.PENDING, max_length=2, blank=False)
message = models.TextField(blank=True)
message = models.TextField(blank=True, default='')
started_by = models.ForeignKey(
to=User,
related_name='started_jobs',
Expand Down

0 comments on commit f6414ce

Please sign in to comment.