-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
461 lines (358 loc) · 12 KB
/
app.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
from flask import Flask, render_template, jsonify, request, json, redirect
from exfm import ExfmClient
from werkzeug.routing import BaseConverter
from werkzeug.urls import url_fix
import urllib
import urllib2
from collections import OrderedDict
import redis
CACHING = True
app = Flask(__name__)
app.config.from_pyfile('settings_local.py')
exfm = ExfmClient()
_redis = None
class NoArtist(Exception):
pass
def get_redis():
global _redis
if not _redis:
_redis = redis.Redis(
db=12
)
return _redis
class UrlConverter(BaseConverter):
regex = '[^/].*?'
is_greedy = True
weight = 50
def to_python(self, value):
return url_fix(value).replace('http:///', 'http://')
app.url_map.converters['url'] = UrlConverter
def get_latest(start, results):
r = get_redis()
ids = r.lrange('latest_staches', start, results)
pipe = r.pipeline()
[pipe.get('cache:data:%s' % _) for _ in ids]
res = pipe.execute()
songs = [json.loads(x) for x in res]
return songs
def get_similar_song_ids(song_id):
r = get_redis()
key = 'cache:similar:%s' % (song_id)
if not r.exists(key):
params = [
('api_key', 'IILIWPF9XK31O9BLS'),
('song_id', song_id)
]
_ = "http://developer.echonest.com/api/v4/playlist/basic?%s" % urllib.urlencode(params)
fp = urllib2.urlopen(_)
data = fp.read()
fp.close()
resp = json.loads(data)
song_ids = [i['id'] for i in resp['response']['songs']]
r.set(key, json.dumps(song_ids))
else:
song_ids = json.loads(r.get(key))
return song_ids
def get_song(id=None):
song = search_en(id=id)
if not isinstance(song, dict):
return song
song = song['response']['songs'][0]
r = get_redis()
key = 'cache:data:%s' % (song['id'])
if not r.exists(key):
analysis_url = song['audio_summary']['analysis_url']
try:
fp = urllib2.urlopen(analysis_url)
data = json.loads(fp.read())
fp.close()
except AttributeError, e:
print e, analysis_url
return {}
song['loudness'] = OrderedDict([(item['start'], item['loudness_max'])
for item in data['segments']])
if CACHING:
r.set(key, json.dumps(song))
r.lpush('latest_staches', song['id'])
else:
song = json.loads(r.get(key))
return song
@app.context_processor
def _context_processor():
r = get_redis()
num_staches = r.llen('latest_staches')
return {'num_staches': num_staches}
@app.errorhandler(404)
def not_found_error(e):
return render_template('error.html', code=404, message="Couldn't find the stache you're looking for."), 404
@app.errorhandler(500)
def error_500(e):
return render_template('error.html', code=500, message="Too much stache action! Please try refreshing the page."), 500
@app.route('/')
def index():
return render_template("index.html", songs=get_latest(0, 5))
@app.route('/about')
def about():
return render_template("about.html")
@app.route('/song/<song_id>/buy/<what>')
def buy(song_id, what):
song = get_song(song_id)
if what == "shirt":
params = {
'stache': song['s3_url'],
'song': "%s %s" % (song['artist_name'], song['title'])
}
url = "http://www.zazzle.com/api/create/at-238283152284673545?ref=238283152284673545&ax=linkover&pd=235941910710981976&fwd=productpage&ed=true&%s" % (urllib.urlencode(params))
else:
params = {
'title': song['title'],
'artist': song['artist_name'],
'stache': song['s3_url']
}
url = "http://www.zazzle.com/api/create/at-238283152284673545?ref=238283152284673545&ax=linkover&pd=168445848112028823&fwd=productpage&ed=true&%s" % (urllib.urlencode(params))
print url
return redirect(url)
@app.route('/make')
def make():
try:
song = get_song()
if not isinstance(song, dict):
return song
return render_template("make.html", song=song)
except NoArtist, e:
return redirect('/no-artist?q=%s' % e.message)
def suggest(q):
params = [
('api_key', 'IILIWPF9XK31O9BLS'),
('name', q)
]
_ = "http://developer.echonest.com/api/v4/artist/suggest?%s" % (urllib.urlencode(params))
print _
fp = urllib2.urlopen(_)
data = fp.read()
fp.close()
data = json.loads(data)
return data['response']['artists']
@app.route('/no-artist')
def no_artist():
q = request.values.get('q')
suggestions = suggest(q)
return render_template('no-artist.html', q=q, suggestions=suggestions)
@app.route('/latest')
def latest():
r = get_redis()
total = r.llen('latest_staches')
start = request.values.get('start', 0)
results = request.values.get('results', 10)
has_more = total < (start + total)
has_less = start > 0
return render_template("latest.html",
songs=get_latest(start, results), has_more=has_more, has_less=has_less)
@app.route('/song/<song_id>')
def song_page(song_id):
song = get_song(song_id)
songs = [get_song(_) for _ in get_similar_song_ids(song_id)]
return render_template("similar.html", song=song,
songs=songs)
@app.route('/years/<year_start>/<year_end>')
def get_song_years(year_start, year_end):
song_ids = get_song_ids_for_years(year_start, year_end)
songs = [get_song(_) for _ in song_ids]
return render_template("years.html", songs=songs,
year_start=year_start, year_end=year_end)
def get_lastfm_image(name):
params = [
('api_key', 'b25b959554ed76058ac220b7b2e0a026'),
('method', 'artist.getimages'),
('artist', name),
('limit', 1),
('format', 'json')
]
url = "http://ws.audioscrobbler.com/2.0/?%s" % urllib.urlencode(params)
fp = urllib2.urlopen(url)
data = fp.read()
fp.close()
data = json.loads(data)
try:
return data['images']['image']['sizes']['size'][-1]['#text']
except:
return None
@app.route('/artist/<name>')
def get_artist(name):
song_ids = get_artist_song_ids(name)
songs = [get_song(_) for _ in song_ids]
artist_image = get_lastfm_image(name)
return render_template("artist.html", songs=songs,
name=name, artist_image=artist_image)
def get_artist_song_ids(name):
r = get_redis()
key = "cache:artist_song_ids:%s" % (name)
if r.exists(key):
return json.loads(r.get(key))
params = [
('api_key', 'IILIWPF9XK31O9BLS'),
('name', name)
]
_ = "http://developer.echonest.com/api/v4/artist/songs?%s" % (urllib.urlencode(params))
print _
fp = urllib2.urlopen(_)
data = fp.read()
fp.close()
data = json.loads(data)
song_ids = [song['id'] for song in data['response']['songs']]
r.set(key, json.dumps(song_ids))
return song_ids
@app.route('/genre/<genre>')
def get_genre(genre):
songs = get_songs_in_genre(genre)
return render_template("genre.html", songs=songs,
genre=genre)
def get_songs_in_genre(genre):
params = [
('api_key', 'IILIWPF9XK31O9BLS'),
('bucket', 'audio_summary'),
('bucket', 'song_hotttnesss'),
('style', genre),
('sort', 'song_hotttnesss-desc')
]
method = "search"
_ = "http://developer.echonest.com/api/v4/song/%s?%s" % (method, urllib.urlencode(params))
print _
fp = urllib2.urlopen(_)
data = fp.read()
fp.close()
data = json.loads(data)
r = get_redis()
songs = []
for song in data['response']['songs']:
key = 'cache:data:%s' % (song['id'])
if not r.exists(key):
analysis_url = song['audio_summary']['analysis_url']
try:
fp = urllib2.urlopen(analysis_url)
data = json.loads(fp.read())
fp.close()
except AttributeError, e:
print e, analysis_url
return {}
song['loudness'] = OrderedDict([(item['start'], item['loudness_max'])
for item in data['segments']])
if CACHING:
r.set(key, json.dumps(song))
r.lpush('latest_staches', song['id'])
else:
song = json.loads(r.get(key))
songs.append(song)
return songs
def get_song_ids_for_years(year_start, year_end):
r = get_redis()
key = "cache:years:%s:%s" % (year_start, year_end)
if r.exists(key):
return json.loads(r.get(key))
params = [
('api_key', 'IILIWPF9XK31O9BLS'),
('bucket', 'songs'),
('artist_start_year_before', year_start),
('artist_end_year_after', year_end),
('sort', 'familiarity-desc')
]
_ = "http://developer.echonest.com/api/v4/artist/search?%s" % urllib.urlencode(params)
print _
fp = urllib2.urlopen(_)
data = fp.read()
fp.close()
resp = json.loads(data)
song_ids = []
for artist in resp['response']['artists']:
if artist['songs']:
song_ids.extend(map(lambda x: x['id'], artist['songs'][0:1]))
r.set(key, json.dumps(song_ids))
return song_ids
@app.route('/api/latest')
def api_latest():
songs = get_latest(request.values.get('start', 0), request.values.get('results', 20))
return jsonify({'staches': songs})
def extract(q):
params = [
('api_key', 'IILIWPF9XK31O9BLS'),
('text', q)
]
try:
fp = urllib2.urlopen("http://developer.echonest.com/api/v4/artist/extract?%s" % urllib.urlencode(params))
data = fp.read()
fp.close()
resp = json.loads(data)
artist = resp['response']['artists'][0]['name'].lower()
title = q.lower().replace(artist.lower(), '')
except IndexError:
raise NoArtist(q)
artist = artist.strip()
title = title.strip()
if not title:
return redirect('/artist/%s' % artist)
return artist, title
def search_en(id=None):
params = [
('api_key', 'IILIWPF9XK31O9BLS'),
('bucket', 'audio_summary'),
('bucket', 'song_hotttnesss'),
]
artist = None
title = None
if id or request.values.get('id'):
_ = request.values.get('id', id)
params.extend([('id', _)])
elif request.values.get('q'):
res = extract(request.values.get('q'))
if not isinstance(res, tuple):
return res
artist, title = res
else:
artist = request.values.get('artist')
title = request.values.get('title')
if artist and title:
params.extend([('artist', artist), ('title', title)])
if id or request.values.get('id'):
method = "profile"
else:
method = "search"
_ = "http://developer.echonest.com/api/v4/song/%s?%s" % (method, urllib.urlencode(params))
fp = urllib2.urlopen(_)
data = fp.read()
fp.close()
return json.loads(data)
@app.route('/api/data')
def api_data():
return jsonify({'song': get_song()})
@app.route('/api/artist/<name>/image')
def api_artist_image():
return jsonify({'song': get_song()})
@app.route('/upload-stache', methods=['POST'])
def upload_stache():
import base64
import tempfile
import boto
from boto.s3.key import Key
import re
dataUrlPattern = re.compile('data:image/(png|jpeg);base64,(.*)$')
conn = boto.connect_s3(app.config['AWS_KEY'], app.config['AWS_SECRET'])
song_id = request.values.get('song_id')
imgb64 = dataUrlPattern.match(request.values.get('stache')).group(2)
data = base64.b64decode(imgb64)
fp = tempfile.NamedTemporaryFile()
# fp = open(song_id, 'w')
fp.write(data)
bucket = conn.get_bucket('staches')
headers = {'Content-Type': 'image/png'}
k = Key(bucket)
k.key = "%s.png" % (song_id)
k.set_contents_from_file(fp, headers=headers)
k.set_acl('public-read')
fp.close()
r = get_redis()
key = 'cache:data:%s' % (song_id)
song = json.loads(r.get(key))
song['s3_url'] = "http://staches.s3.amazonaws.com/%s" % k.key
song['stache_version'] = '0.1'
r.set(key, json.dumps(song))
return song['s3_url']