from flask import Flask, make_response, request from simplejson import dumps from pymongo import MongoClient import datetime import dateutil.parser import bson app = Flask(__name__) client = MongoClient(**{'host': 'localhost', 'port': 27017}) db = client.showtimes miscObjHandler = lambda obj: ( obj.isoformat() if isinstance(obj, datetime.datetime) or isinstance(obj, datetime.date) else str(obj) if isinstance(obj, bson.objectid.ObjectId) else None) @app.route('/flask/') def hello_world(): return 'This comes from Flask ^_^' @app.route('/groups/', methods=['GET']) def groups(): known_groups = ['sf', 'major'] r = make_response(dumps(known_groups)) r.mimetype = 'application/json' return r @app.route('/theaters/', methods=['GET']) @app.route('/theaters//', methods=['GET']) def list_theaters(group=None): if not group: result = db.theater.find() else: result = db.theater.find({'group': group}) items = [i for i in result] r = make_response(dumps(items, default=miscObjHandler)) r.mimetype = 'application/json' return r @app.route('/showtimes//', methods=['GET']) @app.route('/showtimes///', methods=['GET']) def list_showtimes(group=None, code=None): day = request.args.get('d', '') q = {} if day: q['date'] = dateutil.parser.parse(day) if group: q['group'] = group if code: q['theater'] = code result = db.showtimes.find(q) items = [i for i in result] r = make_response(dumps(items, default=miscObjHandler)) r.mimetype = 'application/json' return r if __name__ == '__main__': app.run(debug=True)