114 lines
2.8 KiB
Python
114 lines
2.8 KiB
Python
|
#!/usr/bin/python3
|
||
|
import json
|
||
|
import logging
|
||
|
import os
|
||
|
import time
|
||
|
from urllib.parse import unquote
|
||
|
|
||
|
from tornado.httpserver import HTTPServer
|
||
|
from tornado.ioloop import IOLoop, PeriodicCallback
|
||
|
|
||
|
from tornado.web import StaticFileHandler, Application, HTTPError
|
||
|
import tornado.gen
|
||
|
import tornado.web
|
||
|
|
||
|
|
||
|
from recommendation_engine import Engine
|
||
|
from utils import json_dumps, run_async
|
||
|
|
||
|
|
||
|
BANNER_PUBLIC = 'DD recommondation engine'
|
||
|
BANNER = '%s - use POST at / to access JSON-RPC 2.0 endpoint' % BANNER_PUBLIC
|
||
|
|
||
|
logger = logging.getLogger(__name__)
|
||
|
|
||
|
|
||
|
@run_async
|
||
|
def api_task(request, engine, callback):
|
||
|
try:
|
||
|
if request['method'] == 'getVideos':
|
||
|
result = engine.get_videos(request['params'])
|
||
|
else:
|
||
|
result = {}
|
||
|
response = {
|
||
|
'result': result
|
||
|
}
|
||
|
except:
|
||
|
logger.error('api failed: %s', request, exc_info=True)
|
||
|
response = {'error': {'code': -32000, 'message': 'Server error'}}
|
||
|
callback(response)
|
||
|
|
||
|
|
||
|
class RPCHandler(tornado.web.RequestHandler):
|
||
|
|
||
|
def initialize(self, engine):
|
||
|
self.engine = engine
|
||
|
|
||
|
def get(self):
|
||
|
self.write(BANNER)
|
||
|
|
||
|
@tornado.gen.coroutine
|
||
|
def post(self):
|
||
|
error = None
|
||
|
request = None
|
||
|
try:
|
||
|
request = json.loads(self.request.body.decode())
|
||
|
if request['method'] not in ('getVideos', ):
|
||
|
raise Exception('unknown method')
|
||
|
except:
|
||
|
error = {'error': {'code': -32700, 'message': 'Parse error'}}
|
||
|
if not error:
|
||
|
try:
|
||
|
response = yield tornado.gen.Task(api_task, request, self.engine)
|
||
|
except:
|
||
|
logger.error("ERROR: %s", request, exc_info=True)
|
||
|
error = {'error': {'code': -32000, 'message': 'Server error'}}
|
||
|
if error:
|
||
|
response = error
|
||
|
if request and 'id' in request:
|
||
|
response['id'] = request['id']
|
||
|
response['jsonrpc'] = '2.0'
|
||
|
response = json_dumps(response)
|
||
|
self.write(response)
|
||
|
|
||
|
|
||
|
def main(prefix='json/'):
|
||
|
settings = {
|
||
|
'debug': False,
|
||
|
'port': 8081,
|
||
|
'address': ''
|
||
|
}
|
||
|
engine = Engine(prefix)
|
||
|
handlers = [
|
||
|
(r'/', RPCHandler, dict(engine=engine)),
|
||
|
]
|
||
|
options = {
|
||
|
'debug': settings['debug'],
|
||
|
'gzip': True,
|
||
|
}
|
||
|
if settings['debug']:
|
||
|
log_format = '%(asctime)s:%(levelname)s:%(name)s:%(message)s'
|
||
|
logging.basicConfig(level=logging.DEBUG, format=log_format)
|
||
|
|
||
|
app = Application(handlers, **options)
|
||
|
app.listen(settings['port'], settings['address'])
|
||
|
main = IOLoop.instance()
|
||
|
|
||
|
@run_async
|
||
|
def update():
|
||
|
engine.update()
|
||
|
|
||
|
update_cb = PeriodicCallback(update, 60000)
|
||
|
|
||
|
#main.spawn_callback(update, engine)
|
||
|
|
||
|
#fixme run periodically
|
||
|
try:
|
||
|
main.start()
|
||
|
except:
|
||
|
print('shutting down...')
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
main()
|