From 844132e382b350d6521dfbd859e495ae44cf7854 Mon Sep 17 00:00:00 2001 From: Daniel Underwood Date: Tue, 2 Apr 2019 21:55:00 -0400 Subject: [PATCH 1/3] Add country code param for iTunes and Apple Music charts Resolves #104. Note that both seem to handle an invalid code as US. --- lidarrmetadata/__init__.py | 2 +- lidarrmetadata/chart.py | 29 +++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/lidarrmetadata/__init__.py b/lidarrmetadata/__init__.py index 779b9fc3..af0003b3 100644 --- a/lidarrmetadata/__init__.py +++ b/lidarrmetadata/__init__.py @@ -1 +1 @@ -__version__ = '0.4.13' +__version__ = '0.4.14' diff --git a/lidarrmetadata/chart.py b/lidarrmetadata/chart.py index cc90563a..2911d4b3 100644 --- a/lidarrmetadata/chart.py +++ b/lidarrmetadata/chart.py @@ -28,20 +28,29 @@ def _parse_itunes_chart(URL, count): @util.CACHE.memoize(timeout=60 * 60 * 24 * 7) -def get_apple_music_top_albums_chart(count=10): +def get_apple_music_top_albums_chart(count=10, country='us'): """ Gets and parses itunes chart :param count: Number of results to return. Defaults to 10 + :param country: Country code to request. Defaults to us :return: Chart response for itunes """ - URL = 'https://rss.itunes.apple.com/api/v1/us/apple-music/top-albums/all/{count}/explicit.json'.format( + URL = 'https://rss.itunes.apple.com/api/v1/{country}/apple-music/top-albums/all/{count}/explicit.json'.format( + country=country, count=4 * count) return _parse_itunes_chart(URL, count) @util.CACHE.memoize(timeout=60 * 60 * 24 * 7) -def get_apple_music_new_albums_chart(count=10): - URL = 'https://rss.itunes.apple.com/api/v1/us/apple-music/new-releases/all/{count}/explicit.json'.format( +def get_apple_music_new_albums_chart(count=10, country='us'): + """ + Gets and parses itunes chart + :param count: Number of results to return. Defaults to 10 + :param country: Country code to request. Defaults to us + :return: Chart response for itunes + """ + URL = 'https://rss.itunes.apple.com/api/v1/{country}/apple-music/new-releases/all/{count}/explicit.json'.format( + country=country, count=4 * count) return _parse_itunes_chart(URL, count) @@ -92,25 +101,29 @@ def get_billboard_100_artists_chart(count=10): @util.CACHE.memoize(timeout=60 * 60 * 24 * 7) -def get_itunes_top_albums_chart(count=10): +def get_itunes_top_albums_chart(count=10, country='us'): """ Gets and parses itunes chart :param count: Number of results to return. Defaults to 10 + :param country: Country code to request. Defaults to us :return: Chart response for itunes """ - URL = 'https://rss.itunes.apple.com/api/v1/us/itunes-music/top-albums/all/{count}/explicit.json'.format( + URL = 'https://rss.itunes.apple.com/api/v1/{country}/itunes-music/top-albums/all/{count}/explicit.json'.format( + country=country, count=4 * count) return _parse_itunes_chart(URL, count) @util.CACHE.memoize(timeout=60 * 60 * 24 * 7) -def get_itunes_new_albums_chart(count=10): +def get_itunes_new_albums_chart(count=10, country='us'): """ Gets and parses itunes new chart :param count: Number of results to return. Defaults to 10 + :param country: Country code to request. Defaults to us :return: Chart response for itunes """ - URL = 'https://rss.itunes.apple.com/api/v1/us/itunes-music/new-music/all/{count}/explicit.json'.format( + URL = 'https://rss.itunes.apple.com/api/v1/{country}/itunes-music/new-music/all/{count}/explicit.json'.format( + country=country, count=4 * count) return _parse_itunes_chart(URL, count) From 7ed9076f337066032301e870df7f9bb43b561a6f Mon Sep 17 00:00:00 2001 From: Daniel Underwood Date: Wed, 3 Apr 2019 22:39:31 -0400 Subject: [PATCH 2/3] Add lastfm geo top artists chart --- lidarrmetadata/__init__.py | 2 +- lidarrmetadata/api.py | 3 ++- lidarrmetadata/chart.py | 28 ++++++++++++++++++++++++---- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lidarrmetadata/__init__.py b/lidarrmetadata/__init__.py index af0003b3..7cf92ac4 100644 --- a/lidarrmetadata/__init__.py +++ b/lidarrmetadata/__init__.py @@ -1 +1 @@ -__version__ = '0.4.14' +__version__ = '0.4.15dev1' diff --git a/lidarrmetadata/api.py b/lidarrmetadata/api.py index c63489b5..0869852d 100644 --- a/lidarrmetadata/api.py +++ b/lidarrmetadata/api.py @@ -308,7 +308,8 @@ def chart_route(name, type_, selection): ('itunes', 'album', 'top'): chart.get_itunes_top_albums_chart, ('itunes', 'album', 'new'): chart.get_itunes_new_albums_chart, ('lastfm', 'album', 'top'): chart.get_lastfm_album_chart, - ('lastfm', 'artist', 'top'): chart.get_lastfm_artist_chart + ('lastfm', 'artist', 'top'): chart.get_lastfm_artist_chart, + ('lastfm', 'artist-geo', 'top'): chart.get_lastfm_geo_artists } if key not in charts.keys(): diff --git a/lidarrmetadata/chart.py b/lidarrmetadata/chart.py index 2911d4b3..b3426250 100644 --- a/lidarrmetadata/chart.py +++ b/lidarrmetadata/chart.py @@ -14,10 +14,12 @@ def _parse_itunes_chart(URL, count): response = requests.get(URL) results = filter(lambda r: r.get('kind', '') == 'album', response.json()['feed']['results']) + return results search_provider = provider.get_providers_implementing(provider.AlbumNameSearchMixin)[0] search_results = [] for result in results: - search_result = search_provider.search_album_name(result['name'], artist_name=result['artistName'], limit=1) + search_result = search_provider.search_album_name(result['name'], + artist_name=result['artistName'], limit=1) if search_result: search_result = search_result[0] search_results.append(_parse_album_search_result(search_result)) @@ -128,13 +130,15 @@ def get_itunes_new_albums_chart(count=10, country='us'): return _parse_itunes_chart(URL, count) +@util.CACHE.memoize(timeout=60 * 60 * 24 * 7) def get_lastfm_album_chart(count=10, user=None): """ Gets and parses lastfm chart :param count: Number of results to return. Defaults to 10 :return: Parsed chart """ - client = pylast.LastFMNetwork(api_key=config.get_config().LASTFM_KEY, api_secret=config.get_config().LASTFM_SECRET) + client = pylast.LastFMNetwork(api_key=config.get_config().LASTFM_KEY, + api_secret=config.get_config().LASTFM_SECRET) if user: user = util.cache_or_call(client.get_user, user[0]) @@ -169,13 +173,15 @@ def get_lastfm_album_chart(count=10, user=None): return albums +@util.CACHE.memoize(timeout=60 * 60 * 24 * 7) def get_lastfm_artist_chart(count=10, user=None): """ Gets and parses lastfm chart :param count: Number of results to return. Defaults to 10 :return: Parsed chart """ - client = pylast.LastFMNetwork(api_key=config.get_config().LASTFM_KEY, api_secret=config.get_config().LASTFM_SECRET) + client = pylast.LastFMNetwork(api_key=config.get_config().LASTFM_KEY, + api_secret=config.get_config().LASTFM_SECRET) if user: user = util.cache_or_call(client.get_user, user[0]) @@ -183,8 +189,23 @@ def get_lastfm_artist_chart(count=10, user=None): else: lastfm_artists = util.cache_or_call(client.get_top_artists) + return _parse_lastfm_artists(lastfm_artists) + + +@util.CACHE.memoize(timeout=60 * 60 * 24 * 7) +def get_lastfm_geo_artists(count, country='UNITED STATES'): + client = pylast.LastFMNetwork(api_key=config.get_config().LASTFM_KEY, + api_secret=config.get_config().LASTFM_SECRET) + + lastfm_artists = util.cache_or_call(client.get_geo_top_artists, country) + + return _parse_lastfm_artists(count, lastfm_artists) + + +def _parse_lastfm_artists(count, lastfm_artists): artists = [] search_provider = provider.get_providers_implementing(provider.ArtistNameSearchMixin)[0] + for lastfm_artist in pylast.extract_items(lastfm_artists): artist = {'ArtistName': lastfm_artist.name, 'ArtistId': lastfm_artist.get_mbid()} @@ -198,7 +219,6 @@ def get_lastfm_artist_chart(count=10, user=None): if all(artist.values()): artists.append(artist) - if len(artists) > count: artists = artists[:count] From 1f4a6e84de9089485c6d8c537d224040ca3ee3d8 Mon Sep 17 00:00:00 2001 From: Daniel Underwood Date: Wed, 3 Apr 2019 22:57:01 -0400 Subject: [PATCH 3/3] Fix missed argument --- lidarrmetadata/__init__.py | 2 +- lidarrmetadata/chart.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lidarrmetadata/__init__.py b/lidarrmetadata/__init__.py index 7cf92ac4..89327ddc 100644 --- a/lidarrmetadata/__init__.py +++ b/lidarrmetadata/__init__.py @@ -1 +1 @@ -__version__ = '0.4.15dev1' +__version__ = '0.4.15dev2' diff --git a/lidarrmetadata/chart.py b/lidarrmetadata/chart.py index b3426250..2bd3e4de 100644 --- a/lidarrmetadata/chart.py +++ b/lidarrmetadata/chart.py @@ -189,7 +189,7 @@ def get_lastfm_artist_chart(count=10, user=None): else: lastfm_artists = util.cache_or_call(client.get_top_artists) - return _parse_lastfm_artists(lastfm_artists) + return _parse_lastfm_artists(count, lastfm_artists) @util.CACHE.memoize(timeout=60 * 60 * 24 * 7)