-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
49 lines (40 loc) · 1.3 KB
/
main.py
File metadata and controls
49 lines (40 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import spotipy
import requests
import json
import sys
import spotipy
import spotipy.util as util
scopes = ['user-library-read',
'playlist-modify-public',
'playlist-modify-private',
'playlist-read-collaborative',
'playlist-read-private']
if len(sys.argv) > 1:
username = sys.argv[1]
else:
print("Usage: %s username" % (sys.argv[0],))
sys.exit()
token = util.prompt_for_user_token(username, str.join(' ', scopes))
def create_target_playlist():
playlists = sp.user_playlists(username)
playlist_name = 'QTip'
target_playlist = None
for playlist in playlists['items']:
if playlist['name'] == playlist_name:
target_playlist = playlist
print("Found playlist")
if target_playlist is None:
print("Couldn't find playlist, creating...")
resp = sp.user_playlist_create(username, playlist_name, public=False)
print(resp)
song = find_song("ruff ryders")
sp.user_playlist_add_tracks(username, target_playlist["id"], [song['id']])
def find_song(search_string):
song_resp = sp.search(search_string, market="US")
print(song_resp)
return song_resp["tracks"]["items"][0]
if token:
sp = spotipy.Spotify(auth=token)
create_target_playlist()
else:
print("Can't get token for", username)