Skip to content

Instantly share code, notes, and snippets.

@madx
Last active March 30, 2016 16:41
Show Gist options
  • Save madx/b0a09ae013c628fe22b8f255f6b66fb4 to your computer and use it in GitHub Desktop.
Save madx/b0a09ae013c628fe22b8f255f6b66fb4 to your computer and use it in GitHub Desktop.
Import Rdio collection to Google Play Music using gmusicapi

Usage

$ edit pm_imported.py
Replace USER and PASS with your own credentials
$ python pm_importer.py
# Enjoy.

(Requires python3 and gmusicapi)

#!usr/bin/env python
import sys
import csv
from gmusicapi import Mobileclient
api = Mobileclient()
logged_in = api.login('USER', 'PASS', Mobileclient.FROM_MAC_ADDRESS)
if not logged_in:
print('Unable to login')
sys.exit(1)
# Local CSV file
library = {}
with open('songs.csv', 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
next(reader)
for row in reader:
name, artist, album, tracknumber = row
if artist in library:
if album in library[artist]:
continue
library[artist].append(album)
else:
library[artist] = [album]
# Remote library
upstream = {}
all_songs = api.get_all_songs()
for song in all_songs:
artist = song['artist']
album = song['album']
if artist in upstream:
if album in upstream[artist]:
continue
upstream[artist].append(album)
else:
upstream[artist] = [album]
# Add missing entries
for artist_name in library:
for album_name in library[artist_name]:
if artist_name in upstream and album_name in upstream[artist_name]:
print('Skip %s %s!' % (artist_name, album_name))
continue
try:
search_results = api.search_all_access('%s %s' % (artist_name, album_name))['album_hits']
if len(search_results) > 1:
print('Too many matches for %s %s!' % (artist_name, album_name))
elif len(search_results) == 0:
print('No matches for %s %s!' % (artist_name, album_name))
else:
album = search_results[0]['album']
album_info = api.get_album_info(album['albumId'], True)
for track in album_info['tracks']:
print('%s - %s - %s' % (track['artist'], track['album'], track['title']))
api.add_aa_track(track['storeId'])
except e:
print(e)
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment