-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
273 lines (200 loc) · 8.51 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"""
This program was written by Brandon Pyle
"""
from dotenv import load_dotenv
import os
import base64
from requests import post, get
import json
from requests_oauthlib import OAuth2Session
from requests.auth import HTTPBasicAuth
load_dotenv()
client_id = os.getenv('CLIENT_ID')
client_secret = os.getenv('CLIENT_SECRET')
# Step 1: authenticate and get token
def get_token():
auth_string = client_id + ':' + client_secret
auth_bytes = auth_string.encode('utf-8')
auth_base64 = str(base64.b64encode(auth_bytes), 'utf-8')
url = 'https://accounts.spotify.com/api/token'
headers = {
'Authorization': 'Basic ' + auth_base64,
'Content-Type': 'application/x-www-form-urlencoded'
}
data = {'grant_type': 'client_credentials'}
result = post(url, headers=headers, data=data)
json_result = json.loads(result.content)
token = json_result['access_token']
return token
def get_auth_header():
return {'Authorization': 'Bearer ' + get_token()}
def authorize_user():
token_url = 'https://accounts.spotify.com/api/token'
authorization_base_url = 'https://accounts.spotify.com/authorize'
scope = ['playlist-modify-public', 'playlist-modify-private']
redirect_uri = 'https://brandonpyle.com'
spotify = OAuth2Session(client_id, scope=scope, redirect_uri=redirect_uri)
authorization_url, state = spotify.authorization_url(authorization_base_url)
print(f'Please navigate to this url to authorize access to spotify: {authorization_url}')
redirect_response = input('Paste the full redirect url here: ')
auth = HTTPBasicAuth(client_id, client_secret)
token = spotify.fetch_token(token_url, auth=auth, authorization_response=redirect_response)
print(token)
return token
# Step 2: search for playlists
def search_for_playlists(search_terms, num_playlists):
data = []
url = 'https://api.spotify.com/v1/search'
headers = get_auth_header()
query = f'?q={search_terms}&type=playlist&limit={num_playlists}'
query_url = url + query
result = get(query_url, headers=headers)
json_result = json.loads(result.content)['playlists']['items']
if len(json_result) == 0:
print('No playlists with this name exists')
return None
if len(json_result) < int(num_playlists):
print(f"Only {len(json_result)} playlists found. proceeding...")
for i in range(len(json_result)):
if json_result[i]['owner']['id'] != 'spotify':
print(f"Indexing songs from the playlist '{json_result[i]['name']}'")
data.append(json_result[i]['id'])
else:
print("Playlist by spotify...")
else:
for i in range(int(num_playlists)):
if json_result[i]['owner']['id'] != 'spotify':
print(f"Indexing songs from the playlist '{json_result[i]['name']}'")
data.append(json_result[i]['id'])
else:
print("Playlist by spotify...")
print(len(data))
return data
def get_playlist_songs(playlist_ids, num_playlists, allowExplicit):
playlists = []
song_ids = {}
song_ids_final = {}
for i in range(len(playlist_ids)):
url = f'https://api.spotify.com/v1/playlists/{playlist_ids[i]}'
headers = get_auth_header()
result = get(url, headers=headers)
num_tracks = len(json.loads(result.content)['tracks']['items'])
num_invalid_songs = 0
#print(f'Number of tracks in playlist {playlist_ids[i]}: {num_tracks}')
if allowExplicit:
for j in range(len(playlist_ids)):
try:
id = json.loads(result.content)['tracks']['items'][j]['track']['id']
popularity = json.loads(result.content)['tracks']['items'][j]['track']['popularity']
isExplicit = json.loads(result.content)['tracks']['items'][j]['track']['explicit']
rank = 0
num_occurrences = 1
song_ids.update({id:[popularity, num_occurrences, isExplicit, rank]})
if id not in song_ids_final.keys():
song_ids_final.update({id:[popularity, num_occurrences, isExplicit, rank]})
else:
song_ids_final[id][1] += 1
except:
num_invalid_songs += 1
else:
for j in range(len(playlist_ids)):
try:
id = json.loads(result.content)['tracks']['items'][j]['track']['id']
popularity = json.loads(result.content)['tracks']['items'][j]['track']['popularity']
isExplicit = json.loads(result.content)['tracks']['items'][j]['track']['explicit']
if isExplicit:
break
rank = 0
num_occurrences = 1
song_ids.update({id:[popularity, num_occurrences, isExplicit, rank]})
if id not in song_ids_final.keys():
song_ids_final.update({id:[popularity, num_occurrences, isExplicit, rank]})
else:
song_ids_final[id][1] += 1
except:
num_invalid_songs += 1
if num_invalid_songs > 0:
print(f'{num_invalid_songs} invalid songs found...')
return song_ids_final
# Step 3: rank songs and sort dictionary by rank
def rank_songs(songs):
for id in songs:
songs[id][3] = (songs[id][1] * 2) + (songs[id][0] / 10)
songs_sorted = dict(sorted(songs.items(), key=lambda item: item[1][3], reverse=True))
return songs_sorted
# Step 4: create the playlist
def create_playlist(auth, user_id, name, visibility):
url = 'https://api.spotify.com/v1/users/{}/playlists'.format(user_id)
playlist_info = json.dumps({
'name': name,
"description": "This playlist was automatically generated using a Python program I wrote",
'public': visibility
})
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {}'.format(auth['access_token'])
}
response = post(url, data=playlist_info, headers=headers)
json_result = response.json()
print(response)
return json_result
# Step 5: Add ranked songs to the playlist
def add_songs_to_playlist(auth, id, songs):
song_list = []
total_songs = 100
for song in songs.keys():
if total_songs > 0 and song is not None:
song_list.append("spotify:track:" + song)
total_songs -= 1
else:
break
url = 'https://api.spotify.com/v1/playlists/{}/tracks'.format(id)
data = json.dumps({
'uris': song_list,
'position': 0
})
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {}'.format(auth['access_token'])
}
response = post(url, data=data, headers=headers)
json_result = response.json()
print(response)
user_id = '1xobld5add2v49tsg512lh220'
search_terms = list(map(str,input('Please enter the playlist search terms separated by spaces: ').split()))
search_str = ''
for search_term in search_terms:
search_str += search_term + "+"
search_str = search_str[:-1]
num_playlists = input('Please enter the total number of playlists to search through (0-50): ')
name_list = list(map(str,input('Please enter the name of the playlist you would like to create: ').split()))
name_str = ''
for name in name_list:
name_str += name + " "
visibility = input('Would you like this playlist to be public (y/n): ')
isVisible = True
if visibility.upper() == 'N':
isVisible = False
elif visibility.upper() == 'Y':
isVisible = True
else:
print('Error. Playlist will be created as a public playlist')
explicit = input('Would you like to allow explicit songs in this playlist (y/n): ')
allowExplicit = True
if explicit.upper() == 'N':
allowExplicit = False
elif explicit.upper() == 'Y':
allowExplicit = True
else:
print('Error. Playlist will be created as a public playlist')
auth = authorize_user()
print('Searching for playlists matching your conditions...')
playlist_ids = search_for_playlists(search_str, num_playlists)
print('Getting songs from discovered playlists...')
playlist_song_ids = get_playlist_songs(playlist_ids, num_playlists, allowExplicit)
print('Ranking songs...')
songs = rank_songs(playlist_song_ids)
print('Creating your playlist...')
playlist = create_playlist(auth, user_id, name_str, isVisible)
created_playlist_id = playlist['id']
add_songs_to_playlist(auth, created_playlist_id, songs)