From fa601b8111245a06b18e7a8eab64fc55697d42d0 Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Apr 2018 09:53:31 +0300 Subject: [PATCH] Refactor to store Album/Artist images in info dict --- pylast/__init__.py | 36 +++++++++++++++++++++--------------- tests/test_network.py | 4 ++-- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/pylast/__init__.py b/pylast/__init__.py index 7379850..090cadc 100644 --- a/pylast/__init__.py +++ b/pylast/__init__.py @@ -1375,7 +1375,7 @@ class _Opus(_BaseObject, _Taggable): __hash__ = _BaseObject.__hash__ def __init__(self, artist, title, network, ws_prefix, username=None, - images=None): + info=None): """ Create an opus instance. # Parameters: @@ -1384,6 +1384,9 @@ class _Opus(_BaseObject, _Taggable): * ws_prefix: 'album' or 'track' """ + if info is None: + info = {} + _BaseObject.__init__(self, network, ws_prefix) _Taggable.__init__(self, ws_prefix) @@ -1394,7 +1397,7 @@ class _Opus(_BaseObject, _Taggable): self.title = title self.username = username - self.images = images + self.info = info def __repr__(self): return "pylast.%s(%s, %s, %s)" % ( @@ -1492,9 +1495,9 @@ class Album(_Opus): __hash__ = _Opus.__hash__ - def __init__(self, artist, title, network, username=None, images=None): + def __init__(self, artist, title, network, username=None, info=None): super(Album, self).__init__(artist, title, network, "album", username, - images) + info) def get_cover_image(self, size=SIZE_EXTRA_LARGE): """ @@ -1505,11 +1508,11 @@ class Album(_Opus): SIZE_MEDIUM SIZE_SMALL """ - if not self.images: - self.images = _extract_all( + if "image" not in self.info: + self.info["image"] = _extract_all( self._request(self.ws_prefix + ".getInfo", cacheable=True), - 'image') - return self.images[size] + "image") + return self.info["image"][size] def get_tracks(self): """Returns the list of Tracks on this album.""" @@ -1552,18 +1555,21 @@ class Artist(_BaseObject, _Taggable): __hash__ = _BaseObject.__hash__ - def __init__(self, name, network, username=None, images=None): + def __init__(self, name, network, username=None, info=None): """Create an artist object. # Parameters: * name str: The artist's name. """ + if info is None: + info = {} + _BaseObject.__init__(self, network, 'artist') _Taggable.__init__(self, 'artist') self.name = name self.username = username - self.images = images + self.info = info def __repr__(self): return "pylast.Artist(%s, %s)" % ( @@ -1616,11 +1622,11 @@ class Artist(_BaseObject, _Taggable): SIZE_SMALL """ - if not self.images: - self.images = _extract_all( + if "image" not in self.info: + self.info["image"] = _extract_all( self._request(self.ws_prefix + ".getInfo", cacheable=True), "image") - return self.images[size] + return self.info["image"][size] def get_playcount(self): """Returns the number of plays on the network.""" @@ -2558,7 +2564,7 @@ class AlbumSearch(_Search): _extract(node, "artist"), _extract(node, "name"), self.network, - images=_extract_all(node, 'image')), + info={"image": _extract_all(node, "image")}), ) return seq @@ -2578,7 +2584,7 @@ class ArtistSearch(_Search): seq = [] for node in master_node.getElementsByTagName("artist"): artist = Artist(_extract(node, "name"), self.network, - images=_extract_all(node, "image")) + info={"image": _extract_all(node, "image")}) artist.listener_count = _number(_extract(node, "listeners")) seq.append(artist) diff --git a/tests/test_network.py b/tests/test_network.py index f7eec7e..e1896ea 100755 --- a/tests/test_network.py +++ b/tests/test_network.py @@ -322,7 +322,7 @@ class TestPyLastNetwork(PyLastTestCase): # Act results = search.get_next_page() - images = results[0].images + images = results[0].info["image"] # Assert self.assertEqual(len(images), 4) @@ -354,7 +354,7 @@ class TestPyLastNetwork(PyLastTestCase): # Act results = search.get_next_page() - images = results[0].images + images = results[0].info["image"] # Assert self.assertEqual(len(images), 5)