From eb9daf96b0a4a915b526cf4e2f9ff58e0c6c6e55 Mon Sep 17 00:00:00 2001 From: Rik Veenboer Date: Thu, 12 Nov 2015 12:26:42 +0000 Subject: [PATCH] Clean project. --- README.md | 10 +++++--- duplicates.py | 55 ++++++++++++++++++++++++++++++++++++++++++ gpm_cleaner.py | 42 -------------------------------- delete.py => itunes.py | 21 +++++++++++++--- 4 files changed, 79 insertions(+), 49 deletions(-) create mode 100644 duplicates.py delete mode 100644 gpm_cleaner.py rename delete.py => itunes.py (64%) diff --git a/README.md b/README.md index fed5b7f..c829496 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# Delete songs from Google Music that you deleted from iTunes +# Delete unwanted songs from Google Music -Google Music Manager can automatically upload songs from your local iTunes library to Google Music. When you delete songs from you iTunes library, however, they remain to exist in you Google Music library. This script automates the tedious process of deleting songs from Google Music manually. +Google Music Manager can automatically upload songs from your local iTunes library to Google Music. When you delete songs from you iTunes library, however, they remain to exist in you Google Music library. Two scripts automate the tedious process of deleting those (and duplicated) songs from Google Music manually. ## Installation This script relies on [pyItunes][1] and [Unofficial-Google-Music-API][2]. Installation of these dependencies is easy using pip: @@ -19,7 +19,11 @@ Generate an application specific password for your google account: https://secur Just alter the values in broken brackets and run the script: ``` -python delete.py +python itunes.py +``` +or +``` +python duplicates.py ``` [1]: https://github.com/liamks/pyitunes diff --git a/duplicates.py b/duplicates.py new file mode 100644 index 0000000..13d55e2 --- /dev/null +++ b/duplicates.py @@ -0,0 +1,55 @@ +#!/usr/bin/python +# Copyright (c) 2014 Rik Veenboer +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from gmusicapi import Mobileclient + +# Login +api = Mobileclient() +api.login('', '', Mobileclient.FROM_MAC_ADDRESS) + +# Google Music songs +print 'Getting all songs ...' +all_songs = api.get_all_songs() +new_songs = {} +old_songs = {} + +# Find duplicates +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): + # Display songs + print 'Duplicate songs' + delete = [] + for key in sorted(old_songs.keys()): + delete.append(old_songs[key]['id']) + print ' ' + key.encode('utf-8') + + # Delete songs + if raw_input( 'Delete duplicate songs? (y, n): ') is 'y': + print 'Deleting songs...' + api.delete_songs(delete) +else: + print 'No duplicate songs' \ No newline at end of file diff --git a/gpm_cleaner.py b/gpm_cleaner.py deleted file mode 100644 index 19976c2..0000000 --- a/gpm_cleaner.py +++ /dev/null @@ -1,42 +0,0 @@ -#!/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" diff --git a/delete.py b/itunes.py similarity index 64% rename from delete.py rename to itunes.py index 594043b..11fffe6 100644 --- a/delete.py +++ b/itunes.py @@ -13,21 +13,34 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . + from pyItunes import * from gmusicapi import Mobileclient # iTunes songs -l = Library("") +l = Library('') songs = [(song.artist, song.name) for id,song in l.songs.items()] # Google Music songs api = Mobileclient() -api.login('', '') +api.login('', '', Mobileclient.FROM_MAC_ADDRESS) library = api.get_all_songs() # Find songs to delete delete = filter(lambda song: (song['artist'], song['title']) not in songs, library) delete = [song['id'] for song in delete] -# Delete songs -api.delete_songs(delete) \ No newline at end of file +if len(delete): + # Display songs + print "Duplicate songs" + old_song_ids = [] + for key in sorted(delete): + key = '%s: %d-%02d %s' % (song.get('album'), song.get('discNumber'), song.get('trackNumber'), song.get('title')) + print " " + key.encode('utf-8') + + # Delete songs + if raw_input( "Delete duplicate songs? (y, n): ") is 'y': + print "Deleting songs..." + api.delete_songs(delete) +else: + print 'No deleted songs' \ No newline at end of file