From e24114ad7a8b086270fe1ff4927446273b008bb5 Mon Sep 17 00:00:00 2001 From: pjft Date: Fri, 11 Sep 2020 20:51:06 +0100 Subject: [PATCH 1/3] Slight fixes to gmusic to plex scripts - adds more robust playlist song retrieval from Google Play Music for when, for some reason, the get_shared_playlist_contents() method didn't return all the playlist elements; - removes unnecessary encoding of title and album that were preventing Plex searches from finding the right songs. --- utility/gmusic_playlists_to_plex.py | 38 +++++++++++++++++++---------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/utility/gmusic_playlists_to_plex.py b/utility/gmusic_playlists_to_plex.py index 1956f64..316d31b 100644 --- a/utility/gmusic_playlists_to_plex.py +++ b/utility/gmusic_playlists_to_plex.py @@ -57,21 +57,21 @@ def round_down(num, divisor): return num - (num%divisor) -def compare(ggmusic, pmusic): +def compare(info, pmusic): """ Parameters ---------- - ggmusic (dict): Contains Playlist data from Google Music + info (dict): Contains track data from Google Music pmusic (object): Plex item found from search Returns ------- pmusic (object): Matched Plex item """ - title = str(ggmusic['track']['title'].encode('ascii', 'ignore')) - album = str(ggmusic['track']['album'].encode('ascii', 'ignore')) - tracknum = int(ggmusic['track']['trackNumber']) - duration = int(ggmusic['track']['durationMillis']) + title = str(info['title'].encode('ascii', 'ignore')) + album = str(info['album'].encode('ascii', 'ignore')) + tracknum = int(info['trackNumber']) + duration = int(info['durationMillis']) # Check if track numbers match if int(pmusic.index) == int(tracknum): @@ -87,6 +87,17 @@ def compare(ggmusic, pmusic): elif title == pmusic.title: return [pmusic] +songs = mc.get_all_songs() +def get_songs_info(id_song): + for e in songs: + if e['id'] == id_song: + return { + "title": e['title'], + "artist": e['artist'], + "album": e['album'], + "trackNumber": e['trackNumber'], + "durationMillis": e['durationMillis'] + } def main(): for pl in mc.get_all_user_playlist_contents(): @@ -98,10 +109,11 @@ def main(): playlistContent = [] shareToken = pl['shareToken'] # Go through tracks in Google Music Playlist - for ggmusic in mc.get_shared_playlist_contents(shareToken): - title = str(ggmusic['track']['title'].encode('ascii', 'ignore')) - album = str(ggmusic['track']['album'].encode('ascii', 'ignore')) - artist = str(ggmusic['track']['artist']) + for ggmusic in pl['tracks']: + info = get_songs_info(ggmusic['trackId']) + title = str(info['title']) + album = str(info['album']) + artist = str(info['artist']) # Search Plex for Album title and Track title albumTrackSearch = plex.library.section(MUSIC_LIBRARY_NAME).searchTracks( **{'album.title': album, 'track.title': title}) @@ -110,7 +122,7 @@ def main(): playlistContent += albumTrackSearch if len(albumTrackSearch) > 1: for pmusic in albumTrackSearch: - albumTrackFound = compare(ggmusic, pmusic) + albumTrackFound = compare(info, pmusic) if albumTrackFound: playlistContent += albumTrackFound break @@ -123,7 +135,7 @@ def main(): playlistContent += trackSearch if len(trackSearch) > 1: for pmusic in trackSearch: - trackFound = compare(ggmusic, pmusic) + trackFound = compare(info, pmusic) if trackFound: playlistContent += trackFound break @@ -133,7 +145,7 @@ def main(): artistSearch = plex.library.section(MUSIC_LIBRARY_NAME).searchTracks( **{'artist.title': artist}) for pmusic in artistSearch: - artistFound = compare(ggmusic, pmusic) + artistFound = compare(info, pmusic) if artistFound: playlistContent += artistFound break From a1adb47767574e66c2042b93bbf511037ddcdbbf Mon Sep 17 00:00:00 2001 From: pjft Date: Tue, 15 Sep 2020 20:45:01 +0100 Subject: [PATCH 2/3] Adjust code from feedback Thanks for the feedback. Once again, first incursion in python - unsure if this is what you expected on the dictionary front. Also, added the declaration/instancing of GGMUSICLIST to after the authentication part because it'd fail miserably if we weren't authenticated, I imagine? As for the behavior, I have a free account, all playlists created by myself, mostly uploaded tracks, some purchased. This seemed to be the way it'd work for me. A simple way I got to troubleshoot it was to run print("Playlist length: {}".format(len(mc.get_shared_playlist_contents(shareToken)))) print("Playlist SOURCE length: {}".format(len(pl['tracks']))) and in many cases there was a difference there (the SOURCE would always be the same number or larger than the one from get_shared_playlist_contents(shareToken), and the correct number from what I could tell). Hope this helps, but once again, your mileage may vary. Just wanted to share in case it'd help. --- utility/gmusic_playlists_to_plex.py | 45 ++++++++++++----------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/utility/gmusic_playlists_to_plex.py b/utility/gmusic_playlists_to_plex.py index 316d31b..ac3ff40 100644 --- a/utility/gmusic_playlists_to_plex.py +++ b/utility/gmusic_playlists_to_plex.py @@ -40,7 +40,7 @@ plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess) mc = Mobileclient() if not mc.oauth_login(device_id=Mobileclient.FROM_MAC_ADDRESS): mc.perform_oauth() - +GGMUSICLIST = mc.get_all_songs() def round_down(num, divisor): """ @@ -57,21 +57,21 @@ def round_down(num, divisor): return num - (num%divisor) -def compare(info, pmusic): +def compare(ggmusic, pmusic): """ Parameters ---------- - info (dict): Contains track data from Google Music + ggmusic (dict): Contains track data from Google Music pmusic (object): Plex item found from search Returns ------- pmusic (object): Matched Plex item """ - title = str(info['title'].encode('ascii', 'ignore')) - album = str(info['album'].encode('ascii', 'ignore')) - tracknum = int(info['trackNumber']) - duration = int(info['durationMillis']) + title = str(ggmusic['title'].encode('ascii', 'ignore')) + album = str(ggmusic['album'].encode('ascii', 'ignore')) + tracknum = int(ggmusic['trackNumber']) + duration = int(ggmusic['durationMillis']) # Check if track numbers match if int(pmusic.index) == int(tracknum): @@ -87,17 +87,10 @@ def compare(info, pmusic): elif title == pmusic.title: return [pmusic] -songs = mc.get_all_songs() -def get_songs_info(id_song): - for e in songs: - if e['id'] == id_song: - return { - "title": e['title'], - "artist": e['artist'], - "album": e['album'], - "trackNumber": e['trackNumber'], - "durationMillis": e['durationMillis'] - } +def get_ggmusic(trackId): + for ggmusic in GGMUSICLIST: + if ggmusic['id'] == trackId: + return ggmusic def main(): for pl in mc.get_all_user_playlist_contents(): @@ -109,11 +102,11 @@ def main(): playlistContent = [] shareToken = pl['shareToken'] # Go through tracks in Google Music Playlist - for ggmusic in pl['tracks']: - info = get_songs_info(ggmusic['trackId']) - title = str(info['title']) - album = str(info['album']) - artist = str(info['artist']) + for ggmusicTrackInfo in pl['tracks']: + ggmusic = get_ggmusic(ggmusicTrackInfo['trackId']) + title = str(ggmusic['title']) + album = str(ggmusic['album']) + artist = str(ggmusic['artist']) # Search Plex for Album title and Track title albumTrackSearch = plex.library.section(MUSIC_LIBRARY_NAME).searchTracks( **{'album.title': album, 'track.title': title}) @@ -122,7 +115,7 @@ def main(): playlistContent += albumTrackSearch if len(albumTrackSearch) > 1: for pmusic in albumTrackSearch: - albumTrackFound = compare(info, pmusic) + albumTrackFound = compare(ggmusic, pmusic) if albumTrackFound: playlistContent += albumTrackFound break @@ -135,7 +128,7 @@ def main(): playlistContent += trackSearch if len(trackSearch) > 1: for pmusic in trackSearch: - trackFound = compare(info, pmusic) + trackFound = compare(ggmusic, pmusic) if trackFound: playlistContent += trackFound break @@ -145,7 +138,7 @@ def main(): artistSearch = plex.library.section(MUSIC_LIBRARY_NAME).searchTracks( **{'artist.title': artist}) for pmusic in artistSearch: - artistFound = compare(info, pmusic) + artistFound = compare(ggmusic, pmusic) if artistFound: playlistContent += artistFound break From 68e53f6f9d23eda22e3539197ad9d8ec3a4369bb Mon Sep 17 00:00:00 2001 From: pjft Date: Sun, 27 Sep 2020 08:41:51 +0100 Subject: [PATCH 3/3] Added remaining changes --- utility/gmusic_playlists_to_plex.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/utility/gmusic_playlists_to_plex.py b/utility/gmusic_playlists_to_plex.py index ac3ff40..5145a95 100644 --- a/utility/gmusic_playlists_to_plex.py +++ b/utility/gmusic_playlists_to_plex.py @@ -3,7 +3,7 @@ """ Description: Pull Playlists from Google Music and create Playlist in Plex -Author: Blacktwin +Author: Blacktwin, pjft, sdlynx Requires: gmusicapi, plexapi, requests @@ -41,6 +41,7 @@ mc = Mobileclient() if not mc.oauth_login(device_id=Mobileclient.FROM_MAC_ADDRESS): mc.perform_oauth() GGMUSICLIST = mc.get_all_songs() +PLEX_MUSIC_LIBRARY = plex.library.section(MUSIC_LIBRARY_NAME) def round_down(num, divisor): """ @@ -108,7 +109,7 @@ def main(): album = str(ggmusic['album']) artist = str(ggmusic['artist']) # Search Plex for Album title and Track title - albumTrackSearch = plex.library.section(MUSIC_LIBRARY_NAME).searchTracks( + albumTrackSearch = PLEX_MUSIC_LIBRARY.searchTracks( **{'album.title': album, 'track.title': title}) # Check results if len(albumTrackSearch) == 1: @@ -122,7 +123,7 @@ def main(): # Nothing found from Album title and Track title if not albumTrackSearch or len(albumTrackSearch) == 0: # Search Plex for Track title - trackSearch = plex.library.section(MUSIC_LIBRARY_NAME).searchTracks( + trackSearch = PLEX_MUSIC_LIBRARY.searchTracks( **{'track.title': title}) if len(trackSearch) == 1: playlistContent += trackSearch @@ -135,7 +136,7 @@ def main(): # Nothing found from Track title if not trackSearch or len(trackSearch) == 0: # Search Plex for Artist - artistSearch = plex.library.section(MUSIC_LIBRARY_NAME).searchTracks( + artistSearch = PLEX_MUSIC_LIBRARY.searchTracks( **{'artist.title': artist}) for pmusic in artistSearch: artistFound = compare(ggmusic, pmusic) @@ -145,6 +146,8 @@ def main(): if not artistSearch or len(artistSearch) == 0: print(u"Could not find in Plex:\n\t{} - {} {}".format(artist, album, title)) print("Adding Playlist: {}".format(playlistName)) + print("Google Music Playlist: {}, has {} tracks. {} tracks were added to Plex.".format( + playlistName, len(pl['tracks']), len(playlistContent))) plex.createPlaylist(playlistName, playlistContent) main()