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 |
11e0e84 by Arvind at 2013-04-16 |
14 |
import urllib |
04640f7 by Arvind at 2013-02-07 |
15 |
|
|
16 |
app = Flask(__name__) |
|
17 |
|
a5c95bf by Arvind at 2013-03-28 |
18 |
|
b184f1f by Arvind at 2013-02-08 |
19 |
@app.route('/', methods=['GET']) |
04640f7 by Arvind at 2013-02-07 |
20 |
def index(): |
d611971 by Arvind at 2013-03-26 |
21 |
if request.args.has_key('url'): |
|
22 |
return render_template('index.html', url=request.args['url']) |
b184f1f by Arvind at 2013-02-08 |
23 |
else: |
|
24 |
return render_template('index.html') |
3018c67 by Arvind at 2013-02-08 |
25 |
|
a5c95bf by Arvind at 2013-03-28 |
26 |
|
|
27 |
@app.route('/fetch', methods=['GET']) |
d611971 by Arvind at 2013-03-26 |
28 |
def fetch(): |
|
29 |
connection = pymongo.Connection() |
|
30 |
db = connection['mural'] |
|
31 |
collection = db['data'] |
|
32 |
ret = {} |
|
33 |
x = 0 |
5c5d01a by Arvind at 2013-03-27 |
34 |
resource = "default" |
a5c95bf by Arvind at 2013-03-28 |
35 |
if request.args.has_key('uri'): |
|
36 |
resource = request.args['uri'] |
11e0e84 by Arvind at 2013-04-16 |
37 |
for i in collection.find({'resource':resource}): |
d611971 by Arvind at 2013-03-26 |
38 |
del(i['_id']) |
|
39 |
ret[x] = i |
|
40 |
x = x + 1 |
a5c95bf by Arvind at 2013-03-28 |
41 |
if len(ret) == 0: |
|
42 |
ret['error'] = "Sorry! No re-treats for you." |
5c5d01a by Arvind at 2013-03-27 |
43 |
return jsonify(ret) |
265ce28 by Anon Ray at 2013-02-08 |
44 |
|
a5c95bf by Arvind at 2013-03-28 |
45 |
|
438fa91 by Arvind at 2013-03-30 |
46 |
@app.route('/search', methods=['GET']) |
|
47 |
def search(): |
|
48 |
connection = pymongo.Connection() |
|
49 |
db = connection['mural'] |
|
50 |
collection = db['data'] |
|
51 |
y = 0 |
|
52 |
ret = {} |
7d19e0d by Arvind at 2013-03-30 |
53 |
keywords_dict = json.loads(request.args['data']) |
11e0e84 by Arvind at 2013-04-16 |
54 |
#keywords = json.loads(keywords_dict)['data'] |
438fa91 by Arvind at 2013-03-30 |
55 |
for i in collection.find(): |
11e0e84 by Arvind at 2013-04-16 |
56 |
for keyword in keywords_dict: |
7d19e0d by Arvind at 2013-03-30 |
57 |
try: |
|
58 |
if keyword in i['nodes']: |
|
59 |
del(i['_id']) |
11e0e84 by Arvind at 2013-04-16 |
60 |
i['text'] = urllib.unquote_plus(i['text']) |
7d19e0d by Arvind at 2013-03-30 |
61 |
ret[y] = i |
|
62 |
y = y + 1 |
|
63 |
except: |
|
64 |
pass |
438fa91 by Arvind at 2013-03-30 |
65 |
return render_template('blank.html', content = ret) |
7d19e0d by Arvind at 2013-03-30 |
66 |
|
|
67 |
|
a5c95bf by Arvind at 2013-03-28 |
68 |
@app.route('/submit', methods=['POST']) |
|
69 |
def submit(): |
|
70 |
c = pymongo.Connection() |
|
71 |
db = c['mural'] |
|
72 |
coll = db['data'] |
438fa91 by Arvind at 2013-03-30 |
73 |
requestData = json.loads(request.form['data']) |
a5c95bf by Arvind at 2013-03-28 |
74 |
try: |
438fa91 by Arvind at 2013-03-30 |
75 |
for i in requestData: |
|
76 |
coll.insert(i) |
|
77 |
response = make_response() |
323b73b by Anon Ray at 2013-03-30 |
78 |
response.headers['Access-Control-Allow-Origin'] = '*' |
438fa91 by Arvind at 2013-03-30 |
79 |
response.status = '200 OK' |
|
80 |
response.status_code = 200 |
|
81 |
return response |
a5c95bf by Arvind at 2013-03-28 |
82 |
except: |
|
83 |
response = make_response() |
|
84 |
response.status = "500" |
aa2260f by Anon Ray at 2013-03-28 |
85 |
response.data = "Your post could not be saved. Try posting again." |
a5c95bf by Arvind at 2013-03-28 |
86 |
return response |
|
87 |
|
c6d359e by Anon Ray at 2013-03-30 |
88 |
@app.route('/web', methods=['GET']) |
|
89 |
def web(): |
|
90 |
return render_template('web.html') |
a5c95bf by Arvind at 2013-03-28 |
91 |
|
|
92 |
@app.route('/SWeeText', methods=['GET']) |
d09d88d by Arvind at 2013-03-28 |
93 |
def SWeeText(): |
|
94 |
if request.args.has_key('url'): |
|
95 |
myhandler1 = urllib2.Request(request.args['url'], headers={'User-Agent': "Mozilla/5.0(X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11"}) |
|
96 |
a = urllib2.urlopen(myhandler1) |
|
97 |
page = a.read() |
|
98 |
a.close() |
|
99 |
try: |
a5c95bf by Arvind at 2013-03-28 |
100 |
page = unicode(page, 'utf-8') |
d09d88d by Arvind at 2013-03-28 |
101 |
except UnicodeDecodeError: |
|
102 |
pass |
|
103 |
root = lxml.html.parse(StringIO.StringIO(page)).getroot() |
|
104 |
root.make_links_absolute(request.args['url'], resolve_base_href = True) |
aa2260f by Anon Ray at 2013-03-28 |
105 |
|
|
106 |
# inject the JS toolbar to annotate text |
|
107 |
script = root.makeelement('script') |
|
108 |
script.set('src', 'static/text-annotation.js') |
c6d359e by Anon Ray at 2013-03-30 |
109 |
tree = root.makeelement('script') |
|
110 |
tree.set('src', 'static/tree.js') |
|
111 |
bs_js = root.makeelement('script') |
|
112 |
bs_js.set('src', 'static/bootstrap.js') |
|
113 |
jq = root.makeelement('script') |
|
114 |
jq.set('src', 'static/jquery-1.9.1.min.js') |
|
115 |
jit = root.makeelement('script') |
|
116 |
jit.set('src', 'static/jit.js') |
|
117 |
us = root.makeelement('script') |
|
118 |
us.set('src', 'static/underscore-min-1.4.4.js') |
|
119 |
|
aa2260f by Anon Ray at 2013-03-28 |
120 |
link = root.makeelement('link') |
|
121 |
link.set('href', 'static/text-annotation.css') |
|
122 |
link.set('type', 'text/css') |
|
123 |
link.set('rel', 'stylesheet') |
c6d359e by Anon Ray at 2013-03-30 |
124 |
bs = root.makeelement('link') |
|
125 |
bs.set('href', 'static/bootstrap.css') |
|
126 |
bs.set('type', 'text/css') |
|
127 |
bs.set('rel', 'stylesheet') |
|
128 |
tree_css = root.makeelement('link') |
|
129 |
tree_css.set('href', 'static/tree.css') |
|
130 |
tree_css.set('type', 'text/css') |
|
131 |
tree_css.set('rel', 'stylesheet') |
|
132 |
|
|
133 |
root.body.append(jq) |
|
134 |
root.body.append(bs_js) |
|
135 |
root.body.append(jit) |
|
136 |
root.body.append(us) |
|
137 |
root.body.append(tree) |
aa2260f by Anon Ray at 2013-03-28 |
138 |
root.body.append(script) |
c6d359e by Anon Ray at 2013-03-30 |
139 |
|
|
140 |
root.head.append(bs) |
aa2260f by Anon Ray at 2013-03-28 |
141 |
root.head.append(link) |
c6d359e by Anon Ray at 2013-03-30 |
142 |
root.head.append(tree_css) |
aa2260f by Anon Ray at 2013-03-28 |
143 |
|
d09d88d by Arvind at 2013-03-28 |
144 |
return lxml.html.tostring(root) |
aa2260f by Anon Ray at 2013-03-28 |
145 |
|
438fa91 by Arvind at 2013-03-30 |
146 |
|
8181191 by Arvind at 2013-02-08 |
147 |
#Log the errors, don't depend on apache to log it for you. |
aa2260f by Anon Ray at 2013-03-28 |
148 |
fil = FileHandler(os.path.join(os.path.dirname(__file__), 'logme'),mode='a') |
b184f1f by Arvind at 2013-02-08 |
149 |
fil.setLevel(logging.ERROR) |
|
150 |
app.logger.addHandler(fil) |
8181191 by Arvind at 2013-02-08 |
151 |
|
265ce28 by Anon Ray at 2013-02-08 |
152 |
|
04640f7 by Arvind at 2013-02-07 |
153 |
if __name__ == "__main__": |
b184f1f by Arvind at 2013-02-08 |
154 |
app.run(debug=True, host='0.0.0.0') |
04640f7 by Arvind at 2013-02-07 |
155 |
|