Can now remove "[Explicit]" warning in titles

This commit is contained in:
matthias@arch 2021-09-29 15:32:44 +02:00
parent 711a13d087
commit 1982bcb246
2 changed files with 45 additions and 23 deletions

View File

@ -31,8 +31,9 @@ Command line options:
- `-s` silent, no command-line output - `-s` silent, no command-line output
- `-i` ignore history - `-i` ignore history
- `-n` do not write to history - `-n` do not write to history
- `-o` overwrite if the file already has a comment - `-o` overwrite if the file already a lyrics
- `-h` show this - `-h` show this
- `--rm_explicit` remove the "[Explicit]" lyrics warning from the songs title tag
If you do not specify a directory or file, the program will ask you if you want to use the current working directory. If you do not specify a directory or file, the program will ask you if you want to use the current working directory.

View File

@ -27,7 +27,7 @@ class Nicole:
Nicole creates a azlyrics url from the title and artist mp3-tags of the file. Nicole creates a azlyrics url from the title and artist mp3-tags of the file.
The lyrics are extracted from the html document using regex. The lyrics are extracted from the html document using regex.
""" """
def __init__(self, test_run=False, silent=False, write_history=True, ignore_history=False, overwrite_tag=False, recursive=False): def __init__(self, test_run=False, silent=False, write_history=True, ignore_history=False, overwrite_tag=False, recursive=False, rm_explicit=False):
self.test_run = test_run self.test_run = test_run
self.silent = silent self.silent = silent
@ -45,6 +45,8 @@ class Nicole:
if not self.ignore_history: if not self.ignore_history:
self._load_history() self._load_history()
self.rm_explicit = rm_explicit
def __del__(self): def __del__(self):
if self.write_history: if self.write_history:
self._write_history() self._write_history()
@ -93,6 +95,10 @@ class Nicole:
elif artist[0:3] == "the ": elif artist[0:3] == "the ":
artist = artist[4:] artist = artist[4:]
# remove anything in square bracketrs (eg [Explicit])
for match in re.finditer(r"[.*]", title):
title = title.replace(match.group(), "")
# remove spaces, from the title # remove spaces, from the title
for c in [' ', '-', ',', '.', '\'', '"', '°', '`', '´', '/', '!', '?', '#', '*']: for c in [' ', '-', ',', '.', '\'', '"', '°', '`', '´', '/', '!', '?', '#', '*']:
title = title.replace(c, '') title = title.replace(c, '')
@ -195,13 +201,11 @@ class Nicole:
# mp3/id3 # mp3/id3
if ".mp3" in file: if ".mp3" in file:
try: try:
audio = easyid3.EasyID3(file)
artist = audio["artist"][0]
title = audio["title"][0]
audio = id3.ID3(file) audio = id3.ID3(file)
artist = audio.getall("TPE1")
title = audio.getall("TIT2")
has_lyrics = not (audio.getall("USLT") == []) has_lyrics = not (audio.getall("USLT") == [])
except id3.ID3NoHeaderError: except id3.ID3NoHeaderError:
return (False, f"No id3 header found.") return (False, f"No id3 header found.")
@ -211,16 +215,17 @@ class Nicole:
audio = flac.FLAC(file) audio = flac.FLAC(file)
artist = audio.get("ARTIST") artist = audio.get("ARTIST")
if artist:
artist = artist[0]
title = audio.get("TITLE") title = audio.get("TITLE")
if title:
title = title[0]
has_lyrics = not (audio.get("LYRICS") == None) has_lyrics = not (audio.get("LYRICS") == None)
except flac.FLACNoHeaderError: except flac.FLACNoHeaderError:
return (False, f"No FLAC comment header found.") return (False, f"No FLAC comment header found.")
if artist:
artist = str(artist[0])
if title:
title = str(title[0])
# dont proceed when not overwrite and audio has tags # dont proceed when not overwrite and audio has tags
if not self.overwrite_tag and has_lyrics: if not self.overwrite_tag and has_lyrics:
return (False, f"Already has lyrics") return (False, f"Already has lyrics")
@ -229,6 +234,22 @@ class Nicole:
if not (audio and artist and title): if not (audio and artist and title):
return (False, f"Could not get tags.") return (False, f"Could not get tags.")
if self.rm_explicit:
for word in ["[Explicit]", "[exlicit]"]:
if word in title:
title = str(title).replace(word, "")
title = title.strip(" ")
if type(audio) == id3.ID3:
audio.setall("TIT2", [id3.TIT2(text=title)])
audio.save()
print(f"Removed '{word}' from the title.")
elif type(audio) == flac.FLAC:
audio["TITLE"] = title
audio.save()
print(f"Removed '{word}' from the title.")
print(audio.pprint())
# currently the only supported site # currently the only supported site
if self.lyrics_site == "azlyrics": if self.lyrics_site == "azlyrics":
url = self.get_url_azlyrics(artist, title) url = self.get_url_azlyrics(artist, title)
@ -260,17 +281,16 @@ class Nicole:
def main(): def main():
helpstring = """ helpstring = """Command line options:
Command line options:
-d [directory] process directory [directory] -d [directory] process directory [directory]
-f [file] process file [file] -f [file] process file [file]
-r go through directories recursively -r go through directories recursively
-s silent, no command-line output -s silent, no command-line output
-i ignore history -i ignore history
-n do not write to history -n do not write to history
-o overwrite if the file already has a comment -o overwrite if the file already has lyrics
-h show this -h show this
""" --rm_explicit remove the "[Explicit]" lyrics warning from the songs title tag"""
args = [] args = []
if len(argv) > 1: if len(argv) > 1:
# iterate over argv list and extract the args # iterate over argv list and extract the args
@ -303,6 +323,7 @@ Command line options:
"o": False, "o": False,
"r": False, "r": False,
"h": False, "h": False,
"rm_explicit": False,
} }
directory = None directory = None
@ -329,7 +350,7 @@ Command line options:
return 0 return 0
# create nicole instance # create nicole instance
nicole = Nicole(test_run=options["t"], silent=options["s"], write_history=options["n"], ignore_history=options["i"], overwrite_tag=options["o"], recursive=options["r"]) nicole = Nicole(test_run=options["t"], silent=options["s"], write_history=options["n"], ignore_history=options["i"], overwrite_tag=options["o"], recursive=options["r"], rm_explicit=options["rm_explicit"])
# start with file or directory # start with file or directory
if file: if file: