Skip to content

Instantly share code, notes, and snippets.

@markwhat1
Last active April 27, 2019 10:09
Show Gist options
  • Save markwhat1/484c1b083a58cf623a93fa445aec1cf2 to your computer and use it in GitHub Desktop.
Save markwhat1/484c1b083a58cf623a93fa445aec1cf2 to your computer and use it in GitHub Desktop.
Improved Google Music duplicate deletion script
#!/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)
while logged_in:
print('Getting all songs ...')
all_songs = client.get_all_songs()
new_songs = {}
old_songs = {}
for song in all_songs:
song_id = song.get('id')
song_artist = song.get('artist')
song_album = song.get('album')
song_title = song.get('title')
timestamp = song.get('recentTimestamp')
discnum = 0 if not song.get('discNumber') else song.get('discNumber')
tracknum = 0 if not song.get('trackNumber') else song.get('trackNumber')
discnum = 0 if not song.get('discNumber') else song.get('discNumber')
key = f'{song_artist}:{song_album}; {discnum}-{tracknum:02d} {song_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.')
logged_in = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment