From 125222845bc4bd7239ff62687287ea7acb75c5c3 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Tue, 24 Mar 2026 10:45:49 -0400 Subject: [PATCH] [scrobbles] Add backfill tags command --- .../commands/backfill_scrobble_tags.py | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 vrobbler/apps/scrobbles/management/commands/backfill_scrobble_tags.py diff --git a/vrobbler/apps/scrobbles/management/commands/backfill_scrobble_tags.py b/vrobbler/apps/scrobbles/management/commands/backfill_scrobble_tags.py new file mode 100644 index 0000000..020e653 --- /dev/null +++ b/vrobbler/apps/scrobbles/management/commands/backfill_scrobble_tags.py @@ -0,0 +1,44 @@ +from django.core.management.base import BaseCommand +from scrobbles.models import Scrobble + + +class Command(BaseCommand): + def add_arguments(self, parser): + parser.add_argument( + "--commit", + action="store_true", + help="Actually update tags", + ) + + def handle(self, *args, **options): + commit = options["commit"] + + task_scrobbles = Scrobble.objects.filter(media_type=Scrobble.MediaType.TASK) + + updated_count = 0 + skipped_count = 0 + + for scrobble in task_scrobbles: + logdata = scrobble.logdata + if not logdata or not hasattr(logdata, "labels") or not logdata.labels: + skipped_count += 1 + continue + + existing_tags = list(scrobble.tags.all().values_list("name", flat=True)) + new_labels = [ + label for label in logdata.labels if label not in existing_tags + ] + + if new_labels: + updated_count += 1 + if commit: + for label in new_labels: + scrobble.tags.add(label) + print(f"Updated scrobble {scrobble.id} with tags: {new_labels}") + else: + print( + f"[DRY RUN] Would update scrobble {scrobble.id} with tags: {new_labels}" + ) + + print(f"\nUpdated {updated_count} scrobbles") + print(f"Skipped {skipped_count} scrobbles (no labels in logdata)")