fixed genre formatting issue

This commit is contained in:
nathannathant 2021-02-25 13:58:04 -08:00
parent 98db34bc8e
commit 69728e21ee

View File

@ -30,6 +30,25 @@ def _format_copyright(s: str) -> str:
return s
def _format_genres(genres: list) -> str:
'''Fixes the weirdly formatted genre lists returned by the API.
>>> g = ['Pop/Rock', 'Pop/Rock→Rock', 'Pop/Rock→Rock→Alternatif et Indé']
>>> _format_genres(g)
'Pop/Rock, Rock, Alternatif et Indé'
'''
if len(genres) <= 1:
return ''.join(genres)
prev = genres[0]
new_genres = [prev]
for genre in genres[1:]:
new_genres.append(genre.replace(f'{prev}', ''))
prev = genre
return ', '.join(new_genres)
# Use KeyError catching instead of dict.get to avoid empty tags
def tag_flac(filename, root_dir, final_name, d, album,
istrack=True, em_image=False):
@ -72,14 +91,14 @@ def tag_flac(filename, root_dir, final_name, d, album,
pass
if istrack:
audio["GENRE"] = ", ".join(d["album"]["genres_list"]) # GENRE
audio["ALBUMARTIST"] = d["album"]["artist"]["name"] # ALBUM ARTIST
audio["GENRE"] = _format_genres(d["album"]["genres_list"]) # GENRE
audio["ALBUMARTIST"] = d["album"]["artist"]["name"] # ALBUMARTIST
audio["TRACKTOTAL"] = str(d["album"]["tracks_count"]) # TRACK TOTAL
audio["ALBUM"] = d["album"]["title"] # ALBUM TITLE
audio["DATE"] = d["album"]["release_date_original"]
audio["COPYRIGHT"] = _format_copyright(d["copyright"])
else:
audio["GENRE"] = ", ".join(album["genres_list"]) # GENRE
audio["GENRE"] = _format_genres(album["genres_list"]) # GENRE
audio["ALBUMARTIST"] = album["artist"]["name"] # ALBUM ARTIST
audio["TRACKTOTAL"] = str(album["tracks_count"]) # TRACK TOTAL
audio["ALBUM"] = album["title"] # ALBUM TITLE
@ -161,14 +180,14 @@ def tag_mp3(filename, root_dir, final_name, d, album,
tags['artist'] = album["artist"]["name"]
if istrack:
tags["genre"] = ", ".join(d["album"]["genres_list"])
tags["genre"] = _format_genres(d["album"]["genres_list"])
tags["albumartist"] = d["album"]["artist"]["name"]
tags["album"] = d["album"]["title"]
tags["date"] = d["album"]["release_date_original"]
tags["copyright"] = _format_copyright(d["copyright"])
tracktotal = str(d["album"]["tracks_count"])
else:
tags["genre"] = ", ".join(album["genres_list"])
tags["genre"] = _format_genres(album["genres_list"])
tags["albumartist"] = album["artist"]["name"]
tags["album"] = album["title"]
tags["date"] = album["release_date_original"]