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 |
04640f7 by Arvind at 2013-02-07 |
13 |
|
|
14 |
app = Flask(__name__) |
|
15 |
|
a5c95bf by Arvind at 2013-03-28 |
16 |
|
b184f1f by Arvind at 2013-02-08 |
17 |
@app.route('/', methods=['GET']) |
04640f7 by Arvind at 2013-02-07 |
18 |
def index(): |
d611971 by Arvind at 2013-03-26 |
19 |
if request.args.has_key('url'): |
|
20 |
return render_template('index.html', url=request.args['url']) |
b184f1f by Arvind at 2013-02-08 |
21 |
else: |
|
22 |
return render_template('index.html') |
3018c67 by Arvind at 2013-02-08 |
23 |
|
a5c95bf by Arvind at 2013-03-28 |
24 |
|
|
25 |
@app.route('/fetch', methods=['GET']) |
d611971 by Arvind at 2013-03-26 |
26 |
def fetch(): |
|
27 |
connection = pymongo.Connection() |
|
28 |
db = connection['mural'] |
|
29 |
collection = db['data'] |
|
30 |
ret = {} |
|
31 |
x = 0 |
5c5d01a by Arvind at 2013-03-27 |
32 |
resource = "default" |
a5c95bf by Arvind at 2013-03-28 |
33 |
if request.args.has_key('uri'): |
|
34 |
resource = request.args['uri'] |
|
35 |
for i in collection.find({'uri':resource}): |
d611971 by Arvind at 2013-03-26 |
36 |
del(i['_id']) |
|
37 |
ret[x] = i |
|
38 |
x = x + 1 |
a5c95bf by Arvind at 2013-03-28 |
39 |
if len(ret) == 0: |
|
40 |
ret['error'] = "Sorry! No re-treats for you." |
5c5d01a by Arvind at 2013-03-27 |
41 |
return jsonify(ret) |
265ce28 by Anon Ray at 2013-02-08 |
42 |
|
a5c95bf by Arvind at 2013-03-28 |
43 |
|
|
44 |
@app.route('/submit', methods=['POST']) |
|
45 |
def submit(): |
|
46 |
c = pymongo.Connection() |
|
47 |
db = c['mural'] |
|
48 |
coll = db['data'] |
|
49 |
try: |
|
50 |
for i in d: |
|
51 |
coll.insert(request.args['data']) |
|
52 |
response = make_response() |
|
53 |
response.status = '200 OK' |
|
54 |
response.status_code = 200 |
|
55 |
return response |
|
56 |
except: |
|
57 |
response = make_response() |
|
58 |
response.status = "500" |
|
59 |
respose.data = "Your post could not be saved. Try posting again." |
|
60 |
return response |
|
61 |
|
|
62 |
|
|
63 |
@app.route('/SWeeText', methods=['GET']) |
d09d88d by Arvind at 2013-03-28 |
64 |
def SWeeText(): |
|
65 |
if request.args.has_key('url'): |
|
66 |
myhandler1 = urllib2.Request(request.args['url'], headers={'User-Agent': "Mozilla/5.0(X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11"}) |
|
67 |
a = urllib2.urlopen(myhandler1) |
|
68 |
page = a.read() |
|
69 |
a.close() |
|
70 |
try: |
a5c95bf by Arvind at 2013-03-28 |
71 |
page = unicode(page, 'utf-8') |
d09d88d by Arvind at 2013-03-28 |
72 |
except UnicodeDecodeError: |
|
73 |
pass |
|
74 |
root = lxml.html.parse(StringIO.StringIO(page)).getroot() |
|
75 |
root.make_links_absolute(request.args['url'], resolve_base_href = True) |
|
76 |
return lxml.html.tostring(root) |
8181191 by Arvind at 2013-02-08 |
77 |
#Log the errors, don't depend on apache to log it for you. |
a5c95bf by Arvind at 2013-03-28 |
78 |
fil = FileHandler(os.path.join(os.path.dirname(__file__), 'logme'), mode='a') |
b184f1f by Arvind at 2013-02-08 |
79 |
fil.setLevel(logging.ERROR) |
|
80 |
app.logger.addHandler(fil) |
8181191 by Arvind at 2013-02-08 |
81 |
|
265ce28 by Anon Ray at 2013-02-08 |
82 |
|
04640f7 by Arvind at 2013-02-07 |
83 |
if __name__ == "__main__": |
b184f1f by Arvind at 2013-02-08 |
84 |
app.run(debug=True, host='0.0.0.0') |
04640f7 by Arvind at 2013-02-07 |
85 |
|