Store track images for future use, refactor album image fetching

This commit is contained in:
Hugo 2018-04-16 19:02:32 +03:00
parent 60dea38d10
commit 70aad87a1b
2 changed files with 54 additions and 25 deletions

View file

@ -1431,6 +1431,21 @@ class _Opus(_BaseObject, _Taggable):
return self.artist
def get_cover_image(self, size=SIZE_EXTRA_LARGE):
"""
Returns a URI to the cover image
size can be one of:
SIZE_EXTRA_LARGE
SIZE_LARGE
SIZE_MEDIUM
SIZE_SMALL
"""
if "image" not in self.info:
self.info["image"] = _extract_all(
self._request(self.ws_prefix + ".getInfo", cacheable=True),
"image")
return self.info["image"][size]
def get_title(self, properly_capitalized=False):
"""Returns the artist or track title."""
if properly_capitalized:
@ -1499,21 +1514,6 @@ class Album(_Opus):
super(Album, self).__init__(artist, title, network, "album", username,
info)
def get_cover_image(self, size=SIZE_EXTRA_LARGE):
"""
Returns a URI to the cover image
size can be one of:
SIZE_EXTRA_LARGE
SIZE_LARGE
SIZE_MEDIUM
SIZE_SMALL
"""
if "image" not in self.info:
self.info["image"] = _extract_all(
self._request(self.ws_prefix + ".getInfo", cacheable=True),
"image")
return self.info["image"][size]
def get_tracks(self):
"""Returns the list of Tracks on this album."""
@ -1981,8 +1981,9 @@ class Track(_Opus):
__hash__ = _Opus.__hash__
def __init__(self, artist, title, network, username=None):
super(Track, self).__init__(artist, title, network, "track", username)
def __init__(self, artist, title, network, username=None, info=None):
super(Track, self).__init__(artist, title, network, "track", username,
info)
def get_correction(self):
"""Returns the corrected track name."""
@ -2560,11 +2561,13 @@ class AlbumSearch(_Search):
seq = []
for node in master_node.getElementsByTagName("album"):
seq.append(Album(
_extract(node, "artist"),
_extract(node, "name"),
self.network,
info={"image": _extract_all(node, "image")}),
seq.append(
Album(
_extract(node, "artist"),
_extract(node, "name"),
self.network,
info={"image": _extract_all(node, "image")},
),
)
return seq
@ -2583,8 +2586,11 @@ class ArtistSearch(_Search):
seq = []
for node in master_node.getElementsByTagName("artist"):
artist = Artist(_extract(node, "name"), self.network,
info={"image": _extract_all(node, "image")})
artist = Artist(
_extract(node, "name"),
self.network,
info={"image": _extract_all(node, "image")},
)
artist.listener_count = _number(_extract(node, "listeners"))
seq.append(artist)
@ -2615,7 +2621,9 @@ class TrackSearch(_Search):
track = Track(
_extract(node, "artist"),
_extract(node, "name"),
self.network)
self.network,
info={"image": _extract_all(node, "image")},
)
track.listener_count = _number(_extract(node, "listeners"))
seq.append(track)

View file

@ -380,6 +380,27 @@ class TestPyLastNetwork(PyLastTestCase):
self.assertIsInstance(results, list)
self.assertIsInstance(results[0], pylast.Track)
def test_track_search_images(self):
# Arrange
artist = "Nirvana"
track = "Smells Like Teen Spirit"
search = self.network.search_for_track(artist, track)
# Act
results = search.get_next_page()
images = results[0].info["image"]
# Assert
self.assertEqual(len(images), 4)
self.assert_startswith(images[pylast.SIZE_SMALL], "https://")
self.assert_endswith(images[pylast.SIZE_SMALL], ".png")
self.assertIn("/34s/", images[pylast.SIZE_SMALL])
self.assert_startswith(images[pylast.SIZE_EXTRA_LARGE], "https://")
self.assert_endswith(images[pylast.SIZE_EXTRA_LARGE], ".png")
self.assertIn("/300x300/", images[pylast.SIZE_EXTRA_LARGE])
def test_search_get_total_result_count(self):
# Arrange
artist = "Nirvana"