diff --git a/scripts/gpm_retrieve/.gitignore b/scripts/gpm_retrieve/.gitignore new file mode 100644 index 0000000..d93b329 --- /dev/null +++ b/scripts/gpm_retrieve/.gitignore @@ -0,0 +1,3 @@ +requirements.txt +venv +mobileclient.cred diff --git a/scripts/gpm_retrieve/gpm_retrieve.py b/scripts/gpm_retrieve/gpm_retrieve.py new file mode 100755 index 0000000..c10f9bb --- /dev/null +++ b/scripts/gpm_retrieve/gpm_retrieve.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 + +from gmusicapi import Mobileclient +import argparse +import sys +import requests + +creds_path=sys.path[0] + '/mobileclient.cred' + +def authenticate(api): + creds = api.perform_oauth(storage_filepath=creds_path, open_browser=False) + +def transferLibrary(gpm_api, mudbase_api): + songs = gpm_api.get_all_songs() + + # Determine all unique artists + artists = sorted(set([song['artist'] for song in songs])) + + # Store the artist index of each song + songArtistIdxs = [ artists.index(song['artist']) for song in songs ] + + # Determine all unique albums per artist + artistAlbums = [ sorted(set([ song['album'] for song in songs if song['artist'] == artist ])) for artist in artists ] + + # Create artists and store their mudbase Ids + artistMudbaseIds = [] + for artist in artists: + response = requests.post(mudbase_api + '/artist/create', data = { + 'name': artist + }).json() + print(f"Created artist \"{artist}\", response: {response}") + artistMudbaseIds.append(response['id']) + + # Create songs + for song in songs: + artistMudbaseId = artistMudbaseIds[ artists.index(song['artist']) ] + response = requests.post(mudbase_api + '/song/create', data = { + 'title': song['title'], + 'artistId': artistMudbaseId + }).json() + print(f"Created song \"{song['title']}\" with artist ID {artistMudbaseId}, response: {response}") + +api = Mobileclient() + +parser = argparse.ArgumentParser(description="Import Google Music library into MudBase.") +parser.add_argument('--authenticate', help="Generate credentials for authentication", action="store_true") +parser.add_argument('mudbase_api', help="Address for the Mudbase back-end API") + +args = parser.parse_args() + +if args.authenticate: + authenticate(api) +else: + api.oauth_login(Mobileclient.FROM_MAC_ADDRESS, oauth_credentials=creds_path) + transferLibrary(api, args.mudbase_api) diff --git a/server/server.ts b/server/server.ts index 9a3ad4d..78710f9 100644 --- a/server/server.ts +++ b/server/server.ts @@ -30,14 +30,16 @@ app.post(api.CreateSongEndpoint, (req: any, res: any) => { res.sendStatus(400); return; } - const song = models.Song.create({ + models.Song.create({ title: reqObject.title, ArtistId: reqObject.artistId - }); - const responseObject: api.CreateSongResponse = { - id: song.id - }; - res.status(200).send(responseObject); + }) + .then((song:any) => { + const responseObject: api.CreateSongResponse = { + id: song.id + }; + res.status(200).send(responseObject); + }) }) }); @@ -71,11 +73,13 @@ app.post(api.CreateArtistEndpoint, (req: any, res: any) => { return; } const reqObject: api.CreateArtistRequest = req.body; - const artist = models.Artist.create(reqObject); - const responseObject: api.CreateArtistResponse = { - id: artist.id - }; - res.status(200).send(responseObject); + models.Artist.create(reqObject) + .then((artist: any) => { + const responseObject: api.CreateArtistResponse = { + id: artist.id + }; + res.status(200).send(responseObject); + }) }); app.get(api.ListArtistsEndpoint, (req: any, res: any) => {