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
b184f1f by Arvind at 2013-02-08 16
@app.route('/', methods=['GET'])
04640f7 by Arvind at 2013-02-07 17
def index():
d611971 by Arvind at 2013-03-26 18
    if request.args.has_key('url'):
19
        return render_template('index.html', url=request.args['url'])
b184f1f by Arvind at 2013-02-08 20
    else:
21
        return render_template('index.html')
3018c67 by Arvind at 2013-02-08 22
d611971 by Arvind at 2013-03-26 23
@app.route('/fetch',methods=['GET'])
24
def fetch():
25
    connection = pymongo.Connection()
26
    db = connection['mural']
27
    collection = db['data']
28
    ret = {}
29
    x = 0
5c5d01a by Arvind at 2013-03-27 30
    resource = "default"
31
    if request.args.has_key('res'):
32
        resource = request.args['res']
33
    for i in collection.find({'res':resource}):
d611971 by Arvind at 2013-03-26 34
        del(i['_id'])
35
        ret[x] = i
36
        x = x + 1
5c5d01a by Arvind at 2013-03-27 37
    return jsonify(ret)
265ce28 by Anon Ray at 2013-02-08 38
d09d88d by Arvind at 2013-03-28 39
@app.route('/SWeeText',methods=['GET'])
40
def SWeeText():
41
    if request.args.has_key('url'):
42
        myhandler1 = urllib2.Request(request.args['url'], headers={'User-Agent': "Mozilla/5.0(X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11"})
43
        a = urllib2.urlopen(myhandler1)
44
        page = a.read()
45
        a.close()
46
        try:
47
            page = unicode(page,'utf-8')
48
        except UnicodeDecodeError:
49
            pass
50
        root = lxml.html.parse(StringIO.StringIO(page)).getroot()
51
        root.make_links_absolute(request.args['url'], resolve_base_href = True)
52
        return lxml.html.tostring(root)
8181191 by Arvind at 2013-02-08 53
#Log the errors, don't depend on apache to log it for you.
b184f1f by Arvind at 2013-02-08 54
    fil = FileHandler(os.path.join(os.path.dirname(__file__), 'logme'),mode='a')
55
    fil.setLevel(logging.ERROR)
56
    app.logger.addHandler(fil)
8181191 by Arvind at 2013-02-08 57
265ce28 by Anon Ray at 2013-02-08 58
04640f7 by Arvind at 2013-02-07 59
if __name__ == "__main__":
b184f1f by Arvind at 2013-02-08 60
    app.run(debug=True, host='0.0.0.0')
04640f7 by Arvind at 2013-02-07 61