-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter.py
88 lines (61 loc) · 2.19 KB
/
filter.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
import spotipy
from spotipy.oauth2 import SpotifyOAuth
from dotenv import load_dotenv
import os
load_dotenv()
# print(os.environ['SPOTIPY_CLIENT_ID'])
scopes = ["user-library-read","user-top-read"]
# sp = spotipy.Spotify(
# auth_manager=SpotifyOAuth(
# client_id=SPOTIPY_CLIENT_ID,
# client_secret=SPOTIPY_CLIENT_SECRET,
# redirect_uri="http://example.com",
# scope=scope,
# show_dialog=True,
# cache_path="token.txt"
# )
# )
happy_songs = []
sad_songs = []
calm_songs = []
high_energy_songs = []
cheerful_songs = []
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scopes))
top_tracks = sp.current_user_top_tracks(limit=50)
# genre =
# user_id = sp.current_user()["id"]
# results = sp.current_user_saved_tracks()
for idx, item in enumerate(top_tracks['items']):
# track = item['track']
uri = item['uri']
acousticness = sp.audio_features(uri)[0]['acousticness']
energy = sp.audio_features(uri)[0]['energy']
danceability = sp.audio_features(uri)[0]['danceability']
valence = sp.audio_features(uri)[0]['valence']
print(idx, item['uri'], item['name'], [a['name'] for a in item['artists']],danceability)
if valence > 0.5:
happy_songs.append(item)
else:
sad_songs.append(item)
if energy>0.6:
high_energy_songs.append(item)
else:
calm_songs.append(item)
if danceability>0.7:
cheerful_songs.append(item)
print("LIST OF HAPPY SONGS")
for item in happy_songs:
print(f"Song: {item['name']}, Artists: {[a['name'] for a in item['artists']]}")
print("LIST OF SAD SONGS")
for item in sad_songs:
print(f"Song: {item['name']}, Artists: {[a['name'] for a in item['artists']]}")
print("LIST OF HIGH ENERGY SONGS")
for item in high_energy_songs:
print(f"Song: {item['name']}, Artists: {[a['name'] for a in item['artists']]}")
print("LIST OF CALM SONGS")
for item in calm_songs:
print(f"Song: {item['name']}, Artists: {[a['name'] for a in item['artists']]}")
print("LIST OF CHEERFUL SONGS")
for item in cheerful_songs:
print(f"Song: {item['name']}, Artists: {[a['name'] for a in item['artists']]}")
# sad happy neutral surprise angry disgust fear