[utils] Add crud deduplicator

This commit is contained in:
2024-11-17 22:02:19 -05:00
parent 39004aac0c
commit af0e76b29c

View File

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