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)
# Get all saved tracks: # The result is paginated, so use spotipy.Spotify().next() to retrive all items. 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']) # Save: tracks
San Diego - South Park
Once Upon a Time - Toby Fox
Start Menu - Toby Fox
...
Ribcage - Kettel
Bootmens - Kettel
Alacasa (Rolando Simmons Remix) - Kettel
# Get all saved albums: 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']) # Save: albums, album_tracks
UNDERTALE Soundtrack - Toby Fox
Once Upon a Time
Start Menu
...
Ribcage
Bootmens
Alacasa (Rolando Simmons Remix)
# Get all followed artists: 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']) # Save: artists
Kettel
Domotic
Casey Abrams
...
Aphex Twin
Juana Molina
I Am Robot And Proud
# Get all playlists: 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']) # playlist の中身を表示 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']) # Save: playlists, playlist_tracks
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)