Store track images for future use, refactor album image fetching
This commit is contained in:
parent
60dea38d10
commit
70aad87a1b
|
@ -1431,6 +1431,21 @@ class _Opus(_BaseObject, _Taggable):
|
||||||
|
|
||||||
return self.artist
|
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):
|
def get_title(self, properly_capitalized=False):
|
||||||
"""Returns the artist or track title."""
|
"""Returns the artist or track title."""
|
||||||
if properly_capitalized:
|
if properly_capitalized:
|
||||||
|
@ -1499,21 +1514,6 @@ class Album(_Opus):
|
||||||
super(Album, self).__init__(artist, title, network, "album", username,
|
super(Album, self).__init__(artist, title, network, "album", username,
|
||||||
info)
|
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):
|
def get_tracks(self):
|
||||||
"""Returns the list of Tracks on this album."""
|
"""Returns the list of Tracks on this album."""
|
||||||
|
|
||||||
|
@ -1981,8 +1981,9 @@ class Track(_Opus):
|
||||||
|
|
||||||
__hash__ = _Opus.__hash__
|
__hash__ = _Opus.__hash__
|
||||||
|
|
||||||
def __init__(self, artist, title, network, username=None):
|
def __init__(self, artist, title, network, username=None, info=None):
|
||||||
super(Track, self).__init__(artist, title, network, "track", username)
|
super(Track, self).__init__(artist, title, network, "track", username,
|
||||||
|
info)
|
||||||
|
|
||||||
def get_correction(self):
|
def get_correction(self):
|
||||||
"""Returns the corrected track name."""
|
"""Returns the corrected track name."""
|
||||||
|
@ -2560,11 +2561,13 @@ class AlbumSearch(_Search):
|
||||||
|
|
||||||
seq = []
|
seq = []
|
||||||
for node in master_node.getElementsByTagName("album"):
|
for node in master_node.getElementsByTagName("album"):
|
||||||
seq.append(Album(
|
seq.append(
|
||||||
|
Album(
|
||||||
_extract(node, "artist"),
|
_extract(node, "artist"),
|
||||||
_extract(node, "name"),
|
_extract(node, "name"),
|
||||||
self.network,
|
self.network,
|
||||||
info={"image": _extract_all(node, "image")}),
|
info={"image": _extract_all(node, "image")},
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
return seq
|
return seq
|
||||||
|
@ -2583,8 +2586,11 @@ class ArtistSearch(_Search):
|
||||||
|
|
||||||
seq = []
|
seq = []
|
||||||
for node in master_node.getElementsByTagName("artist"):
|
for node in master_node.getElementsByTagName("artist"):
|
||||||
artist = Artist(_extract(node, "name"), self.network,
|
artist = Artist(
|
||||||
info={"image": _extract_all(node, "image")})
|
_extract(node, "name"),
|
||||||
|
self.network,
|
||||||
|
info={"image": _extract_all(node, "image")},
|
||||||
|
)
|
||||||
artist.listener_count = _number(_extract(node, "listeners"))
|
artist.listener_count = _number(_extract(node, "listeners"))
|
||||||
seq.append(artist)
|
seq.append(artist)
|
||||||
|
|
||||||
|
@ -2615,7 +2621,9 @@ class TrackSearch(_Search):
|
||||||
track = Track(
|
track = Track(
|
||||||
_extract(node, "artist"),
|
_extract(node, "artist"),
|
||||||
_extract(node, "name"),
|
_extract(node, "name"),
|
||||||
self.network)
|
self.network,
|
||||||
|
info={"image": _extract_all(node, "image")},
|
||||||
|
)
|
||||||
track.listener_count = _number(_extract(node, "listeners"))
|
track.listener_count = _number(_extract(node, "listeners"))
|
||||||
seq.append(track)
|
seq.append(track)
|
||||||
|
|
||||||
|
|
|
@ -380,6 +380,27 @@ class TestPyLastNetwork(PyLastTestCase):
|
||||||
self.assertIsInstance(results, list)
|
self.assertIsInstance(results, list)
|
||||||
self.assertIsInstance(results[0], pylast.Track)
|
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):
|
def test_search_get_total_result_count(self):
|
||||||
# Arrange
|
# Arrange
|
||||||
artist = "Nirvana"
|
artist = "Nirvana"
|
||||||
|
|
Loading…
Reference in a new issue