Move get_top_fans up to _BaseObject, removing duplication in Artist and Track
This commit is contained in:
parent
73cf5b3068
commit
caea5e129a
70
pylast.py
70
pylast.py
|
@ -1373,7 +1373,6 @@ class _BaseObject(object):
|
||||||
Returns the weekly artist charts for the week starting from the
|
Returns the weekly artist charts for the week starting from the
|
||||||
from_date value to the to_date value.
|
from_date value to the to_date value.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
params = self._get_params()
|
params = self._get_params()
|
||||||
if from_date and to_date:
|
if from_date and to_date:
|
||||||
params["from"] = from_date
|
params["from"] = from_date
|
||||||
|
@ -1436,6 +1435,31 @@ class _BaseObject(object):
|
||||||
|
|
||||||
return seq
|
return seq
|
||||||
|
|
||||||
|
def get_top_fans(self, limit=None, cacheable=True):
|
||||||
|
"""Returns a list of the Users who played this the most.
|
||||||
|
# Parameters:
|
||||||
|
* limit int: Max elements.
|
||||||
|
# For Artist/Track
|
||||||
|
"""
|
||||||
|
|
||||||
|
doc = self._request(self.ws_prefix + '.getTopFans', cacheable)
|
||||||
|
|
||||||
|
seq = []
|
||||||
|
|
||||||
|
elements = doc.getElementsByTagName('user')
|
||||||
|
|
||||||
|
for element in elements:
|
||||||
|
if limit and len(seq) >= limit:
|
||||||
|
break
|
||||||
|
|
||||||
|
name = _extract(element, 'name')
|
||||||
|
weight = _number(_extract(element, 'weight'))
|
||||||
|
|
||||||
|
seq.append(TopItem(User(name, self.network), weight))
|
||||||
|
|
||||||
|
return seq
|
||||||
|
|
||||||
|
|
||||||
class _Taggable(object):
|
class _Taggable(object):
|
||||||
"""Common functions for classes with tags."""
|
"""Common functions for classes with tags."""
|
||||||
|
|
||||||
|
@ -1975,29 +1999,6 @@ class Artist(_BaseObject, _Taggable):
|
||||||
return self._get_things(
|
return self._get_things(
|
||||||
"getTopTracks", "track", Track, params, cacheable)
|
"getTopTracks", "track", Track, params, cacheable)
|
||||||
|
|
||||||
def get_top_fans(self, limit=None):
|
|
||||||
"""Returns a list of the Users who played this artist the most.
|
|
||||||
# Parameters:
|
|
||||||
* limit int: Max elements.
|
|
||||||
"""
|
|
||||||
|
|
||||||
doc = self._request('artist.getTopFans', True)
|
|
||||||
|
|
||||||
seq = []
|
|
||||||
|
|
||||||
elements = doc.getElementsByTagName('user')
|
|
||||||
|
|
||||||
for element in elements:
|
|
||||||
if limit and len(seq) >= limit:
|
|
||||||
break
|
|
||||||
|
|
||||||
name = _extract(element, 'name')
|
|
||||||
weight = _number(_extract(element, 'weight'))
|
|
||||||
|
|
||||||
seq.append(TopItem(User(name, self.network), weight))
|
|
||||||
|
|
||||||
return seq
|
|
||||||
|
|
||||||
def share(self, users, message=None):
|
def share(self, users, message=None):
|
||||||
"""Shares this artist (sends out recommendations).
|
"""Shares this artist (sends out recommendations).
|
||||||
# Parameters:
|
# Parameters:
|
||||||
|
@ -2322,7 +2323,6 @@ class Event(_BaseObject):
|
||||||
|
|
||||||
self._request("event.Shout", False, params)
|
self._request("event.Shout", False, params)
|
||||||
|
|
||||||
|
|
||||||
class Country(_BaseObject):
|
class Country(_BaseObject):
|
||||||
"""A country at Last.fm."""
|
"""A country at Last.fm."""
|
||||||
|
|
||||||
|
@ -3210,26 +3210,6 @@ class Track(_BaseObject, _Taggable):
|
||||||
|
|
||||||
return seq
|
return seq
|
||||||
|
|
||||||
def get_top_fans(self, limit=None):
|
|
||||||
"""Returns a list of the Users who played this track."""
|
|
||||||
|
|
||||||
doc = self._request('track.getTopFans', True)
|
|
||||||
|
|
||||||
seq = []
|
|
||||||
|
|
||||||
elements = doc.getElementsByTagName('user')
|
|
||||||
|
|
||||||
for element in elements:
|
|
||||||
if limit and len(seq) >= limit:
|
|
||||||
break
|
|
||||||
|
|
||||||
name = _extract(element, 'name')
|
|
||||||
weight = _number(_extract(element, 'weight'))
|
|
||||||
|
|
||||||
seq.append(TopItem(User(name, self.network), weight))
|
|
||||||
|
|
||||||
return seq
|
|
||||||
|
|
||||||
def share(self, users, message=None):
|
def share(self, users, message=None):
|
||||||
"""Shares this track (sends out recommendations).
|
"""Shares this track (sends out recommendations).
|
||||||
* users: A list that can contain usernames, emails, User objects,
|
* users: A list that can contain usernames, emails, User objects,
|
||||||
|
|
|
@ -1003,6 +1003,13 @@ class TestPyLast(unittest.TestCase):
|
||||||
self.assertIn("spotify:track:", links[0])
|
self.assertIn("spotify:track:", links[0])
|
||||||
self.assertIn("spotify:track:", links[1])
|
self.assertIn("spotify:track:", links[1])
|
||||||
|
|
||||||
|
def helper_at_least_one_thing_in_top_list(self, things, expected_type):
|
||||||
|
# Assert
|
||||||
|
self.assertGreater(len(things), 1)
|
||||||
|
self.assertEqual(type(things), list)
|
||||||
|
self.assertEqual(type(things[0]), pylast.TopItem)
|
||||||
|
self.assertEqual(type(things[0].item), expected_type)
|
||||||
|
|
||||||
def helper_only_one_thing_in_top_list(self, things, expected_type):
|
def helper_only_one_thing_in_top_list(self, things, expected_type):
|
||||||
# Assert
|
# Assert
|
||||||
self.assertEqual(len(things), 1)
|
self.assertEqual(len(things), 1)
|
||||||
|
@ -1010,6 +1017,17 @@ class TestPyLast(unittest.TestCase):
|
||||||
self.assertEqual(type(things[0]), pylast.TopItem)
|
self.assertEqual(type(things[0]), pylast.TopItem)
|
||||||
self.assertEqual(type(things[0].item), expected_type)
|
self.assertEqual(type(things[0].item), expected_type)
|
||||||
|
|
||||||
|
def helper_two_different_things_in_top_list(self, things, expected_type):
|
||||||
|
# Assert
|
||||||
|
self.assertEqual(len(things), 2)
|
||||||
|
thing1 = things[0]
|
||||||
|
thing2 = things[1]
|
||||||
|
self.assertEqual(type(thing1), pylast.TopItem)
|
||||||
|
self.assertEqual(type(thing2), pylast.TopItem)
|
||||||
|
self.assertEqual(type(thing1.item), expected_type)
|
||||||
|
self.assertEqual(type(thing2.item), expected_type)
|
||||||
|
self.assertNotEqual(thing1, thing2)
|
||||||
|
|
||||||
def test_user_get_top_tags_with_limit(self):
|
def test_user_get_top_tags_with_limit(self):
|
||||||
# Arrange
|
# Arrange
|
||||||
user = self.network.get_user("RJ")
|
user = self.network.get_user("RJ")
|
||||||
|
@ -1044,17 +1062,6 @@ class TestPyLast(unittest.TestCase):
|
||||||
# Assert
|
# Assert
|
||||||
self.helper_only_one_thing_in_top_list(tracks, pylast.Track)
|
self.helper_only_one_thing_in_top_list(tracks, pylast.Track)
|
||||||
|
|
||||||
def helper_top_things(self, things, expected_type):
|
|
||||||
# Assert
|
|
||||||
self.assertEqual(len(things), 2)
|
|
||||||
thing1 = things[0]
|
|
||||||
thing2 = things[1]
|
|
||||||
self.assertEqual(type(thing1), pylast.TopItem)
|
|
||||||
self.assertEqual(type(thing2), pylast.TopItem)
|
|
||||||
self.assertEqual(type(thing1.item), expected_type)
|
|
||||||
self.assertEqual(type(thing2.item), expected_type)
|
|
||||||
self.assertNotEqual(thing1, thing2)
|
|
||||||
|
|
||||||
def test_artist_top_tracks(self):
|
def test_artist_top_tracks(self):
|
||||||
# Arrange
|
# Arrange
|
||||||
# Pick an artist with plenty of plays
|
# Pick an artist with plenty of plays
|
||||||
|
@ -1064,7 +1071,7 @@ class TestPyLast(unittest.TestCase):
|
||||||
things = artist.get_top_tracks(limit=2)
|
things = artist.get_top_tracks(limit=2)
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
self.helper_top_things(things, pylast.Track)
|
self.helper_two_different_things_in_top_list(things, pylast.Track)
|
||||||
|
|
||||||
def test_artist_top_albums(self):
|
def test_artist_top_albums(self):
|
||||||
# Arrange
|
# Arrange
|
||||||
|
@ -1075,7 +1082,7 @@ class TestPyLast(unittest.TestCase):
|
||||||
things = artist.get_top_albums(limit=2)
|
things = artist.get_top_albums(limit=2)
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
self.helper_top_things(things, pylast.Album)
|
self.helper_two_different_things_in_top_list(things, pylast.Album)
|
||||||
|
|
||||||
def test_artist_top_fans(self):
|
def test_artist_top_fans(self):
|
||||||
# Arrange
|
# Arrange
|
||||||
|
@ -1086,7 +1093,7 @@ class TestPyLast(unittest.TestCase):
|
||||||
things = artist.get_top_fans(limit=2)
|
things = artist.get_top_fans(limit=2)
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
self.helper_top_things(things, pylast.User)
|
self.helper_two_different_things_in_top_list(things, pylast.User)
|
||||||
|
|
||||||
def test_country_top_tracks(self):
|
def test_country_top_tracks(self):
|
||||||
# Arrange
|
# Arrange
|
||||||
|
@ -1096,7 +1103,7 @@ class TestPyLast(unittest.TestCase):
|
||||||
things = country.get_top_tracks(limit=2)
|
things = country.get_top_tracks(limit=2)
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
self.helper_top_things(things, pylast.Track)
|
self.helper_two_different_things_in_top_list(things, pylast.Track)
|
||||||
|
|
||||||
def test_country_network_top_tracks(self):
|
def test_country_network_top_tracks(self):
|
||||||
# Arrange
|
# Arrange
|
||||||
|
@ -1104,7 +1111,7 @@ class TestPyLast(unittest.TestCase):
|
||||||
things = self.network.get_geo_top_tracks("Croatia", limit=2)
|
things = self.network.get_geo_top_tracks("Croatia", limit=2)
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
self.helper_top_things(things, pylast.Track)
|
self.helper_two_different_things_in_top_list(things, pylast.Track)
|
||||||
|
|
||||||
def test_tag_top_tracks(self):
|
def test_tag_top_tracks(self):
|
||||||
# Arrange
|
# Arrange
|
||||||
|
@ -1114,7 +1121,7 @@ class TestPyLast(unittest.TestCase):
|
||||||
things = tag.get_top_tracks(limit=2)
|
things = tag.get_top_tracks(limit=2)
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
self.helper_top_things(things, pylast.Track)
|
self.helper_two_different_things_in_top_list(things, pylast.Track)
|
||||||
|
|
||||||
def test_user_top_tracks(self):
|
def test_user_top_tracks(self):
|
||||||
# Arrange
|
# Arrange
|
||||||
|
@ -1124,7 +1131,7 @@ class TestPyLast(unittest.TestCase):
|
||||||
things = lastfm_user.get_top_tracks(limit=2)
|
things = lastfm_user.get_top_tracks(limit=2)
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
self.helper_top_things(things, pylast.Track)
|
self.helper_two_different_things_in_top_list(things, pylast.Track)
|
||||||
|
|
||||||
def helper_assert_chart(self, chart, expected_type):
|
def helper_assert_chart(self, chart, expected_type):
|
||||||
# Assert
|
# Assert
|
||||||
|
@ -1178,6 +1185,27 @@ class TestPyLast(unittest.TestCase):
|
||||||
self.helper_get_assert_charts(lastfm_user, dates[-1])
|
self.helper_get_assert_charts(lastfm_user, dates[-1])
|
||||||
|
|
||||||
|
|
||||||
|
def test_artist_top_fans(self):
|
||||||
|
# Arrange
|
||||||
|
artist = self.network.get_artist("Test Artist")
|
||||||
|
|
||||||
|
# Act
|
||||||
|
fans = artist.get_top_fans()
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
self.helper_at_least_one_thing_in_top_list(fans, pylast.User)
|
||||||
|
|
||||||
|
def test_track_top_fans(self):
|
||||||
|
# Arrange
|
||||||
|
track = self.network.get_track("The Cinematic Orchestra", "Postlude")
|
||||||
|
|
||||||
|
# Act
|
||||||
|
fans = track.get_top_fans()
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
self.helper_at_least_one_thing_in_top_list(fans, pylast.User)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
# For quick testing of a single case (eg. test = "test_scrobble")
|
# For quick testing of a single case (eg. test = "test_scrobble")
|
||||||
|
|
Loading…
Reference in a new issue