RESTful server to serve showtimes data
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
5.3 KiB

10 years ago
from flask import Flask, make_response, request, current_app
from simplejson import dumps
from pymongo import MongoClient, DESCENDING # ASCENDING
import datetime
import dateutil.parser
import bson
from settings import mongo_config
10 years ago
from datetime import timedelta
from functools import update_wrapper
def crossdomain(origin=None, methods=None, headers=None,
max_age=21600, attach_to_all=True,
automatic_options=True):
if methods is not None:
methods = ', '.join(sorted(x.upper() for x in methods))
if headers is not None and not isinstance(headers, basestring):
headers = ', '.join(x.upper() for x in headers)
if not isinstance(origin, basestring):
origin = ', '.join(origin)
if isinstance(max_age, timedelta):
max_age = max_age.total_seconds()
def get_methods():
if methods is not None:
return methods
options_resp = current_app.make_default_options_response()
return options_resp.headers['allow']
def decorator(f):
def wrapped_function(*args, **kwargs):
if automatic_options and request.method == 'OPTIONS':
resp = current_app.make_default_options_response()
else:
resp = make_response(f(*args, **kwargs))
if not attach_to_all and request.method != 'OPTIONS':
return resp
h = resp.headers
h['Access-Control-Allow-Origin'] = origin
h['Access-Control-Allow-Methods'] = get_methods()
h['Access-Control-Max-Age'] = str(max_age)
if headers is not None:
h['Access-Control-Allow-Headers'] = headers
return resp
f.provide_automatic_options = False
return update_wrapper(wrapped_function, f)
return decorator
app = Flask(__name__)
client = MongoClient(mongo_config)
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/')
10 years ago
@crossdomain(origin='*')
def hello_world():
return 'This comes from Flask ^_^'
@app.route('/movies/', methods=['GET'])
@app.route('/movies/<lang>/', methods=['GET'])
@crossdomain(origin='*')
def movie_list(lang='en'):
result = db.movies.find().sort('release_date', DESCENDING)
movies = []
for i in result:
if 'original' in i['title']:
i['original_title'] = i['title']['original']
i['title'] = i['title'][lang]
i['cast'] = i['cast'][lang]
i['tagline'] = i['tagline'][lang]
i['storyline'] = i['storyline'][lang]
i['director'] = i['director'][lang]
movies.append(i)
json_dict = {
'meta': {
'total_count': len(movies)
},
'objects': movies
}
r = make_response(dumps(json_dict, default=miscObjHandler))
r.mimetype = 'application/json'
return r
@app.route('/movie/<mid>/', methods=['GET'])
@app.route('/movie/<mid>/<lang>/', methods=['GET'])
@crossdomain(origin='*')
def movie_item(mid, lang='en'):
lang = lang if lang in ('en', 'th') else 'en'
try:
movie = db.movies.find_one({'tmdb_id': int(mid)})
except ValueError:
movie = db.movies.find_one({'_id': bson.objectid.ObjectId(mid)})
if not movie:
movie = {}
for k in ('title', 'tagline', 'director', 'cast', 'storyline'):
if movie[k][lang]:
movie[k] = movie[k][lang]
else:
movie[k] = movie[k]['th' if lang == 'en' else lang]
try:
del movie['indexes']
except KeyError:
pass
r = make_response(dumps(movie, default=miscObjHandler))
r.mimetype = 'application/json'
return r
@app.route('/groups/', methods=['GET'])
10 years ago
@crossdomain(origin='*')
def groups():
known_groups = ['sf', 'major']
json_dict = {
'meta': {
'total_count': len(known_groups)
},
'objects': known_groups
}
r = make_response(dumps(json_dict, default=miscObjHandler))
r.mimetype = 'application/json'
return r
@app.route('/theaters/', methods=['GET'])
@app.route('/theaters/<group>/', methods=['GET'])
10 years ago
@crossdomain(origin='*')
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]
json_dict = {
'meta': {
'total_count': len(items)
},
'objects': items
}
r = make_response(dumps(json_dict, default=miscObjHandler))
r.mimetype = 'application/json'
return r
@app.route('/showtimes/<group>/', methods=['GET'])
@app.route('/showtimes/<group>/<code>/', methods=['GET'])
10 years ago
@crossdomain(origin='*')
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).sort('date', DESCENDING)
items = [i for i in result[:300]]
json_dict = {
'meta': {
'total_count': len(items)
},
'objects': items
}
r = make_response(dumps(json_dict, default=miscObjHandler))
r.mimetype = 'application/json'
return r
if __name__ == '__main__':
app.run(debug=True)