openmedialibrary/oml/metaremote.py

32 lines
814 B
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
import json
2014-09-02 22:32:44 +00:00
from urllib.parse import urlencode
2014-08-12 08:16:57 +00:00
from ox.cache import read_url
import logging
2015-11-29 14:56:38 +00:00
logger = logging.getLogger(__name__)
def request(action, data):
data = urlencode({
'action': action,
'data': json.dumps(data)
})
url = 'http://meta.openmedialibrary.com/api/'
try:
2014-10-04 19:05:56 +00:00
return json.loads(read_url(url, data, timeout=60).decode('utf-8'))['data']
except:
2015-12-24 12:28:11 +00:00
logger.debug('metadata request failed', exc_info=1)
return {}
def find(query):
logger.debug('find %s', query)
return request('findMetadata', {'query': query}).get('items', [])
def lookup(key, value):
logger.debug('lookup %s %s', key, value)
return request('getMetadata', {key: value})