forked from NickCarneiro/gifbooth
/
gifbooth.py
114 lines (97 loc) · 3.38 KB
/
gifbooth.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
import base64
import os
from flask import Flask, render_template, request, send_file
from gif_maker import pngs_to_gif
from local import APP_PATH
from utils import return_json, return_json_error
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/gifs')
def get_gifs():
gifs = os.listdir(APP_PATH + 'gifs/')
gif_urls = []
for filename in sorted(gifs, reverse=True):
if '.gif' in filename:
gif_urls.append('/gifs/' + filename)
return return_json({'urls': gif_urls})
@app.route('/winners')
def winners():
filenames = [
'1374210541835.gif',
'1374206136232.gif',
'1374186213781.gif',
'1374185178897.gif',
'1374183688215.gif',
'1374183650872.gif',
'1374183179217.gif',
'1374184430650.gif',
'1374186330306.gif',
'1374194548815.gif',
'1374208461297.gif',
'1374210830057.gif',
'1374212657807.gif',
'1374212817564.gif',
'1374206885481.gif',
'1374205848739.gif',
'1374194458159.gif',
'1374180198784.gif',
'1374247757899.gif',
'1374192031780.gif',
'1374191203721.gif'
]
gif_urls = []
for name in filenames:
gif_urls.append('/gifs/' + name)
return return_json({'urls': gif_urls})
@app.route('/stream')
def get_stream():
return render_template('stream.html')
@app.route('/gifs/<filename>')
def get_gif_file(filename):
gif_path = APP_PATH + 'gifs/' + filename
if os.path.isfile(gif_path):
return send_file(APP_PATH + 'gifs/' + filename)
else:
return return_json_error({'error': 'File not found'}, 400)
@app.route('/random')
def random_page():
return render_template('random.html')
@app.route('/upload', methods=['POST'])
def upload():
try:
#todo: sanitize this
group_timestamp = request.form['timestamp']
base64_image = request.form['image'].replace('data:image/png;base64,', '')
frame_count = int(request.form['frameCount'])
image_data = base64.b64decode(base64_image)
# how long to show each frame in ms
delay_ms = int(request.form['frameDuration'])
# create folder for this set of images with timestamp as folder name
batch_dir = APP_PATH + 'uploads/' + group_timestamp
if not os.path.exists(batch_dir):
os.mkdir(batch_dir)
png_index = request.form['index']
file_name = png_index.zfill(3) + '.png'
f = open(batch_dir + '/' + file_name, 'w')
f.write(image_data)
f.close()
response = {'message': 'Upload successful'}
# if there are frame_count files in the directory, we have gotten all the pngs
# we need to make a gif
if len(os.listdir(batch_dir)) == frame_count:
gif_destination_file = APP_PATH + 'gifs/' + group_timestamp + '.gif'
delay_hundredths = delay_ms / 10
gif_result = pngs_to_gif(batch_dir, gif_destination_file, delay_hundredths)
if gif_result:
#upload image to indgur
gif_url = '/gifs/' + group_timestamp + '.gif'
response['gif_url'] = gif_url
return return_json(response)
except Exception as e:
print e
return return_json({'error': 'Upload failed'})
if __name__ == "__main__":
app.debug = True
app.run(host='0.0.0.0')