Commit b724e6010e4acbbf5e4d212acfc67db2be861cff
- Diff rendering mode:
- inline
- side by side
alipi/alipi.py
(147 / 77)
  | |||
8 | 8 | import urllib2 | |
9 | 9 | import StringIO | |
10 | 10 | from flask import g | |
11 | from flask import redirect | ||
12 | 11 | from urllib import quote_plus | |
13 | 12 | from urllib import unquote_plus | |
14 | 13 | import conf | |
… | … | ||
16 | 16 | import requests | |
17 | 17 | from flask import jsonify | |
18 | 18 | import json | |
19 | from flask import url_for | ||
20 | 19 | ||
20 | |||
21 | 21 | app = Flask(__name__) | |
22 | |||
23 | |||
22 | 24 | @app.before_request | |
23 | 25 | def first(): | |
24 | g.connection = pymongo.MongoClient('localhost',27017) #Create the object once and use it. | ||
26 | g.connection = pymongo.MongoClient('localhost', 27017) # Create the | ||
27 | # object once and use it. | ||
25 | 28 | g.db = g.connection[conf.MONGODB[0]] | |
26 | 29 | ||
27 | # @app.after_request | ||
28 | # def set_secret(response): | ||
29 | # response.set_cookie("key", conf.SWEET_SECRET_KEY[0]) | ||
30 | 30 | ||
31 | |||
32 | 31 | @app.teardown_request | |
33 | 32 | def close(exception): | |
34 | 33 | g.connection.disconnect() | |
35 | 34 | ||
36 | 35 | ||
37 | 36 | @app.route('/') | |
38 | def start_page() : | ||
37 | def start_page(): | ||
39 | 38 | d = {} | |
40 | 39 | d['foruri'] = request.args['foruri'] | |
41 | myhandler1 = urllib2.Request(d['foruri'],headers={'User-Agent':"Mozilla/5.0 (X11; Linux x86_64; rv:25.0) Gecko/20100101 Firefox/25.0)"}) #A fix to send user-agents, so that sites render properly. | ||
40 | myhandler1 = urllib2.Request(d['foruri'], | ||
41 | headers={'User-Agent': | ||
42 | "Mozilla/5.0 (X11; " + | ||
43 | "Linux x86_64; rv:25.0)" + | ||
44 | "Gecko/20100101 Firefox/25.0)"}) | ||
45 | # A fix to send user-agents, so that sites render properly. | ||
42 | 46 | try: | |
43 | 47 | a = urllib2.urlopen(myhandler1) | |
44 | 48 | if a.geturl() != d['foruri']: | |
45 | return "There was a server redirect, please click on the <a href='http://y.a11y.in/web?foruri={0}'>link</a> to continue.".format(quote_plus(a.geturl())) | ||
49 | return ("There was a server redirect, please click on the" + | ||
50 | " <a href='http://y.a11y.in/web?foruri={0}'>link</a> to" + | ||
51 | " continue.".format(quote_plus(a.geturl()))) | ||
46 | 52 | else: | |
47 | 53 | page = a.read() | |
48 | 54 | a.close() | |
49 | 55 | except ValueError: | |
50 | return "The link is malformed, click <a href='http://y.a11y.in/web?foruri={0}&lang={1}&interactive=1'>here</a> to be redirected.".format(quote_plus(unquote_plus(d['foruri'].encode('utf-8'))),request.args['lang']) | ||
56 | return ("The link is malformed, click " + | ||
57 | "<a href='http://y.a11y.in/web?foruri={0}&lang={1}" + | ||
58 | "&interactive=1'>" + | ||
59 | "here</a> to be redirected.".format( | ||
60 | quote_plus(unquote_plus(d['foruri'].encode('utf-8'))), | ||
61 | request.args['lang'])) | ||
51 | 62 | except urllib2.URLError: | |
52 | 63 | return render_template('error.html') | |
53 | 64 | try: | |
54 | page = unicode(page,'utf-8') #Hack to fix improperly displayed chars on wikipedia. | ||
65 | page = unicode(page, 'utf-8') # Hack to fix improperly displayed chars on wikipedia. | ||
55 | 66 | except UnicodeDecodeError: | |
56 | pass #Some pages may not need be utf-8'ed | ||
67 | pass # Some pages may not need be utf-8'ed | ||
57 | 68 | try: | |
58 | 69 | g.root = lxml.html.parse(StringIO.StringIO(page)).getroot() | |
59 | 70 | except ValueError: | |
60 | g.root = lxml.html.parse(d['foruri']).getroot() #Sometimes creators of the page lie about the encoding, thus leading to this execption. http://lxml.de/parsing.html#python-unicode-strings | ||
71 | g.root = lxml.html.parse(d['foruri']).getroot() # Sometimes creators of the page lie about the encoding, thus leading to this execption. http://lxml.de/parsing.html#python-unicode-strings | ||
61 | 72 | if request.args.has_key('lang') == False and request.args.has_key('blog') == False: | |
62 | g.root.make_links_absolute(d['foruri'], resolve_base_href = True) | ||
73 | g.root.make_links_absolute(d['foruri'], | ||
74 | resolve_base_href=True) | ||
63 | 75 | for i in g.root.iterlinks(): | |
64 | 76 | if i[1] == 'href' and i[0].tag != 'link': | |
65 | 77 | try: | |
66 | i[0].attrib['href'] = 'http://{0}?foruri={1}'.format(conf.DEPLOYURL[0],quote_plus(i[0].attrib['href'])) | ||
78 | i[0].attrib['href'] = 'http://{0}?foruri={1}'.format( | ||
79 | conf.DEPLOYURL[0], quote_plus(i[0].attrib['href'])) | ||
67 | 80 | except KeyError: | |
68 | i[0].attrib['href'] = '{0}?foruri={1}'.format(conf.DEPLOYURL[0],quote_plus(i[0].attrib['href'].encode('utf-8'))) | ||
81 | i[0].attrib['href'] = '{0}?foruri={1}'.format( | ||
82 | conf.DEPLOYURL[0], quote_plus( | ||
83 | i[0].attrib['href'].encode('utf-8'))) | ||
69 | 84 | setScripts() | |
70 | g.root.body.set("onload","a11ypi.loadOverlay();") | ||
85 | g.root.body.set("onload", "a11ypi.loadOverlay();") | ||
71 | 86 | response = make_response() | |
72 | 87 | response.data = lxml.html.tostring(g.root) | |
73 | 88 | return response | |
… | … | ||
90 | 90 | elif request.args.has_key('lang') == True and request.args.has_key('interactive') == True and request.args.has_key('blog') == False: | |
91 | 91 | setScripts() | |
92 | 92 | setSocialScript() | |
93 | g.root.body.set("onload","a11ypi.ren();a11ypi.tweet(); a11ypi.facebook(); a11ypi.loadOverlay();") | ||
94 | g.root.make_links_absolute(d['foruri'], resolve_base_href = True) | ||
93 | g.root.body.set("onload", "a11ypi.ren();a11ypi.tweet(); " + | ||
94 | "a11ypi.facebook(); a11ypi.loadOverlay();") | ||
95 | g.root.make_links_absolute(d['foruri'], resolve_base_href=True) | ||
95 | 96 | response = make_response() | |
96 | 97 | response.data = lxml.html.tostring(g.root) | |
97 | 98 | return response | |
98 | 99 | ||
99 | |||
100 | 100 | elif request.args.has_key('lang') == True and request.args.has_key('blog') == False: | |
101 | 101 | script_jq_mini = g.root.makeelement('script') | |
102 | 102 | g.root.body.append(script_jq_mini) | |
… | … | ||
107 | 107 | g.root.body.append(script_test) | |
108 | 108 | script_test.set("src", conf.APPURL[0] + "/alipi/ui.js") | |
109 | 109 | script_test.set("type", "text/javascript") | |
110 | g.root.body.set("onload","a11ypi.ren()"); | ||
110 | g.root.body.set("onload", "a11ypi.ren()") | ||
111 | 111 | response = make_response() | |
112 | 112 | response.data = lxml.html.tostring(g.root) | |
113 | 113 | return response | |
114 | 114 | ||
115 | |||
116 | 115 | elif request.args.has_key('interactive') == True and request.args.has_key('blog') == True and request.args.has_key('lang') == True: | |
117 | 116 | setScripts() | |
118 | 117 | setSocialScript() | |
119 | g.root.body.set("onload","a11ypi.filter(); a11ypi.tweet(); a11ypi.facebook(); a11ypi.loadOverlay();"); | ||
120 | g.root.make_links_absolute(d['foruri'], resolve_base_href = True) | ||
118 | g.root.body.set("onload", "a11ypi.filter(); a11ypi.tweet();" + | ||
119 | "a11ypi.facebook(); a11ypi.loadOverlay();") | ||
120 | g.root.make_links_absolute(d['foruri'], resolve_base_href=True) | ||
121 | 121 | response = make_response() | |
122 | 122 | response.data = lxml.html.tostring(g.root) | |
123 | 123 | return response | |
124 | 124 | ||
125 | 125 | elif request.args.has_key('interactive') == False and request.args.has_key('blog') == True: | |
126 | 126 | setScripts() | |
127 | g.root.make_links_absolute(d['foruri'], resolve_base_href = True) | ||
127 | g.root.make_links_absolute(d['foruri'], resolve_base_href=True) | ||
128 | 128 | g.root.body.set('onload', 'a11ypi.loadOverlay();') | |
129 | 129 | response = make_response() | |
130 | 130 | response.data = lxml.html.tostring(g.root) | |
… | … | ||
143 | 143 | ||
144 | 144 | style = g.root.makeelement('link') | |
145 | 145 | g.root.body.append(style) | |
146 | style.set("rel","stylesheet") | ||
146 | style.set("rel", "stylesheet") | ||
147 | 147 | style.set("type", "text/css") | |
148 | 148 | style.set("href", conf.APPURL[0] + "/alipi/pack.min.css") | |
149 | 149 | ||
… | … | ||
154 | 154 | info_button.set("id", "info") | |
155 | 155 | info_button.set("class", "alipi") | |
156 | 156 | info_button.set("onClick", "a11ypi.showInfo(a11ypi.responseJSON);") | |
157 | info_button.text = "Info" | ||
158 | info_button.set("title", "Have a look at the information of each renarrated element") | ||
157 | info_button.text = "Info" | ||
158 | info_button.set("title", "Have a look at the information of each" + | ||
159 | " renarrated element") | ||
159 | 160 | ||
160 | 161 | share_button = g.root.makeelement('button') | |
161 | 162 | g.root.body.append(share_button) | |
162 | 163 | share_button.set("id", "share") | |
163 | 164 | share_button.set("class", "alipi") | |
164 | 165 | share_button.set("onClick", "a11ypi.share();") | |
165 | share_button.text = "Share" | ||
166 | share_button.text = "Share" | ||
166 | 167 | share_button.set("title", "Share your contribution in your social network") | |
167 | 168 | ||
168 | 169 | see_orig = g.root.makeelement('button') | |
… | … | ||
172 | 172 | see_orig.set("class", "alipi") | |
173 | 173 | see_orig.set("onClick", "a11ypi.showOriginal();") | |
174 | 174 | see_orig.text = "Original Page" | |
175 | see_orig.set("title", "Go to Original link, the original page of this renarrated") | ||
175 | see_orig.set("title", "Go to Original link, the original page of this" + | ||
176 | " renarrated page") | ||
176 | 177 | ||
177 | 178 | tweetroot = g.root.makeelement("div") | |
178 | 179 | tweetroot.set("id", "tweet-root") | |
… | … | ||
187 | 187 | tweet.set("class", "alipi twitter-share-button") | |
188 | 188 | tweet.set("data-via", "a11ypi") | |
189 | 189 | tweet.set("data-lang", "en") | |
190 | tweet.set("data-url", "http://y.a11y.in/web?foruri={0}&lang={1}&interactive=1".format(quote_plus(request.args['foruri']),(request.args['lang']).encode('unicode-escape'))) | ||
190 | tweet.set("data-url", "http://y.a11y.in/web?foruri={0}&lang={1}&" + | ||
191 | "interactive=1".format(quote_plus(request.args['foruri']), | ||
192 | (request.args['lang']).encode( | ||
193 | 'unicode-escape'))) | ||
191 | 194 | tweet.textContent = "Tweet" | |
192 | 195 | tweetroot.append(tweet) | |
193 | 196 | ||
… | … | ||
198 | 198 | fblike.set("id", "fb-like") | |
199 | 199 | fblike.set("class", "alipi fb-like") | |
200 | 200 | fblike.set("style", "display:none;padding:10px;") | |
201 | fblike.set("data-href", "http://y.a11y.in/web?foruri={0}&lang={1}&interactive=1".format(quote_plus(request.args['foruri']),(request.args['lang']).encode('unicode-escape'))) | ||
201 | fblike.set("data-href", "http://y.a11y.in/web?foruri={0}&lang={1}&" + | ||
202 | "interactive=1".format(quote_plus(request.args['foruri']), | ||
203 | (request.args['lang']).encode( | ||
204 | 'unicode-escape'))) | ||
202 | 205 | fblike.set("data-send", "true") | |
203 | 206 | fblike.set("data-layout", "button_count") | |
204 | 207 | fblike.set("data-width", "50") | |
… | … | ||
211 | 211 | ||
212 | 212 | style = g.root.makeelement('link') | |
213 | 213 | g.root.body.append(style) | |
214 | style.set("rel","stylesheet") | ||
214 | style.set("rel", "stylesheet") | ||
215 | 215 | style.set("type", "text/css") | |
216 | 216 | style.set("href", "http://y.a11y.in/alipi/stylesheet.css") | |
217 | 217 | ||
… | … | ||
220 | 220 | def show_directory(): | |
221 | 221 | collection = g.db['post'] | |
222 | 222 | query = collection.group( | |
223 | key = Code('function(doc){return {"about" : doc.about,"lang":doc.lang}}'), | ||
224 | condition={"about":{'$regex':'^[/\S/]'}}, | ||
223 | key=Code('function(doc){' + | ||
224 | 'return {"about" : doc.about,"lang":doc.lang}}'), | ||
225 | condition={"about": {'$regex': '^[/\S/]'}}, | ||
225 | 226 | initial={'na': []}, | |
226 | 227 | reduce=Code('function(doc,out){out.na.push(doc.blog)}') | |
227 | 228 | ) | |
228 | 229 | query.reverse() | |
229 | return render_template('directory.html', name=query, mymodule = quote_plus, myset=set, mylist= list) | ||
230 | return render_template('directory.html', name=query, mymodule=quote_plus, | ||
231 | myset=set, mylist=list) | ||
230 | 232 | ||
233 | |||
231 | 234 | @app.route('/getLoc', methods=['GET']) | |
232 | 235 | def get_loc(): | |
233 | 236 | ||
234 | 237 | term = request.args['term'] | |
235 | connection = oursql.Connection(conf.DBHOST[0],conf.DBUSRNAME[0],conf.DBPASSWD[0],db=conf.DBNAME[0]) | ||
238 | connection = oursql.Connection(conf.DBHOST[0], conf.DBUSRNAME[0], | ||
239 | conf.DBPASSWD[0], db=conf.DBNAME[0]) | ||
236 | 240 | cursor = connection.cursor(oursql.DictCursor) | |
237 | 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)) | ||
241 | cursor.execute('select l.name, c.country_name from `location` as l, ' + | ||
242 | ' `codes` as c where l.name like ? and l.code=c.code' + | ||
243 | ' limit ?', (term+'%', 5)) | ||
238 | 244 | r = cursor.fetchall() | |
239 | 245 | connection.close() | |
240 | 246 | d = {} | |
… | … | ||
248 | 248 | response = jsonify(d) | |
249 | 249 | response.headers['Access-Control-Allow-Origin'] = '*' | |
250 | 250 | return response | |
251 | |||
252 | |||
251 | 253 | @app.route('/getLang', methods=['GET']) | |
252 | 254 | def get_lang(): | |
253 | 255 | term = request.args['term'] | |
254 | connection = oursql.Connection(conf.DBHOST[0],conf.DBUSRNAME[0],conf.DBPASSWD[0],db=conf.DBNAME[0]) | ||
256 | connection = oursql.Connection(conf.DBHOST[0], conf.DBUSRNAME[0], | ||
257 | conf.DBPASSWD[0], db=conf.DBNAME[0]) | ||
255 | 258 | cursor = connection.cursor(oursql.DictCursor) | |
256 | cursor.execute('select * from `languages` as l where l.name like ? limit ?', (term+'%',5)) | ||
259 | cursor.execute('select * from `languages` as l where l.name like' + | ||
260 | ' ? limit ?', (term+'%', 5)) | ||
257 | 261 | r = cursor.fetchall() | |
258 | 262 | connection.close() | |
259 | 263 | d = {} | |
… | … | ||
266 | 266 | response.headers['Access-Control-Allow-Origin'] = '*' | |
267 | 267 | return response | |
268 | 268 | ||
269 | |||
269 | 270 | @app.route('/blank', methods=['GET']) | |
270 | 271 | def serve_blank(): | |
271 | 272 | return render_template('blank.html') | |
272 | 273 | ||
274 | |||
273 | 275 | @app.route('/info', methods=['GET']) | |
274 | 276 | def serve_info(): | |
275 | 277 | coll = g.db['post'] | |
276 | 278 | d = {} | |
277 | 279 | cntr = 0 | |
278 | for i in coll.find({"about":unquote_plus(request.args['about']),"lang":request.args['lang']}): | ||
280 | for i in coll.find({"about": unquote_plus(request.args['about']), | ||
281 | "lang": request.args['lang']}): | ||
279 | 282 | i['_id'] = str(i['_id']) | |
280 | 283 | d[cntr] = i | |
281 | cntr+=1 | ||
284 | cntr += 1 | ||
282 | 285 | response = jsonify(d) | |
283 | 286 | response.headers['Access-Control-Allow-Origin'] = '*' | |
284 | 287 | return response | |
… | … | ||
293 | 293 | lang = request.args['lang'] | |
294 | 294 | url = request.args['url'] | |
295 | 295 | query = collection.group( | |
296 | key = Code('function(doc){return {"xpath" : doc.xpath, "about": doc.url}}'), | ||
297 | condition={"about" : url, "lang" : lang,"elementtype":"text"}, | ||
296 | key=Code('function(doc){' + | ||
297 | 'return {"xpath": doc.xpath, "about": doc.url}}'), | ||
298 | condition={"about": url, "lang": lang, "elementtype": "text"}, | ||
298 | 299 | initial={'narration': []}, | |
299 | 300 | reduce=Code('function(doc,out){out.narration.push(doc);}') | |
300 | 301 | ) | |
301 | 302 | ||
302 | 303 | print query | |
303 | 304 | ||
304 | audio_query =collection.group( | ||
305 | key = Code('function(doc){return {"xpath" : doc.xpath, "about": doc.url}}'), | ||
306 | condition={"about" : url, "lang" : lang, 'elementtype':"audio/ogg"}, | ||
305 | audio_query = collection.group( | ||
306 | key=Code('function(doc){' + | ||
307 | 'return {"xpath": doc.xpath, "about": doc.url}}'), | ||
308 | condition={"about": url, "lang": lang, 'elementtype': "audio/ogg"}, | ||
307 | 309 | initial={'narration': []}, | |
308 | 310 | reduce=Code('function(doc,out){out.narration.push(doc);}') | |
309 | 311 | ) | |
310 | 312 | ||
311 | image_query =collection.group( | ||
312 | key = Code('function(doc){return {"xpath" : doc.xpath, "about": doc.url}}'), | ||
313 | condition={"about" : url, "lang" : lang, 'elementtype':"image"}, | ||
313 | image_query = collection.group( | ||
314 | key=Code('function(doc){' + | ||
315 | 'return {"xpath": doc.xpath, "about": doc.url}}'), | ||
316 | condition={"about": url, "lang": lang, 'elementtype': "image"}, | ||
314 | 317 | initial={'narration': []}, | |
315 | 318 | reduce=Code('function(doc,out){out.narration.push(doc);}') | |
316 | 319 | ) | |
… | … | ||
337 | 337 | response.headers['Access-Control-Allow-Origin'] = '*' | |
338 | 338 | return response | |
339 | 339 | ||
340 | |||
340 | 341 | @app.route('/feeds', methods=['GET']) | |
341 | 342 | def serve_feed_temp(): | |
342 | 343 | return render_template("feeds.html") | |
343 | 344 | ||
345 | |||
344 | 346 | @app.route('/feed', methods=['GET']) | |
345 | 347 | def serve_feed(): | |
346 | 348 | coll = g.db['post'] | |
347 | 349 | d = {} | |
348 | 350 | cntr = 0 | |
349 | for i in coll.find().sort('_id',direction=-1): | ||
351 | for i in coll.find().sort('_id', direction=-1): | ||
350 | 352 | if i['data'] != '<br/>': | |
351 | 353 | i['_id'] = str(i['_id']) | |
352 | 354 | d[cntr] = i | |
353 | cntr+=1 | ||
355 | cntr += 1 | ||
354 | 356 | response = jsonify(d) | |
355 | 357 | response.headers['Access-Control-Allow-Origin'] = '*' | |
356 | 358 | return response | |
357 | 359 | ||
360 | |||
358 | 361 | @app.route('/about', methods=['GET']) | |
359 | 362 | def serve_authors(): | |
360 | 363 | coll = g.db['post'] | |
361 | 364 | d = {} | |
362 | 365 | cntr = 0 | |
363 | for i in coll.find({"about":unquote_plus(request.args['about'])}): | ||
366 | for i in coll.find({"about": unquote_plus(request.args['about'])}): | ||
364 | 367 | i['_id'] = str(i['_id']) | |
365 | 368 | d[cntr] = i | |
366 | cntr+=1 | ||
369 | cntr += 1 | ||
367 | 370 | response = jsonify(d) | |
368 | 371 | response.headers['Access-Control-Allow-Origin'] = '*' | |
369 | 372 | return response | |
373 | |||
374 | |||
370 | 375 | #Retrieve all information about a specific $about and a given $author. | |
371 | 376 | @app.route('/author', methods=['GET']) | |
372 | 377 | def serve_author(): | |
373 | 378 | coll = g.db['post'] | |
374 | 379 | d = {} | |
375 | 380 | cntr = 0 | |
376 | for i in coll.find({"about":unquote_plus(request.args['about']),"author":unquote_plus(request.args['author'])}): | ||
381 | for i in coll.find({"about": unquote_plus(request.args['about']), | ||
382 | "author": unquote_plus(request.args['author'])}): | ||
377 | 383 | i['_id'] = str(i['_id']) | |
378 | 384 | d[cntr] = i | |
379 | 385 | cntr += 1 | |
… | … | ||
387 | 387 | response.headers['Access-Control-Allow-Origin'] = '*' | |
388 | 388 | return response | |
389 | 389 | ||
390 | |||
390 | 391 | @app.route('/getAllLang', methods=['GET']) | |
391 | 392 | def get_all_lang(): | |
392 | 393 | term = request.args['term'] | |
393 | connection = oursql.Connection(conf.DBHOST[0],conf.DBUSRNAME[0],conf.DBPASSWD[0],db=conf.DBNAME[0]) | ||
394 | connection = oursql.Connection(conf.DBHOST[0], conf.DBUSRNAME[0], | ||
395 | conf.DBPASSWD[0], db=conf.DBNAME[0]) | ||
394 | 396 | cursor = connection.cursor(oursql.DictCursor) | |
395 | cursor.execute('select * from `languages` as l where l.name like ?', (term+'%',)) | ||
397 | cursor.execute('select * from `languages` as l where l.name like ?', | ||
398 | (term+'%',)) | ||
396 | 399 | r = cursor.fetchall() | |
397 | 400 | connection.close() | |
398 | 401 | d = {} | |
… | … | ||
410 | 410 | data = json.loads(request.form['data']) | |
411 | 411 | collection = g.db['post'] | |
412 | 412 | page = {} | |
413 | if type(data) is unicode: #A hack to fix malformed data. FIXME. | ||
413 | if type(data) is unicode: # A hack to fix malformed data. FIXME. | ||
414 | 414 | data = json.loads(data) | |
415 | 415 | content = [] | |
416 | for i in data: #Create content objects here for posting to blog. DELETEME. | ||
416 | for i in data: | ||
417 | # Create content objects here for posting to blog. DELETEME. | ||
417 | 418 | if 'comments' in i: | |
418 | 419 | page['comments'] = i['comments'] | |
419 | 420 | else: | |
420 | 421 | contentobj = {} | |
421 | 422 | contentobj['type'] = i['elementtype'] | |
422 | contentobj['attr'] = {"language":i['lang'], "location":i['location'], "about":i['about'], "xpath":i['xpath']} | ||
423 | contentobj['attr'] = {"language": i['lang'], | ||
424 | "location": i['location'], | ||
425 | "about": i['about'], | ||
426 | "xpath": i['xpath']} | ||
423 | 427 | contentobj['data'] = i['data'] | |
424 | 428 | content.append(contentobj) | |
425 | 429 | i['bxpath'] = '' | |
… | … | ||
433 | 433 | page['name'] = "About " + content[0]['attr']['about'] | |
434 | 434 | page['content'] = content | |
435 | 435 | ||
436 | g.response_from_blogger = requests.api.post(conf.CUSTOM_BLOG_POST_URL[0], json.dumps(page), headers={"content-type":"application/json"}) | ||
436 | g.response_from_blogger = requests.api.post(conf.CUSTOM_BLOG_POST_URL[0], | ||
437 | json.dumps(page), | ||
438 | headers={"content-type": | ||
439 | "application/json"}) | ||
437 | 440 | print "response from blogger " + repr(g.response_from_blogger) | |
438 | 441 | sweet(data) | |
439 | 442 | reply = make_response() | |
… | … | ||
444 | 444 | ||
445 | 445 | ||
446 | 446 | def sweet(data): | |
447 | """ A function to sweet the data that is inserted. Accepts a <list of dicts>. """ | ||
447 | """ A function to sweet the data that is inserted. | ||
448 | Accepts a <list of dicts>. """ | ||
448 | 449 | for i in data: | |
449 | 450 | if 'type' in i: | |
450 | 451 | del(i['_id']) | |
451 | sweetmaker.sweet(conf.SWEET_STORE_ADD[0], [{"what":i['type'], "who":i['author'], "where":i['about']+i['xpath'], "how":conf.CUSTOM_BLOG_URL[0]+"/#"+g.response_from_blogger.json()['name']+' {lang: '+i["lang"]+',loc: '+i["location"]+'}'}]) | ||
452 | sweetmaker.sweet(conf.SWEET_STORE_ADD[0], | ||
453 | [{"what": i['type'], | ||
454 | "who": i['author'], | ||
455 | "where":i['about']+i['xpath'], | ||
456 | "how":{'blogUrl': '{0}/#{1}'.format( | ||
457 | conf.CUSTOM_BLOG_URL[0], | ||
458 | g.response_from_blogger.json()['name']), | ||
459 | 'language': i['lang'], | ||
460 | 'location': i['location']}}]) | ||
452 | 461 | return True | |
453 | 462 | # data = json.dumps(data) | |
454 | 463 | # req = requests.api.post(conf.SWEETURL[0]+"/add",{'data':data}) | |
… | … | ||
479 | 479 | reply = make_response() | |
480 | 480 | return reply | |
481 | 481 | ||
482 | @app.route("/menu",methods=['GET']) | ||
482 | |||
483 | @app.route("/menu", methods=['GET']) | ||
483 | 484 | def menuForDialog(): | |
484 | 485 | if request.args.has_key('option') == False: | |
485 | 486 | collection = g.db['post'] | |
486 | 487 | c = {} | |
487 | 488 | cntr = 0 | |
488 | 489 | print request.args['url'] | |
489 | for i in collection.find({"about":request.args['url']}).distinct('lang'): | ||
490 | for j in collection.find({"about":request.args['url'],'lang':i}).distinct('type'): | ||
490 | for i in collection.find({"about": | ||
491 | request.args['url']}).distinct('lang'): | ||
492 | for j in collection.find({"about": request.args['url'], | ||
493 | 'lang': i}).distinct('type'): | ||
491 | 494 | d = {} | |
492 | 495 | d['lang'] = i | |
493 | 496 | d['type'] = j | |
… | … | ||
502 | 502 | collection = g.db['post'] | |
503 | 503 | #get the ren languages for the received url | |
504 | 504 | langForUrl = collection.group( | |
505 | key = Code('function(doc){return {"about" : doc.about}}'), | ||
506 | condition={"about" : d['url'],"blog":{'$regex':'/'+d['option']+'.*/'}}, | ||
505 | key=Code('function(doc){return {"about" : doc.about}}'), | ||
506 | condition={"about": d['url'], "blog": {'$regex': | ||
507 | '/'+d['option']+'.*/'}}, | ||
507 | 508 | initial={'lang': []}, | |
508 | reduce=Code('function(doc, out){if (out.lang.indexOf(doc.lang) == -1) out.lang.push(doc.lang)}') #here xpath for test | ||
509 | reduce=Code('function(doc, out){' + | ||
510 | 'if (out.lang.indexOf(doc.lang) == -1)' + | ||
511 | 'out.lang.push(doc.lang)}') # here xpath for test | ||
509 | 512 | ) | |
510 | 513 | ||
511 | 514 | #send the response | |
… | … | ||
526 | 526 | url = request.args['url'] | |
527 | 527 | #all re-narrations of the same xpath are grouped | |
528 | 528 | query = collection.group( | |
529 | key = None, | ||
530 | condition={"about" :{'$regex':url+'*'}}, | ||
529 | key=None, | ||
530 | condition={"about": {'$regex': url+'*'}}, | ||
531 | 531 | initial={'narration': []}, | |
532 | 532 | reduce=Code('function(doc,out){out.narration.push(doc["about"]);}') | |
533 | 533 | ) | |
534 | 534 | ||
535 | string='' | ||
536 | if len(query)==0: | ||
537 | return jsonify({'0':'empty'}) | ||
535 | string = '' | ||
536 | if len(query) == 0: | ||
537 | return jsonify({'0': 'empty'}) | ||
538 | 538 | else: | |
539 | 539 | otherlist = {} | |
540 | 540 | cntr = -1 | |
… | … | ||
548 | 548 | return jsonify(otherlist) | |
549 | 549 | ||
550 | 550 | ||
551 | import logging,os | ||
551 | import logging | ||
552 | import os | ||
552 | 553 | from logging import FileHandler | |
553 | 554 | ||
554 | fil = FileHandler(os.path.join(os.path.dirname(__file__),'logme'),mode='a') | ||
555 | fil = FileHandler(os.path.join(os.path.dirname(__file__), 'logme'), mode='a') | ||
555 | 556 | fil.setLevel(logging.ERROR) | |
556 | 557 | app.logger.addHandler(fil) | |
557 | 558 |