2014-05-22 11:06:30 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
2014-09-03 00:32:44 +02:00
|
|
|
|
2014-05-22 11:06:30 +02:00
|
|
|
|
|
|
|
import json
|
2014-09-03 00:32:44 +02:00
|
|
|
from urllib.parse import urlencode
|
2014-05-22 11:06:30 +02:00
|
|
|
|
2014-08-12 10:16:57 +02:00
|
|
|
from ox.cache import read_url
|
|
|
|
|
2016-01-05 21:44:57 +05:30
|
|
|
import settings
|
|
|
|
|
2014-05-22 11:06:30 +02:00
|
|
|
import logging
|
2015-11-29 15:56:38 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-05-22 11:06:30 +02:00
|
|
|
|
|
|
|
def request(action, data):
|
|
|
|
data = urlencode({
|
|
|
|
'action': action,
|
|
|
|
'data': json.dumps(data)
|
|
|
|
})
|
|
|
|
url = 'http://meta.openmedialibrary.com/api/'
|
|
|
|
try:
|
2014-10-04 21:05:56 +02:00
|
|
|
return json.loads(read_url(url, data, timeout=60).decode('utf-8'))['data']
|
2014-05-22 11:06:30 +02:00
|
|
|
except:
|
2015-12-24 17:58:11 +05:30
|
|
|
logger.debug('metadata request failed', exc_info=1)
|
2014-05-22 11:06:30 +02:00
|
|
|
return {}
|
|
|
|
|
|
|
|
def find(query):
|
|
|
|
logger.debug('find %s', query)
|
2015-03-01 13:19:59 +05:30
|
|
|
return request('findMetadata', {'query': query}).get('items', [])
|
2014-05-22 11:06:30 +02:00
|
|
|
|
|
|
|
def lookup(key, value):
|
|
|
|
logger.debug('lookup %s %s', key, value)
|
2016-01-05 21:44:57 +05:30
|
|
|
data = request('getMetadata', {key: value})
|
|
|
|
for key in [k['id'] for k in settings.config['itemKeys'] if isinstance(k['type'], list)]:
|
|
|
|
if key in data and not isinstance(data[key], list):
|
|
|
|
data[key] = [data[key]]
|
|
|
|
return data
|