-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify.py
63 lines (41 loc) · 1.61 KB
/
spotify.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import credentials
import spotifyRequests
API_URL = "https://api.spotify.com/v1"
SEARCH_COMMAND = "/search"
def search_artist(artistName):
url = API_URL + "/search"
params = {
'q' : artistName,
'type' : 'artist'
}
result = spotifyRequests.request("GET", url, authenticated=True, params=params)
resultJson = result.json()
# Keep the first result from the list
artist = resultJson['artists']['items'][0]['name']
uid = resultJson['artists']['items'][0]['id']
print(f"Found artist '{artist}' with id '{uid}'")
def create_playlist(userId, name):
data = {
"name": name,
"description": "New playlist description",
"public": "false"
}
header = {
'Content-Type' : "application/x-www-form-urlencoded"
}
url = API_URL + f"/users/{userId}/playlists"
# Se necesita estar autenticado con unos scopes concretos. Leer:
# https://developer.spotify.com/documentation/web-api/tutorials/code-flow
result = spotifyRequests.request("POST", url, authenticated=True, params=data, header=header)
print(result.json())
def main():
client_id = credentials.get_credential("spotifyClientId")
client_secret = credentials.get_credential("spotifyClientSecret")
userId = credentials.get_credential("spotifyUserIdGuille")
scopes = "playlist-modify-public playlist-modify-private"
#spotifyRequests.authenticate_basic(client_id, client_secret)
#search_artist("Taylor Swift")
#create_playlist(userId,"wisi wisi")
spotifyRequests.authenticate_user(client_id, scopes)
if __name__ == '__main__':
main()