Skip to content

Instantly share code, notes, and snippets.

@epochblue
Last active December 9, 2015 04:25
Show Gist options
  • Save epochblue/380867790fd4430eb4ee to your computer and use it in GitHub Desktop.
Save epochblue/380867790fd4430eb4ee to your computer and use it in GitHub Desktop.
A quick-and-dirty script for getting all my album and playlist info out of Rdio before my subscription is up. This script was only tested on Python 3.5.0.
import requests
from requests.auth import HTTPBasicAuth
CLIENT_ID = ''
CLIENT_SECRET = ''
TOKEN_URL = 'https://services.rdio.com/oauth2/token'
API_URL = 'https://services.rdio.com/api/1/'
# GET TOKEN
# =========
api_token = requests.post(TOKEN_URL,
auth=HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET),
data={
'grant_type': 'client_credentials'
}
).json().get('access_token')
# GET USER INFO
# =============
user = requests.post(API_URL,
data={
'method': 'findUser',
'vanityName': 'epochblue',
'extras': 'albumCount'
},
headers={
'Authorization': 'Bearer {}'.format(api_token)
}
).json().get('result', {})
album_count = user.get('albumCount', 0)
user_key = user.get('key')
# GET ALBUMS IN COLLECTION
# ========================
start = 0
COUNT = 100
with open('albums.txt', 'w') as albums_out:
while start < album_count:
albums = requests.post(API_URL,
data={
'method': 'getAlbumsInCollection',
'count': COUNT,
'start': start,
'sort': 'playCount',
'user': user_key
},
headers={'Authorization': 'Bearer {}'.format(api_token)}
).json().get('result', [])
for album in albums:
artist = album.get('artist')
name = album.get('name')
albums_out.write('{} - {}\n'.format(artist, name))
start += COUNT
# GET PLAYLISTS AND TRACKS THEREIN
# ================================
start = 0
playlists = requests.post(API_URL,
data={
'method': 'getUserPlaylists',
'count': COUNT,
'start': start,
'sort': 'name',
'user': user_key,
'extras': 'tracks'
},
headers={'Authorization': 'Bearer {}'.format(api_token)}
).json().get('result', [])
with open('playlists.txt', 'w') as playlists_out:
for playlist in playlists:
name = playlist.get('name')
tracks = playlist.get('tracks')
playlists_out.write(name + '\n')
playlists_out.write('-' * len(name) + '\n')
for track in tracks:
playlists_out.write('{} - {} ({})\n'.format(track.get('artist'), track.get('name'), track.get('album')))
playlists_out.write('\n\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment