forked from maxkirchoff/google-music-dupe-killer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kill_dupes
executable file
·106 lines (92 loc) · 4.27 KB
/
kill_dupes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
from gmusicapi import Mobileclient
def map_track_duplication(tracks):
album_track_duplicate_map = {}
for track in tracks:
if 'album' in track.keys():
albumNorm = track['album'].lower()
titleNorm = track['title'].lower()
if albumNorm not in album_track_duplicate_map:
album_track_duplicate_map.update({albumNorm: {}})
if titleNorm in album_track_duplicate_map[albumNorm]:
album_track_duplicate_map[albumNorm][titleNorm] += 1
else:
album_track_duplicate_map[albumNorm][titleNorm] = 1
return album_track_duplicate_map
def sort_tracks_by_album(tracks):
tracks_by_album = {}
for track in tracks:
if 'album' in track.keys():
albumNorm = track['album'].lower()
if albumNorm not in tracks_by_album:
tracks_by_album[albumNorm] = []
tracks_by_album[albumNorm].append(track)
return tracks_by_album
def get_duplicate_tracks(all_tracks_by_album, album_track_duplicate_map):
duplicate_tracks = []
for album_title in album_track_duplicate_map:
for track_title in album_track_duplicate_map[album_title]:
duplicates = album_track_duplicate_map[album_title][track_title] - 1
if duplicates > 0:
for album in all_tracks_by_album:
if album_title == album:
for track in all_tracks_by_album[album]:
if 'album' in track.keys():
albumNorm = track['album'].lower()
titleNorm = track['title'].lower()
if titleNorm == track_title and duplicates > 0:
duplicate_tracks.append(track)
print "Queueing for removal: '" + track['title'].encode('utf-8').strip() + "' from album '" + track['album'].encode('utf-8').strip() + "' by '" + track['artist'].encode('utf-8').strip()
all_tracks.remove(track)
duplicates += -1
return duplicate_tracks
import sys
def query_yes_no(question, default="yes"):
"""Ask a yes/no question via raw_input() and return their answer.
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no" or None (meaning
an answer is required of the user).
The "answer" return value is one of "yes" or "no".
"""
valid = {"yes":True, "y":True, "ye":True,
"no":False, "n":False}
if default == None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = raw_input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' "\
"(or 'y' or 'n').\n")
def get_track_ids(tracks):
track_ids = []
for track in tracks:
track_ids.append(track['id'])
return track_ids
api = Mobileclient()
logged_in = api.login('username', 'password', Mobileclient.FROM_MAC_ADDRESS)
if logged_in:
print "Successfully logged in. Beginning duplicate detection process."
all_tracks = api.get_all_songs()
album_track_duplicate_map = map_track_duplication(all_tracks)
all_tracks_by_album = sort_tracks_by_album(all_tracks)
duplicate_tracks = get_duplicate_tracks(all_tracks_by_album, album_track_duplicate_map)
duplicate_track_ids = get_track_ids(duplicate_tracks)
if len(duplicate_track_ids) > 0:
if query_yes_no("Found " + str(len(duplicate_track_ids)) + " duplicate tracks. Delete duplicates?", "no"):
deleted_track_ids = api.delete_songs(duplicate_track_ids)
print "Successfully deleted " + str(len(deleted_track_ids)) + " of " + str(len(duplicate_track_ids)) + " queued songs for removal."
else:
print "I didn't find any duplicate tracks."
print "Thank you!"