-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspotify.py
239 lines (194 loc) · 8.77 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
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
import os
import sys
import json
from flask import Flask, request, redirect, session, url_for, jsonify
from spotipy import Spotify
from spotipy.oauth2 import SpotifyOAuth
from spotipy.cache_handler import FlaskSessionCacheHandler
from flask_cors import CORS
sys.path.append("NicheRank/algo_src")
import control as ctrl
from analyze_history import User_Metrics
#this is how to start the file with Flask, then create a randomized secret key
app = Flask(__name__)
CORS(app)
app.config['SECRET_KEY'] = os.urandom(64)
#this is connecting the Spotify for Developers (SfD) with my project- the client ID and Secret are what connects them
client_id = '52500f70b3534d0bae16a8efac5a70af'
client_secret = '42de3627a2d14129a605b2472cefbfc3'
#this redirect uri is authorized within the SfD, and is used within the auth_url (very important!!)
redirect_uri = 'http://localhost:5000/callback'
#this is what determines what displays when I am asking for user permission for their data
scope = 'user-read-recently-played'
#CHANGE THIS OPTION FOR DIFFERENT USERS.
# 0 is spotify login. (spotify accounts need to be authenticated in SfD) login with user: Amanda Brannon pw: Workingonit1!
# 1 through 7 are differently generated users with 100000 points (or more) of data
user_option = 0
DEFAULT_DATABASE = 'default_db_100000'
DATABASE_USED = DEFAULT_DATABASE
#this makes a new session upon opening the page- this is important as the authorization token is only temporary
cache_handler = FlaskSessionCacheHandler(session)
sp_oauth = SpotifyOAuth(
client_id=client_id,
client_secret=client_secret,
redirect_uri=redirect_uri,
scope=scope,
cache_handler=cache_handler,
show_dialog=True
)
sp = Spotify(auth_manager=sp_oauth)
#this is the landing page. it checks whether you have logged in yet. if you have (you probably haven't) it goes straight to collecting
#data and automatically rerouting you to your score page. It HAS to redirect to auth_url, or else you will get stuck in a deadlock.
#that auth_url contains the client id and redirect_uri and looks like
#this: https://accounts.spotify.com/authorize?client_id=52500f70b3534d0bae16a8efac5a70af&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A5000%2Fcallback&scope=user-read-recently-played&show_dialog=True
@app.route('/')
def home():
if (user_option == 0):
if not sp_oauth.validate_token(cache_handler.get_cached_token()):
auth_url = sp_oauth.get_authorize_url()
return redirect(auth_url)
return redirect(url_for('get_recently_played'))
else:
return redirect(url_for('get_recently_played'))
#this callback function happens when you have logged in- it authorizes with your access token and reroutes you to collecting data
#and going to the score page
@app.route('/callback')
def callback():
sp_oauth.get_access_token(request.args['code'])
return redirect(url_for('get_recently_played'))
#this happens so fast you never see the page! this checks again to make sure you are authorized. It then takes the 50 most recently
#played songs and puts them in a .json file to be taken in by another file for analysis. the redirectUri is manually changed
#so that you can get to your score page (this redirect connection point took us hours to figure out.)
@app.route('/get_recently_played')
def get_recently_played():
if (user_option == 0):
if not sp_oauth.validate_token(cache_handler.get_cached_token()):
auth_url = sp_oauth.get_authorize_url()
return redirect(auth_url)
data = sp.current_user_recently_played()
file_path = "user_history.json"
with open(file_path, "w") as f:
json.dump(data, f)
#return redirect(url_for('fake_user1'))
redirect_uri = 'http://127.0.0.1:8000/Score'
return redirect(redirect_uri)
@app.route('/user_metrics', methods=['GET']) #http://127.0.0.1:5000/user_metrics
def user_metrics():
if (user_option == 0):
sorting_type = "q" # can be q or m
history_path = "user_history.json"
metrics: User_Metrics = ctrl.get_metrics_spotify_user(history=history_path, sorting_type=sorting_type)
# Access the favorites attribute directly from the Artist_Metrics
artist_list = metrics.artist_metrics.favorites
#Access song metrics
song_list = metrics.song_metrics.favorites
#print(song_list[:10])
# Access the pop_score attribute
pop_score = metrics.pop_score
# Create a response dictionary containing both the artist list and the pop score
response = {
"topArtists": artist_list[:5], # get only the top 10 favorite artists
"pop_score": pop_score,
"topSongs": song_list[:5]
}
return jsonify(response)
#THIS ARE THE FAKE USER PROFILES. YOU CAN CHANGE THE VARIABLES WITHIN get_metrics_fake_user() IF YOU WANT MORE VARIATIONS
elif (user_option == 1):
metrics: User_Metrics= ctrl.get_metrics_fake_user(history_size=100000, pop_level="c", sorting_type="q", database_name=DATABASE_USED)
artist_list = metrics.artist_metrics.favorites
song_list = metrics.song_metrics.favorites
pop_score=100
pop_score = metrics.pop_score
print(pop_score)
response = {
"topArtists": artist_list[:5],
"pop_score": pop_score,
"topSongs": song_list[:5]
}
return jsonify(response)
elif (user_option == 2):
metrics: User_Metrics= ctrl.get_metrics_fake_user(history_size=100000, pop_level="b", sorting_type="q", database_name=DATABASE_USED)
artist_list = metrics.artist_metrics.favorites
song_list = metrics.song_metrics.favorites
pop_score=100
pop_score = metrics.pop_score
print(pop_score)
response = {
"topArtists": artist_list[:5],
"pop_score": pop_score,
"topSongs": song_list[:5]
}
return jsonify(response)
elif (user_option == 3):
metrics: User_Metrics= ctrl.get_metrics_fake_user(history_size=100000, pop_level="a", sorting_type="q", database_name=DATABASE_USED)
artist_list = metrics.artist_metrics.favorites
song_list = metrics.song_metrics.favorites
pop_score=100
pop_score = metrics.pop_score
print(pop_score)
response = {
"topArtists": artist_list[:5],
"pop_score": pop_score,
"topSongs": song_list[:5]
}
return jsonify(response)
elif (user_option == 4):
metrics: User_Metrics= ctrl.get_metrics_fake_user(history_size=100000, pop_level="c", sorting_type="m", database_name=DATABASE_USED)
artist_list = metrics.artist_metrics.favorites
song_list = metrics.song_metrics.favorites
pop_score=100
pop_score = metrics.pop_score
print(pop_score)
response = {
"topArtists": artist_list[:5],
"pop_score": pop_score,
"topSongs": song_list[:5]
}
return jsonify(response)
elif (user_option == 5):
metrics: User_Metrics= ctrl.get_metrics_fake_user(history_size=100000, pop_level="b", sorting_type="m", database_name=DATABASE_USED)
artist_list = metrics.artist_metrics.favorites
song_list = metrics.song_metrics.favorites
pop_score=100
pop_score = metrics.pop_score
print(pop_score)
response = {
"topArtists": artist_list[:5],
"pop_score": pop_score,
"topSongs": song_list[:5]
}
return jsonify(response)
elif (user_option == 6):
metrics: User_Metrics= ctrl.get_metrics_fake_user(history_size=100000, pop_level="a", sorting_type="m", database_name=DATABASE_USED)
artist_list = metrics.artist_metrics.favorites
song_list = metrics.song_metrics.favorites
pop_score=100
pop_score = metrics.pop_score
print(pop_score)
response = {
"topArtists": artist_list[:5],
"pop_score": pop_score,
"topSongs": song_list[:5]
}
return jsonify(response)
elif (user_option == 7):
metrics: User_Metrics= ctrl.get_metrics_fake_user(history_size=200000, pop_level="c", sorting_type="q", database_name=DATABASE_USED)
artist_list = metrics.artist_metrics.favorites
song_list = metrics.song_metrics.favorites
pop_score=100
pop_score = metrics.pop_score
print(pop_score)
response = {
"topArtists": artist_list[:5],
"pop_score": pop_score,
"topSongs": song_list[:5]
}
return jsonify(response)
#this never happens since we redirect to the frontend :)
@app.route('/logout')
def logout():
session.clear()
return redirect(url)
#the backbone of all python files
if __name__ == '__main__':
app.run(debug=True)