-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.py
executable file
·234 lines (177 loc) · 8.74 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2010-2012 Dan Moore
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import re
import math
import logging
import os
import urllib
from google.appengine.ext import webapp
from google.appengine.api import images
from google.appengine.api import memcache
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.api.urlfetch import fetch
# load up our 1x1 PNG file for compositing when no label is defined or can be loaded
empty_png_file = open('1x1.png', 'rb')
empty_png = empty_png_file.read()
empty_png_file.close()
empty_png_def = (empty_png, 0, 0, 0.0, images.CENTER_CENTER)
class MainHandler(webapp.RequestHandler):
def get(self):
# special pre-defined sizes to use in lieu of number dimensions. taken from dummyimage.com.
sizes = {
# Ad Sizes
'mediumrectangle': '300x250',
'squarepopup': '250x250',
'verticalrectangle': '240x400',
'largerectangle': '336x280',
'rectangle': '180x150',
'popunder': '720x300',
'fullbanner': '468x60',
'halfbanner': '234x60',
'microbar': '88x31',
'button1': '120x90',
'button2': '120x60',
'verticalbanner': '120x240',
'squarebutton': '125x125',
'leaderboard': '728x90',
'wideskyscraper': '60x600',
'skyscraper': '120x600',
'halfpage': '300x600',
# Screen Resolutions
'cga': '320x200',
'qvga': '320x240',
'vga': '640x480',
'wvga': '800x480',
'svga': '800x480',
'wsvga': '1024x600',
'xga': '1024x768',
'wxga': '1280x800',
'wsxga': '1440x900',
'wuxga': '1920x1200',
'wqxga': '2560x1600',
# Video Resolutions
'ntsc': '720x480',
'pal': '768x576',
'hd720': '1280x720',
'hd1080': '1920x1080',
}
size = None
path = re.match(r"/(?P<size>[a-zA-Z0-9x]+)(\.(?P<ext>jpg|jpeg|png))?(,(?P<bgcolor>[0-9a-fA-F]{6}|[0-9a-fA-F]{3}|[0-9a-fA-F]{2}|[0-9a-fA-F]{1}))?$", self.request.path_info)
if path:
path_size = self.request.get('d', path.group('size'))
if path_size in sizes:
path_size = sizes[path_size]
size = re.match(r"(?P<width>\d+)(x(?P<height>\d+))?", path_size)
if size:
## determine extension/file type
ext = self.request.get('t', path.group('ext') or 'png')
if ext == 'png':
mimetype = 'image/png'
encoding = images.PNG
else:
mimetype = 'image/jpeg'
encoding = images.JPEG
## determine colors
bgcolor = self.request.get('b', path.group('bgcolor') or 'aaaaaa')
fgcolor = self.request.get('f', '000000')
# handle shortcut colors
if len(bgcolor) == 1:
bgcolor = bgcolor[0] + bgcolor[0] + bgcolor[0] + bgcolor[0] + bgcolor[0] + bgcolor[0]
elif len(bgcolor) == 2:
bgcolor = bgcolor[0] + bgcolor[1] + bgcolor[0] + bgcolor[1] + bgcolor[0] + bgcolor[1]
elif len(bgcolor) == 3:
bgcolor = bgcolor[0] + bgcolor[0] + bgcolor[1] + bgcolor[1] + bgcolor[2] + bgcolor[2]
bgmatch = re.match(r"^[0-9a-fA-F]{6}$", bgcolor)
if not bgmatch:
bgcolor = 'aaaaaa'
if len(fgcolor) == 1:
fgcolor = fgcolor[0] + fgcolor[0] + fgcolor[0] + fgcolor[0] + fgcolor[0] + fgcolor[0]
elif len(fgcolor) == 2:
fgcolor = fgcolor[0] + fgcolor[1] + fgcolor[0] + fgcolor[1] + fgcolor[0] + fgcolor[1]
elif len(fgcolor) == 3:
fgcolor = fgcolor[0] + fgcolor[0] + fgcolor[1] + fgcolor[1] + fgcolor[2] + fgcolor[2]
fgmatch = re.match(r"^[0-9a-fA-F]{6}$", fgcolor)
if not fgmatch:
fgcolor = 'aaaaaa'
## determine size
width = int(size.group('width'))
# if only one dimension defined, image is square
try:
height = int(size.group('height'))
except TypeError:
height = width
## dimensions
dimensions = str(width) + 'x' + str(height)
## label is what will be shown
default_label = str(width) + u'×' + str(height)
if path.group('size') in sizes:
default_label = path.group('size') + '|(' + default_label + ')'
label = self.request.get('l', default_label)
## determine size of the font used for title needs to vary depending on width
title_font_size = (float(width)/max(len(label),1)) * (1.4 if width > 16 else 1.9)
title_font_size = min(title_font_size, 100)
# make sure it is reasonably sized based on height too
title_font_size = min(title_font_size, height*0.6)
# allow font size to be overridden
try:
title_font_size = int(self.request.get('s'))
except ValueError:
pass
if width <= 4000 and height <= 4000:
# the label images must be 1000 pixels or less in each dimension
label_height = min(height, 1000, int(title_font_size + 8) * (1 + label.count('|')) + 10*label.count('|'))
label_width = min(width, 1000, int(300000/label_height)) # must also be less than 300000 total pixels
url = 'http://chart.apis.google.com/chart?chs=' + str(label_width) + 'x' + str(label_height) + '&cht=p3&chtt=' + urllib.quote_plus(label.encode('utf-8')) + '&chts=' + fgcolor + ',' + str(title_font_size) + '&chf=bg,s,' + bgcolor
# see if we've already generated this image and have it cached in memcache
cache_key = os.environ['CURRENT_VERSION_ID'] + "|" + url + "|" + str(width) + "|" + str(height) + "|" + str(bgcolor) + "|" + str(encoding)
full_img = memcache.get(cache_key)
if full_img is None:
# not found in memcache, so try creating it
try:
label_img = fetch(url=url, deadline=10)
full_img = images.composite([(label_img.content, 0, 0, 1.0, images.CENTER_CENTER)], width, height, int('ff'+bgcolor,16), encoding)
memcache.add(cache_key, full_img)
except images.BadImageError:
# self.response.set_status(400)
# self.response.out.write("Error from Google Chart: '" + label_img.content + "'.")
logging.error("Error from Google Chart: '" + label_img.content + "'.")
full_img = images.composite([empty_png_def], width, height, int('ff'+bgcolor,16), encoding)
# since this is the result of an error, we're returning an image without intended label now but we want client to try again next time
self.response.headers['Cache-Control'] = 'no-cache'
except Exception, ex:
logging.warning(ex)
full_img = images.composite([empty_png_def], width, height, int('ff'+bgcolor,16), encoding)
# since this is the result of an error, we're returning an image without intended label now but we want client to try again next time
self.response.headers['Cache-Control'] = 'no-cache'
self.response.headers['Content-Type'] = mimetype
self.response.out.write(full_img)
## image requested is too damn big
else:
self.response.set_status(400)
self.response.out.write("Dimensions requested (" + dimensions + ") are bigger than supported.")
## URL request isn't recognized
else:
self.response.set_status(400)
writeError(self.response)
def writeError(response):
path = os.path.join(os.path.dirname(__file__), 'error.html')
response.out.write(template.render(path, {}))
application = webapp.WSGIApplication([('.*', MainHandler)],
debug=True)
util.run_wsgi_app(application)