forked from Manal-jpg/csc111-group-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_data.py
More file actions
152 lines (128 loc) · 5.92 KB
/
user_data.py
File metadata and controls
152 lines (128 loc) · 5.92 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
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
"""
CSC111 Winter 2023 Project:
MelodyMatch: Tailored Music Recommendations Derived From Your Spotify Habits
This Python module contains the code used to start a local web-server that is used to obtain the user's
authorization to access data from their Spotify account.
Notes
===============================
This file contains some code borrowed directly from examples included the Spotipy library documentation, which can be
found at the following link:
- https://spotipy.readthedocs.io/en/2.22.1/#
Copyright and Usage Information
===============================
This file is provided solely for the personal and private use by students and faculty
of the CSC111 course department at the University of Toronto. All forms of distribution
of this code, whether as is or with changes, are prohibited. For more information on
copyright for this project's materials, please contact the developers directly.
This file is Copyright (c) 2023 Manaljav Munkhbayar, Kevin Hu, Stanley Pang, Jaeyong Lee.
"""
from bottle import route, request
from spotipy import oauth2
import spotipy
import csv
import bottle
app = bottle.default_app()
# The Spotify OAuth object contains the access token and refresh
# token required to make authenticated requests to the Spotify Web API
sp_oauth = oauth2.SpotifyOAuth(client_id='2aef90bcdc834c17b59ff1358099865a',
client_secret='2ab27bed30c441aab581dbac673736f2',
redirect_uri='http://localhost:8080',
scope='user-library-read, user-top-read',
)
# Initialize empty lists to store the attributes of the user's top songs
top_tracks_ids = []
top_tracks_names = []
top_tracks_danceability = []
top_tracks_energy = []
top_tracks_loudness = []
top_tracks_speechiness = []
top_tracks_acousticness = []
top_tracks_instrumentalness = []
top_tracks_valence = []
top_tracks_liveness = []
top_tracks_tempo = []
@route('/')
def get_access_token() -> str:
"""
This function does several things:
- Obtain an access token by calling the get_access_token method on the Spotify OAuth object.
- If an access token is successfully retrieved, create a "Spotify" object with said token.
- Make the API call to obtain the user's top 50 tracks.
- The return value of this function is what gets sent back to the client that made the request, in
this case being HTML code displayed in the user's browser.
"""
# Declare the global variables
global top_tracks_ids, top_tracks_names, top_tracks_danceability, top_tracks_energy, top_tracks_loudness, \
top_tracks_speechiness, top_tracks_acousticness, top_tracks_instrumentalness, top_tracks_valence, \
top_tracks_liveness, top_tracks_tempo
token_information = sp_oauth.get_access_token()
access_token = ""
if token_information:
print("An access token has been found.")
access_token = token_information['access_token']
else:
auth_code = sp_oauth.parse_response_code(request.url)
if auth_code:
token_information = sp_oauth.get_access_token(auth_code)
access_token = token_information['access_token']
if access_token:
sp = spotipy.Spotify(access_token)
top_tracks = sp.current_user_top_tracks(limit=50, offset=0, time_range='medium_term')
top_tracks_names = [track['name'] for track in top_tracks['items']]
top_tracks_ids = [track['id'] for track in top_tracks['items']]
audio_features = sp.audio_features(top_tracks_ids)
for i in range(len(top_tracks_ids)):
top_tracks_danceability.append(audio_features[i]['danceability'])
top_tracks_energy.append(audio_features[i]['energy'])
top_tracks_loudness.append(audio_features[i]['loudness'])
top_tracks_speechiness.append(audio_features[i]['speechiness'])
top_tracks_acousticness.append(audio_features[i]['acousticness'])
top_tracks_instrumentalness.append(audio_features[i]['instrumentalness'])
top_tracks_valence.append(audio_features[i]['valence'])
top_tracks_liveness.append(audio_features[i]['liveness'])
top_tracks_tempo.append(audio_features[i]['tempo'])
write_to_csv()
return '''
<html>
<head>
<title>MelodyMatch</title>
</head>
<body>
<h1>Authorization status: successful.</h1>
<p>You may now close this tab and enter ctrl+c to stop the local server!</p>
</body>
</html>
'''
else:
return '''
<html>
<head>
<title>MelodyMatch</title>
</head>
<body>
<h1>Error Obtaining Access Token</h1>
<p>We were unable to gain access to your Spotify account.</p>
</body>
</html>
'''
def run_server() -> None:
"""Runs the local web-server using bottle.
"""
bottle.run(app, host='')
def write_to_csv() -> None:
"""Writes the attributes of the top songs belonging to the user in a new CSV file for later analysis.
"""
with open('data/user_top_songs.csv', 'w', newline='') as output_file:
writer = csv.writer(output_file, delimiter=',')
for i in range(len(top_tracks_ids)):
writer.writerow([top_tracks_ids[i], top_tracks_names[i], top_tracks_danceability[i], top_tracks_energy[i],
top_tracks_loudness[i], top_tracks_speechiness[i], top_tracks_acousticness[i],
top_tracks_instrumentalness[i], top_tracks_valence[i], top_tracks_liveness[i],
top_tracks_tempo[i]])
if __name__ == '__main__':
import doctest
doctest.testmod(verbose=True)
import python_ta
python_ta.check_all(config={
'max-line-length': 120
})