Added Python script which transfers songs, artists from GPM.

pull/7/head
Sander Vocke 5 years ago
parent bbf233c428
commit 648b5201f7
  1. 3
      scripts/gpm_retrieve/.gitignore
  2. 55
      scripts/gpm_retrieve/gpm_retrieve.py
  3. 26
      server/server.ts

@ -0,0 +1,3 @@
requirements.txt
venv
mobileclient.cred

@ -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)

@ -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) => {

Loading…
Cancel
Save