|
|
|
@ -12,78 +12,109 @@ 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 |
|
|
|
|
# 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) |
|
|
|
|
# For keeping track what we have already created. |
|
|
|
|
storedArtists = dict() |
|
|
|
|
storedAlbums = dict() |
|
|
|
|
storedGenreTags = dict() |
|
|
|
|
|
|
|
|
|
response = requests.post(mudbase_api + '/song', json = { |
|
|
|
|
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': [ artistMudbaseId ], |
|
|
|
|
'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) ], |
|
|
|
|
}).json() |
|
|
|
|
print(f"Created song \"{song['title']}\" with artist ID {artistMudbaseId}, response: {response}") |
|
|
|
|
} |
|
|
|
|
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 { |
|
|
|
|