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