-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
130 lines (103 loc) · 4.58 KB
/
application.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
import flask
import urllib
import sys
import os
import random
from exploji import convert_image_to_emoji
from resources import emoji_character, emoji_color
app = flask.Flask(__name__)
file_name = 'temp_{}.png'
@app.route('/')
def index():
message = u'\U0001f601 \U0001f389'
return flask.render_template("index.html", message=message)
@app.route('/about')
def about():
return flask.render_template("about.html")
@app.route('/error', methods = ['GET'])
def error():
valid_sources = ['/api']
if 'source' in flask.request.args and flask.request.args.get('source') in valid_sources:
return '''<html><head><title>Oops...</title><meta http-equiv="refresh" content=5;url="/api"></head>
<body><h3><span style="color:red">An error occurred</span><br>Redirecting in 5 seconds... <a href="/api">click here to manually redirect</a></h3>
</body></html>'''
return '''<html><head><title>Oops...</title><meta http-equiv="refresh" content=5;url="/"></head>
<body><h3><span style="color:red">An error occurred</span><br>Redirecting in 5 seconds... <a href="/">click here to manually redirect</a></h3>
</body></html>'''
@app.route('/api', methods = ['GET'])
def api(url=None, k=5, width=75, json=False):
global file_name
redirect = False
if 'url' in flask.request.args and not flask.request.args.get('url').strip() == '':
url = flask.request.args.get('url')
if 'k' in flask.request.args and not flask.request.args.get('k').strip() == '':
k = int(flask.request.args.get('k'))
if 'width' in flask.request.args and not flask.request.args.get('width').strip() == '':
width = int(flask.request.args.get('width'))
if 'json' in flask.request.args:
json = True
if url is None:
return '<a href="/">Home</a>'\
'<h3>HTTP GET => /api<br><br>Required params:<br> url=<source image url>'\
'<br><br>Optional params:<br> k=<number of clusters; default=5>'\
'<br> width=<number of emoji characters per row; default=75>'\
'<br> json (returns json response; default is unformatted emoji characters)</h3>'
file_counter = ''.join(random.choice('0123456789ABCDEF') for i in range(16))
file = file_name.format(file_counter)
try:
urllib.urlretrieve(url, file)
output_string, dimension = convert_image_to_emoji(file, emoji_color, emoji_character, k=k, width=width)
if json:
out = []
for row in output_string:
row_dict = {'row':[]}
for character, url in row:
row_dict['row'].append({'character':character, 'image_url':url})
out.append(row_dict)
return flask.jsonify(out)
else:
out = ""
for row in output_string:
for character, url in row:
out += character
out += "<br>"
return out
except:
redirect = True
finally:
try:
os.remove(file)
except:
pass
if redirect:
return flask.redirect('/error?source=/api')
@app.route('/exploji', methods = ['POST'])
def exploji_post(url=None, k=5, width=75):
global file_name
redirect = False
if flask.request.form is not None:
if 'url' in flask.request.form and not flask.request.form['url'].strip() == '':
url = flask.request.form['url']
if 'k' in flask.request.form and not flask.request.form['k'].strip() == '':
k = int(flask.request.form['k'])
if 'width' in flask.request.form and not flask.request.form['width'].strip() == '':
width = int(flask.request.form['width'])
if url is None:
return flask.redirect('/error')
file_counter = ''.join(random.choice('0123456789ABCDEF') for i in range(16))
file = file_name.format(file_counter)
try:
urllib.urlretrieve(url, file)
output_string, dimension = convert_image_to_emoji(file, emoji_color, emoji_character, k=k, width=width)
return flask.render_template("exploji.html", source_image=url, output_string=output_string)
except:
redirect = True
finally:
try:
os.remove(file)
except:
pass
if redirect:
return flask.redirect('/error')
if __name__ == '__main__':
app.run()