-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspiffy.py
181 lines (149 loc) · 5.47 KB
/
spiffy.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
import pyen
from spotipy import client, util, oauth2
import numpy
import json
import sys, random, os
nest_key = os.environ['ECHONEST_KEY']
nest = pyen.Pyen(nest_key)
steve_spotify_id = '1210159879'
PLAYLIST_IDS = {
'fall': '4wm4J4d2cKeTTCJ4Mzz06M',
'winter': '7EUoqdUjR91tMmp41nw7Y4',
'spring': '0rmr2dJ3fuudkrXyIXZgIZ',
'summer': '3mpSb7dwheLtB6QdvIBV2m',
'rainy': '2u7t4X9wA2B493qQgR47aW'
}
SONG_DATA_FIELDS = {
'danceability',
'energy',
'loudness',
'mode',
'key',
'tempo',
'valence'
}
use_cache = True
"""FILE IO"""
# Load cached songs
def load_cache():
song_cache = {}
with open('songCache.json') as data_file:
song_cache = json.load(data_file)
return song_cache
# Write songs to cache
def write_cache(songs):
with open('songCache.json', 'w') as outfile:
json.dump(songs, outfile)
# Write song metrics
def write_metrics(stats):
with open('metrics.json', 'w') as outfile:
json.dump(stats, outfile)
def load_metrics():
stats = {}
with open('metrics.json') as data_file:
stats = json.load(data_file)
return stats
#Compute stats for a season
def compute_stats(season):
stats = {}
for field in SONG_DATA_FIELDS:
raw = [track['audio_summary'][field] for track in season]
mean = numpy.mean(raw)
std = numpy.std(raw)
stats[field] = {
'raw': raw,
'mean': mean,
'std': std
}
return stats
#Fetch songs from remote API
def fetch_song_data():
songs = {}
for season in PLAYLIST_IDS:
print 'Fetching season playlist: ' + season
playlist = spot.user_playlist_tracks(steve_spotify_id, PLAYLIST_IDS[season])
playlist_tracks = []
for item in playlist['items']:
track = item['track']
playlist_tracks.append({
'title': track['name'],
'artist': track['artists'][0]['name']
})
analyzed_tracks = []
print 'Fetching song analysis: ' + season
for track in playlist_tracks:
results = nest.get('song/search', artist=track['artist'],
title=track['title'], bucket='audio_summary')
# Should prolly be exception handled but WHATEVER HACKDAYZ
if results['songs']:
analyzed_tracks.append(results['songs'][0])
songs[season] = analyzed_tracks
return songs
#Wire this up to a weather API
def is_raining():
return False
#Should have a dict of valid season values somewhere
#Gonna hard code each seasonf or now because HACKDAYZ
def get_seasonal_params(season, stats):
query = {}
season_stats = stats[season]
if season == 'fall' or season == 'winter':
query['max_energy'] = season_stats['energy']['mean'] + (season_stats['energy']['std'] / 2)
query['min_energy'] = season_stats['energy']['mean'] - season_stats['energy']['std']
query['max_valence'] = season_stats['valence']['mean'] + (season_stats['valence']['std'] / 2)
query['min_valence'] = season_stats['valence']['mean'] - season_stats['valence']['std']
elif season == 'spring' or season == 'summer':
query['max_energy'] = season_stats['energy']['mean'] + season_stats['energy']['std']
query['min_energy'] = season_stats['energy']['mean'] - (season_stats['energy']['std'] / 2)
query['max_valence'] = season_stats['valence']['mean'] + season_stats['valence']['std']
query['min_valence'] = season_stats['valence']['mean'] - (season_stats['valence']['std'] / 2)
if is_raining():
query['max_tempo'] = stats['rainy']['tempo']['mean']
query['min_tempo'] = stats['rainy']['tempo']['mean'] - stats['rainy']['tempo']['std']
#Also decrease the valence and energy by something
return query
def get_new_songs(season, stats):
query = {}
query['genre'] = ['stomp and holler', 'indie folk']
query['sort'] = ['hotttnesss-desc']
query['results'] = ['100']
# Get up to 200 artists. This is so messy
artists = nest.get('artist/search', query)['artists']
query['start'] = ['100']
artists.extend(nest.get('artist/search', query)['artists'])
songs = []
song_query = get_seasonal_params(season, stats)
for artist in artists:
song_query['artist_id'] = artist['id']
song_query['results'] = ['10']
songs.extend(nest.get('song/search', song_query)['songs'])
return random.sample(songs, 30)
def make_playlist(spot, songs, name):
playlist = spot.user_playlist_create(steve_spotify_id, name)
ids = []
for song in songs:
query = 'track:'+ song['title'] + ' artist:' + song['artist_name']
#lmao this is hideous
results = spot.search(query, 1, 0, 'track')['tracks']['items']
if(results):
ids.append(results[0]['id'])
spot.user_playlist_add_tracks(steve_spotify_id, playlist['id'], ids)
def main(argv):
token = util.prompt_for_user_token(steve_spotify_id, 'playlist-modify-public')
spot = client.Spotify(token)
songs = {}
stats = {}
if use_cache:
songs = load_cache()
stats = load_metrics()
else:
songs = fetch_song_data()
write_cache(songs)
for season in songs:
print 'Analyzing season: ' + season
stats[season] = compute_stats(songs[season])
write_metrics(stats)
new_songs = get_new_songs('summer', stats)
make_playlist(spot, new_songs, 'Spiffy: Fall New')
if __name__ == "__main__":
main(sys.argv[1:])