Commit a5c95bff8dbcbe4770050ff286bd3bbb376772d1
Adding API
- Adding /submit. This endpoint accepts a annotation and stores it in the server.
- fetch.py 38 -------+++++++++++++++++++++++++++++++
| | | | 13 | | 13 | |
---|
14 | app = Flask(__name__) | 14 | app = Flask(__name__) |
---|
15 | | 15 | |
---|
| | 16 | |
---|
16 | @app.route('/', methods=['GET']) | 17 | @app.route('/', methods=['GET']) |
---|
17 | def index(): | 18 | def index(): |
---|
18 | if request.args.has_key('url'): | 19 | if request.args.has_key('url'): |
---|
… | | … | |
---|
21 | else: | 21 | else: |
---|
22 | return render_template('index.html') | 22 | return render_template('index.html') |
---|
23 | | 23 | |
---|
24 | @app.route('/fetch',methods=['GET']) | | @app.route('/fetch',methods=['GET']) |
---|
| | 24 | | | | 25 | @app.route('/fetch', methods=['GET']) |
---|
25 | def fetch(): | 26 | def fetch(): |
---|
26 | connection = pymongo.Connection() | 27 | connection = pymongo.Connection() |
---|
27 | db = connection['mural'] | 28 | db = connection['mural'] |
---|
… | | … | |
---|
30 | ret = {} | 30 | ret = {} |
---|
31 | x = 0 | 31 | x = 0 |
---|
32 | resource = "default" | 32 | resource = "default" |
---|
33 | if request.args.has_key('res'): | | if request.args.has_key('res'): |
---|
34 | resource = request.args['res'] | | resource = request.args['res'] |
---|
35 | for i in collection.find({'res':resource}): | | for i in collection.find({'res':resource}): |
---|
| | 33 | if request.args.has_key('uri'): | | | 34 | resource = request.args['uri'] |
---|
| | 35 | for i in collection.find({'uri':resource}): |
---|
36 | del(i['_id']) | 36 | del(i['_id']) |
---|
37 | ret[x] = i | 37 | ret[x] = i |
---|
38 | x = x + 1 | 38 | x = x + 1 |
---|
| | 39 | if len(ret) == 0: |
---|
| | 40 | ret['error'] = "Sorry! No re-treats for you." |
---|
39 | return jsonify(ret) | 41 | return jsonify(ret) |
---|
40 | | 42 | |
---|
41 | @app.route('/SWeeText',methods=['GET']) | | @app.route('/SWeeText',methods=['GET']) |
---|
| | 43 | | | | 44 | @app.route('/submit', methods=['POST']) |
---|
| | 45 | def submit(): |
---|
| | 46 | c = pymongo.Connection() |
---|
| | 47 | db = c['mural'] |
---|
| | 48 | coll = db['data'] |
---|
| | 49 | try: |
---|
| | 50 | for i in d: |
---|
| | 51 | coll.insert(request.args['data']) |
---|
| | 52 | response = make_response() |
---|
| | 53 | response.status = '200 OK' |
---|
| | 54 | response.status_code = 200 |
---|
| | 55 | return response |
---|
| | 56 | except: |
---|
| | 57 | response = make_response() |
---|
| | 58 | response.status = "500" |
---|
| | 59 | respose.data = "Your post could not be saved. Try posting again." |
---|
| | 60 | return response |
---|
| | 61 | |
---|
| | 62 | |
---|
| | 63 | @app.route('/SWeeText', methods=['GET']) |
---|
42 | def SWeeText(): | 64 | def SWeeText(): |
---|
43 | if request.args.has_key('url'): | 65 | if request.args.has_key('url'): |
---|
44 | myhandler1 = urllib2.Request(request.args['url'], headers={'User-Agent': "Mozilla/5.0(X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11"}) | 66 | myhandler1 = urllib2.Request(request.args['url'], headers={'User-Agent': "Mozilla/5.0(X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11"}) |
---|
… | | … | |
---|
68 | page = a.read() | 68 | page = a.read() |
---|
69 | a.close() | 69 | a.close() |
---|
70 | try: | 70 | try: |
---|
71 | page = unicode(page,'utf-8') | | page = unicode(page,'utf-8') |
---|
| | 71 | page = unicode(page, 'utf-8') | 72 | except UnicodeDecodeError: | 72 | except UnicodeDecodeError: |
---|
73 | pass | 73 | pass |
---|
74 | root = lxml.html.parse(StringIO.StringIO(page)).getroot() | 74 | root = lxml.html.parse(StringIO.StringIO(page)).getroot() |
---|
75 | root.make_links_absolute(request.args['url'], resolve_base_href = True) | 75 | root.make_links_absolute(request.args['url'], resolve_base_href = True) |
---|
76 | return lxml.html.tostring(root) | 76 | return lxml.html.tostring(root) |
---|
77 | #Log the errors, don't depend on apache to log it for you. | 77 | #Log the errors, don't depend on apache to log it for you. |
---|
78 | fil = FileHandler(os.path.join(os.path.dirname(__file__), 'logme'),mode='a') | | fil = FileHandler(os.path.join(os.path.dirname(__file__), 'logme'),mode='a') |
---|
| | 78 | fil = FileHandler(os.path.join(os.path.dirname(__file__), 'logme'), mode='a') | 79 | fil.setLevel(logging.ERROR) | 79 | fil.setLevel(logging.ERROR) |
---|
80 | app.logger.addHandler(fil) | 80 | app.logger.addHandler(fil) |
---|
81 | | 81 | |
---|