- Add to a list of saved songs you liked on Spotify app.
pip install spotipy
- API Scopes:
user-library-read playlist-modify-private
- Python Verison: Python3.7
python app.py tolgahanuzun my_liked_song_list
import sys | |
import spotipy | |
import spotipy.util as util | |
scope = 'user-library-read playlist-modify-private' | |
if len(sys.argv) > 1: | |
username = sys.argv[1] | |
playlist_name = sys.argv[2] | |
else: | |
sys.exit() | |
# token = util.prompt_for_user_token( | |
# username, | |
# scope, | |
# 'client_id', | |
# 'client_secret', | |
# 'http://localhost:9999/callback/' | |
# ) | |
token = 'previously-taken-token-asdkajda' | |
if token: | |
# Get liked song list | |
sp = spotipy.Spotify(auth=token) | |
user_id = sp.me()['id'] | |
results = sp.current_user_saved_tracks() | |
datas = [] | |
for item in range(round(results['total']/50)): | |
_results = sp.current_user_saved_tracks(limit=50, offset=item*50) | |
datas.extend(_results['items']) | |
# Create playlist | |
playlists = sp.user_playlist_create(user_id, playlist_name, public=False) | |
for data in range(round(len(datas)/100)): | |
_datas = datas[data*100:(data+1)*100] | |
# Playlist batch add tracks | |
sp.user_playlist_add_tracks( | |
user_id, | |
playlists['id'], | |
[data['track']['id'] for data in _datas] | |
) | |
else: | |
print(f"Can't get token for {username}") |