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
import urllib
15
16
app = Flask(__name__)
17
18
19
@app.route('/', methods=['GET'])
20
def index():
21
    if request.args.has_key('url'):
22
        return render_template('index.html', url=request.args['url'])
23
    else:
24
        return render_template('index.html')
25
26
27
@app.route('/fetch', methods=['GET'])
28
def fetch():
29
    connection = pymongo.Connection()
30
    db = connection['mural']
31
    collection = db['data']
32
    ret = {}
33
    x = 0
34
    resource = "default"
35
    if request.args.has_key('uri'):
36
        resource = request.args['uri']
37
    for i in collection.find({'uri':resource}):
38
        del(i['_id'])
39
        ret[x] = i
40
        x = x + 1
41
    else:
42
        for i in collection.find():
43
            del(i['_id'])
44
            ret[x] = i
45
            x = x + 1
46
    if len(ret) == 0:
47
        ret['error'] = "Sorry! No re-treats for you."
48
    return jsonify(ret)
49
50
@app.route('/sweets', methods=['GET'])
51
def displaySweet():
52
    return render_template('sweets.html')
53
54
@app.route('/search', methods=['GET'])
55
def search():
56
    connection = pymongo.Connection()
57
    db = connection['mural']
58
    collection = db['data']
59
    y = 0
60
    ret = {}
61
    keywords_dict = json.loads(request.args['data'])
62
    #keywords = json.loads(keywords_dict)['data']
63
    for i in collection.find():
64
        for keyword in keywords_dict:
65
            try:
66
                if keyword in i['nodes']:
67
                    del(i['_id'])
68
                    ret[y] = i
69
                    y = y + 1
70
            except:
71
                pass
72
    return render_template('blank.html', content = ret)
73
74
75
@app.route('/submit', methods=['POST'])
76
def submit():
77
    c = pymongo.Connection()
78
    db = c['mural']
79
    coll = db['data']
80
    requestData = json.loads(request.form['data'])
81
    try:
82
        for i in requestData:
83
            coll.insert(i)
84
        response = make_response()
85
        response.headers['Access-Control-Allow-Origin'] = '*'
86
        response.status = '200 OK'
87
        response.status_code = 200
88
        return response
89
    except:
90
        response = make_response()
91
        response.status = "500"
92
        response.data = "Your post could not be saved. Try posting again."
93
        return response
94
95
@app.route('/web/', methods=['GET'])
96
def web():
97
  return render_template('web.html')
98
99
@app.route('/SWeeText', methods=['GET'])
100
def SWeeText():
101
    if request.args.has_key('url'):
102
        # Log -- comment them
103
        print "Got URL " + request.args['url'] + " .. Fetching and Parsing.."
104
        myhandler1 = urllib2.Request(request.args['url'], headers={'User-Agent': "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/20.0"})
105
        a = urllib2.urlopen(myhandler1)
106
        page = a.read()
107
        a.close()
108
        try:
109
            page = unicode(page, 'utf-8')
110
        except UnicodeDecodeError:
111
            pass
112
        root = lxml.html.parse(StringIO.StringIO(page)).getroot()
113
        root.make_links_absolute(request.args['url'], resolve_base_href = True)
114
        # Log -- comment them
115
        print "Page parsed.. Preparing to send.."
116
117
        # inject the JS toolbar to annotate text
118
        jq = root.makeelement('script')
119
        jq.set('src', 'static/jquery-1.9.1.min.js')
120
121
        script = root.makeelement('script')
122
        script.set('src', 'static/text-annotation.js')
123
124
        tree = root.makeelement('script')
125
        tree.set('src', 'static/tree.js')
126
127
        bs_js = root.makeelement('script')
128
        bs_js.set('src', 'static/bootstrap.js')
129
130
        jit = root.makeelement('script')
131
        jit.set('src', 'static/jit.js')
132
133
        us = root.makeelement('script')
134
        us.set('src', 'static/underscore-min-1.4.4.js')
135
136
        link = root.makeelement('link')
137
        link.set('href', 'static/text-annotation.css')
138
        link.set('type', 'text/css')
139
        link.set('rel', 'stylesheet')
140
141
        bs = root.makeelement('link')
142
        bs.set('href', 'static/bootstrap.css')
143
        bs.set('type', 'text/css')
144
        bs.set('rel', 'stylesheet')
145
146
        tree_css = root.makeelement('link')
147
        tree_css.set('href', 'static/tree.css')
148
        tree_css.set('type', 'text/css')
149
        tree_css.set('rel', 'stylesheet')
150
151
        root.head.append(bs)
152
        root.head.append(link)
153
        root.head.append(tree_css)
154
155
        root.head.append(jq)
156
        root.head.append(bs_js)
157
        root.head.append(jit)
158
        root.head.append(us)
159
        root.head.append(tree)
160
        root.head.append(script)
161
162
        return lxml.html.tostring(root)
163
164
165
#Log the errors, don't depend on apache to log it for you.
166
    fil = FileHandler(os.path.join(os.path.dirname(__file__), 'logme'),mode='a')
167
    fil.setLevel(logging.ERROR)
168
    app.logger.addHandler(fil)
169
170
171
if __name__ == "__main__":
172
    app.run(debug=True, host='0.0.0.0')