/
whats_on_translate.py
executable file
·136 lines (92 loc) · 3.02 KB
/
whats_on_translate.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/python
from __future__ import print_function
import yaml
VERBOSE = False
# Translations:
#
# * YAML based takes precedence
# * Artist: Remove anything past " feat " or " feat." or " w/" or " &"
# * Song: Remove parens
# * Song: Remove anything past "/"
EXAMPLE_ARTISTS = ['Jimmy Buffett W/Clint Black, Kenny Chesney, Alan Jackson, Toby Keith & Geor',
'Dude & Foo feat dude1',
'Guess Who',
]
EXAMPLE_SONGS = [('Trews', 'Power Of Positive Drinking'),
('Prince', '- 1999 -'),
('Trooper', 'The Boys In The Bright White Sports Cars')]
TRANSLATIONS = {}
def load_yaml():
try:
translations_yaml = open('translations.yaml')
translations = yaml.load(translations_yaml)
except IOError:
translations = {}
return translations
def override_artist( artist ):
"""Overrides the artist name
Arguments:
artist -- string containing name
"""
if artist in TRANSLATIONS['artists'].keys():
if VERBOSE:
print("Translating", artist, "to", TRANSLATIONS['artists'][artist])
return TRANSLATIONS['artists'][artist]
return None
def translate_artist( artist ):
"""Converts artist name into something that last.fm / Musicbrainz
understands
Arguments:
artist -- Artist name to be translated
Returns the translated name"""
artist_lower = artist.lower()
shortest_pos = len(artist_lower)
for suffix in [" feat ",
" feat.",
" w/",
" &"]:
pos = artist_lower.find(suffix)
if pos > 0 and pos < shortest_pos:
shortest_pos = pos
return artist[0:shortest_pos]
def override_song( song ):
"""Overrides the song title from YAML config
Arguments:
song -- tuple of (artist, song)
Returns new song title or None
"""
artist = translate_artist(song[0])
if artist in TRANSLATIONS['songs'].keys():
if song[1] in TRANSLATIONS['songs'][artist].keys():
return str(TRANSLATIONS['songs'][artist][song[1]])
return None
def translate_song( song ):
"""Converts artist and song title into something that last.fm / Musicbrainz
understands
Arguments:
song -- Song title to be translated
Returns the translated name"""
song_lower = song[1].lower()
shortest_pos = len(song_lower)
for suffix in [" (",
"/"]:
pos = song_lower.find(suffix)
if pos > 0 and pos < shortest_pos:
shortest_pos = pos
return song[1][0:shortest_pos]
def main():
for artist in EXAMPLE_ARTISTS:
artist_or = override_artist( artist )
if artist_or:
print(artist_or)
else:
print(translate_artist( artist ))
for song in EXAMPLE_SONGS:
song_or = override_song( song )
if song_or:
print(song_or)
else:
print(translate_song( song ))
TRANSLATIONS = load_yaml()
if __name__ == '__main__':
main()