04640f7 by Arvind at 2013-02-07 |
1 |
from flask import Flask |
|
2 |
from flask import request |
|
3 |
from flask import render_template |
265ce28 by Anon Ray at 2013-02-08 |
4 |
from flask import make_response |
d611971 by Arvind at 2013-03-26 |
5 |
from flask import jsonify |
e922d3b by Arvind at 2013-02-08 |
6 |
import logging |
8181191 by Arvind at 2013-02-08 |
7 |
from logging import FileHandler |
5c78a27 by Arvind at 2012-06-17 |
8 |
import pymongo |
5c5d01a by Arvind at 2013-03-27 |
9 |
import os |
d09d88d by Arvind at 2013-03-28 |
10 |
import lxml.html |
|
11 |
import urllib2 |
|
12 |
import StringIO |
c6d359e by Anon Ray at 2013-03-30 |
13 |
import json |
04640f7 by Arvind at 2013-02-07 |
14 |
|
|
15 |
app = Flask(__name__) |
|
16 |
|
a5c95bf by Arvind at 2013-03-28 |
17 |
|
b184f1f by Arvind at 2013-02-08 |
18 |
@app.route('/', methods=['GET']) |
04640f7 by Arvind at 2013-02-07 |
19 |
def index(): |
d611971 by Arvind at 2013-03-26 |
20 |
if request.args.has_key('url'): |
|
21 |
return render_template('index.html', url=request.args['url']) |
b184f1f by Arvind at 2013-02-08 |
22 |
else: |
|
23 |
return render_template('index.html') |
3018c67 by Arvind at 2013-02-08 |
24 |
|
a5c95bf by Arvind at 2013-03-28 |
25 |
|
|
26 |
@app.route('/fetch', methods=['GET']) |
d611971 by Arvind at 2013-03-26 |
27 |
def fetch(): |
|
28 |
connection = pymongo.Connection() |
|
29 |
db = connection['mural'] |
|
30 |
collection = db['data'] |
|
31 |
ret = {} |
|
32 |
x = 0 |
5c5d01a by Arvind at 2013-03-27 |
33 |
resource = "default" |
a5c95bf by Arvind at 2013-03-28 |
34 |
if request.args.has_key('uri'): |
|
35 |
resource = request.args['uri'] |
|
36 |
for i in collection.find({'uri':resource}): |
d611971 by Arvind at 2013-03-26 |
37 |
del(i['_id']) |
|
38 |
ret[x] = i |
|
39 |
x = x + 1 |
a5c95bf by Arvind at 2013-03-28 |
40 |
if len(ret) == 0: |
|
41 |
ret['error'] = "Sorry! No re-treats for you." |
5c5d01a by Arvind at 2013-03-27 |
42 |
return jsonify(ret) |
265ce28 by Anon Ray at 2013-02-08 |
43 |
|
a5c95bf by Arvind at 2013-03-28 |
44 |
|
|
45 |
@app.route('/submit', methods=['POST']) |
|
46 |
def submit(): |
|
47 |
c = pymongo.Connection() |
|
48 |
db = c['mural'] |
|
49 |
coll = db['data'] |
c6d359e by Anon Ray at 2013-03-30 |
50 |
for i in request.form['data']: |
|
51 |
coll.insert(i) |
|
52 |
response = make_response() |
|
53 |
response.status = "200 OK" |
|
54 |
return response |
a5c95bf by Arvind at 2013-03-28 |
55 |
|
c6d359e by Anon Ray at 2013-03-30 |
56 |
@app.route('/web', methods=['GET']) |
|
57 |
def web(): |
|
58 |
return render_template('web.html') |
a5c95bf by Arvind at 2013-03-28 |
59 |
|
|
60 |
@app.route('/SWeeText', methods=['GET']) |
d09d88d by Arvind at 2013-03-28 |
61 |
def SWeeText(): |
|
62 |
if request.args.has_key('url'): |
|
63 |
myhandler1 = urllib2.Request(request.args['url'], headers={'User-Agent': "Mozilla/5.0(X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11"}) |
|
64 |
a = urllib2.urlopen(myhandler1) |
|
65 |
page = a.read() |
|
66 |
a.close() |
|
67 |
try: |
a5c95bf by Arvind at 2013-03-28 |
68 |
page = unicode(page, 'utf-8') |
d09d88d by Arvind at 2013-03-28 |
69 |
except UnicodeDecodeError: |
|
70 |
pass |
|
71 |
root = lxml.html.parse(StringIO.StringIO(page)).getroot() |
|
72 |
root.make_links_absolute(request.args['url'], resolve_base_href = True) |
aa2260f by Anon Ray at 2013-03-28 |
73 |
|
|
74 |
# inject the JS toolbar to annotate text |
|
75 |
script = root.makeelement('script') |
|
76 |
script.set('src', 'static/text-annotation.js') |
c6d359e by Anon Ray at 2013-03-30 |
77 |
tree = root.makeelement('script') |
|
78 |
tree.set('src', 'static/tree.js') |
|
79 |
bs_js = root.makeelement('script') |
|
80 |
bs_js.set('src', 'static/bootstrap.js') |
|
81 |
jq = root.makeelement('script') |
|
82 |
jq.set('src', 'static/jquery-1.9.1.min.js') |
|
83 |
jit = root.makeelement('script') |
|
84 |
jit.set('src', 'static/jit.js') |
|
85 |
us = root.makeelement('script') |
|
86 |
us.set('src', 'static/underscore-min-1.4.4.js') |
|
87 |
|
aa2260f by Anon Ray at 2013-03-28 |
88 |
link = root.makeelement('link') |
|
89 |
link.set('href', 'static/text-annotation.css') |
|
90 |
link.set('type', 'text/css') |
|
91 |
link.set('rel', 'stylesheet') |
c6d359e by Anon Ray at 2013-03-30 |
92 |
bs = root.makeelement('link') |
|
93 |
bs.set('href', 'static/bootstrap.css') |
|
94 |
bs.set('type', 'text/css') |
|
95 |
bs.set('rel', 'stylesheet') |
|
96 |
tree_css = root.makeelement('link') |
|
97 |
tree_css.set('href', 'static/tree.css') |
|
98 |
tree_css.set('type', 'text/css') |
|
99 |
tree_css.set('rel', 'stylesheet') |
|
100 |
|
|
101 |
root.body.append(jq) |
|
102 |
root.body.append(bs_js) |
|
103 |
root.body.append(jit) |
|
104 |
root.body.append(us) |
|
105 |
root.body.append(tree) |
aa2260f by Anon Ray at 2013-03-28 |
106 |
root.body.append(script) |
c6d359e by Anon Ray at 2013-03-30 |
107 |
|
|
108 |
root.head.append(bs) |
aa2260f by Anon Ray at 2013-03-28 |
109 |
root.head.append(link) |
c6d359e by Anon Ray at 2013-03-30 |
110 |
root.head.append(tree_css) |
aa2260f by Anon Ray at 2013-03-28 |
111 |
|
d09d88d by Arvind at 2013-03-28 |
112 |
return lxml.html.tostring(root) |
aa2260f by Anon Ray at 2013-03-28 |
113 |
|
8181191 by Arvind at 2013-02-08 |
114 |
#Log the errors, don't depend on apache to log it for you. |
aa2260f by Anon Ray at 2013-03-28 |
115 |
fil = FileHandler(os.path.join(os.path.dirname(__file__), 'logme'),mode='a') |
b184f1f by Arvind at 2013-02-08 |
116 |
fil.setLevel(logging.ERROR) |
|
117 |
app.logger.addHandler(fil) |
8181191 by Arvind at 2013-02-08 |
118 |
|
265ce28 by Anon Ray at 2013-02-08 |
119 |
|
04640f7 by Arvind at 2013-02-07 |
120 |
if __name__ == "__main__": |
b184f1f by Arvind at 2013-02-08 |
121 |
app.run(debug=True, host='0.0.0.0') |
04640f7 by Arvind at 2013-02-07 |
122 |
|