-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspotify_init.py
More file actions
69 lines (55 loc) · 2.09 KB
/
spotify_init.py
File metadata and controls
69 lines (55 loc) · 2.09 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Created 29/10/2019
# Last Edited: 29/10/2019
# Author(s): Will Woodruff
# Collaborator(s): Craig Clephane
import datetime
import requests
import json
def run_request(bearer, search_text):
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + bearer,
}
params = (
('q', search_text),
('type', 'track'),
)
response = requests.get('https://api.spotify.com/v1/search', headers=headers, params=params)
json_data = json.loads(response.text)
if response.status_code == 200:
print('Success! We are generating your songs!')
print(json_data)
elif response.status_code == 404:
print('Not Found. Sorry, there wern\'t any results for that article')
else:
print('An error occurred -', response.status_code)
print(len(json_data["tracks"]["items"]))
if len(json_data["tracks"]["items"]) is not 0:
song = json_data["tracks"]["items"][0]["id"]
print(song)
return song
def create_playlist(bearer, user_id='7wxlddrk3temavvzzao5qidyu', playlist_name='Daily News Playlist'):
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + bearer,
}
data = '{"name":"' + playlist_name + ' - ' + str(datetime.date.today().strftime('%d/%m/%Y')) + '", "public":false}'
response = requests.post(f'https://api.spotify.com/v1/users/{user_id}/playlists', headers=headers,
data=data)
json_data = json.loads(response.text)
playlist_id = json_data['external_urls']['spotify'].split('/')[-1]
return playlist_id
def add_track(bearer, track, playlist_id):
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + bearer,
}
params = (
('uris', 'spotify:track:' + track),
)
response = requests.post(f'https://api.spotify.com/v1/playlists/{playlist_id}/tracks', headers=headers,
params=params)
print(response)