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 |
|
16 |
app = Flask(__name__) |
17 |
|
18 |
|
19 |
@app.route('/', methods=['GET']) |
20 |
def index(): |
21 |
if request.args.has_key('url'): |
22 |
return render_template('index.html', url=request.args['url']) |
23 |
else: |
24 |
return render_template('index.html') |
25 |
|
26 |
|
27 |
@app.route('/fetch', methods=['GET']) |
28 |
def fetch(): |
29 |
connection = pymongo.Connection() |
30 |
db = connection['mural'] |
31 |
collection = db['data'] |
32 |
ret = {} |
33 |
x = 0 |
34 |
resource = "default" |
35 |
if request.args.has_key('uri'): |
36 |
resource = request.args['uri'] |
37 |
for i in collection.find({'uri':resource}): |
38 |
del(i['_id']) |
39 |
ret[x] = i |
40 |
x = x + 1 |
41 |
if len(ret) == 0: |
42 |
ret['error'] = "Sorry! No re-treats for you." |
43 |
return jsonify(ret) |
44 |
|
45 |
|
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 = {} |
53 |
for i in collection.find(): |
54 |
try: |
55 |
if request.args['data'] in i['nodes']: |
56 |
del(i['_id']) |
57 |
ret[y] = i |
58 |
y = y + 1 |
59 |
except: |
60 |
pass |
61 |
return render_template('blank.html', content = ret) |
62 |
@app.route('/submit', methods=['POST']) |
63 |
def submit(): |
64 |
c = pymongo.Connection() |
65 |
db = c['mural'] |
66 |
coll = db['data'] |
67 |
requestData = json.loads(request.form['data']) |
68 |
try: |
69 |
for i in requestData: |
70 |
coll.insert(i) |
71 |
response = make_response() |
72 |
response.status = '200 OK' |
73 |
response.status_code = 200 |
74 |
return response |
75 |
except: |
76 |
response = make_response() |
77 |
response.status = "500" |
78 |
response.data = "Your post could not be saved. Try posting again." |
79 |
return response |
80 |
|
81 |
|
82 |
@app.route('/SWeeText', methods=['GET']) |
83 |
def SWeeText(): |
84 |
if request.args.has_key('url'): |
85 |
myhandler1 = urllib2.Request(request.args['url'], headers={'User-Agent': "Mozilla/5.0(X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11"}) |
86 |
a = urllib2.urlopen(myhandler1) |
87 |
page = a.read() |
88 |
a.close() |
89 |
try: |
90 |
page = unicode(page, 'utf-8') |
91 |
except UnicodeDecodeError: |
92 |
pass |
93 |
root = lxml.html.parse(StringIO.StringIO(page)).getroot() |
94 |
root.make_links_absolute(request.args['url'], resolve_base_href = True) |
95 |
|
96 |
# inject the JS toolbar to annotate text |
97 |
script = root.makeelement('script') |
98 |
script.set('src', 'static/text-annotation.js') |
99 |
link = root.makeelement('link') |
100 |
link.set('href', 'static/text-annotation.css') |
101 |
link.set('type', 'text/css') |
102 |
link.set('rel', 'stylesheet') |
103 |
|
104 |
root.body.append(script) |
105 |
root.head.append(link) |
106 |
|
107 |
return lxml.html.tostring(root) |
108 |
|
109 |
|
110 |
#Log the errors, don't depend on apache to log it for you. |
111 |
fil = FileHandler(os.path.join(os.path.dirname(__file__), 'logme'),mode='a') |
112 |
fil.setLevel(logging.ERROR) |
113 |
app.logger.addHandler(fil) |
114 |
|
115 |
|
116 |
if __name__ == "__main__": |
117 |
app.run(debug=True, host='0.0.0.0') |