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