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