Skip to content

Instantly share code, notes, and snippets.

@tolgahanuzun
Last active September 4, 2019 12:58
Show Gist options
  • Save tolgahanuzun/77de32bbc105d696d5f08c2428217da9 to your computer and use it in GitHub Desktop.
Save tolgahanuzun/77de32bbc105d696d5f08c2428217da9 to your computer and use it in GitHub Desktop.
In #Spotify, I wrote a tiny #python script to add my favorite songs to a list. You can create your own list by taking your own app and oauth information.
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}")

Create a list for Spotify's saved songs

  • Add to a list of saved songs you liked on Spotify app.

Install

pip install spotipy

Detail:

  • API Scopes: user-library-read playlist-modify-private
  • Python Verison: Python3.7

How to run?

python app.py tolgahanuzun my_liked_song_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment