[scrobbles] Add backfill tags command
All checks were successful
build & deploy / test (push) Successful in 1m45s
build & deploy / deploy (push) Successful in 22s

This commit is contained in:
2026-03-24 10:45:49 -04:00
parent 41bb52c551
commit 125222845b

View File

@ -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)")