-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorpicker.py
259 lines (194 loc) · 8.35 KB
/
colorpicker.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
from collections import namedtuple
from math import sqrt
import random
try:
import Image
except ImportError:
from PIL import Image
import json
import colorsys
import colorific
import urllib2
import urllib, cStringIO
import re
import translitcodec
def get_gimages(query):
answer = []
for i in range(0,8,4):
url = 'https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=%s&start=%d&imgsz=large&imgtype=photo' % (query,i)
r = urllib2.urlopen(url)
for k in json.loads(r.read())['responseData']['results']:
answer.append(k['tbUrl'])
return answer
def chunks(l, n):
""" Yield successive n-sized chunks from l.
"""
for i in xrange(0, len(l), n):
yield l[i:i+n]
## Slug functions ##
_punct_re = re.compile(r'[\t !"#$%&\()*\-/<=>?@\[\\\]^_`{|},.]+')
def slugify(text, delim=u'-'):
"""Generates an ASCII-only slug."""
result = []
for word in _punct_re.split(text.lower()):
word = word.replace('\'', '') #Don't replace apostrophes with a dash, just strip them
word = word.encode('translit/long')
if word:
result.append(word)
return unicode(delim.join(result))
def gettitle(recipe):
return recipe['title']
from flask.ext.wtf import Form
from wtforms.fields import TextField
from wtforms.validators import Required
from wtforms.fields import StringField
from wtforms.fields import BooleanField
from wtforms.widgets import TextArea
class RecipeForm(Form):
title = TextField('title', validators = [Required()])
ingredients = StringField(u'Ingredients', widget=TextArea(),validators = [Required()])
directions = StringField(u'directions', widget=TextArea(), validators = [Required()])
class DeleteForm(Form):
deleterecipe = BooleanField('deleterecipe')
from flask import *
app = Flask(__name__)
app.secret_key = 'correcthorsebatterystaples'
# rethink imports
import rethinkdb as r
from rethinkdb.errors import RqlRuntimeError, RqlDriverError
# rethink config
RDB_HOST = 'localhost'
RDB_PORT = 28015
TODO_DB = 'recipes'
# db setup; only run once
def dbSetup():
connection = r.connect(host=RDB_HOST, port=RDB_PORT)
try:
r.db_create(TODO_DB).run(connection)
r.db(TODO_DB).table_create('recipes').run(connection)
print 'Database setup completed'
except RqlRuntimeError:
print 'Database already exists.'
finally:
connection.close()
dbSetup()
# open connection before each request
@app.before_request
def before_request():
try:
g.rdb_conn = r.connect(host=RDB_HOST, port=RDB_PORT, db=TODO_DB)
except RqlDriverError:
abort(503, "Database connection could be established.")
# close the connection after each request
@app.teardown_request
def teardown_request(exception):
try:
g.rdb_conn.close()
except AttributeError:
pass
@app.route('/')
def index():
allrecipes = list(r.table('recipes').run(g.rdb_conn))
allrecipes.sort(key=gettitle)
return render_template("index.html", allrecipes=allrecipes)
@app.route('/favicon.ico')
def favicon():
abort(404)
@app.route('/add', methods = ['GET', 'POST'])
def add():
form = RecipeForm()
#form.ingredients.data = "1c ingredient\nenter ingredients here "
if request.method == 'POST' and form.validate():
lightness = []
rainbow = []
resp=[]
urls = get_gimages("+".join(form.title.data.split()))
for url in urls:
i = cStringIO.StringIO(urllib.urlopen(url).read())
quiche = colorific.extract_colors(i, max_colors=5)
resp.extend([each.value for each in quiche.colors])
resp = [(m,sqrt(0.299 * m[0]**2 + 0.587 * m[1]**2 + 0.114 * m[2]**2)) for m in resp]
lightness = sorted(resp,key=lambda x: x[1])
lightness = [i[0] for i in lightness]
lightness.sort(key=lambda tup: colorsys.rgb_to_hsv(tup[0],tup[1],tup[2])[2])
for each in chunks(lightness,10):
avg = tuple(map(lambda y: sum(y) / len(y), zip(*each)))
rainbow.append(avg)
slug = slugify(form.title.data)
recipe = { 'title': form.title.data.title(), 'ingredients': [{'amount': " ".join(ingredient.split()[0:2]), 'what': " ".join(ingredient.split()[2:])} for ingredient in form.ingredients.data.split('\r\n')], 'directions': form.directions.data.split('\r\n'), 'urls': urls, 'slug': slug, 'avgcolors': [list(i) for i in rainbow]}
recipe['ingredients'] = [i for i in recipe['ingredients'] if not i['what']=='']
recipe['directions'] = [i for i in recipe['directions'] if not i=='']
r.table('recipes').insert(recipe).run(g.rdb_conn)
return redirect(url_for('recipe', query=slug))
#r.table('recipes').get('de670bed-791d-41d0-97cb-1ceaf9ba54ac').update({'time_updated': r.now(), 'slug': "broccoli-cheese-soup", 'urls': urls, 'avgcolors': [list(i) for i in rainbow]}).run(g.rdb_conn)
return render_template("add.html", form=form)
@app.route('/<query>')
def recipe(query):
recipe = list(r.table('recipes').filter({'slug': query}).run(g.rdb_conn))
if recipe:
recipe = recipe[0]
else:
abort(404)
steps = []
for i in recipe['directions']:
for j in [k['what'].split(",")[0] for k in recipe['ingredients']]:
i = i.replace(j, ("<kbd>" + j + "</kbd>"))
steps.append(Markup(i))
rainbow = [tuple(l) for l in recipe['avgcolors']]
return render_template("recipes.html", urls = recipe['urls'], avg=rainbow, ingredients = recipe['ingredients'], steps = steps, title=recipe['title'], query=query)
@app.route('/<query>/edit', methods = ['GET', 'POST'])
def edit(query):
form = RecipeForm()
recipe = list(r.table('recipes').filter({'slug': query}).run(g.rdb_conn))
if recipe:
recipe = recipe[0]
else:
abort(404)
if request.method == 'GET':
form.ingredients.data = "\r\n".join([(i['amount'] + " " + i['what']) for i in recipe['ingredients']])
form.directions.data = "\r\n".join(i for i in recipe['directions'])
form.title.data = recipe['title']
return render_template("edit.html", form=form, recipe=recipe)
if request.method == 'POST' and form.validate():
lightness = []
rainbow = []
resp=[]
urls = get_gimages("+".join(form.title.data.split()))
for url in urls:
i = cStringIO.StringIO(urllib.urlopen(url).read())
quiche = colorific.extract_colors(i, max_colors=5)
resp.extend([each.value for each in quiche.colors])
resp = [(m,sqrt(0.299 * m[0]**2 + 0.587 * m[1]**2 + 0.114 * m[2]**2)) for m in resp]
lightness = sorted(resp,key=lambda x: x[1])
lightness = [i[0] for i in lightness]
lightness.sort(key=lambda tup: colorsys.rgb_to_hsv(tup[0],tup[1],tup[2])[2])
for each in chunks(lightness,10):
avg = tuple(map(lambda y: sum(y) / len(y), zip(*each)))
rainbow.append(avg)
slug = slugify(form.title.data)
id = recipe['id']
recipe = { 'title': form.title.data.title(), 'ingredients': [{'amount': " ".join(ingredient.split()[0:2]), 'what': " ".join(ingredient.split()[2:])} for ingredient in form.ingredients.data.split('\r\n')], 'directions': form.directions.data.split('\r\n'), 'urls': urls, 'slug': slug, 'avgcolors': [list(i) for i in rainbow]}
recipe['ingredients'] = [i for i in recipe['ingredients'] if not i['what']=='']
recipe['directions'] = [i for i in recipe['directions'] if not i=='']
r.table('recipes').get(id).update(recipe).run(g.rdb_conn)
return redirect(url_for('recipe', query=slug))
@app.route('/<query>/delete', methods = ['GET', 'POST'])
def delete(query):
form = DeleteForm()
recipe = list(r.table('recipes').filter({'slug': query}).run(g.rdb_conn))
if recipe:
recipe = recipe[0]
else:
abort(404)
if request.method == 'GET':
return render_template("delete.html", query=query, recipe=recipe, form=form)
if request.method == 'POST':
if 'deleterecipe' in request.form:
id = recipe['id']
r.table('recipes').get(id).delete().run(g.rdb_conn)
return redirect(url_for('index'))
else:
flash("Are you sure? Check the box")
return render_template("delete.html", query=query, recipe=recipe, form=form)
if __name__ == '__main__':
app.run(debug = True, host='0.0.0.0')