-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoverfm.py
521 lines (404 loc) · 16.6 KB
/
coverfm.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#!usr/bin/env python
# Author: Kirill Lashuk
from __future__ import division
import os
import logging
import datetime
import StringIO
from StringIO import StringIO
from PIL import Image
from libs import pylast
from google.appengine.api import memcache
from google.appengine.api import urlfetch
from google.appengine.api import users
from google.appengine.api.labs import taskqueue
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api.urlfetch import DownloadError
import config
# Application models
class Permission(db.Model):
email = db.StringProperty()
def id(self):
return self.key().id()
@classmethod
def authorized(cls):
'''Return True if user is allowed to use the application.'''
return users.is_current_user_admin() or cls.has_permission(users.get_current_user())
@classmethod
def has_permission(cls, user):
return user is not None and Permission.all().filter('email =', user.email()).count() > 0
class TopArt(db.Model):
'''Store user's topart information:
nick - user's nick on last.fm;
owner - user's UserProperty.
'''
nick = db.StringProperty()
owner = db.UserProperty()
period = db.StringProperty()
width = db.IntegerProperty()
height = db.IntegerProperty()
image = db.BlobProperty()
auto_upd = db.BooleanProperty(default=False)
wait_for_upd = db.BooleanProperty(default=False)
creation_date = db.DateTimeProperty(auto_now_add=True)
last_upd_date = db.DateTimeProperty(auto_now_add=True)
def url(self):
'''Return url for this TopArt.'''
return get_topart_url(self.nick, self.period,
self.width, self.height)
def id(self):
'''Return TopArt ID.'''
return self.key().id()
def __str__(self):
return 'nick=%s, period=%s, size=%dx%d' % (self.nick,
self.period, self.width, self.height)
def get_topart_url(nick, period, w, h):
'''Generate url for TopArt with specific parameters.'''
return '/topart/%s/%s/%dx%d' % (nick, period, w, h)
##################################
## Application request handlers ##
##################################
class BaseRequestHandler(webapp.RequestHandler):
'''Base request handler class. Add basic information about the user to the request.'''
def generate(self, template_name, template_values=None):
'''Generate html from template. Add some parameters to the teplate_values list:
user_name - current user name;
user_is_admin - True if current user is admin of the application;
sign - sign label text;
sign_url - sign in/out url;
host - application host name.'''
sign_url, sign_label, user_name, user_is_admin, user_is_auth = get_user_info()
host = self.request.host_url
values = {'user_name': user_name,
'user_is_admin': user_is_admin,
'user_is_auth': user_is_auth,
'sign': sign_label,
'sign_url': sign_url,
'host': host}
if template_values:
values.update(template_values)
root_dir = os.path.dirname(__file__)
path = os.path.join(root_dir, 'templates', template_name)
html = template.render(path, values, debug=config.DEBUG)
self.response.out.write(html)
@staticmethod
def authorized_only(method):
'''Decorate method in a such way that it will be processed only for authorized users.'''
def wrapped(self, *args, **kwargs):
if not Permission.authorized():
user = users.get_current_user()
if user:
logging.info('Unauthorized access from %s' % (user.email()))
return self.redirect('/faq')
else:
method(self, *args, **kwargs)
return wrapped
@staticmethod
def admin_only(method):
'''Decorate method in a such way that it will be processed only for admin users.'''
def wrapped(self, *args, **kwargs):
if not users.is_current_user_admin():
return self.redirect('/')
else:
method(self, *args, **kwargs)
return wrapped
class MainPage(BaseRequestHandler):
'''MainPage request.'''
@BaseRequestHandler.authorized_only
def get(self):
self.generate('index.html')
@BaseRequestHandler.authorized_only
def post(self):
if 'generate' not in self.request.POST:
return self.redirect('/')
nick = self.request.get('nick')
period = self.request.get('period')
w = int(self.request.get('width'))
h = int(self.request.get('height'))
if self.request.get('autoupd') == 'on':
auto_upd = True
else:
auto_upd = False
# try to get topart from cache or db
topart = get_topart(nick, period, w, h, False)
# generate requested topart if there is no one already
if not topart:
img, error = generate_topart(nick, period, w, h)
if error:
return self.generate('index.html', {'error': error})
topart = TopArt(nick=nick, period=period, width=w, height=h)
topart.owner = users.get_current_user()
topart.image = img
topart.auto_upd = auto_upd
topart.put()
memcache.set(topart.url(), topart, config.EXPIRATION_TIME)
self.redirect(topart.url())
class Permissions(BaseRequestHandler):
'''MainPage request.'''
@BaseRequestHandler.admin_only
def get(self):
permissions = Permission.all().fetch(10)
self.generate('permissions.html', { 'permissions': permissions })
@BaseRequestHandler.admin_only
def post(self):
email = self.request.get('email')
if email and not Permission.all().filter('email =', email).count():
Permission(email=email).put()
self.redirect('/permissions')
class DeletePermission(BaseRequestHandler):
@BaseRequestHandler.admin_only
def get(self, id):
permission = Permission.get_by_id(int(id))
if not permission:
logging.error('''DELETE ERROR: Failed to delete id=%d -
missing permission''' % id)
logging.info('DELETED: %s permission' % permission.email)
permission.delete()
self.redirect('/permissions')
class FAQ(BaseRequestHandler):
def get(self):
self.generate('faq.html')
class TopArtImage(BaseRequestHandler):
def get(self, nick, period, width, height):
width = int(width)
height = int(height)
topart = get_topart(nick, period, width, height)
if topart:
self.response.headers['Content-Type'] = 'image/png'
self.response.out.write(topart.image)
class TopArtPage(BaseRequestHandler):
@BaseRequestHandler.authorized_only
def get(self, nick, period, width, height):
width = int(width)
height = int(height)
topart = get_topart(nick, period, width, height)
if topart:
self.generate('topart_page.html', {'topart': topart, 'nick': topart.nick})
else:
self.redirect('/toparts')
class ManageTopArts(BaseRequestHandler):
'''TopArts managing page request handler.'''
@BaseRequestHandler.authorized_only
def get(self):
toparts = TopArt.all()
if users.is_current_user_admin():
toparts = toparts.order('last_upd_date')
toparts = toparts.fetch(20)
else:
toparts = toparts.filter('owner =', users.get_current_user())
toparts = toparts.order('-last_upd_date')
toparts = toparts.fetch(10)
self.generate('toparts.html', { 'toparts': toparts })
class UpdateAllTopArts(BaseRequestHandler):
'''Add no more than config.UPDATE_LIMIT toparts to update task queue. Choose
toparts, that arn't waiting for update and haven't been updated for the longest time.'''
def get(self):
logging.info('UPDATE all')
self.fill_update_queue()
if not self.request.headers.get('X-AppEngine-Cron'):
self.redirect('/toparts')
def fill_update_queue(self):
toparts = TopArt.all()
toparts = toparts.filter('auto_upd =', True)
toparts = toparts.filter('wait_for_upd =', False)
toparts = toparts.order('last_upd_date')
toparts = toparts.fetch(1000)
#logging.info('UPDATE fill taskqueue (size=%d)' % toparts.count())
tasks = [taskqueue.Task(url='/ad/update/%d' % topart.id()) for topart in toparts]
set_wait_for_upd(toparts, True)
for task in tasks:
task.add('update')
class ResetAllWaitingUpdates(BaseRequestHandler):
'''Reset all toparts that are waiting for update, so they won't be skiped while updating.'''
def get(self):
toparts = TopArt.all().filter('wait_for_upd =', True)
set_wait_for_upd(toparts, False)
logging.info('RESET all')
return self.redirect('/toparts')
class UpdateTopArtRequestHandler(BaseRequestHandler):
def update_topart(self, topart):
img, error = generate_topart(topart.nick, topart.period,
topart.width, topart.height)
if not error:
topart.image = img
topart.last_upd_date = datetime.datetime.now()
#logging.info('memcache.delete in UpdateTopArts')
memcache.delete(topart.url())
logging.info('UPDATED %s' % topart)
return True
else:
logging.error('''UPDATE ERROR: %s\n Failed to update
%s - generating error''' % (error, topart.id()))
return False
class UpdateTopArt(UpdateTopArtRequestHandler):
@BaseRequestHandler.authorized_only
def get(self, id):
topart = TopArt.get_by_id(int(id))
if not topart:
return self.redirect('/toparts')
has_access = users.is_current_user_admin() or users.get_current_user() == topart.owner
if has_access and self.update_topart(topart):
topart.put()
return self.redirect(topart.url())
else:
return self.redirect('/toparts')
class UpdateTopArtTask(UpdateTopArtRequestHandler):
def post(self, id):
#logging.info(self.request.headers)
if self.request.headers.get('X-AppEngine-TaskName'):
topart = TopArt.get_by_id(int(id))
if not topart:
logging.error('''UPDATE ERROR: Failed to update id=%d -
missing previous topart''' % id)
if topart.wait_for_upd:
self.update_topart(topart)
topart.wait_for_upd = False
topart.put()
else:
return self.redirect('/')
class DeleteTopArt(BaseRequestHandler):
@BaseRequestHandler.authorized_only
def get(self, id):
topart = TopArt.get_by_id(int(id))
if not topart:
logging.error('''DELETE ERROR: Failed to delete id=%d -
missing topart''' % id)
return self.redirect('/')
if not (users.is_current_user_admin() or users.get_current_user() == topart.owner):
return self.redirect('/')
logging.info('DELETED: %s' % topart)
topart.delete()
self.redirect('/toparts')
# Useful functions
def set_wait_for_upd(toparts, state):
storage = []
for topart in toparts:
topart.wait_for_upd = state
storage.append(topart)
if len(storage) == config.BATCH_PUT_LIMIT:
db.put(storage)
storage = []
if storage:
db.put(storage)
def get_topart(nick, period, w, h, use_cache=True):
key = get_topart_url(nick, period, w, h)
topart = memcache.get(key) if use_cache else None
if not topart:
toparts = TopArt.all()
toparts.filter('nick =', nick)
toparts.filter('period =', period)
toparts.filter('width =', w)
toparts.filter('height =', h)
toparts = toparts.fetch(1)
topart = toparts[0] if toparts else None
if topart:
#logging.info('memcache.set in get_topart')
memcache.set(topart.url(), topart, config.EXPIRATION_TIME)
#logging.info('new request for key=%s' % topart.key())
return topart
def cover_filter(link):
# return link is not None and 'default_album' not in link
# if 'last' not in link: logging.info(link)
return link is not None and 'default_album' not in link and 'last' in link
ERROR_RESERVE_SIZE = 10
def get_arts_images(nick, period=pylast.PERIOD_OVERALL, num=5,
size=config.COVER_SIZE):
net = pylast.get_lastfm_network(api_key=config.LASTFM_API_KEY)
images = []
error = ''
try:
arts_data = net.get_user(nick).get_top_albums_with_arts(period, size)
arts_urls = filter(cover_filter, (data.image for data in arts_data))
for art_url in arts_urls:
if len(images) == num: break
image = get_art_image(art_url)
if image: images.append(image)
except pylast.WSError, e:
logging.error('Failed to fetch images: %s (user - %s)' % (e, nick))
logging.exception(e)
error = 'Failed to fetch artworks images.'
return images, error
def get_art_image(url):
try:
return Image.open(StringIO(urlfetch.Fetch(url).content))
except (IOError, DownloadError), e:
logging.error('Failed to fetch image: %s (url - "%s")' % (e, url))
logging.exception(e)
def generate_topart(nick, period, width, height):
size = config.ABOUT_ME_WIDTH // width
req_size = opt_size(size)
error = ''
topart = None
images, error = get_arts_images(nick, period, width * height, req_size)
if images and not error:
if len(images) < width * height:
if len(images) >= width:
height = len(images) // width
images = images[:width * height]
else:
height = 1
width = len(images)
canvas = Image.new('RGBA', (size * width, size * height))
for index, image in enumerate(images):
append_image(canvas, image, index, width, size)
output = StringIO()
canvas.save(output, format="PNG")
topart = output.getvalue()
output.close()
else:
error = 'Topart generation failed'
return topart, error
def append_image(canvas, image, index, width, size):
left = (index % width) * size
top = (index // width) * size
image.thumbnail((size, 'auto'))
canvas.paste(image.crop((0, 0, size, size)), (left, top))
def opt_size(size):
'''Return optimal pylast size constant based on required artwork size.
Sizes are:
COVER_SMALL = 0 - 34x34 px
COVER_MEDIUM = 1 - 64x64 px
COVER_LARGE = 2 - 126x126 px
COVER_EXTRA_LARGE = 3 - 300x300 px'''
sizes = [34, 64, 126, 300]
#sizes = [34, 64, 174, 300]
for i, s in enumerate(sizes):
if s >= size:
return i
return 3
def get_user_info():
'''Gather user information and generate sign in/out url.'''
user = users.get_current_user()
if user:
sign_url = users.create_logout_url('/')
sign_label = 'Sign out'
user_name = users.get_current_user().email()
is_admin = users.is_current_user_admin()
is_auth = Permission.authorized()
else:
sign_url = users.create_login_url('/')
sign_label = 'Sign in'
user_name = ''
is_admin = False
is_auth = False
return sign_url, sign_label, user_name, is_admin, is_auth
# Application instance
application = webapp.WSGIApplication(
[
('/', MainPage),
('/faq', FAQ),
('/update/(\d+)', UpdateTopArt),
('/ad/update/(\d+)', UpdateTopArtTask),
('/ad/update/all', UpdateAllTopArts),
('/ad/reset/all', ResetAllWaitingUpdates),
('/delete/(\d+)', DeleteTopArt),
('/toparts', ManageTopArts),
('/topart/(.*)/(.*)/(\d+)x(\d+).png', TopArtImage),
('/topart/(.*)/(.*)/(\d+)x(\d+)', TopArtPage),
('/permissions', Permissions),
('/permission/delete/(\d+)', DeletePermission)
],
debug=config.DEBUG)