From c2dfe04e1786226eb68ccaaa3e85fc0e64ab2ce5 Mon Sep 17 00:00:00 2001 From: zinootje Date: Sat, 14 Apr 2018 19:43:47 +0200 Subject: [PATCH 1/4] add images url on get next page --- pylast/__init__.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/pylast/__init__.py b/pylast/__init__.py index 16dc353..2bf9b1b 100644 --- a/pylast/__init__.py +++ b/pylast/__init__.py @@ -1369,7 +1369,7 @@ class _Opus(_BaseObject, _Taggable): __hash__ = _BaseObject.__hash__ - def __init__(self, artist, title, network, ws_prefix, username=None): + def __init__(self, artist, title, network, ws_prefix, username=None, images=None): """ Create an opus instance. # Parameters: @@ -1388,6 +1388,7 @@ class _Opus(_BaseObject, _Taggable): self.title = title self.username = username + self.images = images def __repr__(self): return "pylast.%s(%s, %s, %s)" % ( @@ -1485,8 +1486,8 @@ class Album(_Opus): __hash__ = _Opus.__hash__ - def __init__(self, artist, title, network, username=None): - super(Album, self).__init__(artist, title, network, "album", username) + def __init__(self, artist, title, network, username=None, images=None): + super(Album, self).__init__(artist, title, network, "album", username, images) def get_cover_image(self, size=SIZE_EXTRA_LARGE): """ @@ -1497,10 +1498,12 @@ class Album(_Opus): SIZE_MEDIUM SIZE_SMALL """ - - return _extract_all( - self._request( - self.ws_prefix + ".getInfo", cacheable=True), 'image')[size] + if not self.images: + return _extract_all( + self._request( + self.ws_prefix + ".getInfo", cacheable=True), 'image')[size] + else: + return self.images[size] def get_tracks(self): """Returns the list of Tracks on this album.""" @@ -2544,7 +2547,8 @@ class AlbumSearch(_Search): seq.append(Album( _extract(node, "artist"), _extract(node, "name"), - self.network)) + self.network, + images=_extract_all(node, 'image'))) return seq From 971e4e35719b497c4e4a204668577b5fbd37b67c Mon Sep 17 00:00:00 2001 From: Hugo Date: Sat, 14 Apr 2018 22:06:57 +0300 Subject: [PATCH 2/4] flake8 --- pylast/__init__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pylast/__init__.py b/pylast/__init__.py index 2bf9b1b..0586b42 100644 --- a/pylast/__init__.py +++ b/pylast/__init__.py @@ -1369,7 +1369,8 @@ class _Opus(_BaseObject, _Taggable): __hash__ = _BaseObject.__hash__ - def __init__(self, artist, title, network, ws_prefix, username=None, images=None): + def __init__(self, artist, title, network, ws_prefix, username=None, + images=None): """ Create an opus instance. # Parameters: @@ -1487,7 +1488,8 @@ class Album(_Opus): __hash__ = _Opus.__hash__ def __init__(self, artist, title, network, username=None, images=None): - super(Album, self).__init__(artist, title, network, "album", username, images) + super(Album, self).__init__(artist, title, network, "album", username, + images) def get_cover_image(self, size=SIZE_EXTRA_LARGE): """ @@ -1500,8 +1502,8 @@ class Album(_Opus): """ if not self.images: return _extract_all( - self._request( - self.ws_prefix + ".getInfo", cacheable=True), 'image')[size] + self._request(self.ws_prefix + ".getInfo", cacheable=True), + 'image')[size] else: return self.images[size] From cf2d9113dd20a3b15f082d8851daf85cd613082b Mon Sep 17 00:00:00 2001 From: Hugo Date: Sat, 14 Apr 2018 22:38:43 +0300 Subject: [PATCH 3/4] Add test for storing album images on search --- tests/test_network.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test_network.py b/tests/test_network.py index fafe66f..ab5dad2 100755 --- a/tests/test_network.py +++ b/tests/test_network.py @@ -315,6 +315,26 @@ class TestPyLastNetwork(PyLastTestCase): self.assertIsInstance(results, list) self.assertIsInstance(results[0], pylast.Album) + def test_album_search_images(self): + # Arrange + album = "Nevermind" + search = self.network.search_for_album(album) + + # Act + results = search.get_next_page() + images = results[0].images + + # Assert + self.assertEqual(len(images), 4) + + self.assertTrue(images[pylast.SIZE_SMALL].startswith("https://")) + self.assertTrue(images[pylast.SIZE_SMALL].endswith(".png")) + self.assertIn("/34s/", images[pylast.SIZE_SMALL]) + + self.assertTrue(images[pylast.SIZE_EXTRA_LARGE].startswith("https://")) + self.assertTrue(images[pylast.SIZE_EXTRA_LARGE].endswith(".png")) + self.assertIn("/300x300/", images[pylast.SIZE_EXTRA_LARGE]) + def test_artist_search(self): # Arrange artist = "Nirvana" From 4083f72ef7825c4919ee424fc600f6808979311d Mon Sep 17 00:00:00 2001 From: Hugo Date: Sat, 14 Apr 2018 22:50:42 +0300 Subject: [PATCH 4/4] Store all images on get_cover_image for future use --- pylast/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pylast/__init__.py b/pylast/__init__.py index 0586b42..3b9b0c1 100644 --- a/pylast/__init__.py +++ b/pylast/__init__.py @@ -732,6 +732,7 @@ class _Request(object): """Representing an abstract web service operation.""" def __init__(self, network, method_name, params=None): + print(method_name) if params is None: params = {} @@ -1501,11 +1502,10 @@ class Album(_Opus): SIZE_SMALL """ if not self.images: - return _extract_all( + self.images = _extract_all( self._request(self.ws_prefix + ".getInfo", cacheable=True), - 'image')[size] - else: - return self.images[size] + 'image') + return self.images[size] def get_tracks(self): """Returns the list of Tracks on this album."""