openmedialibrary/oml/metaserver.py

89 lines
1.8 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2014-09-02 22:32:44 +00:00
from tornado.web import Application
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
2014-09-02 23:09:42 +00:00
import oxtornado
from oxtornado import actions
2014-09-02 23:09:42 +00:00
import meta
import utils
import logging
logger = logging.getLogger('metaoml')
logging.basicConfig(level=logging.DEBUG)
def findMetadata(data):
'''
takes {
query: string,
}
returns {
items: [{
key: value
}]
}
2014-08-11 11:08:28 +00:00
key is one of the supported identifiers: isbn, olid...
'''
response = {}
logger.debug('findMetadata %s', data)
response['items'] = meta.find(data['query'])
return response
actions.register(findMetadata)
def getMetadata(data):
'''
takes {
key: value
includeEdits: boolean
}
2014-08-11 11:08:28 +00:00
key can be one of the supported identifiers: isbn, oclc, olid,...
'''
logger.debug('getMetadata %s', data)
if 'includeEdits' in data:
include_edits = data.pop('includeEdits')
else:
include_edits = False
2014-09-02 22:32:44 +00:00
key, value = next(iter(data.items()))
if key == 'isbn':
value = utils.normalize_isbn(value)
response = meta.lookup(key, value)
if response:
response['primaryid'] = [key, value]
return response
actions.register(getMetadata)
def run():
options = {
'debug': True
}
handlers = [
2014-08-09 17:09:48 +00:00
(r'/api/', oxtornado.ApiHandler),
]
http_server = HTTPServer(Application(handlers, **options))
port = 9855
address = ''
http_server.listen(port, '')
main = IOLoop.instance()
if ':' in address:
host = '[%s]' % address
elif not address:
host = '[::1]'
else:
host = address
url = 'http://%s:%s/' % (host, port)
2014-08-09 21:40:13 +00:00
print(url)
main.start()
if __name__ == '__main__':
run()