Spotify で曲を保存したりしていても、いつか解約したときにすべて消えてしまうような気がしてつらい。
せめてどんな曲を保存していたかの情報があれば、乗り換え先の音楽サービスで同じものを探すことができるかもしれない。
幸い Spotify は善良なサービスなので、API を公開している。これを利用すれば情報を取得できる。
事前に Spotify for Developers に登録してクライアントIDなどを取得する必要がある。 https://developer.spotify.com/dashboard/login
また、Python から Spotify の情報にアクセスするために、Spotipy というライブラリを利用する。 https://github.com/plamere/spotipy
import sys
import spotipy
import spotipy.util as util
scope = 'user-library-read user-follow-read playlist-read-collaborative playlist-read-private'
username = 'your_username'
token = util.prompt_for_user_token(username, scope, client_id='your_client_id',client_secret='your_client_secret',redirect_uri='http://localhost/')
if not token:
print("Can't get token for", username)
sys.exit()
sp = spotipy.Spotify(auth=token)
results = sp.current_user_saved_tracks(limit=50)
tracks = results['items']
while results['next']:
results = sp.next(results)
tracks.extend(results['items'])
for item in tracks:
track = item['track']
print(track['name'] + ' - ' + track['artists'][0]['name'])
San Diego - South Park
Once Upon a Time - Toby Fox
Start Menu - Toby Fox
...
Ribcage - Kettel
Bootmens - Kettel
Alacasa (Rolando Simmons Remix) - Kettel
results = sp.current_user_saved_albums(limit=50)
albums = results['items']
while results['next']:
results = sp.next(results)
albums.extend(results['items'])
album_tracks = {}
for album in albums:
album = album['album']
print(album['name'] + ' - ' + album['artists'][0]['name'])
results = sp.album_tracks(album['id'])
this_album_tracks = results['items']
while results['next']:
results = sp.next(results)
this_album_tracks.extend(results['items'])
album_tracks[album['id']] = this_album_tracks
for item in this_album_tracks:
print(item['name'])
UNDERTALE Soundtrack - Toby Fox
Once Upon a Time
Start Menu
...
Ribcage
Bootmens
Alacasa (Rolando Simmons Remix)
results = sp.current_user_followed_artists(limit=50)['artists']
artists = results['items']
while results['next']:
results = sp.next(results)
artists.extend(results['items'])
for item in artists:
artist = item
print(artist['name'])
Kettel
Domotic
Casey Abrams
...
Aphex Twin
Juana Molina
I Am Robot And Proud
results = sp.current_user_playlists(limit=50)
playlists = results['items']
while results['next']:
results = sp.next(results)
playlists.extend(results['items'])
playlist_tracks = {}
for playlist in playlists:
print(playlist['name'])
results = sp.user_playlist_tracks(user=sp.me()['id'], playlist_id=playlist['id'])
this_playlist_tracks = results['items']
while results['next']:
results = sp.next(results)
this_playlist_tracks.extend(results['items'])
playlist_tracks[playlist['id']] = this_playlist_tracks
for item in this_playlist_tracks:
print(item['track']['name'])
Jazz Favorite
Del Sasser
IDM
...
Words (feat. Bri Tolani) [kr1sh Remix]
Angery
Without Your Love (feat. Ralph Larenzo)
data = {'tracks': tracks, 'albums': albums, 'album_tracks': album_tracks, 'artists': artists, 'playlists': playlists, 'playlist_tracks': playlist_tracks}
import pickle
with open('spotify_data.pickle', mode='wb') as f:
pickle.dump(data, f)