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
    for i in collection.find():
53
        try:
54
            if request.args['data'] in i['nodes']:
55
                del(i['_id'])
56
                ret[y] = i
57
                y = y + 1
58
        except:
59
            pass
60
    return render_template('blank.html', content = ret)
61
@app.route('/submit', methods=['POST'])
62
def submit():
63
    c = pymongo.Connection()
64
    db = c['mural']
65
    coll = db['data']
66
    requestData = json.loads(request.form['data'])
67
    try:
68
        for i in requestData:
69
            coll.insert(i)
70
        response = make_response()
71
        response.headers['Access-Control-Allow-Origin'] = '*'
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
@app.route('/web', methods=['GET'])
82
def web():
83
  return render_template('web.html')
84
85
@app.route('/SWeeText', methods=['GET'])
86
def SWeeText():
87
    if request.args.has_key('url'):
88
        myhandler1 = urllib2.Request(request.args['url'], headers={'User-Agent': "Mozilla/5.0(X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11"})
89
        a = urllib2.urlopen(myhandler1)
90
        page = a.read()
91
        a.close()
92
        try:
93
            page = unicode(page, 'utf-8')
94
        except UnicodeDecodeError:
95
            pass
96
        root = lxml.html.parse(StringIO.StringIO(page)).getroot()
97
        root.make_links_absolute(request.args['url'], resolve_base_href = True)
98
99
        # inject the JS toolbar to annotate text
100
        script = root.makeelement('script')
101
        script.set('src', 'static/text-annotation.js')
102
        tree = root.makeelement('script')
103
        tree.set('src', 'static/tree.js')
104
        bs_js = root.makeelement('script')
105
        bs_js.set('src', 'static/bootstrap.js')
106
        jq = root.makeelement('script')
107
        jq.set('src', 'static/jquery-1.9.1.min.js')
108
        jit = root.makeelement('script')
109
        jit.set('src', 'static/jit.js')
110
        us = root.makeelement('script')
111
        us.set('src', 'static/underscore-min-1.4.4.js')
112
113
        link = root.makeelement('link')
114
        link.set('href', 'static/text-annotation.css')
115
        link.set('type', 'text/css')
116
        link.set('rel', 'stylesheet')
117
        bs = root.makeelement('link')
118
        bs.set('href', 'static/bootstrap.css')
119
        bs.set('type', 'text/css')
120
        bs.set('rel', 'stylesheet')
121
        tree_css = root.makeelement('link')
122
        tree_css.set('href', 'static/tree.css')
123
        tree_css.set('type', 'text/css')
124
        tree_css.set('rel', 'stylesheet')
125
126
        root.body.append(jq)
127
        root.body.append(bs_js)
128
        root.body.append(jit)
129
        root.body.append(us)
130
        root.body.append(tree)
131
        root.body.append(script)
132
133
        root.head.append(bs)
134
        root.head.append(link)
135
        root.head.append(tree_css)
136
137
        return lxml.html.tostring(root)
138
139
140
#Log the errors, don't depend on apache to log it for you.
141
    fil = FileHandler(os.path.join(os.path.dirname(__file__), 'logme'),mode='a')
142
    fil.setLevel(logging.ERROR)
143
    app.logger.addHandler(fil)
144
145
146
if __name__ == "__main__":
147
    app.run(debug=True, host='0.0.0.0')