-
Notifications
You must be signed in to change notification settings - Fork 2
/
description_provider.py
89 lines (70 loc) · 3.91 KB
/
description_provider.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import musicbrainzngs
from blottertrax.config import Config
from blottertrax.exceptions.description_exception import DescriptionException
from blottertrax.helper import templates
from blottertrax.helper.array_util import ArrayUtil
from blottertrax.value_objects.parsed_submission import ParsedSubmission
class DescriptionProvider:
whitelist = ['twitter.com', 'facebook.com', 'instagram.com', 'allmusic.com', 'bandcamp.com',
'bandsintown.com', 'discogs.com', 'genius.com', 'rateyourmusic.com', 'last.fm',
'soundcloud.com', 'youtube.com', 'www.whosampled.com']
def __init__(self):
config = Config()
self.musicbrainz = musicbrainzngs
self.musicbrainz.auth(config.MUSICBRAINZ.USER, config.MUSICBRAINZ.PASSWORD)
self.musicbrainz.set_useragent("BlotterTrax", "0.1", "https://github.com/martijnboers/BlotterTrax")
def get_reply(self, parsed_submission: ParsedSubmission) -> str:
if parsed_submission.success is False:
raise DescriptionException('DescriptionProvider requires the submission title')
query = '"{}" AND artist:"{}"'.format(parsed_submission.track_title, parsed_submission.artist)
result = self.musicbrainz.search_recordings(query=query, limit=1, )
if result['recording-count'] == 0:
raise DescriptionException('No recordings found for artist')
recording = result['recording-list'][0]
artist = self._get_artist_by_id(recording.get('artist-credit')[0]['artist']['id'])
album_title = ArrayUtil.safe_list_get(recording, recording['title'], 'release-list', 0, 'title')
album_release_date = ArrayUtil.safe_list_get(recording, False, 'release-list', 0, 'date')
life_span_begin = ArrayUtil.safe_list_get(artist, '?', 'life-span', 'begin')
life_span_end = ArrayUtil.safe_list_get(artist, 'now', 'life-span', 'end')
has_life_span = life_span_end != 'now' or life_span_begin != '?'
has_tags = ArrayUtil.safe_list_get(artist, False, 'tag-list') is not False
has_socials = ArrayUtil.safe_list_get(artist, False, 'url-relation-list') is not False
if has_tags is False and has_socials is False:
raise DescriptionException('Neither tags nor socials found, skipping')
album_release_date = '' if album_release_date is False else '({})'.format(album_release_date)
life_span = '' if has_life_span is False else '({} to {})'.format(life_span_begin, life_span_end)
tags = ', '.join(map(lambda t: t['name'], artist['tag-list'][:5])) if has_tags else 'none'
socials = ', '.join(
map(self.format_network_to_friendly_name, filter(self.apply_whitelist, artist['url-relation-list']))
) if has_socials else 'none'
return templates.musicbrainz_artist_info.strip().format(
artist['name'],
life_span,
album_title,
album_release_date,
tags,
'none' if not socials else socials,
artist['id']
)
def _get_artist_by_id(self, artist_id: str):
return self.musicbrainz.get_artist_by_id(
id=artist_id, includes=['tags', 'ratings', 'annotation', 'url-rels', 'user-tags']
)['artist']
@staticmethod
def apply_whitelist(info) -> bool:
for domain in DescriptionProvider.whitelist:
if domain in info['target']:
return True
return False
@staticmethod
def format_network_to_friendly_name(info) -> str:
social_network = ['twitter.com', 'facebook.com', 'instagram.com']
link_type = info['type']
target = info['target']
target = target.replace('(', r'\(').replace(')', r'\)')
if link_type == 'social network':
for domain in social_network:
if domain in target:
link_type = domain.split('.')[0]
break
return '[{}]({})'.format(link_type, target)