Skip to content

Commit

Permalink
Added logger and whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
westscz committed Feb 15, 2018
1 parent 1204845 commit 275e26c
Showing 1 changed file with 18 additions and 43 deletions.
61 changes: 18 additions & 43 deletions interfaces/lastfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,42 @@
import musicbrainzngs
Logger = create_logger("LastFM")

whitelist = ["pop", "rock", "soul", "r&b", "trap rap", "electronic", "dubstep"]

class ILastFM():
def __init__(self):
# self.http = urllib3.PoolManager()
# 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__':
Expand All @@ -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)

0 comments on commit 275e26c

Please sign in to comment.