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