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
c6d359e by Anon Ray at 2013-03-30 13
import json
04640f7 by Arvind at 2013-02-07 14
15
app = Flask(__name__)
16
a5c95bf by Arvind at 2013-03-28 17
b184f1f by Arvind at 2013-02-08 18
@app.route('/', methods=['GET'])
04640f7 by Arvind at 2013-02-07 19
def index():
d611971 by Arvind at 2013-03-26 20
    if request.args.has_key('url'):
21
        return render_template('index.html', url=request.args['url'])
b184f1f by Arvind at 2013-02-08 22
    else:
23
        return render_template('index.html')
3018c67 by Arvind at 2013-02-08 24
a5c95bf by Arvind at 2013-03-28 25
26
@app.route('/fetch', methods=['GET'])
d611971 by Arvind at 2013-03-26 27
def fetch():
28
    connection = pymongo.Connection()
29
    db = connection['mural']
30
    collection = db['data']
31
    ret = {}
32
    x = 0
5c5d01a by Arvind at 2013-03-27 33
    resource = "default"
a5c95bf by Arvind at 2013-03-28 34
    if request.args.has_key('uri'):
35
        resource = request.args['uri']
36
    for i in collection.find({'uri':resource}):
d611971 by Arvind at 2013-03-26 37
        del(i['_id'])
38
        ret[x] = i
39
        x = x + 1
a5c95bf by Arvind at 2013-03-28 40
    if len(ret) == 0:
41
        ret['error'] = "Sorry! No re-treats for you."
5c5d01a by Arvind at 2013-03-27 42
    return jsonify(ret)
265ce28 by Anon Ray at 2013-02-08 43
a5c95bf by Arvind at 2013-03-28 44
438fa91 by Arvind at 2013-03-30 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)
a5c95bf by Arvind at 2013-03-28 61
@app.route('/submit', methods=['POST'])
62
def submit():
63
    c = pymongo.Connection()
64
    db = c['mural']
65
    coll = db['data']
438fa91 by Arvind at 2013-03-30 66
    requestData = json.loads(request.form['data'])
a5c95bf by Arvind at 2013-03-28 67
    try:
438fa91 by Arvind at 2013-03-30 68
        for i in requestData:
69
            coll.insert(i)
70
        response = make_response()
323b73b by Anon Ray at 2013-03-30 71
        response.headers['Access-Control-Allow-Origin'] = '*'
438fa91 by Arvind at 2013-03-30 72
        response.status = '200 OK'
73
        response.status_code = 200
74
        return response
a5c95bf by Arvind at 2013-03-28 75
    except:
76
        response = make_response()
77
        response.status = "500"
aa2260f by Anon Ray at 2013-03-28 78
        response.data = "Your post could not be saved. Try posting again."
a5c95bf by Arvind at 2013-03-28 79
        return response
80
c6d359e by Anon Ray at 2013-03-30 81
@app.route('/web', methods=['GET'])
82
def web():
83
  return render_template('web.html')
a5c95bf by Arvind at 2013-03-28 84
85
@app.route('/SWeeText', methods=['GET'])
d09d88d by Arvind at 2013-03-28 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:
a5c95bf by Arvind at 2013-03-28 93
            page = unicode(page, 'utf-8')
d09d88d by Arvind at 2013-03-28 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)
aa2260f by Anon Ray at 2013-03-28 98
99
        # inject the JS toolbar to annotate text
100
        script = root.makeelement('script')
101
        script.set('src', 'static/text-annotation.js')
c6d359e by Anon Ray at 2013-03-30 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
aa2260f by Anon Ray at 2013-03-28 113
        link = root.makeelement('link')
114
        link.set('href', 'static/text-annotation.css')
115
        link.set('type', 'text/css')
116
        link.set('rel', 'stylesheet')
c6d359e by Anon Ray at 2013-03-30 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)
aa2260f by Anon Ray at 2013-03-28 131
        root.body.append(script)
c6d359e by Anon Ray at 2013-03-30 132
133
        root.head.append(bs)
aa2260f by Anon Ray at 2013-03-28 134
        root.head.append(link)
c6d359e by Anon Ray at 2013-03-30 135
        root.head.append(tree_css)
aa2260f by Anon Ray at 2013-03-28 136
d09d88d by Arvind at 2013-03-28 137
        return lxml.html.tostring(root)
aa2260f by Anon Ray at 2013-03-28 138
438fa91 by Arvind at 2013-03-30 139
8181191 by Arvind at 2013-02-08 140
#Log the errors, don't depend on apache to log it for you.
aa2260f by Anon Ray at 2013-03-28 141
    fil = FileHandler(os.path.join(os.path.dirname(__file__), 'logme'),mode='a')
b184f1f by Arvind at 2013-02-08 142
    fil.setLevel(logging.ERROR)
143
    app.logger.addHandler(fil)
8181191 by Arvind at 2013-02-08 144
265ce28 by Anon Ray at 2013-02-08 145
04640f7 by Arvind at 2013-02-07 146
if __name__ == "__main__":
b184f1f by Arvind at 2013-02-08 147
    app.run(debug=True, host='0.0.0.0')
04640f7 by Arvind at 2013-02-07 148