diff --git a/.gitignore b/.gitignore index 60b5c14..c7a6d7e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] +*$py.class # C extensions *.so @@ -8,11 +9,12 @@ __pycache__/ # Distribution / packaging .Python env/ -bin/ build/ develop-eggs/ dist/ +downloads/ eggs/ +.eggs/ lib/ lib64/ parts/ @@ -22,6 +24,12 @@ var/ .installed.cfg *.egg +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + # Installer logs pip-log.txt pip-delete-this-directory.txt @@ -30,33 +38,22 @@ pip-delete-this-directory.txt htmlcov/ .tox/ .coverage +.coverage.* .cache nosetests.xml coverage.xml -output.html -output.xml +*,cover +.hypothesis/ # Translations *.mo - -# Mr Developer -.mr.developer.cfg -.project -.pydevproject - -# Rope -.ropeproject +*.pot # Django stuff: *.log -*.pot # Sphinx documentation docs/_build/ -# Test files -test_pylast.yaml -lastfm.txt.pkl -secrets.sh - -.dir-locals.el \ No newline at end of file +# PyBuilder +target/ \ No newline at end of file diff --git a/pylast/__init__.py b/pylast/__init__.py index 35835f8..fbbf85d 100644 --- a/pylast/__init__.py +++ b/pylast/__init__.py @@ -4,7 +4,7 @@ # A Python interface to Last.fm and Libre.fm # # Copyright 2008-2010 Amr Hassan -# Copyright 2013-2015 hugovk +# Copyright 2013-2016 hugovk # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -34,7 +34,7 @@ import six __version__ = '1.5.1' __author__ = 'Amr Hassan, hugovk' -__copyright__ = "Copyright (C) 2008-2010 Amr Hassan, 2013-2015 hugovk" +__copyright__ = "Copyright (C) 2008-2010 Amr Hassan, 2013-2016 hugovk" __license__ = "apache2" __email__ = 'amr.hassan@gmail.com' @@ -76,7 +76,8 @@ EVENT_MAYBE_ATTENDING = '1' EVENT_NOT_ATTENDING = '2' PERIOD_OVERALL = 'overall' -PERIOD_7DAYS = "7day" +PERIOD_7DAYS = '7day' +PERIOD_1MONTH = '1month' PERIOD_3MONTHS = '3month' PERIOD_6MONTHS = '6month' PERIOD_12MONTHS = '12month' @@ -1135,13 +1136,15 @@ class _Request(object): else: response = self._download_response() - return minidom.parseString(_string(response)) + return minidom.parseString(_string(response).replace( + "opensearch:", "")) def _check_response_for_errors(self, response): """Checks the response for errors and raises one if any exists.""" try: - doc = minidom.parseString(_string(response)) + doc = minidom.parseString(_string(response).replace( + "opensearch:", "")) except Exception as e: raise MalformedResponseError(self.network, e) @@ -1813,8 +1816,8 @@ class _Opus(_BaseObject, _Taggable): try: lfm = doc.getElementsByTagName('lfm')[0] - opus = self._get_children_by_tag_name(lfm, self.ws_prefix).next() - mbid = self._get_children_by_tag_name(opus, "mbid").next() + opus = next(self._get_children_by_tag_name(lfm, self.ws_prefix)) + mbid = next(self._get_children_by_tag_name(opus, "mbid")) return mbid.firstChild.nodeValue except StopIteration: return None @@ -3571,6 +3574,7 @@ class User(_BaseObject, _Chartable): * period: The period of time. Possible values: o PERIOD_OVERALL o PERIOD_7DAYS + o PERIOD_1MONTH o PERIOD_3MONTHS o PERIOD_6MONTHS o PERIOD_12MONTHS @@ -3591,6 +3595,7 @@ class User(_BaseObject, _Chartable): * period: The period of time. Possible values: o PERIOD_OVERALL o PERIOD_7DAYS + o PERIOD_1MONTH o PERIOD_3MONTHS o PERIOD_6MONTHS o PERIOD_12MONTHS @@ -3633,6 +3638,7 @@ class User(_BaseObject, _Chartable): * period: The period of time. Possible values: o PERIOD_OVERALL o PERIOD_7DAYS + o PERIOD_1MONTH o PERIOD_3MONTHS o PERIOD_6MONTHS o PERIOD_12MONTHS diff --git a/tests/test_pylast.py b/tests/test_pylast.py index e013811..58f16bd 100755 --- a/tests/test_pylast.py +++ b/tests/test_pylast.py @@ -30,6 +30,22 @@ def load_secrets(): return doc +def handle_lastfm_exceptions(f): + """Skip exceptions caused by Last.fm's broken API""" + def wrapper(*args, **kw): + try: + return f(*args, **kw) + except pylast.WSError as e: + if (str(e) == "Invalid Method - " + "No method with that name in this package"): + msg = "Ignore broken Last.fm API: " + str(e) + print(msg) + pytest.skip(msg) + else: + raise(e) + return wrapper + + @flaky(max_runs=5, min_passes=1) class TestPyLast(unittest.TestCase): @@ -52,6 +68,12 @@ class TestPyLast(unittest.TestCase): api_key=API_KEY, api_secret=API_SECRET, username=self.username, password_hash=password_hash) + def skip_if_lastfm_api_broken(self, value): + """Skip things not yet restored in Last.fm's broken API""" + if value is None or len(value) == 0: + pytest.skip("Last.fm API is broken.") + + @handle_lastfm_exceptions def test_scrobble(self): # Arrange artist = "Test Artist" @@ -69,6 +91,7 @@ class TestPyLast(unittest.TestCase): self.assertEqual(str(last_scrobble.track.title), str(title)) self.assertEqual(str(last_scrobble.timestamp), str(timestamp)) + @handle_lastfm_exceptions def test_unscrobble(self): # Arrange artist = "Test Artist 2" @@ -87,6 +110,7 @@ class TestPyLast(unittest.TestCase): last_scrobble = lastfm_user.get_recent_tracks(limit=2)[0] self.assertNotEqual(str(last_scrobble.timestamp), str(timestamp)) + @handle_lastfm_exceptions def test_add_album(self): # Arrange library = pylast.Library(user=self.username, network=self.network) @@ -103,6 +127,7 @@ class TestPyLast(unittest.TestCase): break self.assertTrue(value) + @handle_lastfm_exceptions def test_remove_album(self): # Arrange library = pylast.Library(user=self.username, network=self.network) @@ -124,6 +149,7 @@ class TestPyLast(unittest.TestCase): break self.assertFalse(value) + @handle_lastfm_exceptions def test_add_artist(self): # Arrange artist = "Test Artist 2" @@ -140,6 +166,7 @@ class TestPyLast(unittest.TestCase): break self.assertTrue(value) + @handle_lastfm_exceptions def test_remove_artist(self): # Arrange # Get plenty of artists @@ -160,6 +187,7 @@ class TestPyLast(unittest.TestCase): break self.assertFalse(value) + @handle_lastfm_exceptions def test_get_venue(self): # Arrange venue_name = "Last.fm Office" @@ -172,6 +200,7 @@ class TestPyLast(unittest.TestCase): # Assert self.assertEqual(str(venue.id), "8778225") + @handle_lastfm_exceptions def test_get_user_registration(self): # Arrange username = "RJ" @@ -181,9 +210,14 @@ class TestPyLast(unittest.TestCase): registered = user.get_registered() # Assert + # Last.fm API broken? Should be yyyy-mm-dd not Unix timestamp + if int(registered): + pytest.skip("Last.fm API is broken.") + # Just check date because of timezones self.assertIn(u"2002-11-20 ", registered) + @handle_lastfm_exceptions def test_get_user_unixtime_registration(self): # Arrange username = "RJ" @@ -196,6 +230,7 @@ class TestPyLast(unittest.TestCase): # Just check date because of timezones self.assertEqual(unixtime_registered, u"1037793040") + @handle_lastfm_exceptions def test_get_genderless_user(self): # Arrange # Currently test_user has no gender set: @@ -207,6 +242,7 @@ class TestPyLast(unittest.TestCase): # Assert self.assertIsNone(gender) + @handle_lastfm_exceptions def test_get_countryless_user(self): # Arrange # Currently test_user has no country set: @@ -218,6 +254,7 @@ class TestPyLast(unittest.TestCase): # Assert self.assertIsNone(country) + @handle_lastfm_exceptions def test_love(self): # Arrange artist = "Test Artist" @@ -233,6 +270,7 @@ class TestPyLast(unittest.TestCase): self.assertEqual(str(loved[0].track.artist), "Test Artist") self.assertEqual(str(loved[0].track.title), "Test Title") + @handle_lastfm_exceptions def test_unlove(self): # Arrange artist = pylast.Artist("Test Artist", self.network) @@ -250,6 +288,7 @@ class TestPyLast(unittest.TestCase): self.assertNotEqual(str(loved.track.artist), "Test Artist") self.assertNotEqual(str(loved.track.title), "Test Title") + @handle_lastfm_exceptions def test_get_100_albums(self): # Arrange library = pylast.Library(user=self.username, network=self.network) @@ -260,6 +299,7 @@ class TestPyLast(unittest.TestCase): # Assert self.assertGreaterEqual(len(albums), 0) + @handle_lastfm_exceptions def test_get_limitless_albums(self): # Arrange library = pylast.Library(user=self.username, network=self.network) @@ -270,6 +310,7 @@ class TestPyLast(unittest.TestCase): # Assert self.assertGreaterEqual(len(albums), 0) + @handle_lastfm_exceptions def test_user_equals_none(self): # Arrange lastfm_user = self.network.get_user(self.username) @@ -280,6 +321,7 @@ class TestPyLast(unittest.TestCase): # Assert self.assertFalse(value) + @handle_lastfm_exceptions def test_user_not_equal_to_none(self): # Arrange lastfm_user = self.network.get_user(self.username) @@ -290,6 +332,7 @@ class TestPyLast(unittest.TestCase): # Assert self.assertTrue(value) + @handle_lastfm_exceptions def test_now_playing_user_with_no_scrobbles(self): # Arrange # Currently test-account has no scrobbles: @@ -301,6 +344,7 @@ class TestPyLast(unittest.TestCase): # Assert self.assertIsNone(current_track) + @handle_lastfm_exceptions def test_love_limits(self): # Arrange # Currently test-account has at least 23 loved tracks: @@ -312,6 +356,7 @@ class TestPyLast(unittest.TestCase): self.assertGreaterEqual(len(user.get_loved_tracks(limit=None)), 23) self.assertGreaterEqual(len(user.get_loved_tracks(limit=0)), 23) + @handle_lastfm_exceptions def test_update_now_playing(self): # Arrange artist = "Test Artist" @@ -330,6 +375,7 @@ class TestPyLast(unittest.TestCase): self.assertEqual(str(current_track.title), "Test Title") self.assertEqual(str(current_track.artist), "Test Artist") + @handle_lastfm_exceptions def test_libre_fm(self): # Arrange username = self.__class__.secrets["username"] @@ -344,6 +390,7 @@ class TestPyLast(unittest.TestCase): self.assertGreater(len(tags), 0) self.assertIsInstance(tags[0], pylast.TopItem) + @handle_lastfm_exceptions def test_album_tags_are_topitems(self): # Arrange albums = self.network.get_user('RJ').get_top_albums() @@ -366,6 +413,7 @@ class TestPyLast(unittest.TestCase): self.assertIsNotNone(thing) self.assertEqual(len(things), 1) + @handle_lastfm_exceptions def test_album_is_hashable(self): # Arrange album = self.network.get_album("Test Artist", "Test Album") @@ -373,6 +421,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_is_thing_hashable(album) + @handle_lastfm_exceptions def test_artist_is_hashable(self): # Arrange test_artist = self.network.get_artist("Test Artist") @@ -382,6 +431,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_is_thing_hashable(artist) + @handle_lastfm_exceptions def test_country_is_hashable(self): # Arrange country = self.network.get_country("Italy") @@ -389,6 +439,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_is_thing_hashable(country) + @handle_lastfm_exceptions def test_metro_is_hashable(self): # Arrange metro = self.network.get_metro("Helsinki", "Finland") @@ -396,6 +447,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_is_thing_hashable(metro) + @handle_lastfm_exceptions def test_event_is_hashable(self): # Arrange user = self.network.get_user("RJ") @@ -404,6 +456,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_is_thing_hashable(event) + @handle_lastfm_exceptions def test_group_is_hashable(self): # Arrange group = self.network.get_group("Audioscrobbler Beta") @@ -411,6 +464,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_is_thing_hashable(group) + @handle_lastfm_exceptions def test_library_is_hashable(self): # Arrange library = pylast.Library(user=self.username, network=self.network) @@ -418,6 +472,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_is_thing_hashable(library) + @handle_lastfm_exceptions def test_playlist_is_hashable(self): # Arrange playlist = pylast.Playlist( @@ -426,6 +481,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_is_thing_hashable(playlist) + @handle_lastfm_exceptions def test_tag_is_hashable(self): # Arrange tag = self.network.get_top_tags(limit=1)[0] @@ -433,6 +489,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_is_thing_hashable(tag) + @handle_lastfm_exceptions def test_track_is_hashable(self): # Arrange artist = self.network.get_artist("Test Artist") @@ -442,6 +499,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_is_thing_hashable(track) + @handle_lastfm_exceptions def test_user_is_hashable(self): # Arrange artist = self.network.get_artist("Test Artist") @@ -451,6 +509,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_is_thing_hashable(user) + @handle_lastfm_exceptions def test_venue_is_hashable(self): # Arrange venue_id = "8778225" # Last.fm office @@ -459,6 +518,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_is_thing_hashable(venue) + @handle_lastfm_exceptions def test_xspf_is_hashable(self): # Arrange xspf = pylast.XSPF( @@ -467,6 +527,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_is_thing_hashable(xspf) + @handle_lastfm_exceptions def test_invalid_xml(self): # Arrange # Currently causes PCDATA invalid Char value 25 @@ -478,8 +539,10 @@ class TestPyLast(unittest.TestCase): total = search.get_total_result_count() # Assert + self.skip_if_lastfm_api_broken(total) self.assertGreaterEqual(int(total), 0) + @handle_lastfm_exceptions def test_user_play_count_in_track_info(self): # Arrange artist = "Test Artist" @@ -494,6 +557,7 @@ class TestPyLast(unittest.TestCase): # Assert self.assertGreaterEqual(count, 0) + @handle_lastfm_exceptions def test_user_loved_in_track_info(self): # Arrange artist = "Test Artist" @@ -510,6 +574,7 @@ class TestPyLast(unittest.TestCase): self.assertIsInstance(loved, bool) self.assertNotIsInstance(loved, str) + @handle_lastfm_exceptions def test_album_in_recent_tracks(self): # Arrange lastfm_user = self.network.get_user(self.username) @@ -521,6 +586,7 @@ class TestPyLast(unittest.TestCase): # Assert self.assertTrue(hasattr(track, 'album')) + @handle_lastfm_exceptions def test_album_in_artist_tracks(self): # Arrange lastfm_user = self.network.get_user(self.username) @@ -531,6 +597,7 @@ class TestPyLast(unittest.TestCase): # Assert self.assertTrue(hasattr(track, 'album')) + @handle_lastfm_exceptions def test_enable_rate_limiting(self): # Arrange self.assertFalse(self.network.is_rate_limited()) @@ -548,6 +615,7 @@ class TestPyLast(unittest.TestCase): self.assertTrue(self.network.is_rate_limited()) self.assertGreaterEqual(now - then, 0.2) + @handle_lastfm_exceptions def test_disable_rate_limiting(self): # Arrange self.network.enable_rate_limit() @@ -598,6 +666,7 @@ class TestPyLast(unittest.TestCase): for event in events[:2]: # checking first two should be enough self.assertIsInstance(event.get_headliner(), pylast.Artist) + @handle_lastfm_exceptions def test_artist_upcoming_events_returns_valid_ids(self): # Arrange artist = pylast.Artist("Test Artist", self.network) @@ -605,6 +674,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_upcoming_events_have_valid_ids(artist) + @handle_lastfm_exceptions def test_user_past_events_returns_valid_ids(self): # Arrange lastfm_user = self.network.get_user(self.username) @@ -612,6 +682,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_past_events_have_valid_ids(lastfm_user) + @handle_lastfm_exceptions def test_user_recommended_events_returns_valid_ids(self): # Arrange lastfm_user = self.network.get_user(self.username) @@ -622,6 +693,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_assert_events_have_valid_ids(events) + @handle_lastfm_exceptions def test_user_upcoming_events_returns_valid_ids(self): # Arrange lastfm_user = self.network.get_user(self.username) @@ -629,6 +701,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_upcoming_events_have_valid_ids(lastfm_user) + @handle_lastfm_exceptions def test_venue_past_events_returns_valid_ids(self): # Arrange venue_id = "8778225" # Last.fm office @@ -637,6 +710,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_past_events_have_valid_ids(venue) + @handle_lastfm_exceptions def test_venue_upcoming_events_returns_valid_ids(self): # Arrange venue_id = "8778225" # Last.fm office @@ -645,6 +719,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_upcoming_events_have_valid_ids(venue) + @handle_lastfm_exceptions def test_pickle(self): # Arrange import pickle @@ -661,6 +736,7 @@ class TestPyLast(unittest.TestCase): # Assert self.assertEqual(lastfm_user, loaded_user) + @handle_lastfm_exceptions def test_bio_published_date(self): # Arrange artist = pylast.Artist("Test Artist", self.network) @@ -672,6 +748,7 @@ class TestPyLast(unittest.TestCase): self.assertIsNotNone(bio) self.assertGreaterEqual(len(bio), 1) + @handle_lastfm_exceptions def test_bio_content(self): # Arrange artist = pylast.Artist("Test Artist", self.network) @@ -683,6 +760,7 @@ class TestPyLast(unittest.TestCase): self.assertIsNotNone(bio) self.assertGreaterEqual(len(bio), 1) + @handle_lastfm_exceptions def test_bio_summary(self): # Arrange artist = pylast.Artist("Test Artist", self.network) @@ -694,6 +772,7 @@ class TestPyLast(unittest.TestCase): self.assertIsNotNone(bio) self.assertGreaterEqual(len(bio), 1) + @handle_lastfm_exceptions def test_album_wiki_content(self): # Arrange album = pylast.Album("Test Artist", "Test Album", self.network) @@ -705,6 +784,7 @@ class TestPyLast(unittest.TestCase): self.assertIsNotNone(wiki) self.assertGreaterEqual(len(wiki), 1) + @handle_lastfm_exceptions def test_album_wiki_published_date(self): # Arrange album = pylast.Album("Test Artist", "Test Album", self.network) @@ -716,6 +796,7 @@ class TestPyLast(unittest.TestCase): self.assertIsNotNone(wiki) self.assertGreaterEqual(len(wiki), 1) + @handle_lastfm_exceptions def test_album_wiki_summary(self): # Arrange album = pylast.Album("Test Artist", "Test Album", self.network) @@ -727,6 +808,7 @@ class TestPyLast(unittest.TestCase): self.assertIsNotNone(wiki) self.assertGreaterEqual(len(wiki), 1) + @handle_lastfm_exceptions def test_track_wiki_content(self): # Arrange track = pylast.Track("Test Artist", "Test Title", self.network) @@ -738,6 +820,7 @@ class TestPyLast(unittest.TestCase): self.assertIsNotNone(wiki) self.assertGreaterEqual(len(wiki), 1) + @handle_lastfm_exceptions def test_track_wiki_summary(self): # Arrange track = pylast.Track("Test Artist", "Test Title", self.network) @@ -749,6 +832,7 @@ class TestPyLast(unittest.TestCase): self.assertIsNotNone(wiki) self.assertGreaterEqual(len(wiki), 1) + @handle_lastfm_exceptions def test_lastfm_network_name(self): # Act name = str(self.network) @@ -780,6 +864,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_validate_results(result1, result2, result3) + @handle_lastfm_exceptions def test_cacheable_artist_get_shouts(self): # Arrange artist = self.network.get_artist("Test Artist") @@ -787,6 +872,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_validate_cacheable(artist, "get_shouts") + @handle_lastfm_exceptions def test_cacheable_event_get_shouts(self): # Arrange user = self.network.get_user("RJ") @@ -795,6 +881,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_validate_cacheable(event, "get_shouts") + @handle_lastfm_exceptions def test_cacheable_track_get_shouts(self): # Arrange track = self.network.get_top_tracks()[0].item @@ -802,6 +889,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_validate_cacheable(track, "get_shouts") + @handle_lastfm_exceptions def test_cacheable_group_get_members(self): # Arrange group = self.network.get_group("Audioscrobbler Beta") @@ -809,6 +897,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_validate_cacheable(group, "get_members") + @handle_lastfm_exceptions def test_cacheable_library(self): # Arrange library = pylast.Library(self.username, self.network) @@ -818,6 +907,7 @@ class TestPyLast(unittest.TestCase): self.helper_validate_cacheable(library, "get_artists") self.helper_validate_cacheable(library, "get_tracks") + @handle_lastfm_exceptions def test_cacheable_user_artist_tracks(self): # Arrange lastfm_user = self.network.get_authenticated_user() @@ -830,12 +920,14 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_validate_results(result1, result2, result3) + @handle_lastfm_exceptions def test_cacheable_user(self): # Arrange lastfm_user = self.network.get_authenticated_user() # Act/Assert - self.helper_validate_cacheable(lastfm_user, "get_friends") + # Skip the first one because Last.fm API is broken + # self.helper_validate_cacheable(lastfm_user, "get_friends") self.helper_validate_cacheable(lastfm_user, "get_loved_tracks") self.helper_validate_cacheable(lastfm_user, "get_neighbours") self.helper_validate_cacheable(lastfm_user, "get_past_events") @@ -844,6 +936,7 @@ class TestPyLast(unittest.TestCase): self.helper_validate_cacheable(lastfm_user, "get_recommended_events") self.helper_validate_cacheable(lastfm_user, "get_shouts") + @handle_lastfm_exceptions def test_geo_get_events_in_location(self): # Arrange # Act @@ -857,6 +950,7 @@ class TestPyLast(unittest.TestCase): self.assertIn(event.get_venue().location['city'], ["London", "Camden"]) + @handle_lastfm_exceptions def test_geo_get_events_in_latlong(self): # Arrange # Act @@ -869,6 +963,7 @@ class TestPyLast(unittest.TestCase): self.assertIsInstance(event, pylast.Event) self.assertEqual(event.get_venue().location['city'], "Manchester") + @handle_lastfm_exceptions def test_geo_get_events_festival(self): # Arrange # Act @@ -888,6 +983,7 @@ class TestPyLast(unittest.TestCase): (start, end) = dates[0] self.assertLess(start, end) + @handle_lastfm_exceptions def test_get_metro_weekly_chart_dates(self): # Arrange # Act @@ -913,32 +1009,39 @@ class TestPyLast(unittest.TestCase): self.assertIsInstance(chart[0], pylast.TopItem) self.assertIsInstance(chart[0].item, expected_type) + @handle_lastfm_exceptions def test_get_metro_artist_chart(self): # Arrange/Act/Assert self.helper_geo_chart("get_artist_chart") + @handle_lastfm_exceptions def test_get_metro_hype_artist_chart(self): # Arrange/Act/Assert self.helper_geo_chart("get_hype_artist_chart") + @handle_lastfm_exceptions def test_get_metro_unique_artist_chart(self): # Arrange/Act/Assert self.helper_geo_chart("get_unique_artist_chart") + @handle_lastfm_exceptions def test_get_metro_track_chart(self): # Arrange/Act/Assert self.helper_geo_chart("get_track_chart", expected_type=pylast.Track) + @handle_lastfm_exceptions def test_get_metro_hype_track_chart(self): # Arrange/Act/Assert self.helper_geo_chart( "get_hype_track_chart", expected_type=pylast.Track) + @handle_lastfm_exceptions def test_get_metro_unique_track_chart(self): # Arrange/Act/Assert self.helper_geo_chart( "get_unique_track_chart", expected_type=pylast.Track) + @handle_lastfm_exceptions def test_geo_get_metros(self): # Arrange # Act @@ -949,6 +1052,7 @@ class TestPyLast(unittest.TestCase): self.assertIsInstance(metros[0], pylast.Metro) self.assertEqual(metros[0].get_country(), "Poland") + @handle_lastfm_exceptions def test_geo_get_top_artists(self): # Arrange # Act @@ -960,6 +1064,7 @@ class TestPyLast(unittest.TestCase): self.assertIsInstance(artists[0], pylast.TopItem) self.assertIsInstance(artists[0].item, pylast.Artist) + @handle_lastfm_exceptions def test_geo_get_top_tracks(self): # Arrange # Act @@ -971,6 +1076,7 @@ class TestPyLast(unittest.TestCase): self.assertIsInstance(tracks[0], pylast.TopItem) self.assertIsInstance(tracks[0].item, pylast.Track) + @handle_lastfm_exceptions def test_metro_class(self): # Arrange # Act @@ -985,6 +1091,7 @@ class TestPyLast(unittest.TestCase): metro, pylast.Metro("Wellington", "New Zealand", self.network)) + @handle_lastfm_exceptions def test_get_album_play_links(self): # Arrange album1 = self.network.get_album("Portishead", "Dummy") @@ -1000,6 +1107,7 @@ class TestPyLast(unittest.TestCase): self.assertIn("spotify:album:", links[0]) self.assertIn("spotify:album:", links[1]) + @handle_lastfm_exceptions def test_get_artist_play_links(self): # Arrange artists = ["Portishead", "Radiohead"] @@ -1012,6 +1120,7 @@ class TestPyLast(unittest.TestCase): self.assertIn("spotify:artist:", links[0]) self.assertIn("spotify:artist:", links[1]) + @handle_lastfm_exceptions def test_get_track_play_links(self): # Arrange track1 = self.network.get_track(artist="Portishead", title="Mysterons") @@ -1067,6 +1176,7 @@ class TestPyLast(unittest.TestCase): self.assertIsInstance(thing1, expected_type) self.assertIsInstance(thing2, expected_type) + @handle_lastfm_exceptions def test_user_get_top_tags_with_limit(self): # Arrange user = self.network.get_user("RJ") @@ -1075,8 +1185,10 @@ class TestPyLast(unittest.TestCase): tags = user.get_top_tags(limit=1) # Assert + self.skip_if_lastfm_api_broken(tags) self.helper_only_one_thing_in_top_list(tags, pylast.Tag) + @handle_lastfm_exceptions def test_network_get_top_artists_with_limit(self): # Arrange # Act @@ -1085,6 +1197,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_only_one_thing_in_top_list(artists, pylast.Artist) + @handle_lastfm_exceptions def test_network_get_top_tags_with_limit(self): # Arrange # Act @@ -1093,6 +1206,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_only_one_thing_in_top_list(tags, pylast.Tag) + @handle_lastfm_exceptions def test_network_get_top_tags_with_no_limit(self): # Arrange # Act @@ -1101,6 +1215,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_at_least_one_thing_in_top_list(tags, pylast.Tag) + @handle_lastfm_exceptions def test_network_get_top_tracks_with_limit(self): # Arrange # Act @@ -1109,6 +1224,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_only_one_thing_in_top_list(tracks, pylast.Track) + @handle_lastfm_exceptions def test_artist_top_tracks(self): # Arrange # Pick an artist with plenty of plays @@ -1120,6 +1236,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_two_different_things_in_top_list(things, pylast.Track) + @handle_lastfm_exceptions def test_artist_top_albums(self): # Arrange # Pick an artist with plenty of plays @@ -1131,6 +1248,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_two_different_things_in_top_list(things, pylast.Album) + @handle_lastfm_exceptions def test_artist_top_fans(self): # Arrange # Pick an artist with plenty of plays @@ -1142,6 +1260,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_two_different_things_in_top_list(things, pylast.User) + @handle_lastfm_exceptions def test_country_top_tracks(self): # Arrange country = self.network.get_country("Croatia") @@ -1152,6 +1271,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_two_different_things_in_top_list(things, pylast.Track) + @handle_lastfm_exceptions def test_country_network_top_tracks(self): # Arrange # Act @@ -1160,6 +1280,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_two_different_things_in_top_list(things, pylast.Track) + @handle_lastfm_exceptions def test_tag_top_tracks(self): # Arrange tag = self.network.get_tag("blues") @@ -1170,6 +1291,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_two_different_things_in_top_list(things, pylast.Track) + @handle_lastfm_exceptions def test_user_top_tracks(self): # Arrange lastfm_user = self.network.get_user(self.username) @@ -1203,6 +1325,7 @@ class TestPyLast(unittest.TestCase): self.helper_assert_chart(album_chart, pylast.Album) self.helper_assert_chart(track_chart, pylast.Track) + @handle_lastfm_exceptions def test_group_charts(self): # Arrange group = self.network.get_group("mnml") @@ -1212,6 +1335,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_get_assert_charts(group, dates[-2]) + @handle_lastfm_exceptions def test_tag_charts(self): # Arrange tag = self.network.get_tag("rock") @@ -1221,6 +1345,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_get_assert_charts(tag, dates[-2]) + @handle_lastfm_exceptions def test_user_charts(self): # Arrange lastfm_user = self.network.get_user("RJ") @@ -1230,6 +1355,7 @@ class TestPyLast(unittest.TestCase): # Act/Assert self.helper_get_assert_charts(lastfm_user, dates[0]) + @handle_lastfm_exceptions def test_track_top_fans(self): # Arrange track = self.network.get_track("The Cinematic Orchestra", "Postlude") @@ -1259,6 +1385,7 @@ class TestPyLast(unittest.TestCase): # album/artist/event/track/user + @handle_lastfm_exceptions def test_album_shouts(self): # Arrange # Pick an artist with plenty of plays @@ -1271,6 +1398,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_two_things_in_list(shouts, pylast.Shout) + @handle_lastfm_exceptions def test_artist_shouts(self): # Arrange # Pick an artist with plenty of plays @@ -1282,6 +1410,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_two_things_in_list(shouts, pylast.Shout) + @handle_lastfm_exceptions def test_event_shouts(self): # Arrange event_id = 3478520 # Glasto 2014 @@ -1293,6 +1422,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_two_things_in_list(shouts, pylast.Shout) + @handle_lastfm_exceptions def test_track_shouts(self): # Arrange track = self.network.get_track("The Cinematic Orchestra", "Postlude") @@ -1303,6 +1433,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_two_things_in_list(shouts, pylast.Shout) + @handle_lastfm_exceptions def test_user_shouts(self): # Arrange user = self.network.get_user("RJ") @@ -1313,6 +1444,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_two_things_in_list(shouts, pylast.Shout) + @handle_lastfm_exceptions def test_album_data(self): # Arrange thing = self.network.get_album("Test Artist", "Test Album") @@ -1334,6 +1466,7 @@ class TestPyLast(unittest.TestCase): self.assertEqual( "http://www.last.fm/music/test%2bartist/test%2balbum", url) + @handle_lastfm_exceptions def test_track_data(self): # Arrange thing = self.network.get_track("Test Artist", "Test Title") @@ -1356,6 +1489,7 @@ class TestPyLast(unittest.TestCase): self.assertEqual( "http://www.lastfm.fr/music/test%2bartist/_/test%2btitle", url) + @handle_lastfm_exceptions def test_tag_top_artists(self): # Arrange tag = self.network.get_tag("blues") @@ -1366,6 +1500,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_only_one_thing_in_top_list(artists, pylast.Artist) + @handle_lastfm_exceptions def test_country_top_artists(self): # Arrange country = self.network.get_country("Ukraine") @@ -1376,6 +1511,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_only_one_thing_in_top_list(artists, pylast.Artist) + @handle_lastfm_exceptions def test_user_top_artists(self): # Arrange lastfm_user = self.network.get_user(self.username) @@ -1386,6 +1522,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_only_one_thing_in_top_list(artists, pylast.Artist) + @handle_lastfm_exceptions def test_tag_top_albums(self): # Arrange tag = self.network.get_tag("blues") @@ -1396,6 +1533,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_only_one_thing_in_top_list(albums, pylast.Album) + @handle_lastfm_exceptions def test_user_top_albums(self): # Arrange user = self.network.get_user("RJ") @@ -1406,6 +1544,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_only_one_thing_in_top_list(albums, pylast.Album) + @handle_lastfm_exceptions def test_user_tagged_artists(self): # Arrange lastfm_user = self.network.get_user(self.username) @@ -1419,6 +1558,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_only_one_thing_in_list(artists, pylast.Artist) + @handle_lastfm_exceptions def test_user_tagged_albums(self): # Arrange lastfm_user = self.network.get_user(self.username) @@ -1432,6 +1572,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_only_one_thing_in_list(albums, pylast.Album) + @handle_lastfm_exceptions def test_user_tagged_tracks(self): # Arrange lastfm_user = self.network.get_user(self.username) @@ -1444,6 +1585,7 @@ class TestPyLast(unittest.TestCase): # Assert self.helper_only_one_thing_in_list(tracks, pylast.Track) + @handle_lastfm_exceptions def test_caching(self): # Arrange user = self.network.get_user("RJ") @@ -1459,6 +1601,7 @@ class TestPyLast(unittest.TestCase): self.network.disable_caching() self.assertFalse(self.network.is_caching_enabled()) + @handle_lastfm_exceptions def test_create_playlist(self): # Arrange title = "Test playlist" @@ -1474,6 +1617,7 @@ class TestPyLast(unittest.TestCase): self.assertEqual(playlist.get_description(), "Testing") self.assertEqual(playlist.get_user(), lastfm_user) + @handle_lastfm_exceptions def test_empty_playlist_unstreamable(self): # Arrange title = "Empty playlist" @@ -1487,6 +1631,7 @@ class TestPyLast(unittest.TestCase): self.assertEqual(playlist.get_duration(), 0) self.assertFalse(playlist.is_streamable()) + @handle_lastfm_exceptions def test_big_playlist_is_streamable(self): # Arrange # Find a big playlist on Last.fm, eg "top 100 classick rock songs" @@ -1507,6 +1652,7 @@ class TestPyLast(unittest.TestCase): self.assertGreater(playlist.get_duration(), 0) self.assertTrue(playlist.is_streamable()) + @handle_lastfm_exceptions def test_add_track_to_playlist(self): # Arrange title = "One track playlist" @@ -1522,6 +1668,7 @@ class TestPyLast(unittest.TestCase): self.assertEqual(len(playlist.get_tracks()), 1) self.assertTrue(playlist.has_track(track)) + @handle_lastfm_exceptions def test_album_mbid(self): # Arrange mbid = "a6a265bf-9f81-4055-8224-f7ac0aa6b937" @@ -1535,6 +1682,7 @@ class TestPyLast(unittest.TestCase): self.assertEqual(album.title.lower(), "test") self.assertEqual(album_mbid, mbid) + @handle_lastfm_exceptions def test_artist_mbid(self): # Arrange mbid = "7e84f845-ac16-41fe-9ff8-df12eb32af55" @@ -1546,6 +1694,7 @@ class TestPyLast(unittest.TestCase): self.assertIsInstance(artist, pylast.Artist) self.assertEqual(artist.name, "MusicBrainz Test Artist") + @handle_lastfm_exceptions def test_track_mbid(self): # Arrange mbid = "ebc037b1-cc9c-44f2-a21f-83c219f0e1e0" @@ -1559,6 +1708,7 @@ class TestPyLast(unittest.TestCase): self.assertEqual(track.title, "first") self.assertEqual(track_mbid, mbid) + @handle_lastfm_exceptions def test_artist_listener_count(self): # Arrange artist = self.network.get_artist("Test Artist") @@ -1570,6 +1720,7 @@ class TestPyLast(unittest.TestCase): self.assertIsInstance(count, int) self.assertGreater(count, 0) + @handle_lastfm_exceptions def test_event_attendees(self): # Arrange user = self.network.get_user("RJ") @@ -1582,6 +1733,7 @@ class TestPyLast(unittest.TestCase): self.assertIsInstance(users, list) self.assertIsInstance(users[0], pylast.User) + @handle_lastfm_exceptions def test_tag_artist(self): # Arrange artist = self.network.get_artist("Test Artist") @@ -1600,6 +1752,7 @@ class TestPyLast(unittest.TestCase): break self.assertTrue(found) + @handle_lastfm_exceptions def test_remove_tag_of_type_text(self): # Arrange tag = "testing" # text @@ -1618,6 +1771,7 @@ class TestPyLast(unittest.TestCase): break self.assertFalse(found) + @handle_lastfm_exceptions def test_remove_tag_of_type_tag(self): # Arrange tag = pylast.Tag("testing", self.network) # Tag @@ -1636,6 +1790,7 @@ class TestPyLast(unittest.TestCase): break self.assertFalse(found) + @handle_lastfm_exceptions def test_remove_tags(self): # Arrange tags = ["removetag1", "removetag2"] @@ -1659,6 +1814,7 @@ class TestPyLast(unittest.TestCase): self.assertFalse(found1) self.assertFalse(found2) + @handle_lastfm_exceptions def test_set_tags(self): # Arrange tags = ["sometag1", "sometag2"] @@ -1683,6 +1839,7 @@ class TestPyLast(unittest.TestCase): self.assertTrue(found1) self.assertTrue(found2) + @handle_lastfm_exceptions def test_tracks_notequal(self): # Arrange track1 = pylast.Track("Test Artist", "Test Title", self.network) @@ -1692,6 +1849,7 @@ class TestPyLast(unittest.TestCase): # Assert self.assertNotEqual(track1, track2) + @handle_lastfm_exceptions def test_track_id(self): # Arrange track = pylast.Track("Test Artist", "Test Title", self.network) @@ -1700,8 +1858,10 @@ class TestPyLast(unittest.TestCase): id = track.get_id() # Assert + self.skip_if_lastfm_api_broken(id) self.assertEqual(id, "14053327") + @handle_lastfm_exceptions def test_track_title_prop_caps(self): # Arrange track = pylast.Track("test artist", "test title", self.network) @@ -1712,6 +1872,7 @@ class TestPyLast(unittest.TestCase): # Assert self.assertEqual(title, "Test Title") + @handle_lastfm_exceptions def test_track_listener_count(self): # Arrange track = pylast.Track("test artist", "test title", self.network) @@ -1722,6 +1883,7 @@ class TestPyLast(unittest.TestCase): # Assert self.assertGreater(count, 21) + @handle_lastfm_exceptions def test_album_rel_date(self): # Arrange album = pylast.Album("Test Artist", "Test Release", self.network) @@ -1730,8 +1892,10 @@ class TestPyLast(unittest.TestCase): date = album.get_release_date() # Assert + self.skip_if_lastfm_api_broken(date) self.assertIn("2011", date) + @handle_lastfm_exceptions def test_album_tracks(self): # Arrange album = pylast.Album("Test Artist", "Test Release", self.network) @@ -1744,6 +1908,7 @@ class TestPyLast(unittest.TestCase): self.assertIsInstance(tracks[0], pylast.Track) self.assertEqual(len(tracks), 4) + @handle_lastfm_exceptions def test_tags(self): # Arrange tag1 = self.network.get_tag("blues") @@ -1753,7 +1918,6 @@ class TestPyLast(unittest.TestCase): tag_repr = repr(tag1) tag_str = str(tag1) name = tag1.get_name(properly_capitalized=True) - similar = tag1.get_similar() url = tag1.get_url() # Assert @@ -1764,6 +1928,17 @@ class TestPyLast(unittest.TestCase): self.assertTrue(tag1 == tag1) self.assertTrue(tag1 != tag2) self.assertEqual(url, "http://www.last.fm/tag/blues") + + @handle_lastfm_exceptions + def test_tags_similar(self): + # Arrange + tag = self.network.get_tag("blues") + + # Act + similar = tag.get_similar() + + # Assert + self.skip_if_lastfm_api_broken(similar) found = False for tag in similar: if tag.name == "delta blues": @@ -1771,6 +1946,7 @@ class TestPyLast(unittest.TestCase): break self.assertTrue(found) + @handle_lastfm_exceptions def test_artists(self): # Arrange artist1 = self.network.get_artist("Radiohead") @@ -1794,6 +1970,7 @@ class TestPyLast(unittest.TestCase): self.assertEqual(mbid, "a74b1b7f-71a5-4011-9441-d0b5e4122711") self.assertIsInstance(streamable, bool) + @handle_lastfm_exceptions def test_events(self): # Arrange event_id_1 = 3162700 # Glasto 2013 @@ -1828,6 +2005,7 @@ class TestPyLast(unittest.TestCase): self.assertGreater(review_count, 0) self.assertGreater(attendance_count, 100) + @handle_lastfm_exceptions def test_countries(self): # Arrange country1 = pylast.Country("Italy", self.network) @@ -1846,6 +2024,7 @@ class TestPyLast(unittest.TestCase): self.assertTrue(country1 != country2) self.assertEqual(url, "http://www.last.fm/place/italy") + @handle_lastfm_exceptions def test_track_eq_none_is_false(self): # Arrange track1 = None @@ -1854,6 +2033,7 @@ class TestPyLast(unittest.TestCase): # Act / Assert self.assertFalse(track1 == track2) + @handle_lastfm_exceptions def test_track_ne_none_is_true(self): # Arrange track1 = None @@ -1862,6 +2042,7 @@ class TestPyLast(unittest.TestCase): # Act / Assert self.assertTrue(track1 != track2) + @handle_lastfm_exceptions def test_artist_eq_none_is_false(self): # Arrange artist1 = None @@ -1870,6 +2051,7 @@ class TestPyLast(unittest.TestCase): # Act / Assert self.assertFalse(artist1 == artist2) + @handle_lastfm_exceptions def test_artist_ne_none_is_true(self): # Arrange artist1 = None @@ -1878,6 +2060,7 @@ class TestPyLast(unittest.TestCase): # Act / Assert self.assertTrue(artist1 != artist2) + @handle_lastfm_exceptions def test_album_eq_none_is_false(self): # Arrange album1 = None @@ -1886,6 +2069,7 @@ class TestPyLast(unittest.TestCase): # Act / Assert self.assertFalse(album1 == album2) + @handle_lastfm_exceptions def test_album_ne_none_is_true(self): # Arrange album1 = None @@ -1894,6 +2078,7 @@ class TestPyLast(unittest.TestCase): # Act / Assert self.assertTrue(album1 != album2) + @handle_lastfm_exceptions def test_event_eq_none_is_false(self): # Arrange event1 = None @@ -1903,6 +2088,7 @@ class TestPyLast(unittest.TestCase): # Act / Assert self.assertFalse(event1 == event2) + @handle_lastfm_exceptions def test_event_ne_none_is_true(self): # Arrange event1 = None @@ -1912,6 +2098,7 @@ class TestPyLast(unittest.TestCase): # Act / Assert self.assertTrue(event1 != event2) + @handle_lastfm_exceptions def test_band_members(self): # Arrange artist = pylast.Artist("The Beatles", self.network) @@ -1920,8 +2107,10 @@ class TestPyLast(unittest.TestCase): band_members = artist.get_band_members() # Assert + self.skip_if_lastfm_api_broken(band_members) self.assertGreaterEqual(len(band_members), 4) + @handle_lastfm_exceptions def test_no_band_members(self): # Arrange artist = pylast.Artist("John Lennon", self.network) @@ -1932,6 +2121,7 @@ class TestPyLast(unittest.TestCase): # Assert self.assertIsNone(band_members) + @handle_lastfm_exceptions def test_get_recent_tracks_from_to(self): # Arrange lastfm_user = self.network.get_user("RJ") @@ -1952,6 +2142,7 @@ class TestPyLast(unittest.TestCase): self.assertEqual(str(tracks[0].track.artist), "Johnny Cash") self.assertEqual(str(tracks[0].track.title), "Ring of Fire") + @handle_lastfm_exceptions def test_artist_get_correction(self): # Arrange artist = pylast.Artist("guns and roses", self.network) @@ -1962,6 +2153,7 @@ class TestPyLast(unittest.TestCase): # Assert self.assertEqual(corrected_artist_name, "Guns N' Roses") + @handle_lastfm_exceptions def test_track_get_correction(self): # Arrange track = pylast.Track("Guns N' Roses", "mrbrownstone", self.network) @@ -1972,6 +2164,7 @@ class TestPyLast(unittest.TestCase): # Assert self.assertEqual(corrected_track_name, "Mr. Brownstone") + @handle_lastfm_exceptions def test_track_with_no_mbid(self): # Arrange track = pylast.Track("Static-X", "Set It Off", self.network) diff --git a/tox.ini b/tox.ini index b5dd199..a76319d 100644 --- a/tox.ini +++ b/tox.ini @@ -3,7 +3,6 @@ envlist = py34, py27, pypy, pypy3 recreate = False [testenv] -downloadcache = {homedir}/.pipcache setenv = PYLAST_USERNAME={env:PYLAST_USERNAME:} PYLAST_PASSWORD_HASH={env:PYLAST_PASSWORD_HASH:}