Implement artist/album/track.getPlaylinks, closes #74

This commit is contained in:
hugovk 2014-03-05 00:41:26 +02:00
parent e02893bbaa
commit 3d169178ae
3 changed files with 83 additions and 0 deletions

1
.gitignore vendored
View file

@ -55,3 +55,4 @@ docs/_build/
# Test files
test_pylast.yaml
lastfm.txt.pkl
secrets.sh

View file

@ -699,6 +699,39 @@ class _Network(object):
if remaining_tracks:
self.scrobble_many(remaining_tracks)
def get_play_links(self, type, things, cacheable=True):
method = type + ".getPlaylinks"
params = {}
for i, thing in enumerate(things):
if type == "artist":
params['artist[' + str(i) + ']'] = thing
elif type == "album":
params['artist[' + str(i) + ']'] = thing.artist
params['album[' + str(i) + ']'] = thing.title
elif type == "track":
params['artist[' + str(i) + ']'] = thing.artist
params['track[' + str(i) + ']'] = thing.title
doc = _Request(self, method, params).execute(cacheable)
seq = []
for node in doc.getElementsByTagName("externalids"):
spotify = _extract(node, "spotify")
seq.append(spotify)
return seq
def get_artist_play_links(self, artists, cacheable=True):
return self.get_play_links("artist", artists)
def get_album_play_links(self, albums, cacheable=True):
return self.get_play_links("album", albums)
def get_track_play_links(self, tracks, cacheable=True):
return self.get_play_links("track", tracks)
class LastFMNetwork(_Network):
"""A Last.fm network object

View file

@ -972,6 +972,7 @@ class TestPyLast(unittest.TestCase):
# Assert
self.assertGreaterEqual(len(metros), 1)
self.assertEqual(type(metros[0]), pylast.Metro)
self.assertEqual(metros[0].get_country(), "Poland")
def test_geo_get_top_artists(self):
@ -1009,6 +1010,54 @@ class TestPyLast(unittest.TestCase):
self.assertNotEqual(metro, pylast.Metro("Wellington", "New Zealand", self.network))
def test_get_album_play_links(self):
# Arrange
album1 = self.network.get_album(artist = "Portishead", title = "Dummy")
album2 = self.network.get_album(artist = "Radiohead", title = "OK Computer")
albums = [album1, album2]
# Act
links = self.network.get_album_play_links(albums)
# Assert
self.assertEqual(type(links), list)
self.assertEqual(len(links), 2)
# How permanent are Spotify IDs? If they change, make tests more robust
self.assertEqual(links[0], "spotify:album:3gxOtUSRzweDWBKlpj7cG6")
self.assertEqual(links[1], "spotify:album:2fGCAYUMssLKiUAoNdxGLx")
def test_get_artist_play_links(self):
# Arrange
artists = ["Portishead", "Radiohead"]
# Act
links = self.network.get_artist_play_links(artists)
# Assert
self.assertEqual(type(links), list)
self.assertEqual(len(links), 2)
# How permanent are Spotify IDs? If they change, make tests more robust
self.assertEqual(links[0], "spotify:artist:6liAMWkVf5LH7YR9yfFy1Y")
self.assertEqual(links[1], "spotify:artist:4Z8W4fKeB5YxbusRsdQVPb")
def test_get_track_play_links(self):
# Arrange
track1 = self.network.get_track(artist = "Portishead", title = "Mysterons")
track2 = self.network.get_track(artist = "Radiohead", title = "Creep")
tracks = [track1, track2]
# Act
links = self.network.get_track_play_links(tracks)
# Assert
self.assertEqual(type(links), list)
self.assertEqual(len(links), 2)
# How permanent are Spotify IDs? If they change, make tests more robust
self.assertEqual(links[0], "spotify:track:2bt04YlMnqiwA3T6O9UqBO")
self.assertEqual(links[1], "spotify:track:0KYHSg38GsU1naJ5jh1llP")
if __name__ == '__main__':
# For quick testing of a single case (eg. test = "test_scrobble")