From 275e26cf939510ac4942739d222e16d9382a3056 Mon Sep 17 00:00:00 2001 From: westscz Date: Thu, 15 Feb 2018 19:03:18 +0100 Subject: [PATCH] Added logger and whitelist --- interfaces/lastfm.py | 61 +++++++++++++------------------------------- 1 file changed, 18 insertions(+), 43 deletions(-) diff --git a/interfaces/lastfm.py b/interfaces/lastfm.py index 558f650..c72a250 100755 --- a/interfaces/lastfm.py +++ b/interfaces/lastfm.py @@ -4,6 +4,7 @@ import musicbrainzngs Logger = create_logger("LastFM") +whitelist = ["pop", "rock", "soul", "r&b", "trap rap", "electronic", "dubstep"] class ILastFM(): def __init__(self): @@ -11,59 +12,34 @@ def __init__(self): # urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) musicbrainzngs.set_useragent("LLameDL", "0.1", "http://github.com/music") - # def remove_pl_chars(self, text): - # s = list(text) - # pl_dict = {261: 'a', 260: 'A', 263: 'c', 262: 'C', 281: 'e', 280: 'E', 322: 'l', 321: 'L', 243: 'o', 211: 'O', - # 347: 's', 346: 'S', 380: 'z', 379: 'Z', 378: 'z', 377: 'Z', 235: 'e'} - # for idx, val in enumerate(s): - # char_ord = ord(val) - # if char_ord in pl_dict.keys(): - # s[idx] = pl_dict[char_ord] - # elif char_ord > 600: - # s[idx] = "#" - # return "".join(s) - - # def get_tags_for_artist(self, artist): - # if artist == "Unknown": - # Logger.info(artist + ' nie posiada jeszcze tagow, dodaj je samodzielnie') - # return [] - # - # artist_mod = self.remove_pl_chars(artist) - # url = 'http://www.last.fm/pl/music/' + artist_mod.replace(' ', '+') - # - # r = self.http.request('GET', url) - # html_content = r.data - # tree = html.fromstring(html_content) - # - # x = tree.xpath('//section[@class="tag-section"]/ul//a') - # tags = [] - # for f in x: - # tags.append(f.text.title()) - # if tags: - # Logger.info(artist + ' - ' + ', '.join(tags)) - # return tags - # else: - # Logger.info(artist + ' nie posiada jeszcze tagow, dodaj je samodzielnie: ' + url + '/+tags') - # return [] - def get_artist_data(self, artist_name): result = musicbrainzngs.search_artists(artist_name) if result.get('artist-count', 0): for artist_data in result.get('artist-list'): if artist_data['name']==artist_name: - print(artist_data) + Logger.debug(artist_data) return artist_data if artist_data else {} return {} def get_tags_for_artist(self, artist_name): artist_data = self.get_artist_data(artist_name) tags_list = artist_data.get('tag-list', []) - return [tag.get('name') for tag in tags_list] + if not tags_list: + self.add_tag(artist_data.get('id', None)) + w=[tag.get('name') for tag in tags_list] + Logger.debug(w) + return self.whitelist_tags(w) + + def whitelist_tags(self, tags_list): + return [tag for tag in tags_list if tag in whitelist] - def add_tag(self): - pass - #https://musicbrainz.org/artist/{artist_id}/tags/upvote?tags={tag%20list} - #this can help to add tags + def add_tag(self, artist_id): + if not artist_id: + Logger.info("Artist is not visible in Music Brainz Database") + Logger.info("https://musicbrainz.org/artist/create") + else: + Logger.info("Artist have not any tags in Music Brainz Database") + Logger.info("https://musicbrainz.org/artist/{artist_id}".format(artist_id=artist_id)) if __name__ == '__main__': @@ -73,6 +49,5 @@ def add_tag(self): lfm = ILastFM() for x in band_list: - print(x) result = lfm.get_tags_for_artist(x) - print(result) + print(x, result)