From 393c4147e86b5f76e7b7aa8b544f46d3e83d336b Mon Sep 17 00:00:00 2001 From: M Date: Sun, 16 Jan 2022 15:14:40 +0100 Subject: [PATCH 1/2] add similar artists to bands --- README.md | 4 +++ metallum.py | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 762c294..6edf840 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,10 @@ bands[0].name # Fetch band page band = bands[0].get() +# Fetch band's similiar artists +band.similar_artists +# -> + # Get all albums band.albums # -> [, , ...] diff --git a/metallum.py b/metallum.py index 945818d..b1a9d5d 100755 --- a/metallum.py +++ b/metallum.py @@ -15,11 +15,11 @@ import requests_cache from dateutil import parser as date_parser from pyquery import PyQuery -from requests_cache.core import remove_expired_responses +#from requests_cache.core import remove_expired_responses CACHE_FILE = os.path.join(tempfile.gettempdir(), 'metallum_cache') requests_cache.install_cache(cache_name=CACHE_FILE, expire_after=300) -remove_expired_responses() +#remove_expired_responses() # Site details BASE_URL = 'https://www.metal-archives.com' @@ -534,6 +534,96 @@ def albums(self) -> List['AlbumCollection']: url = 'band/discography/id/{0}/tab/all'.format(self.id) return AlbumCollection(url) + @property + def similar_artists(self): + """ + >>> band.similar_artists + + """ + + url = 'band/ajax-recommendations/id/' + self.id + return SimilarArtists(url, SimilarArtistsResult) + + +class SimilarArtists(Metallum, list): + """Entries in the similar artists tab + """ + + def __init__(self, url, result_handler): + super().__init__(url) + data = self._content + + links_list = PyQuery(data)('a') + values_list = PyQuery(data)('tr') + + # assert(len(links_list) == len(values_list) - 1) + for i in range(0, len(links_list) -1): + details = [links_list[i].attrib.get('href')] + details.extend(values_list[i+1].text_content().split('\n')[1:-1]) + self.append(result_handler(details)) + self.result_count = i + + def __repr__(self): + + def similar_artist_str(SimilarArtistsResult): + return f'{SimilarArtistsResult.name} ({SimilarArtistsResult.score})' + if not self: + return '' + names = list(map(similar_artist_str, self)) + s = ' | '.join(names) + return ''.format(s) + + +class SimilarArtistsResult(list): + """Represents a entry in the similar artists tab + """ + _resultType = Band + + def __init__(self, details): + super().__init__() + self._details = details + for d in details: + self.append(d) + + @property + def id(self) -> str: + # url = PyQuery(self._details[0])('a').attr('href') + return re.search(r'\d+$', self[0]).group(0) + + @property + def url(self) -> str: + return 'bands/_/{0}'.format(self.id) + + @property + def name(self) -> str: + return self[1] + + @property + def country(self) -> str: + """ + >>> search_results[0].country + 'United States' + """ + return self[2] + + @property + def genres(self) -> List[str]: + return split_genres(self[3]) + + @property + def score(self) -> int: + return int(self[4]) + + + def __repr__(self): + s = ' | '.join(self[1:]) + return ''.format(s) + + def get(self) -> 'Metallum': + return self._resultType(self.url) + + + class AlbumCollection(MetallumCollection): @@ -987,6 +1077,7 @@ def __str__(self): # Test objects search_results = band_search('metallica') band = search_results[0].get() + band.similar_artists album = band.albums.search(type=AlbumTypes.FULL_LENGTH)[2] track = album.tracks[0] From fac88e5d7de45f3ec44ec108c29441db4e7fe75d Mon Sep 17 00:00:00 2001 From: msanlop Date: Mon, 7 Feb 2022 19:57:16 +0100 Subject: [PATCH 2/2] similar artists now shows all entries --- metallum.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/metallum.py b/metallum.py index b1a9d5d..9a49345 100755 --- a/metallum.py +++ b/metallum.py @@ -538,10 +538,9 @@ def albums(self) -> List['AlbumCollection']: def similar_artists(self): """ >>> band.similar_artists - - """ + """ - url = 'band/ajax-recommendations/id/' + self.id + url = 'band/ajax-recommendations/id/' + self.id + '/showMoreSimilar/1' return SimilarArtists(url, SimilarArtistsResult)