[scrobbles] Add backfill tags command
This commit is contained in:
@ -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)")
|
||||||
Reference in New Issue
Block a user