Przeglądaj źródła

[utils] Add crud deduplicator

Colin Powell 7 miesięcy temu
rodzic
commit
af0e76b29c
1 zmienionych plików z 20 dodań i 0 usunięć
  1. 20 0
      vrobbler/apps/scrobbles/utils.py

+ 20 - 0
vrobbler/apps/scrobbles/utils.py

@@ -269,3 +269,23 @@ def get_file_md5_hash(file_path: str) -> str:
         while chunk := f.read(8192):
             file_hash.update(chunk)
     return file_hash.hexdigest()
+
+
+def deduplicate_tracks():
+    from music.models import Track
+
+    # TODO This whole thing should iterate over users
+    dups = []
+
+    for t in Track.objects.all():
+        if Track.objects.filter(title=t.title, artist=t.artist).exists():
+            dups.append(t)
+
+    for b in dups:
+        tracks = Track.objects.filter(artist=b.artist, title=b.title)
+        first = tracks.first()
+        for other in tracks.exclude(id=first.id):
+            print("moving scrobbles for ", other.id, " to ", first.id)
+            other.scrobble_set.update(track=first)
+            print("deleting ", other.id, " - ", other)
+            other.delete()