add metaserver and use meta lookup service

This commit is contained in:
j 2014-05-22 11:06:30 +02:00
parent f2221188ea
commit 5bbf612a38
6 changed files with 134 additions and 4 deletions

View file

@ -16,7 +16,8 @@ import settings
import state import state
from websocket import trigger_event from websocket import trigger_event
import meta #import meta
import metaremote as meta
import utils import utils

View file

@ -23,7 +23,9 @@ from settings import db, config
from person import get_sort_name from person import get_sort_name
import media import media
import meta
#import meta
import metaremote as meta
import state import state
import utils import utils

29
oml/metaremote.py Normal file
View file

@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division
import json
from ox.cache import read_url
from urllib import urlencode
import logging
logger = logging.getLogger('metaremote')
def request(action, data):
data = urlencode({
'action': action,
'data': json.dumps(data)
})
url = 'http://meta.openmedialibrary.com/api/'
try:
return json.loads(read_url(url, data))['data']
except:
return {}
def find(query):
logger.debug('find %s', query)
return request('findMetadata', {'query': query})['items']
def lookup(key, value):
logger.debug('lookup %s %s', key, value)
return request('getMetadata', {key: value})

94
oml/metaserver.py Normal file
View file

@ -0,0 +1,94 @@
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import os
import sys
from tornado.web import Application
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from flask import Flask
import oxtornado
from oxtornado import actions
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
}]
}
key is one of the supported identifiers: isbn10, isbn13...
'''
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
}
key can be one of the supported identifiers: isbn10, isbn13, oclc, olid,...
'''
logger.debug('getMetadata %s', data)
if 'includeEdits' in data:
include_edits = data.pop('includeEdits')
else:
include_edits = False
key, value = data.iteritems().next()
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():
root_dir = os.path.normpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..'))
options = {
'debug': True
}
app = Flask('metaoml')
handlers = [
(r'/api/', oxtornado.ApiHandler, dict(app=app)),
]
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)
print url
main.start()
if __name__ == '__main__':
run()

View file

@ -87,7 +87,11 @@ def api_task(app, request, callback):
f = actions.get(action) f = actions.get(action)
if f: if f:
with app.app_context(): with app.app_context():
response = f(data) try:
response = f(data)
except:
logger.debug('FAILED %s %s', action, data, exc_info=1)
response = json_response(status=500, text='%s failed' % action)
else: else:
response = json_response(status=400, text='Unknown action %s' % action) response = json_response(status=400, text='Unknown action %s' % action)
callback(response) callback(response)

View file

@ -44,7 +44,7 @@ server_defaults = {
'extract_text': True, 'extract_text': True,
'localnode_discovery': True, 'localnode_discovery': True,
'directory_service': 'http://[2a01:4f8:120:3201::3]:25519', 'directory_service': 'http://[2a01:4f8:120:3201::3]:25519',
'lookup_service': 'http://data.openmedialibrary.com', 'meta_service': 'http://meta.openmedialibrary.com/api/',
} }
for key in server_defaults: for key in server_defaults: