You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.9 KiB
55 lines
1.9 KiB
#!/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', 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', json = { |
|
'title': song['title'], |
|
'artistIds': [ 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)
|
|
|