1 |
from flask import Flask |
2 |
from flask import request |
3 |
from flask import render_template |
4 |
import lxml.html |
5 |
import pymongo |
6 |
from bson import Code |
7 |
import urllib2 |
8 |
import StringIO |
9 |
from flask import g |
10 |
from flask import redirect |
11 |
from urllib import quote_plus |
12 |
from urllib import unquote_plus |
13 |
import conf |
14 |
import oursql |
15 |
from flask import jsonify |
16 |
app = Flask(__name__) |
17 |
@app.before_request |
18 |
def first(): |
19 |
#return "<h1>Hello</h1>"; |
20 |
#g.connection = pymongo.Connection('localhost',27017) #Create the object once and use it. |
21 |
#g.db = g.connection['dev_alipi'] |
22 |
pass |
23 |
@app.teardown_request |
24 |
def close(exception): |
25 |
#g.connection.disconnect() |
26 |
pass |
27 |
@app.route('/') |
28 |
def start_page() : |
29 |
d = {} |
30 |
d['foruri'] = request.args['foruri'] |
31 |
myhandler1 = urllib2.Request(d['foruri'],headers={'User-Agent':"Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11"}) #A fix to send user-agents, so that sites render properly. |
32 |
try: |
33 |
a = urllib2.urlopen(myhandler1) |
34 |
if a.geturl() != d['foruri']: |
35 |
return "There was a server redirect, please click on the <a href='http://dev.a11y.in/web?foruri={0}'>link</a> to continue.".format(quote_plus(a.geturl())) |
36 |
else: |
37 |
page = a.read() |
38 |
a.close() |
39 |
except ValueError: |
40 |
return "The link is malformed, click <a href='http://dev.a11y.in/web?foruri={0}&lang={1}&interactive=1'>here</a> to be redirected.".format(quote_plus(unquote_plus(d['foruri'])),request.args['lang']) |
41 |
except urllib2.URLError: |
42 |
return render_template('error.html') |
43 |
try: |
44 |
page = unicode(page,'utf-8') #Hack to fix improperly displayed chars on wikipedia. |
45 |
except UnicodeDecodeError: |
46 |
pass #Some pages may not need be utf-8'ed |
47 |
root = lxml.html.parse(StringIO.StringIO(page)).getroot() |
48 |
if request.args.has_key('lang') == False and request.args.has_key('blog') == False: |
49 |
root.make_links_absolute(d['foruri'], resolve_base_href = True) |
50 |
for i in root.iterlinks(): |
51 |
if i[1] == 'href' and i[0].tag != 'link': |
52 |
i[0].attrib['href'] = 'http://{0}?foruri={1}'.format(conf.DEPLOYURL[0],quote_plus(i[0].attrib['href'])) |
53 |
script_test = root.makeelement('script') |
54 |
script_edit = root.makeelement('script') |
55 |
root.body.append(script_test) |
56 |
root.body.append(script_edit) |
57 |
script_test.set("src", conf.APPURL[0] + "/server/ui.js") |
58 |
script_test.set("type", "text/javascript") |
59 |
script_edit.set("src", conf.APPURL[0] + "/server/wsgi/pageEditor.js") |
60 |
script_edit.set("type","text/javascript") |
61 |
|
62 |
script_jq_mini = root.makeelement('script') |
63 |
root.body.append(script_jq_mini) |
64 |
script_jq_mini.set("src", conf.JQUERYURL[0] + "/jquery-1.7.min.js") |
65 |
script_jq_mini.set("type", "text/javascript") |
66 |
|
67 |
style = root.makeelement('link') |
68 |
root.body.append(style) |
69 |
style.set("rel","stylesheet") |
70 |
style.set("type", "text/css") |
71 |
style.set("href", conf.APPURL[0] + "/server/stylesheet.css") |
72 |
|
73 |
jit_script = root.makeelement('script') |
74 |
root.body.append(jit_script) |
75 |
jit_script.set("src", conf.APPURL[0] + "/server/jit.js") |
76 |
jit_script.set("type", "text/javascript") |
77 |
|
78 |
tree_script = root.makeelement('script') |
79 |
root.body.append(tree_script) |
80 |
tree_script.set("src", conf.APPURL[0] + "/server/tree.js") |
81 |
tree_script.set("type", "text/javascript") |
82 |
|
83 |
script_jq_cust = root.makeelement('script') |
84 |
root.body.append(script_jq_cust) |
85 |
script_jq_cust.set("src", conf.JQUERYUI[0] + "/jquery-ui.min.js") |
86 |
script_jq_cust.set("type", "text/javascript") |
87 |
|
88 |
style_cust = root.makeelement('link') |
89 |
style_cust.set("rel","stylesheet") |
90 |
style_cust.set("type", "text/css") |
91 |
style_cust.set("href", conf.JQUERYCSS[0] + "/jquery-ui.css") |
92 |
root.body.append(style_cust) |
93 |
|
94 |
root.body.set("onload","a11ypi.loadOverlay();") |
95 |
return lxml.html.tostring(root) |
96 |
|
97 |
elif request.args.has_key('lang') == True and request.args.has_key('interactive') == True and request.args.has_key('blog') == False: |
98 |
root.make_links_absolute(d['foruri'], resolve_base_href = True) |
99 |
script_test = root.makeelement('script') |
100 |
script_edit = root.makeelement('script') |
101 |
root.body.append(script_test) |
102 |
root.body.append(script_edit) |
103 |
|
104 |
script_jq_mini = root.makeelement('script') |
105 |
root.body.append(script_jq_mini) |
106 |
script_jq_mini.set("src", conf.JQUERYURL[0] + "/jquery-1.7.min.js") |
107 |
script_jq_mini.set("type", "text/javascript") |
108 |
|
109 |
script_jqui = root.makeelement('script') |
110 |
script_jqui.set("type","text/javascript") |
111 |
script_jqui.set("src",conf.JQUERYUI[0] + "/jquery-ui.min.js") |
112 |
root.body.append(script_jqui) |
113 |
script_test.set("src", conf.APPURL[0] + "/server/ui.js") |
114 |
script_test.set("type", "text/javascript") |
115 |
script_edit.set("src", conf.APPURL[0] + "/server/wsgi/pageEditor.js") |
116 |
script_edit.set("type","text/javascript") |
117 |
|
118 |
jit_script = root.makeelement('script') |
119 |
root.body.append(jit_script) |
120 |
jit_script.set("src", conf.APPURL[0] + "/server/jit.js") |
121 |
jit_script.set("type", "text/javascript") |
122 |
|
123 |
tree_script = root.makeelement('script') |
124 |
root.body.append(tree_script) |
125 |
tree_script.set("src", conf.APPURL[0] + "/server/tree.js") |
126 |
tree_script.set("type", "text/javascript") |
127 |
|
128 |
ui_css = root.makeelement("link") |
129 |
ui_css.set("rel", "stylesheet"); |
130 |
ui_css.set("type", "text/css"); |
131 |
ui_css.set("href", conf.JQUERYCSS[0] + "/jquery-ui.css"); |
132 |
root.body.append(ui_css); |
133 |
|
134 |
ren_overlay = root.makeelement('div') |
135 |
root.body.append(ren_overlay) |
136 |
ren_overlay.set("id", "social_overlay") |
137 |
|
138 |
see_orig = root.makeelement('input') |
139 |
ren_overlay.append(see_orig) |
140 |
see_orig.set("id", "see_orig-button") |
141 |
see_orig.set("type", "submit") |
142 |
see_orig.set("onClick", "a11ypi.showOriginal();") |
143 |
see_orig.set("value", "See original page") |
144 |
see_orig.set("style","position:fixed;left:5px;top:6px;") |
145 |
|
146 |
tweet = root.makeelement("a") |
147 |
tweet.set("id", "tweet") |
148 |
tweet.set("href", "https://twitter.com/share") |
149 |
tweet.set("class", "twitter-share-button") |
150 |
tweet.set("data-via", "a11ypi") |
151 |
tweet.set("data-lang", "en") |
152 |
tweet.set("data-url", "http://dev.a11y.in/web?foruri={0}&lang={1}&interactive=1".format(quote_plus(d['foruri']),request.args['lang'])) |
153 |
tweet.textContent = "Tweet" |
154 |
ren_overlay.append(tweet) |
155 |
|
156 |
fbroot = root.makeelement("div") |
157 |
fbroot.set("id", "fb-root") |
158 |
ren_overlay.append(fbroot) |
159 |
|
160 |
fblike = root.makeelement("div") |
161 |
fblike.set("class", "fb-like") |
162 |
fblike.set("data-href", "http://dev.a11y.in/web?foruri={0}&lang={1}&interactive=1".format(quote_plus(d['foruri']),request.args['lang'])) |
163 |
fblike.set("data-send", "true") |
164 |
fblike.set("data-layout", "button_count") |
165 |
fblike.set("data-width", "50") |
166 |
fblike.set("data-show-faces", "true") |
167 |
fblike.set("data-font", "arial") |
168 |
ren_overlay.append(fblike) |
169 |
|
170 |
style = root.makeelement('link') |
171 |
root.body.append(style) |
172 |
style.set("rel","stylesheet") |
173 |
style.set("type", "text/css") |
174 |
style.set("href", "http://dev.a11y.in/server/stylesheet.css") |
175 |
|
176 |
root.body.set("onload","a11ypi.ren();a11ypi.tweet(); a11ypi.facebook();a11ypi.loadOverlay();") |
177 |
return lxml.html.tostring(root) |
178 |
|
179 |
elif request.args.has_key('lang') == True and request.args.has_key('blog') == False: |
180 |
script_jq_mini = root.makeelement('script') |
181 |
root.body.append(script_jq_mini) |
182 |
script_jq_mini.set("src", conf.JQUERYURL[0] + "/jquery-1.7.min.js") |
183 |
script_jq_mini.set("type", "text/javascript") |
184 |
d['lang'] = request.args['lang'] |
185 |
script_test = root.makeelement('script') |
186 |
root.body.append(script_test) |
187 |
script_test.set("src", conf.APPURL[0] + "/server/ui.js") |
188 |
script_test.set("type", "text/javascript") |
189 |
root.body.set("onload","a11ypi.ren()"); |
190 |
root.make_links_absolute(d['foruri'], resolve_base_href = True) |
191 |
return lxml.html.tostring(root) |
192 |
|
193 |
elif request.args.has_key('interactive') == True and request.args.has_key('blog') == True and request.args.has_key('lang') == True: |
194 |
script_jqui = root.makeelement('script') |
195 |
|
196 |
script_test = root.makeelement('script') |
197 |
script_test.set("src", conf.APPURL[0] + "/server/ui.js") |
198 |
script_test.set("type", "text/javascript") |
199 |
root.body.append(script_test) |
200 |
|
201 |
script_jq_mini = root.makeelement('script') |
202 |
script_jq_mini.set("src", conf.JQUERYURL[0] + "/jquery-1.7.min.js") |
203 |
script_jq_mini.set("type", "text/javascript") |
204 |
root.body.append(script_jq_mini) |
205 |
|
206 |
script_edit = root.makeelement('script') |
207 |
script_edit.set("src", conf.APPURL[0] + "/server/wsgi/pageEditor.js") |
208 |
script_edit.set("type","text/javascript") |
209 |
root.body.append(script_edit) |
210 |
|
211 |
jit_script = root.makeelement('script') |
212 |
root.body.append(jit_script) |
213 |
jit_script.set("src", conf.APPURL[0] + "/server/jit.js") |
214 |
jit_script.set("type", "text/javascript") |
215 |
|
216 |
tree_script = root.makeelement('script') |
217 |
root.body.append(tree_script) |
218 |
tree_script.set("src", conf.APPURL[0] + "/server/tree.js") |
219 |
tree_script.set("type", "text/javascript") |
220 |
|
221 |
script_jqui.set("type","text/javascript") |
222 |
script_jqui.set("src",conf.JQUERYUI[0] + "/jquery-ui.min.js") |
223 |
root.body.append(script_jqui) |
224 |
ui_css = root.makeelement("link") |
225 |
ui_css.set("rel", "stylesheet"); |
226 |
ui_css.set("type", "text/css"); |
227 |
ui_css.set("href", conf.JQUERYCSS[0] + "/jquery-ui.css"); |
228 |
root.body.append(ui_css); |
229 |
|
230 |
ren_overlay = root.makeelement('div') |
231 |
root.body.append(ren_overlay) |
232 |
ren_overlay.set("id", "social_overlay") |
233 |
|
234 |
see_orig = root.makeelement('input') |
235 |
ren_overlay.append(see_orig) |
236 |
see_orig.set("id", "see_orig-button") |
237 |
see_orig.set("type", "submit") |
238 |
see_orig.set("onClick", "a11ypi.showOriginal();") |
239 |
see_orig.set("value", "See original page") |
240 |
see_orig.set("style","position:fixed;left:5px;top:6px;") |
241 |
|
242 |
tweet = root.makeelement("a") |
243 |
tweet.set("id", "tweet") |
244 |
tweet.set("href", "https://twitter.com/share") |
245 |
tweet.set("class", "twitter-share-button") |
246 |
tweet.set("data-via", "a11ypi") |
247 |
tweet.set("data-lang", "en") |
248 |
tweet.set("data-url", conf.APPURL[0] + "/web?foruri={0}&lang={1}&interactive=1".format(quote_plus(d['foruri']),request.args['lang'])) |
249 |
tweet.textContent = "Tweet" |
250 |
ren_overlay.append(tweet) |
251 |
|
252 |
fbroot = root.makeelement("div") |
253 |
fbroot.set("id", "fb-root") |
254 |
ren_overlay.append(fbroot) |
255 |
|
256 |
fblike = root.makeelement("div") |
257 |
fblike.set("class", "fb-like") |
258 |
fblike.set("data-href", conf.APPURL[0] + "/web?foruri={0}&lang={1}&interactive=1".format(quote_plus(d['foruri']),request.args['lang'])) |
259 |
fblike.set("data-send", "true") |
260 |
fblike.set("data-layout", "button_count") |
261 |
fblike.set("data-width", "50") |
262 |
fblike.set("data-show-faces", "true") |
263 |
fblike.set("data-font", "arial") |
264 |
ren_overlay.append(fblike) |
265 |
|
266 |
|
267 |
style = root.makeelement('link') |
268 |
root.body.append(style) |
269 |
style.set("rel","stylesheet") |
270 |
style.set("type", "text/css") |
271 |
style.set("href", conf.APPURL[0] + "/server/stylesheet.css") |
272 |
|
273 |
overlay2 = root.makeelement('div') |
274 |
root.body.append(overlay2) |
275 |
overlay2.set("id", "overlay2") |
276 |
|
277 |
btn = root.makeelement('input') |
278 |
overlay2.append(btn) |
279 |
btn.set("id", "edit-button") |
280 |
btn.set("type", "submit") |
281 |
btn.set("onClick", "a11ypi.testContext();page_edit('4seiz', '4l85060vb9', '336e2nootv6nxjsvyjov', 'VISUAL', 'false', '');") |
282 |
btn.set("value", "EDIT") |
283 |
|
284 |
script_test = root.makeelement('script') |
285 |
root.body.append(script_test) |
286 |
script_test.set("src", conf.APPURL[0] + "/server/ui.js") |
287 |
script_test.set("type", "text/javascript") |
288 |
root.body.set("onload","a11ypi.filter(); a11ypi.tweet(); a11ypi.facebook();"); |
289 |
root.make_links_absolute(d['foruri'], resolve_base_href = True) |
290 |
return lxml.html.tostring(root) |
291 |
|
292 |
elif request.args.has_key('interactive') == False and request.args.has_key('blog') == True: |
293 |
script_test = root.makeelement('script') |
294 |
root.body.append(script_test) |
295 |
script_test.set("src", conf.APPURL[0] + "/server/ui.js") |
296 |
script_test.set("type", "text/javascript") |
297 |
|
298 |
script_jq_mini = root.makeelement('script') |
299 |
root.body.append(script_jq_mini) |
300 |
script_jq_mini.set("src", conf.JQUERYURL[0] + "/jquery-1.7.min.js") |
301 |
script_jq_mini.set("type", "text/javascript") |
302 |
|
303 |
script_jq_cust = root.makeelement('script') |
304 |
root.body.append(script_jq_cust) |
305 |
script_jq_cust.set("src", conf.JQUERYUI[0] + "/jquery-ui.min.js") |
306 |
script_jq_cust.set("type", "text/javascript") |
307 |
|
308 |
style_cust = root.makeelement('link') |
309 |
style_cust.set("rel","stylesheet") |
310 |
style_cust.set("type", "text/css") |
311 |
style_cust.set("href", conf.JQUERYCSS[0] + "/jquery-ui.css") |
312 |
root.body.append(style_cust) |
313 |
|
314 |
style = root.makeelement('link') |
315 |
root.body.append(style) |
316 |
style.set("rel","stylesheet") |
317 |
style.set("type", "text/css") |
318 |
style.set("href", conf.APPURL[0] + "/server/stylesheet.css") |
319 |
|
320 |
collection = g.db['post'] #FIXME Move this logic to JS. |
321 |
if collection.find_one({"about" : request.args['foruri']}) is not None: |
322 |
overlay1 = root.makeelement('div') |
323 |
root.body.append(overlay1) |
324 |
overlay1.set("id", "overlay1") |
325 |
|
326 |
opt = root.makeelement('option') |
327 |
opt.text = "Choose a narration" |
328 |
|
329 |
rpl = root.makeelement('select') |
330 |
overlay1.append(rpl) |
331 |
rpl.append(opt) |
332 |
rpl.set("id", "menu-button") |
333 |
rpl.set("onclick", "a11ypi.ajax1();") |
334 |
root.make_links_absolute(d['foruri'], resolve_base_href = True) |
335 |
return lxml.html.tostring(root) |
336 |
|
337 |
@app.route('/directory') |
338 |
def show_directory(): |
339 |
collection = g.db['post'] |
340 |
query = collection.group( |
341 |
key = Code('function(doc){return {"about" : doc.about,"lang":doc.lang}}'), |
342 |
condition={"about":{'$regex':'^[/\S/]'}}, |
343 |
initial={'na': []}, |
344 |
reduce=Code('function(doc,out){out.na.push(doc.blog)}') |
345 |
) |
346 |
query.reverse() |
347 |
return render_template('directory.html', name=query, mymodule = quote_plus, myset=set, mylist= list) |
348 |
|
349 |
@app.route('/getLoc', methods=['GET']) |
350 |
def get_loc(): |
351 |
term = request.args['term'] |
352 |
connection = oursql.Connection(conf.DBHOST[0],conf.DBUSRNAME[0],conf.DBPASSWD[0],db=conf.DBNAME[0]) |
353 |
cursor = connection.cursor(oursql.DictCursor) |
354 |
cursor.execute('select l.name, c.country_name from `location` as l, `codes` as c where l.name like ? and l.code=c.code limit ?', (term+'%', 5)) |
355 |
r = cursor.fetchall() |
356 |
connection.close() |
357 |
d = {} |
358 |
d['return'] = r |
359 |
return jsonify(d) |
360 |
@app.route('/getLang', methods=['GET']) |
361 |
def get_lang(): |
362 |
term = request.args['term'] |
363 |
connection = oursql.Connection(conf.DBHOST[0],conf.DBUSRNAME[0],conf.DBPASSWD[0],db=conf.DBNAME[0]) |
364 |
cursor = connection.cursor(oursql.DictCursor) |
365 |
cursor.execute('select * from `languages` as l where l.name like ? limit ?', (term+'%',5)) |
366 |
r = cursor.fetchall() |
367 |
connection.close() |
368 |
d = {} |
369 |
d['return'] = r |
370 |
return jsonify(d) |
371 |
import logging,os |
372 |
from logging import FileHandler |
373 |
|
374 |
fil = FileHandler(os.path.join(os.path.dirname(__file__),'logme'),mode='a') |
375 |
fil.setLevel(logging.ERROR) |
376 |
app.logger.addHandler(fil) |
377 |
|
378 |
if __name__ == '__main__': |
379 |
app.run(debug=True, host='0.0.0.0') |