-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
263 lines (219 loc) · 9.44 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
import dash_table
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials, SpotifyOAuth
import dash
import dash_html_components as html
from dash.dependencies import Input, Output, State
import plotly.express as px
import pandas as pd
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
colors = {
'background': '#191414',
'text': '#1DB954'
}
scope = "user-read-recently-played user-top-read user-read-currently-playing playlist-read-private " \
"playlist-read-collaborative user-library-read user-read-playback-state app-remote-control streaming"
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))
# wyświetlanie tytułów top utworów + nazwy wykonawców
def top_tracks(time_range="short_term", limit=20, offset=0):
if time_range != "short_term" and time_range != "medium_term" and time_range != "long_term":
return
top_tracks = sp.current_user_top_tracks(limit, offset, time_range)
position = []
position_ = offset
name = []
artists = []
song_id = []
artists_id = []
img_url = []
for song in top_tracks["items"]:
position_ += 1
position.append(position_)
name.append(song["name"])
artists_ = []
artists_id_ = []
for artist in song["artists"]:
artists_.append(artist["name"])
artists_id_.append(artist["id"])
artists.append(artists_)
artists_id.append(artists_id_)
song_id.append(song["id"])
img_url.append("![](" + str(song["album"]["images"][-1]["url"]) + ")")
top_tracks_df = pd.DataFrame({"Pos.": position,
"Song": name,
"Artists": artists,
"Song_ID": song_id,
"Artists_ID": artists_id,
"Album cover": img_url})
return top_tracks_df
# średnia popularność topowych utworów
def top_tracks_avg_popularity(time_range="short_term", limit=10, offset=0):
if time_range != "short_term" and time_range != "medium_term" and time_range != "long_term":
return
top_tracks = sp.current_user_top_tracks(limit, offset, time_range)
i = 0
avg_popularity = 0
for song in top_tracks["items"]:
i += 1
avg_popularity += song['popularity']
avg_popularity = avg_popularity / i
print(
"\nŚREDNIA POPULARNOŚĆ TWOICH TOP " + str(limit) + " UTWORÓW TO : " + str(avg_popularity) + " W SKALI 1 - 100.")
return avg_popularity
# wyświetlanie top wykonawców
def top_artists(time_range="short_term", limit=10, offset=0):
if time_range != "short_term" and time_range != "medium_term" and time_range != "long_term":
return
top_artists = sp.current_user_top_artists(limit, offset, time_range)
position = []
position_ = offset
name = []
image = []
for artist in top_artists["items"]:
position_ += 1
position.append(position_)
name.append(artist["name"])
image.append("![](" + str(artist["images"][-1]["url"]) + ")")
top_artists_df = pd.DataFrame({"Pos.": position, "Artist": name, "Artist image": image})
return top_artists_df
# sprawdzanie top 10 najpopularniejszych gatunków
def top_genres(time_range="short_term", limit=10, offset=0):
if time_range != "short_term" and time_range != "medium_term" and time_range != "long_term":
return
top_tracks = sp.current_user_top_tracks(limit, offset, time_range)
genres = dict()
for song in top_tracks["items"]:
for artist in song['artists']:
for genre in sp.artist(artist['id'])['genres']:
if genre in genres:
genres[genre] += 1
else:
genres[genre] = 1
genres = {k: v for k, v in sorted(genres.items(), key=lambda item: item[1], reverse=True)}
k = offset
position = []
position_ = offset
genrs = []
values = []
for gen in genres.keys():
position_ += 1
position.append(position_)
genrs.append(gen)
values.append(genres[gen])
top_genres_df = pd.DataFrame({"Pos.": position,
"Genres": genrs,
"No. tracks": values})
return top_genres_df
# najpopularniejsze ery (np muzyka '90)
def top_tracks_era(time_range="short_term", limit=50, offset=0):
if time_range != "short_term" and time_range != "medium_term" and time_range != "long_term":
return
eras = dict()
top_tracks = sp.current_user_top_tracks(limit, offset, time_range)
for track in top_tracks["items"]:
if track['album']['release_date_precision'] == "day":
year = int(track['album']['release_date'][0:4])
era = year - year % 10
if era in eras:
eras[era] += 1
else:
eras[era] = 1
eras = {k: v for k, v in sorted(eras.items(), key=lambda item: item[1], reverse=True)}
position =[]
position_ = offset
eras2 = []
values = []
for era in eras.keys():
position_ +=1
position.append(position_)
eras2.append(era)
values.append(eras[era])
top_eras_df = pd.DataFrame({"Pos.": position,
"Era" : eras2,
"No. tracks" : values})
return top_eras_df
def top_tracks_features(time_range="short_term", limit=50, offset=0):
if time_range != "short_term" and time_range != "medium_term" and time_range != "long_term":
return
top_tracks = sp.current_user_top_tracks(limit, offset, time_range)
top_tracks_ids = []
for track in top_tracks["items"]:
top_tracks_ids.append(track["id"])
avg_danceability = 0
avg_energy = 0
avg_duration = 0
i = 0
for track in sp.audio_features(top_tracks_ids): # ["audio_features"]:
i += 1
avg_danceability += track["danceability"]
avg_energy += track["energy"]
avg_duration += track["duration_ms"]
avg_duration = avg_duration / i / 1000
avg_danceability = avg_danceability / i
avg_energy = avg_energy / i
features = pd.DataFrame({"Stat": ["Average of dance ability", "Average of energy", "Average duration of track"],
"Value": [f"{avg_danceability*100:.2f}%", f"{avg_energy*100:.2f}%",
f"{(avg_duration // 60):.0f} min {(avg_duration % 60):.0f} s"]
})
return features
# top_genres()
# top_artists()
# top_tracks_avg_popularity()
# top_tracks_era(time_range="long_term")
# top_tracks_features()
app.layout = html.Div(style={'backgroundColor': colors['background']}, children=[
html.H1(
children='Spotify statistics',
style={
'textAlign': 'center',
'color': colors['text']
}
),
html.Button('Top tracks', id='top_tracks', style={'color': 'white'}, n_clicks=0),
html.Button('Top genres', id='top_genres', style={'color': 'white'}, n_clicks=0),
html.Button('Top artists', id='top_artists', style={'color': 'white'}, n_clicks=0),
html.Button('Top eras', id='top_eras', style={'color': 'white'}, n_clicks=0),
html.Button('Analysis', id='analysis', style={'color': 'white'}, n_clicks=0),
html.Output(id='output',
children=''),
dash_table.DataTable(id='table', fixed_rows={'headers': True, 'data': 0},
style_header={'backgroundColor': '#1DB954'})
])
@app.callback(
[Output("table", "data"), Output('table', 'columns')],
Input('top_tracks', 'n_clicks'),
Input('top_artists','n_clicks'),
Input('top_eras', 'n_clicks'),
Input('top_genres','n_clicks'),
Input('analysis', 'n_clicks')
# State('ScreenName_Input','value')
)
def displayClick(btn1,btn2,btn3,btn4,btn5):
data = []
columns = []
changed_id = [p['prop_id'] for p in dash.callback_context.triggered][0]
if 'top_tracks' in changed_id:
output = top_tracks()
columns = [{'name': col, 'id': col, 'type': 'text', 'presentation': 'markdown'} for col in output.columns if
col != "Song_ID" and col != "Artists_ID"]
data = output.to_dict('records')
elif 'top_artists' in changed_id:
output = top_artists()
columns = [{'name': col, 'id': col, 'type': 'text', 'presentation': 'markdown'} for col in output.columns]
data = output.to_dict('records')
elif 'top_genres' in changed_id:
output = top_genres()
columns = [{'name': col, 'id': col, 'type': 'text', 'presentation': 'markdown'} for col in output.columns]
data = output.to_dict('records')
elif 'top_eras' in changed_id:
output = top_tracks_era()
columns = [{'name': col, 'id': col, 'type': 'text', 'presentation': 'markdown'} for col in output.columns]
data = output.to_dict('records')
elif 'analysis' in changed_id:
output = top_tracks_features()
columns = [{'name': col, 'id': col, 'type': 'text', 'presentation': 'markdown'} for col in output.columns]
data = output.to_dict('records')
return data, columns
if __name__ == '__main__':
app.run_server(debug=True)