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
14
app = Flask(__name__)
15
16
@app.route('/', methods=['GET'])
17
def index():
18
    if request.args.has_key('url'):
19
        return render_template('index.html', url=request.args['url'])
20
    else:
21
        return render_template('index.html')
22
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
30
    resource = "default"
31
    if request.args.has_key('res'):
32
        resource = request.args['res']
33
    for i in collection.find({'res':resource}):
34
        del(i['_id'])
35
        ret[x] = i
36
        x = x + 1
37
    return jsonify(ret)
38
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)
53
#Log the errors, don't depend on apache to log it for you.
54
    fil = FileHandler(os.path.join(os.path.dirname(__file__), 'logme'),mode='a')
55
    fil.setLevel(logging.ERROR)
56
    app.logger.addHandler(fil)
57
58
59
if __name__ == "__main__":
60
    app.run(debug=True, host='0.0.0.0')