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.
This commit is contained in:
pjft 2020-09-15 20:45:01 +01:00 committed by GitHub
parent e24114ad7a
commit a1adb47767
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -40,7 +40,7 @@ plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
mc = Mobileclient() mc = Mobileclient()
if not mc.oauth_login(device_id=Mobileclient.FROM_MAC_ADDRESS): if not mc.oauth_login(device_id=Mobileclient.FROM_MAC_ADDRESS):
mc.perform_oauth() mc.perform_oauth()
GGMUSICLIST = mc.get_all_songs()
def round_down(num, divisor): def round_down(num, divisor):
""" """
@ -57,21 +57,21 @@ def round_down(num, divisor):
return num - (num%divisor) return num - (num%divisor)
def compare(info, pmusic): def compare(ggmusic, pmusic):
""" """
Parameters Parameters
---------- ----------
info (dict): Contains track data from Google Music ggmusic (dict): Contains track data from Google Music
pmusic (object): Plex item found from search pmusic (object): Plex item found from search
Returns Returns
------- -------
pmusic (object): Matched Plex item pmusic (object): Matched Plex item
""" """
title = str(info['title'].encode('ascii', 'ignore')) title = str(ggmusic['title'].encode('ascii', 'ignore'))
album = str(info['album'].encode('ascii', 'ignore')) album = str(ggmusic['album'].encode('ascii', 'ignore'))
tracknum = int(info['trackNumber']) tracknum = int(ggmusic['trackNumber'])
duration = int(info['durationMillis']) duration = int(ggmusic['durationMillis'])
# Check if track numbers match # Check if track numbers match
if int(pmusic.index) == int(tracknum): if int(pmusic.index) == int(tracknum):
@ -87,17 +87,10 @@ def compare(info, pmusic):
elif title == pmusic.title: elif title == pmusic.title:
return [pmusic] return [pmusic]
songs = mc.get_all_songs() def get_ggmusic(trackId):
def get_songs_info(id_song): for ggmusic in GGMUSICLIST:
for e in songs: if ggmusic['id'] == trackId:
if e['id'] == id_song: return ggmusic
return {
"title": e['title'],
"artist": e['artist'],
"album": e['album'],
"trackNumber": e['trackNumber'],
"durationMillis": e['durationMillis']
}
def main(): def main():
for pl in mc.get_all_user_playlist_contents(): for pl in mc.get_all_user_playlist_contents():
@ -109,11 +102,11 @@ def main():
playlistContent = [] playlistContent = []
shareToken = pl['shareToken'] shareToken = pl['shareToken']
# Go through tracks in Google Music Playlist # Go through tracks in Google Music Playlist
for ggmusic in pl['tracks']: for ggmusicTrackInfo in pl['tracks']:
info = get_songs_info(ggmusic['trackId']) ggmusic = get_ggmusic(ggmusicTrackInfo['trackId'])
title = str(info['title']) title = str(ggmusic['title'])
album = str(info['album']) album = str(ggmusic['album'])
artist = str(info['artist']) artist = str(ggmusic['artist'])
# Search Plex for Album title and Track title # Search Plex for Album title and Track title
albumTrackSearch = plex.library.section(MUSIC_LIBRARY_NAME).searchTracks( albumTrackSearch = plex.library.section(MUSIC_LIBRARY_NAME).searchTracks(
**{'album.title': album, 'track.title': title}) **{'album.title': album, 'track.title': title})
@ -122,7 +115,7 @@ def main():
playlistContent += albumTrackSearch playlistContent += albumTrackSearch
if len(albumTrackSearch) > 1: if len(albumTrackSearch) > 1:
for pmusic in albumTrackSearch: for pmusic in albumTrackSearch:
albumTrackFound = compare(info, pmusic) albumTrackFound = compare(ggmusic, pmusic)
if albumTrackFound: if albumTrackFound:
playlistContent += albumTrackFound playlistContent += albumTrackFound
break break
@ -135,7 +128,7 @@ def main():
playlistContent += trackSearch playlistContent += trackSearch
if len(trackSearch) > 1: if len(trackSearch) > 1:
for pmusic in trackSearch: for pmusic in trackSearch:
trackFound = compare(info, pmusic) trackFound = compare(ggmusic, pmusic)
if trackFound: if trackFound:
playlistContent += trackFound playlistContent += trackFound
break break
@ -145,7 +138,7 @@ def main():
artistSearch = plex.library.section(MUSIC_LIBRARY_NAME).searchTracks( artistSearch = plex.library.section(MUSIC_LIBRARY_NAME).searchTracks(
**{'artist.title': artist}) **{'artist.title': artist})
for pmusic in artistSearch: for pmusic in artistSearch:
artistFound = compare(info, pmusic) artistFound = compare(ggmusic, pmusic)
if artistFound: if artistFound:
playlistContent += artistFound playlistContent += artistFound
break break