Store artist images for future use

This commit is contained in:
Hugo 2018-04-15 23:43:39 +03:00
parent 942ced319a
commit d1af8d3ebc
3 changed files with 32 additions and 6 deletions

View file

@ -133,5 +133,5 @@ network = pylast.LastFMNetwork(...)
To enable from pytest:
```sh
pytest -k test_album_search_images --log-cli-level debug
pytest --log-cli-level debug -k test_album_search_images
```

View file

@ -1552,7 +1552,7 @@ class Artist(_BaseObject, _Taggable):
__hash__ = _BaseObject.__hash__
def __init__(self, name, network, username=None):
def __init__(self, name, network, username=None, images=None):
"""Create an artist object.
# Parameters:
* name str: The artist's name.
@ -1563,6 +1563,7 @@ class Artist(_BaseObject, _Taggable):
self.name = name
self.username = username
self.images = images
def __repr__(self):
return "pylast.Artist(%s, %s)" % (
@ -1615,8 +1616,11 @@ class Artist(_BaseObject, _Taggable):
SIZE_SMALL
"""
return _extract_all(
self._request(self.ws_prefix + ".getInfo", True), "image")[size]
if not self.images:
self.images = _extract_all(
self._request(self.ws_prefix + ".getInfo", cacheable=True),
"image")
return self.images[size]
def get_playcount(self):
"""Returns the number of plays on the network."""
@ -2554,7 +2558,8 @@ class AlbumSearch(_Search):
_extract(node, "artist"),
_extract(node, "name"),
self.network,
images=_extract_all(node, 'image')))
images=_extract_all(node, 'image')),
)
return seq
@ -2572,7 +2577,8 @@ class ArtistSearch(_Search):
seq = []
for node in master_node.getElementsByTagName("artist"):
artist = Artist(_extract(node, "name"), self.network)
artist = Artist(_extract(node, "name"), self.network,
images=_extract_all(node, "image"))
artist.listener_count = _number(_extract(node, "listeners"))
seq.append(artist)

View file

@ -347,6 +347,26 @@ class TestPyLastNetwork(PyLastTestCase):
self.assertIsInstance(results, list)
self.assertIsInstance(results[0], pylast.Artist)
def test_artist_search_images(self):
# Arrange
artist = "Nirvana"
search = self.network.search_for_artist(artist)
# Act
results = search.get_next_page()
images = results[0].images
# Assert
self.assertEqual(len(images), 5)
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_track_search(self):
# Arrange
artist = "Nirvana"