-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslothr.py
49 lines (42 loc) · 1.49 KB
/
slothr.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
import datetime
import mimetypes
from random import randrange
from flask import Flask, abort, send_file
from models import FlickrImage, CroppedFlickrImage, Session
s = Session()
app = Flask(__name__)
app.config.from_object('settings')
@app.route('/<int:width>/<int:height>/')
def get_image(width, height):
s = Session()
ratio = float(width) / height
imgs = []
for img in s.query(FlickrImage).all():
imgs.append({
'ratio_diff': abs((float(img.width) / img.height) - ratio),
'img': img
})
imgs = sorted(imgs, key=lambda d: d['ratio_diff'])
# Choose a random image to spice things up a bit ;)
img = imgs[randrange(4)]['img']
cropped = img.get_cropped(width, height, s)
response = send_file(cropped.path, mimetypes.guess_type(cropped.path)[0])
s.close()
response.headers.add('Last-Modified', datetime.datetime.now())
response.headers.add('Cache-Control',
'no-store, no-cache, must-revalidate, post-check=0, pre-check=0')
response.headers.add('Pragma', 'no-cache')
return response
@app.route('/<int:img_id>/<int:width>/<int:height>/')
def get_by_id(img_id, width, height):
s = Session()
img = s.query(FlickrImage).filter(FlickrImage.id==img_id).first()
if not img:
abort(404)
cropped = img.get_cropped(width, height, s)
response = send_file(cropped.path, mimetypes.guess_type(cropped.path)[0])
s.close()
return response
if __name__ == '__main__':
app.debug = True
app.run()