Python script to delete duplicate tracks from Google Play Music library. Please retry until "No duplicate songs" message is displayed. Use this script at your own risk. gmusicapi is required.
-
-
Save sckzw/8159878 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python | |
from gmusicapi import Mobileclient | |
from getpass import getpass | |
client = Mobileclient() | |
client.login( raw_input( "Username: " ), getpass() ) | |
print "Getting all songs ..." | |
all_songs = client.get_all_songs() | |
new_songs = {} | |
old_songs = {} | |
for song in all_songs: | |
song_id = song.get('id') | |
timestamp = song.get('recentTimestamp') | |
key = "%s: %d-%02d %s" % ( song.get('album'), song.get('discNumber'), song.get('trackNumber'), song.get('title') ) | |
if key in new_songs: | |
if new_songs[key]['timestamp'] < timestamp: | |
old_songs[key] = new_songs[key] | |
new_songs[key] = { 'id': song_id, 'timestamp': timestamp } | |
else: | |
old_songs[key] = { 'id': song_id, 'timestamp': timestamp } | |
new_songs[key] = { 'id': song_id, 'timestamp': timestamp } | |
if len( old_songs ): | |
print "Duplicate songs" | |
old_song_ids = [] | |
for key in sorted( old_songs.keys() ): | |
old_song_ids.append( old_songs[key]['id'] ) | |
print " " + key.encode('utf-8') | |
if raw_input( "Delete duplicate songs? (y, n): ") is 'y': | |
print "Deleting songs ..." | |
client.delete_songs( old_song_ids ) | |
else: | |
print "No duplicate songs" |
Hi,
I have a gMusic utility that does a lot of things with Google Play Music like export library and playlists as CSV or HTMl, download all songs in a playlist, import CSV and some other features. Someone sent me a link to your GitHub page and asked if I could add this duplicate check to my application. Would you mind if I use your code (I'll give you credit for it) in my application ?
Thanks for this, great stuff!
Below is the code to remove duplicates from all playlists (rather than songs in the library). Don't forget the , Mobileclient.FROM_MAC_ADDRESS
from TheRealWag's comment if it's appropriate.
#!/usr/bin/env python
from gmusicapi import Mobileclient
from getpass import getpass
client = Mobileclient()
client.login( raw_input( "Username: " ), getpass() )
print "Getting all playlists ..."
all_plists = client.get_all_user_playlist_contents()
all_dup_entries = []
for plist in all_plists:
unique_track_list = []
duplen = len( all_dup_entries )
for entry in plist.get('tracks'):
track_id = entry.get('trackId')
entry_id = entry.get('id')
if track_id in unique_track_list:
all_dup_entries.append(entry_id)
else:
unique_track_list.append(track_id)
duplen = len( all_dup_entries ) - duplen
if duplen:
print "\n " + plist.get('name').encode('utf-8') + " - " + str(duplen) + " duplicates"
del unique_track_list
del duplen
if len( all_dup_entries ):
print "Duplicate playlist entries exist."
if raw_input( "Delete duplicate entries? (y, n): ") is 'y':
print "Deleting entries ..."
client.remove_entries_from_playlist( all_dup_entries )
else:
print "No duplicate entries"
Can you tell me what i'm doing wrong? I get this error:
python2 gm-cleaner.py
Username: XXXXXXXX
Password:
Getting all songs ...
Traceback (most recent call last):
File "gm-cleaner.py", line 18, in
key = "%s: %d-%02d %s" % ( song.get('album'), song.get('discNumber'), song.get('trackNumber'), song.get('title') )
TypeError: %d format: a number is required, not NoneType
login function now takes 4 parameters instead of 3 so this line:
client.login( raw_input( "Username: " ), getpass() )
should look like this:
client.login( raw_input( "Username: " ), getpass(), Mobileclient.FROM_MAC_ADDRESS)
(worked for me)