Skip to content

Commit

Permalink
Simplify library config.
Browse files Browse the repository at this point in the history
  • Loading branch information
tkem committed Nov 23, 2015
1 parent bb0f5e4 commit 8172fe0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
11 changes: 6 additions & 5 deletions mopidy_internetarchive/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ class InternetArchiveBackend(pykka.ThreadingActor, backend.Backend):

def __init__(self, config, audio):
super(InternetArchiveBackend, self).__init__()
ext_config = config[Extension.ext_name]

self.client = client = InternetArchiveClient(
config[Extension.ext_name]['base_url'],
retries=config[Extension.ext_name]['retries'],
timeout=config[Extension.ext_name]['timeout']
ext_config['base_url'],
retries=ext_config['retries'],
timeout=ext_config['timeout']
)
product = '%s/%s' % (Extension.dist_name, Extension.version)
client.useragent = httpclient.format_user_agent(product)
proxy = httpclient.format_proxy(config['proxy'])
client.proxies.update({'http': proxy, 'https': proxy})
client.cache = _cache(**config[Extension.ext_name])
client.cache = _cache(**ext_config)

self.library = InternetArchiveLibraryProvider(config, self)
self.library = InternetArchiveLibraryProvider(ext_config, self)
self.playback = InternetArchivePlaybackProvider(audio, self)
38 changes: 23 additions & 15 deletions mopidy_internetarchive/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@ class InternetArchiveLibraryProvider(backend.LibraryProvider):

def __init__(self, config, backend):
super(InternetArchiveLibraryProvider, self).__init__(backend)
self.__config = ext_config = config[Extension.ext_name]
self.__root_collections = config['collections']
self.__audio_formats = config['audio_formats']
self.__image_formats = config['image_formats']

self.__browse_filter = '(mediatype:collection OR format:(%s))' % (
' OR '.join(map(translator.quote, ext_config['audio_formats']))
' OR '.join(map(translator.quote, config['audio_formats']))
)
self.__browse_limit = config['browse_limit']
self.__browse_views = config['browse_views']

self.__search_filter = 'format:(%s)' % (
' OR '.join(map(translator.quote, ext_config['audio_formats']))
' OR '.join(map(translator.quote, config['audio_formats']))
)
self.__search_limit = config['search_limit']
self.__search_order = config['search_order']

self.__lookup = {} # track cache for faster lookup

def browse(self, uri):
Expand All @@ -48,11 +57,10 @@ def get_images(self, uris):
else:
logger.warn('No images for %s', uri)
results = {}
formats = self.__config['image_formats']
for identifier in urimap:
item = client.getitem(identifier)
images = translator.images(item, formats, client.geturl)
results.update(dict.fromkeys(urimap[identifier], images))
results.update(dict.fromkeys(urimap[identifier], translator.images(
client.getitem(identifier), self.__image_formats, client.geturl
)))
return results

def lookup(self, uri):
Expand Down Expand Up @@ -81,7 +89,7 @@ def search(self, query=None, uris=None, exact=False):
if self.root_directory.uri in uris:
# TODO: from cached root collections?
uris.update(translator.uri(identifier)
for identifier in self.__config['collections'])
for identifier in self.__root_collections)
uris.remove(self.root_directory.uri)
try:
qs = translator.query(query, uris, exact)
Expand All @@ -93,8 +101,8 @@ def search(self, query=None, uris=None, exact=False):
result = self.backend.client.search(
'%s AND %s' % (qs, self.__search_filter),
fields=['identifier', 'title', 'creator', 'date'],
rows=self.__config['search_limit'],
sort=self.__config['search_order']
rows=self.__search_limit,
sort=self.__search_order
)
return models.SearchResult(
uri=translator.uri(q=result.query),
Expand All @@ -109,7 +117,7 @@ def __browse_collection(self, identifier, q=[], sort=['downloads desc']):
return list(map(translator.ref, self.backend.client.search(
'%s AND %s' % (qs, self.__browse_filter),
fields=['identifier', 'title', 'mediatype', 'creator'],
rows=self.__config['browse_limit'],
rows=self.__browse_limit,
sort=sort
)))

Expand All @@ -128,13 +136,13 @@ def __browse_root(self):
# TODO: cache this
result = self.backend.client.search(
'mediatype:collection AND identifier:(%s)' % (
' OR '.join(self.__config['collections'])
' OR '.join(self.__root_collections)
),
fields=['identifier', 'title', 'mediatype', 'creator']
)
refs = []
objs = {obj['identifier']: obj for obj in result}
for identifier in self.__config['collections']:
for identifier in self.__root_collections:
try:
obj = objs[identifier]
except KeyError:
Expand All @@ -145,13 +153,13 @@ def __browse_root(self):
return refs

def __tracks(self, item, key=lambda t: (t.track_no or 0, t.uri)):
tracks = translator.tracks(item, self.__config['audio_formats'])
tracks = translator.tracks(item, self.__audio_formats)
tracks.sort(key=key)
return tracks

def __views(self, identifier):
refs = []
for order, name in self.__config['browse_views'].items():
for order, name in self.__browse_views.items():
uri = translator.uri(identifier, sort=order)
refs.append(models.Ref.directory(name=name, uri=uri))
return refs
5 changes: 1 addition & 4 deletions tests/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ class LibraryTest(unittest.TestCase):
config = {
'internetarchive': {
'base_url': 'http://archive.org',
'username': None,
'collections': ('etree', 'audio'),
'audio_formats': ('MP3'),
'image_formats': ('JPEG'),
'browse_limit': None,
'browse_order': None,
'browse_views': None,
'search_limit': None,
'search_order': None,
'exclude_collections': (),
'exclude_mediatypes': (),
'cache_size': 1,
'cache_ttl': None,
'retries': 0,
Expand Down

0 comments on commit 8172fe0

Please sign in to comment.