04640f7 by Arvind at 2013-02-07 |
1 |
from flask import Flask |
|
2 |
from flask import request |
d786060 by Anon Ray at 2013-07-09 |
3 |
from flask import render_template, url_for |
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 |
|
3a2f834 by Anon Ray at 2013-07-09 |
16 |
import conf |
|
17 |
import sweetmaker |
|
18 |
|
04640f7 by Arvind at 2013-02-07 |
19 |
app = Flask(__name__) |
|
20 |
|
3a2f834 by Anon Ray at 2013-07-09 |
21 |
SWEET_STORE_URL = conf.SWEET_STORE_URL |
|
22 |
|
a5c95bf by Arvind at 2013-03-28 |
23 |
|
b184f1f by Arvind at 2013-02-08 |
24 |
@app.route('/', methods=['GET']) |
04640f7 by Arvind at 2013-02-07 |
25 |
def index(): |
d611971 by Arvind at 2013-03-26 |
26 |
if request.args.has_key('url'): |
|
27 |
return render_template('index.html', url=request.args['url']) |
b184f1f by Arvind at 2013-02-08 |
28 |
else: |
|
29 |
return render_template('index.html') |
3018c67 by Arvind at 2013-02-08 |
30 |
|
a5c95bf by Arvind at 2013-03-28 |
31 |
|
|
32 |
@app.route('/fetch', methods=['GET']) |
d611971 by Arvind at 2013-03-26 |
33 |
def fetch(): |
|
34 |
connection = pymongo.Connection() |
|
35 |
db = connection['mural'] |
|
36 |
collection = db['data'] |
|
37 |
ret = {} |
|
38 |
x = 0 |
5c5d01a by Arvind at 2013-03-27 |
39 |
resource = "default" |
a5c95bf by Arvind at 2013-03-28 |
40 |
if request.args.has_key('uri'): |
|
41 |
resource = request.args['uri'] |
031803b by Arvind at 2013-04-16 |
42 |
for i in collection.find({'uri':resource}): |
d611971 by Arvind at 2013-03-26 |
43 |
del(i['_id']) |
|
44 |
ret[x] = i |
|
45 |
x = x + 1 |
031803b by Arvind at 2013-04-16 |
46 |
else: |
|
47 |
for i in collection.find(): |
|
48 |
del(i['_id']) |
|
49 |
ret[x] = i |
|
50 |
x = x + 1 |
a5c95bf by Arvind at 2013-03-28 |
51 |
if len(ret) == 0: |
|
52 |
ret['error'] = "Sorry! No re-treats for you." |
5c5d01a by Arvind at 2013-03-27 |
53 |
return jsonify(ret) |
265ce28 by Anon Ray at 2013-02-08 |
54 |
|
031803b by Arvind at 2013-04-16 |
55 |
@app.route('/sweets', methods=['GET']) |
|
56 |
def displaySweet(): |
|
57 |
return render_template('sweets.html') |
a5c95bf by Arvind at 2013-03-28 |
58 |
|
438fa91 by Arvind at 2013-03-30 |
59 |
@app.route('/search', methods=['GET']) |
|
60 |
def search(): |
|
61 |
connection = pymongo.Connection() |
|
62 |
db = connection['mural'] |
|
63 |
collection = db['data'] |
|
64 |
y = 0 |
|
65 |
ret = {} |
7d19e0d by Arvind at 2013-03-30 |
66 |
keywords_dict = json.loads(request.args['data']) |
11e0e84 by Arvind at 2013-04-16 |
67 |
#keywords = json.loads(keywords_dict)['data'] |
3a2f834 by Anon Ray at 2013-07-09 |
68 |
if 'nodes' in keywords_dict: |
|
69 |
for i in collection.find(): |
7d19e0d by Arvind at 2013-03-30 |
70 |
try: |
3a2f834 by Anon Ray at 2013-07-09 |
71 |
if 'how' in i: |
|
72 |
i['nodes'] = i['how'] |
7d19e0d by Arvind at 2013-03-30 |
73 |
except: |
|
74 |
pass |
3a2f834 by Anon Ray at 2013-07-09 |
75 |
for node in keywords_dict['nodes']: |
|
76 |
try: |
|
77 |
if node in i['nodes']: |
|
78 |
del(i['_id']) |
|
79 |
ret[y] = i |
|
80 |
y = y + 1 |
|
81 |
except: |
|
82 |
pass |
|
83 |
elif 'where' in keywords_dict: |
|
84 |
for i in collection.find({'uri': keywords_dict['where']}): |
|
85 |
del(i['_id']) |
|
86 |
ret[y] = i |
|
87 |
y = y + 1 |
|
88 |
for i in collection.find({'where': {'$regex':\ |
|
89 |
keywords_dict['where']}}): |
|
90 |
del(i['_id']) |
|
91 |
ret[y] = i |
|
92 |
y = y + 1 |
10b1159 by Arvind at 2013-07-10 |
93 |
return render_template('blank.html', content = ret, flag = request.args['flag']) |
7d19e0d by Arvind at 2013-03-30 |
94 |
|
|
95 |
|
a5c95bf by Arvind at 2013-03-28 |
96 |
@app.route('/submit', methods=['POST']) |
|
97 |
def submit(): |
|
98 |
c = pymongo.Connection() |
|
99 |
db = c['mural'] |
|
100 |
coll = db['data'] |
438fa91 by Arvind at 2013-03-30 |
101 |
requestData = json.loads(request.form['data']) |
a5c95bf by Arvind at 2013-03-28 |
102 |
try: |
438fa91 by Arvind at 2013-03-30 |
103 |
for i in requestData: |
|
104 |
coll.insert(i) |
3a2f834 by Anon Ray at 2013-07-09 |
105 |
print 'inserted' |
|
106 |
print requestData |
438fa91 by Arvind at 2013-03-30 |
107 |
response = make_response() |
323b73b by Anon Ray at 2013-03-30 |
108 |
response.headers['Access-Control-Allow-Origin'] = '*' |
438fa91 by Arvind at 2013-03-30 |
109 |
response.status_code = 200 |
3a2f834 by Anon Ray at 2013-07-09 |
110 |
for i in requestData: |
|
111 |
del(i['_id']) |
|
112 |
i['how'] = '{concepts: ' + ', '.join(i['how']) + '}' |
|
113 |
#i['how'] = attribs |
|
114 |
print 'payload for sweet' |
|
115 |
print requestData |
|
116 |
sweetmaker.sweet(SWEET_STORE_URL, requestData) |
a5c95bf by Arvind at 2013-03-28 |
117 |
except: |
|
118 |
response = make_response() |
3a2f834 by Anon Ray at 2013-07-09 |
119 |
response.status_code = 500 |
aa2260f by Anon Ray at 2013-03-28 |
120 |
response.data = "Your post could not be saved. Try posting again." |
3a2f834 by Anon Ray at 2013-07-09 |
121 |
|
|
122 |
return response |
|
123 |
|
a5c95bf by Arvind at 2013-03-28 |
124 |
|
4dcdc24 by Anon Ray at 2013-06-14 |
125 |
@app.route('/web/', methods=['GET']) |
c6d359e by Anon Ray at 2013-03-30 |
126 |
def web(): |
|
127 |
return render_template('web.html') |
a5c95bf by Arvind at 2013-03-28 |
128 |
|
|
129 |
@app.route('/SWeeText', methods=['GET']) |
d09d88d by Arvind at 2013-03-28 |
130 |
def SWeeText(): |
|
131 |
if request.args.has_key('url'): |
d1dac3f by Anon Ray at 2013-04-16 |
132 |
# Log -- comment them |
|
133 |
print "Got URL " + request.args['url'] + " .. Fetching and Parsing.." |
|
134 |
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 |
135 |
a = urllib2.urlopen(myhandler1) |
|
136 |
page = a.read() |
|
137 |
a.close() |
|
138 |
try: |
a5c95bf by Arvind at 2013-03-28 |
139 |
page = unicode(page, 'utf-8') |
d09d88d by Arvind at 2013-03-28 |
140 |
except UnicodeDecodeError: |
|
141 |
pass |
|
142 |
root = lxml.html.parse(StringIO.StringIO(page)).getroot() |
|
143 |
root.make_links_absolute(request.args['url'], resolve_base_href = True) |
d1dac3f by Anon Ray at 2013-04-16 |
144 |
# Log -- comment them |
d786060 by Anon Ray at 2013-07-09 |
145 |
#print "Page parsed.. Preparing to send.." |
aa2260f by Anon Ray at 2013-03-28 |
146 |
|
|
147 |
# inject the JS toolbar to annotate text |
d1dac3f by Anon Ray at 2013-04-16 |
148 |
jq = root.makeelement('script') |
d786060 by Anon Ray at 2013-07-09 |
149 |
jq.set('src', url_for('static', filename='jquery-1.9.1.min.js')) |
d1dac3f by Anon Ray at 2013-04-16 |
150 |
|
aa2260f by Anon Ray at 2013-03-28 |
151 |
script = root.makeelement('script') |
d786060 by Anon Ray at 2013-07-09 |
152 |
script.set('src', url_for('static', filename='text-annotation.js')) |
d1dac3f by Anon Ray at 2013-04-16 |
153 |
|
a182289 by Arvind at 2013-07-10 |
154 |
|
|
155 |
config_script = root.makeelement('script') |
|
156 |
config_script.set('src', url_for('static', filename='config.js')) |
|
157 |
|
c6d359e by Anon Ray at 2013-03-30 |
158 |
tree = root.makeelement('script') |
d786060 by Anon Ray at 2013-07-09 |
159 |
tree.set('src', url_for('static', filename='tree.js')) |
d1dac3f by Anon Ray at 2013-04-16 |
160 |
|
c6d359e by Anon Ray at 2013-03-30 |
161 |
bs_js = root.makeelement('script') |
d786060 by Anon Ray at 2013-07-09 |
162 |
bs_js.set('src', url_for('static', filename='bootstrap.js')) |
031803b by Arvind at 2013-04-16 |
163 |
|
c6d359e by Anon Ray at 2013-03-30 |
164 |
jit = root.makeelement('script') |
d786060 by Anon Ray at 2013-07-09 |
165 |
jit.set('src', url_for('static', filename='jit.js')) |
d1dac3f by Anon Ray at 2013-04-16 |
166 |
|
c6d359e by Anon Ray at 2013-03-30 |
167 |
us = root.makeelement('script') |
d786060 by Anon Ray at 2013-07-09 |
168 |
us.set('src', url_for('static', filename='underscore-min-1.4.4.js')) |
c6d359e by Anon Ray at 2013-03-30 |
169 |
|
aa2260f by Anon Ray at 2013-03-28 |
170 |
link = root.makeelement('link') |
d786060 by Anon Ray at 2013-07-09 |
171 |
link.set('href', url_for('static', filename='text-annotation.css')) |
aa2260f by Anon Ray at 2013-03-28 |
172 |
link.set('type', 'text/css') |
|
173 |
link.set('rel', 'stylesheet') |
d1dac3f by Anon Ray at 2013-04-16 |
174 |
|
c6d359e by Anon Ray at 2013-03-30 |
175 |
bs = root.makeelement('link') |
d786060 by Anon Ray at 2013-07-09 |
176 |
bs.set('href', url_for('static', filename='bootstrap.css')) |
c6d359e by Anon Ray at 2013-03-30 |
177 |
bs.set('type', 'text/css') |
|
178 |
bs.set('rel', 'stylesheet') |
d1dac3f by Anon Ray at 2013-04-16 |
179 |
|
c6d359e by Anon Ray at 2013-03-30 |
180 |
tree_css = root.makeelement('link') |
d786060 by Anon Ray at 2013-07-09 |
181 |
tree_css.set('href', url_for('static', filename='tree.css')) |
c6d359e by Anon Ray at 2013-03-30 |
182 |
tree_css.set('type', 'text/css') |
|
183 |
tree_css.set('rel', 'stylesheet') |
|
184 |
|
|
185 |
root.head.append(bs) |
aa2260f by Anon Ray at 2013-03-28 |
186 |
root.head.append(link) |
c6d359e by Anon Ray at 2013-03-30 |
187 |
root.head.append(tree_css) |
aa2260f by Anon Ray at 2013-03-28 |
188 |
|
d1dac3f by Anon Ray at 2013-04-16 |
189 |
root.head.append(jq) |
|
190 |
root.head.append(bs_js) |
|
191 |
root.head.append(jit) |
|
192 |
root.head.append(us) |
|
193 |
root.head.append(tree) |
|
194 |
root.head.append(script) |
a182289 by Arvind at 2013-07-10 |
195 |
root.head.append(config_script) |
d1dac3f by Anon Ray at 2013-04-16 |
196 |
|
d09d88d by Arvind at 2013-03-28 |
197 |
return lxml.html.tostring(root) |
aa2260f by Anon Ray at 2013-03-28 |
198 |
|
438fa91 by Arvind at 2013-03-30 |
199 |
|
8181191 by Arvind at 2013-02-08 |
200 |
#Log the errors, don't depend on apache to log it for you. |
aa2260f by Anon Ray at 2013-03-28 |
201 |
fil = FileHandler(os.path.join(os.path.dirname(__file__), 'logme'),mode='a') |
b184f1f by Arvind at 2013-02-08 |
202 |
fil.setLevel(logging.ERROR) |
|
203 |
app.logger.addHandler(fil) |
8181191 by Arvind at 2013-02-08 |
204 |
|
265ce28 by Anon Ray at 2013-02-08 |
205 |
|
04640f7 by Arvind at 2013-02-07 |
206 |
if __name__ == "__main__": |
b184f1f by Arvind at 2013-02-08 |
207 |
app.run(debug=True, host='0.0.0.0') |
04640f7 by Arvind at 2013-02-07 |
208 |
|