-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotifyRecs.py
47 lines (35 loc) · 1.35 KB
/
spotifyRecs.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
import random
import pandas as pd
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import math
import json
import setVars
setVars.setVars()
scope = "user-library-read"
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))
def main():
songRecs = pullRecs(300)
with open("localdata/spotifyrecommendations.json", "w") as outfile:
json.dump(songRecs, outfile)
def pullRecs(numSongs):
songRecs ={}
favDF = pd.read_csv('localdata/favorites.csv')
#seems like URL length limits track seeds to ~5 songs long
seedTracks = random.choices(favDF.loc[:,'ID'], k=5)
if numSongs < 100:
results = sp.recommendations(seed_tracks=list(seedTracks),limit=numSongs)
for song in results['tracks']:
songRecs[[song['name']]] = song['external_urls']['spotify']
else:
for i in range(math.ceil(numSongs/100)):
results = sp.recommendations(seed_tracks=list(seedTracks),limit=100)
for song in results['tracks']:
if song['id'] not in list(favDF.loc[:,'ID']):
songRecs[song['name']] = [song['external_urls']['spotify'], song['id']]
else:
print("Skipped " + song['name'])
seedTracks = random.choices(favDF.loc[:,'ID'], k=5)
return songRecs
if __name__ == "__main__":
main()