-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpsychedelizer.py
348 lines (243 loc) · 9.68 KB
/
psychedelizer.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
# -*- coding: utf-8 -*-
#!/usr/bin/env python
#
# psychedelizer.0x80.ru
# psychedelizer.py (c) Mikhail Mezyakov <[email protected]>
#
import os
import time
import uuid
import urllib
import json
import shutil
from datetime import datetime
import motor
from tornado import web, websocket, ioloop, gen
from tornado.options import define, options
from pynbome import pynbome, image
import utils
import database
from config import SETTINGS
define("port", default=8000, help="run on the given port", type=int)
class UploadHandler(web.RequestHandler):
"""Upload image file from computer or URL"""
def post(self):
# Upload from computer
if self.request.files:
fileinfo = self.request.files['file'][0]
fname = fileinfo['filename']
ext = os.path.splitext(fname)[1] # file extension
cname = str(uuid.uuid4()) + ext
image_file = fileinfo['body']
# Upload from URL
else:
# self.get_argument() doesn't work here,
# because Angular returns json.
# So try to manually parse request body
body = json.loads(self.request.body)
if 'url' in body:
# Retrieve image file from specified urllib
image_file = urllib.urlopen(body['url']).read()
ext = urllib.unquote(body['url']).decode('utf-8').split('.')[-1]
cname = str(uuid.uuid4()) + '.' + ext
else:
data = {'error': 'No URL'}
self.finish(data)
full_image_path = os.path.join(SETTINGS['upload_tmp'], cname)
with open(full_image_path, 'w') as f:
f.write(image_file)
# Create pynbome image object
img = image.Image(filename=full_image_path)
width, height = img.size()
if width > 600:
img.resize(width=600)
elif height > 600:
img.resize(height=600)
preview_cname = str(uuid.uuid4()) + '.jpg'
img.save(os.path.join(SETTINGS['upload_tmp'], preview_cname))
shutil.copy2(
os.path.join(SETTINGS['upload_tmp'], preview_cname),
os.path.join(SETTINGS['upload_tmp'], cname)
)
data = {'original': cname, 'preview': preview_cname}
self.finish(data)
class PreviewHandler(web.RequestHandler):
"""Apply patterns and filters and preview edited image"""
def post(self):
body = json.loads(self.request.body)
original_image_path = os.path.join(SETTINGS['upload_tmp'], body['original'])
preview_image_path = os.path.join(SETTINGS['upload_tmp'], body['preview'])
os.remove(preview_image_path)
img = image.Image(filename=original_image_path)
# Apply patterns if necessary
if body['filters'] and body['combine']:
img.combine()
# Apply selected filters
for filter_name in body['filters']:
img.psychedelic(filter_name)
# Save image preview
preview_cname = str(uuid.uuid4()) + '.jpg'
img.save(os.path.join(SETTINGS['upload_tmp'], preview_cname))
data = {'original': body['original'], 'preview': preview_cname}
self.finish(data)
class SaveHandler(web.RequestHandler):
"""Save image when editing was done"""
def post(self):
body = json.loads(self.request.body)
ext = body['image'].split('.')[-1]
tmp_image_path = os.path.join(SETTINGS['upload_tmp'], body['image'])
image_name = '{0}.{1}'.format(time.time(), ext)
image_path = os.path.join(SETTINGS['saved_files'], image_name)
small_image_path = os.path.join(
SETTINGS['saved_files'],
's' + image_name
)
os.rename(tmp_image_path, image_path)
img = image.Image(filename=image_path)
width, height = img.size()
# Create cropped and resized thumbnail
img.image.crop(width/2-120, height/2-120, width=width/3, height=width/3)
img.resize(120)
img.save(small_image_path)
new_image = {
'src': image_name,
'date': utils.from_unix(image_name[:-4]),
'unixtime': float(image_name[:-4]),
'ip': utils.get_ip(self.request),
'likes': database.images['likes']
}
# Inserting item into db
self.insert(new_image.copy())
data = {'new_image': new_image}
# Send new image data to websocket clients
UpdatesHandler.send_updates(data)
self.finish(data)
def insert(self, item):
db = SETTINGS['db']
item.update(likes=database.images['likes'])
db.images.insert(item, callback=database.insert_callback)
class GetLatestHandler(web.RequestHandler):
"""Get latest images, sort if necessary"""
# Sort constants
SORT_BY_DATE = 'new'
SORT_BY_LIKES = 'best'
SORT_CRITERIAS = (SORT_BY_DATE, SORT_BY_LIKES)
@web.asynchronous
def get(self):
"""
GET request without params means that we need images
sorted by date
"""
self.sort_by = self.SORT_BY_DATE
db = SETTINGS['db']
fields = {'_id': False}
# Select image items
cursor = db.images.find(fields=fields).sort([('_id', -1)]).limit(36)
cursor.to_list(callback=self.finish_callback)
@web.asynchronous
def post(self):
"""POST request's body must contain sort_by parameter"""
body = json.loads(self.request.body)
self.sort_by = body['sort_by']
db = SETTINGS['db']
fields = {'_id': False}
if self.sort_by == self.SORT_BY_LIKES:
# Sort by likes count
sort_command = [('likes.count', -1), ('_id', -1)]
cursor = db.images.find(fields=fields).sort(sort_command).limit(36)
cursor.to_list(callback=self.finish_callback)
def finish_callback(self, result, error):
"""Finish data retrieving"""
if error:
raise error
elif result:
data = {
'images': result,
'client_ip': utils.get_ip(self.request),
'sort_by': self.sort_by,
'sort_criterias': self.SORT_CRITERIAS
}
self.finish(data)
else:
# No data, trying to import existing files into database
utils.import_files_to_mongo()
class GetFiltersHandler(web.RequestHandler):
"""Return list of pynbome filters"""
def get(self):
data = {'filters': pynbome.list_filters()}
self.finish(data)
class LikeHandler(web.RequestHandler):
"""Like/unlike image handler"""
@web.asynchronous
@gen.engine # we'll use inline callbacks
def post(self):
db = SETTINGS['db']
body = json.loads(self.request.body)
item = body['image']
ip = utils.get_ip(self.request)
# Get item
db_item = yield motor.Op(db.images.find_one, {'unixtime': item['unixtime']})
_id = db_item['_id']
# Save like/unlike on item
if ip in db_item['likes']['data']:
db_item['likes']['data'].remove(ip)
db_item['likes']['count'] -= 1
else:
db_item['likes']['data'].append(ip)
db_item['likes']['count'] += 1
# Update item
update_command = {'$set': {'likes': db_item['likes']}}
yield motor.Op(db.images.update, {'_id': _id}, update_command)
data = {'likes': db_item['likes']}
self.finish(data)
class ImageHandler(web.RequestHandler):
"""Fetch one image info"""
@web.asynchronous
@gen.engine
def get(self, unixtime):
db = SETTINGS['db']
db_item = yield motor.Op(db.images.find_one, {'unixtime': float(unixtime)})
data = {
'src': db_item['src'],
'likes': db_item['likes'],
'date': db_item['date'],
'unixtime': db_item['unixtime']
}
self.finish(data)
class UpdatesHandler(websocket.WebSocketHandler):
"""Handler for send updates through websocket"""
socket_clients = set()
def open(self):
self.socket_clients.add(self)
def on_close(self):
self.socket_clients.remove(self)
@classmethod
def send_updates(cls, data):
for client in cls.socket_clients:
client.write_message(data)
class MainHandler(web.RequestHandler):
"""Angular app's main page"""
def get(self):
# We don't use render() because angular templates
# and tornado templates are not friends
with open('public/index.html') as f:
self.write(f.read())
application = web.Application([
(r'/api/upload', UploadHandler),
(r'/api/preview', PreviewHandler),
(r'/api/save', SaveHandler),
(r'/api/get_latest', GetLatestHandler),
(r'/api/get_filters', GetFiltersHandler),
(r'/api/like', LikeHandler),
(r'/api/image/(\d+\.\d+)', ImageHandler),
(r'/updates', UpdatesHandler),
(r'/', MainHandler),
(r'/image/\d+.\d+', MainHandler),
(r'/(.*)', web.StaticFileHandler, {"path": "public"})
],
**SETTINGS
)
if __name__ == "__main__":
options.parse_command_line()
application.listen(options.port)
ioloop.IOLoop.instance().start()