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