-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspotify.py
More file actions
48 lines (42 loc) · 1.6 KB
/
spotify.py
File metadata and controls
48 lines (42 loc) · 1.6 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
# -*- coding: utf-8 -*-
import config
import spotipy
import datetime
import spotipy.util as util
import spotipy
# getting token and creating spotify object based on [SPOTIFY_CLIENT_ID] and [SPOTIFY_CLIENT_SECRET]
token =util.oauth2.SpotifyClientCredentials(config.SPOTIFY_CLIENT_ID, config.SPOTIFY_CLIENT_SECRET)
cache_token = token.get_access_token()
spotify = spotipy.Spotify(cache_token)
#the ids of the tracks to be added to the playlist
tids = []
track_list = []
for track in best_songs:
results = spotify.search(q=track,limit = 1)
for i, t in enumerate(results['tracks']['items']):
tids.append(t['uri'])
#creates user playlist
now = datetime.datetime.now()
now = str(now)
scope = 'playlist-modify'
token = util.prompt_for_user_token(
config.SPOTIFY_USERNAME,scope,
client_id=config.SPOTIFY_CLIENT_ID,
client_secret=config.SPOTIFY_CLIENT_SECRET,
redirect_uri=config.SPOTIFY_REDIRECT_URI)
if token:
sp = spotipy.Spotify(auth=token)
results = sp.user_playlist_create(SPOTIFY_USERNAME, name=now, public=True)
else:
print "Can't get token for", SPOTIFY_USERNAME
scope = 'playlist-modify'
token = util.prompt_for_user_token(SPOTIFY_USERNAME,scope,client_id="SPOTIFY_CLIENT_ID",client_secret="8e0a4bbeb59b4822bd69ab85a6209c13",redirect_uri='https://musicrecommendation.com/callback')
if token:
sp = spotipy.Spotify(auth=token)
sp.trace = False
results = sp.current_user_playlists(limit=50)
#get the uri most recently created playlist
first_playlist = (results['items'])[0]
playlist_uri = first_playlist['uri']
else:
print("Can't get token for", SPOTIFY_USERNAME)