From 6443888faa166543dbf7815bb20830f1b64836f9 Mon Sep 17 00:00:00 2001 From: Sander Vocke Date: Fri, 14 Aug 2020 19:06:57 +0200 Subject: [PATCH] Seem to have fixed GPM script for albums. Web interface became wonky. --- scripts/gpm_retrieve/gpm_retrieve.py | 145 ++++++++++++++++----------- 1 file changed, 88 insertions(+), 57 deletions(-) diff --git a/scripts/gpm_retrieve/gpm_retrieve.py b/scripts/gpm_retrieve/gpm_retrieve.py index 83e335b..9d6806c 100755 --- a/scripts/gpm_retrieve/gpm_retrieve.py +++ b/scripts/gpm_retrieve/gpm_retrieve.py @@ -11,80 +11,111 @@ creds_path=sys.path[0] + '/mobileclient.cred' def authenticate(api): creds = api.perform_oauth(storage_filepath=creds_path, open_browser=False) -def uploadLibrary(mudbase_api, songs): - # Determine all unique artists - artists = sorted(set([song['artist'] for song in songs if 'artist' in song])) - - # Determine all genres - genres = sorted(set([song['genre'] for song in songs if 'genre' in song])) - - # Store the artist index of each song - songArtistIdxs = [ artists.index(song['artist']) for song in songs if 'artist' in song ] - - # Store the genre index of each song - songGenreIdxs = [ genres.index(song['genre']) for song in songs if 'genre' in song ] - - # Determine all unique albums per artist - artistAlbums = [ sorted(set([ song['album'] for song in songs if song['artist'] == artist ])) for artist in artists ] - - # Determine store ID for all artists +def uploadLibrary(mudbase_api, songs): + # Helpers def getArtistStoreIds(song): if 'artistId' in song: return [ song['artistId'][0] ] return []; - artistStoreIds = [ [ getArtistStoreIds(song) for song in songs if song['artist'] == artist ][0] for artist in artists ] + def getSongStoreIds(song): + if 'storeId' in song: + return [ song['storeId'] ] + return [] # Create GPM import tag gpmTagIdResponse = requests.post(mudbase_api + '/tag', data = { 'name': 'GPM Import' }).json() + gpmTagId = gpmTagIdResponse['id'] print(f"Created tag \"GPM Import\", response: {gpmTagIdResponse}") - # Create genres and store their mudbase Ids + # Create the root genre tag genreRootResponse = requests.post(mudbase_api + '/tag', data = { 'name': 'Genre' }).json() + genreRootTagId = genreRootResponse['id'] print(f"Created tag \"Genre\", response: {genreRootResponse}") - genreMudbaseIds = [] - for idx,genre in enumerate(genres): - response = requests.post(mudbase_api + '/tag', data = { - 'name': genre, - 'parentId': genreRootResponse['id'] - }).json() - print(f"Created tag \"Genre/{genre}\", response: {response}") - genreMudbaseIds.append(response['id']) - - # Create artists and store their mudbase Ids - artistMudbaseIds = [] - for idx,artist in enumerate(artists): - response = requests.post(mudbase_api + '/artist', json = { - 'name': artist, - 'storeLinks': [ 'https://play.google.com/music/m/' + id for id in artistStoreIds[idx] ], - 'tagIds': [ gpmTagIdResponse['id'] ] - }).json() - print(f"Created artist \"{artist}\", response: {response}") - artistMudbaseIds.append(response['id']) - - # Create songs - def getSongStoreIds(song): - if 'storeId' in song: - return [ song['storeId'] ] - return [] - for song in songs: - artistMudbaseId = artistMudbaseIds[ artists.index(song['artist']) ] - tagIds = [ gpmTagIdResponse['id'] ] - if 'genre' in song: - genreMudbaseId = genreMudbaseIds[ genres.index(song['genre']) ] - tagIds.append(genreMudbaseId) - response = requests.post(mudbase_api + '/song', json = { - 'title': song['title'], - 'artistIds': [ artistMudbaseId ], - 'tagIds' : tagIds, - 'storeLinks': [ 'https://play.google.com/music/m/' + id for id in getSongStoreIds(song) ], - }).json() - print(f"Created song \"{song['title']}\" with artist ID {artistMudbaseId}, response: {response}") + # For keeping track what we have already created. + storedArtists = dict() + storedAlbums = dict() + storedGenreTags = dict() + for song in songs: + # TODO: check if these items already exist + + # Determine artist properties. + artist = { + 'name': song['artist'], + 'storeLinks': [ 'https://play.google.com/music/m' + id for id in getArtistStoreIds(song) ], + 'tagIds': [ gpmTagId ] + } if 'artist' in song else None + + # Determine album properties. + album = { + 'name': song['album'], + 'tagIds': [ gpmTagId ] + } if 'album' in song else None + + # Determine genre properties. + genre = { + 'name': song['genre'], + 'parentId': genreRootTagId + } if 'genre' in song else None + + # Upload artist if not already done + artistId = None + if artist: + for key,value in storedArtists.items(): + if value == artist: + artistId = key + break + if not artistId: + response = requests.post(mudbase_api + '/artist', json = artist).json() + artistId = response['id'] + print(f"Created artist \"{artist['name']}\", response: {response}") + storedArtists[artistId] = artist + + # Upload album if not already done + albumId = None + if album: + for key,value in storedAlbums.items(): + if value == album: + albumId = key + break + if not albumId: + response = requests.post(mudbase_api + '/album', json = album).json() + albumId = response['id'] + print(f"Created album \"{album['name']}\", response: {response}") + storedAlbums[albumId] = album + + # Upload genre if not already done + genreTagId = None + if genre: + for key,value in storedGenreTags.items(): + if value == genre: + genreTagId = key + break + if not genreTagId: + response = requests.post(mudbase_api + '/tag', json = genre).json() + genreTagId = response['id'] + print(f"Created genre tag \"Genre / {genre['name']}\", response: {response}") + storedGenreTags[genreTagId] = genre + + # Upload the song itself + tagIds = [ gpmTagId ] + if genreTagId: + tagIds.append(genreTagId) + _song = { + 'title': song['title'], + 'artistIds': [ artistId ] if artistId else [], + 'albumIds': [ albumId ] if albumId else [], + 'tagIds': tagIds, + 'storeLinks': [ 'https://play.google.com/music/m/' + id for id in getSongStoreIds(song) ], + } + response = requests.post(mudbase_api + '/song', json = song).json() + print(f"Created song \"{song['title']}\" with artist ID {artistId}, response: {response}") + def getData(api): return { "songs": api.get_all_songs(),