Browse Source

Add ConfigParser for easier management

* update /movies/ to sort by release_date
* update /movie/:id which accepts both tmdb_id & object_id
master
sipp11 10 years ago
parent
commit
bbb44f5d1e
  1. 3
      .gitignore
  2. 17
      flasky.py
  3. 19
      settings.py
  4. 8
      site.default.cfg

3
.gitignore vendored

@ -1,2 +1,3 @@
.DS_Store .DS_Store
*.pyc *.pyc
site.cfg

17
flasky.py

@ -1,10 +1,10 @@
from flask import Flask, make_response, request, current_app from flask import Flask, make_response, request, current_app
from simplejson import dumps from simplejson import dumps
from pymongo import MongoClient from pymongo import MongoClient, DESCENDING # ASCENDING
import datetime import datetime
import dateutil.parser import dateutil.parser
import bson import bson
from settings import mongo_config
from datetime import timedelta from datetime import timedelta
from functools import update_wrapper from functools import update_wrapper
@ -52,7 +52,7 @@ def crossdomain(origin=None, methods=None, headers=None,
app = Flask(__name__) app = Flask(__name__)
client = MongoClient(**{'host': '10.10.10.51', 'port': 27017}) client = MongoClient(mongo_config)
db = client.showtimes db = client.showtimes
miscObjHandler = lambda obj: ( miscObjHandler = lambda obj: (
@ -71,7 +71,7 @@ def hello_world():
@app.route('/movies/<lang>/', methods=['GET']) @app.route('/movies/<lang>/', methods=['GET'])
@crossdomain(origin='*') @crossdomain(origin='*')
def movie_list(lang='en'): def movie_list(lang='en'):
result = db.movies.find() result = db.movies.find().sort('release_date', DESCENDING)
movies = [] movies = []
for i in result: for i in result:
if 'original' in i['title']: if 'original' in i['title']:
@ -97,7 +97,10 @@ def movie_list(lang='en'):
@app.route('/movie/<mid>/', methods=['GET']) @app.route('/movie/<mid>/', methods=['GET'])
@crossdomain(origin='*') @crossdomain(origin='*')
def movie_item(mid): def movie_item(mid):
movie = db.movies.find_one({'id': int(mid)}) 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: if not movie:
movie = {} movie = {}
r = make_response(dumps(movie, default=miscObjHandler)) r = make_response(dumps(movie, default=miscObjHandler))
@ -153,8 +156,8 @@ def list_showtimes(group=None, code=None):
if code: if code:
q['theater'] = code q['theater'] = code
result = db.showtimes.find(q) result = db.showtimes.find(q).sort('date', DESCENDING)
items = [i for i in result] items = [i for i in result[:300]]
json_dict = { json_dict = {
'meta': { 'meta': {
'total_count': len(items) 'total_count': len(items)

19
settings.py

@ -3,14 +3,31 @@ import tornado
import tornado.template import tornado.template
from tornado.options import define, options from tornado.options import define, options
import os import os
import urllib
import logconfig import logconfig
import ConfigParser
conf = ConfigParser.RawConfigParser()
conf.read('site.cfg')
port = conf.getint('app', 'port') if conf.has_option('app', 'port') else 8888
mongo_conf_data = {
'host': conf.get('mongo', 'host') if conf.has_option('mongo', 'host') else 'localhost',
'port': conf.getint('mongo', 'port') if conf.has_option('mongo', 'port') else 27017,
'user': conf.get('mongo', 'user') if conf.has_option('mongo', 'user') else '',
'password': conf.get('mongo', 'password') if conf.has_option('mongo', 'password') else '',
}
mongo_config = 'mongodb://%(host)s:%(port)s'
if mongo_conf_data['user'] and mongo_conf_data['password']:
mongo_conf_data['password'] = urllib.quote_plus(mongo_conf_data['password'])
mongo_config = 'mongodb://%(user)s:%(password)s@%(host)s:%(port)s'
mongo_config = mongo_config % mongo_conf_data
# Make filepaths relative to settings. # Make filepaths relative to settings.
path = lambda root, *a: os.path.join(root, *a) path = lambda root, *a: os.path.join(root, *a)
ROOT = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.dirname(os.path.abspath(__file__))
define("port", default=8888, help="run on the given port", type=int) define("port", default=port, help="run on the given port", type=int)
define("config", default=None, help="tornado config file") define("config", default=None, help="tornado config file")
define("debug", default=False, help="debug mode") define("debug", default=False, help="debug mode")
tornado.options.parse_command_line() tornado.options.parse_command_line()

8
site.default.cfg

@ -0,0 +1,8 @@
[app]
port=8888
[mongo]
host=localhost
port=27017
#user=
#password=
Loading…
Cancel
Save