Skip to content

Instantly share code, notes, and snippets.

@TKIPisalegacycipher
Forked from sckzw/README.md
Last active June 12, 2020 16:22
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save TKIPisalegacycipher/060da84f55b13837b310 to your computer and use it in GitHub Desktop.
Save TKIPisalegacycipher/060da84f55b13837b310 to your computer and use it in GitHub Desktop.
Python script to find and delete duplicate tracks from Google Play Music library.

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.

#!/usr/bin/env python
# created by shuichinet https://gist.github.com/shuichinet
# forked from https://gist.github.com/shuichinet/8159878 21 Nov 2015
# using minor edits by fcrimins https://www.reddit.com/user/fcrimins from https://www.reddit.com/r/google/comments/2xzgyv/remove_duplicate_songs_from_google_play_music/csh6mrh
# also using clever edits by Morgan Gothard https://medium.com/@mgothard
# updated for Python 3.5 by John M. Kuchta https://medium.com/@sebvance 22 Nov 2016 (hey I was busy)
# compiled by John M. Kuchta https://medium.com/@sebvance
# thanks to shuichinet, fcrimins and Mr. Gothard for their work
from gmusicapi import Mobileclient
from getpass import getpass
client = Mobileclient()
logged_in = client.login(input('Username:'), getpass(), Mobileclient.FROM_MAC_ADDRESS)
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')
if song.get('discNumber') is None:
discnum = 0
else:
discnum = song.get('discNumber')
if song.get('trackNumber') is None:
tracknum = 0
else:
tracknum = song.get('trackNumber')
key = "%s: %d-%02d %s" % ( song.get('album'), discnum, tracknum, 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(' ' + str(key.encode('utf-8')))
if input('Delete duplicate songs? (y, n): ') is 'y':
print('Deleting songs ...')
client.delete_songs( old_song_ids )
else:
print('Finally. No duplicate songs.')
@guidoevers
Copy link

thanks for the modified script. Can this also be modified to work with python 3?

@TKIPisalegacycipher
Copy link
Author

Yes Guido, in fact I've just done this. See above.

@bouchaet
Copy link

Hello, thanks for the script! Many many of my duplicates were gone it few secs.

Small note: on windows, not sure if it's ok but I had to replace the "input(...)" command by "raw_input(...)" to make it work.

@MIKAd3v
Copy link

MIKAd3v commented Nov 28, 2016

Hello, first thank you for this script!
For information, on my system (debian 8 jessie - python v2.7.9), it's working only if I put the username and the y/n answer between double quotes.

@Forceflow
Copy link

Worked perfectly!
Script would be a little better if "password" would say "one-time generated password", because that's what was confusing me at first.

@devinludwig
Copy link

This is what I get when I try to run this:

(response was: u'{"error":{"errors":[{"domain":"global","reason":"backendError","message":"Backend Error"}],"code":500,"message":"Backend Error"}}')

Any ideas?

@cgiAlexis
Copy link

I'm finding in Python 3.6 that I need to place quotation marks (") around my responses to the Username: and y/n inputs in order to get this script to work. Once I put my username and y response within quotation marks then the script runs perfectly :)

Is there a way of fixing this? I'm having to teach myself Python in the meantime to see if I can fix it :)

@blech
Copy link

blech commented Dec 13, 2017

I patched the previous Python 2 version with code to chunk deletes, because with over 700 I was getting server errors:

        print "Deleting songs ..."
        while old_song_ids:
            delete_ids = old_song_ids[:100]
            old_song_ids = old_song_ids[100:]

            print "  {} songs ...".format(len(delete_ids))
            client.delete_songs( delete_ids )```

@giantsfan006
Copy link

Getting the following error:
gmusicapi.exceptions.NotLoggedIn

Any help is greatly appreciated!

@bmorgan99
Copy link

bmorgan99 commented Jun 27, 2018

Also getting Backend Error, Code 500
Python v3.6.1

Edit: See blech's post above (2nd print line needed parens for me)

@tortdog
Copy link

tortdog commented Jul 22, 2018

I am also receiving a .NotLoggedIn error. I have tried putting my gmail address in quotes with the @gmail.com extension, have tried with just the username in quotes, and have tried both without quotes. I have tried putting the password in quotes and not. Running Python 3.6 and everything installed without an error. Any suggestions? Can anyone confirm this script still works please?

@giantsfan006
Any luck on your end?

@infinitejones
Copy link

I was able to log in by setting up at app password at https://myaccount.google.com/apppasswords and using that

It appears necessary to add @gmail.com to the username too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment