From f20d63482029c02890239f70eb7057f696192021 Mon Sep 17 00:00:00 2001 From: Jackson Taylor Date: Sun, 7 Aug 2022 09:17:34 -0400 Subject: Handle special characters in file names Do this with a function to remove all of the special characters I've found so far. --- jamos | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/jamos b/jamos index 12cfd07..bca6f0c 100755 --- a/jamos +++ b/jamos @@ -94,16 +94,32 @@ def get_video_urls_in_playlist(playlist_url, ytdl): return urls +def remove_special_characters_for_filename(filename): + special_chars = [ + ['-', ' '], + ['(', ''], + [')', ''], + ['/', ' '], + ['/', ' '], + [' ', '_'], + ["'", ''], + ["&", 'and'], + [chr(8217), ''], + ['$', 's'], + ['.', ''] + ] + new_name = filename + + for char_set in special_chars: + new_name = new_name.replace(char_set[0], char_set[1]) + + return new_name.lower() + + def move_file(file, metadata, output_directory): - artist = metadata['artist'].replace( - '-', ' ').replace("/", " ").replace(' ', '_').replace("'", "").replace( - "&", "and").replace(chr(8217), "").lower() - album = metadata['album'].replace( - '-', ' ').replace("/", " ").replace(' ', '_').replace("'", "").replace( - "&", "and").replace(chr(8217), "").lower() - title = metadata['title'].replace( - '-', ' ').replace("/", " ").replace(' ', '_').replace("'", "").replace( - "&", "and").replace(chr(8217), "").lower() + artist = remove_special_characters_for_filename(metadata['artist']) + album = remove_special_characters_for_filename(metadata['album']) + title = remove_special_characters_for_filename(metadata['title']) final_directory = os.path.join( output_directory, @@ -112,7 +128,6 @@ def move_file(file, metadata, output_directory): Path(final_directory).mkdir(parents=True, exist_ok=True) - # TODO: Include album title in filename # TODO: Research converting to mp3 instead of just naming it such. # TODO: Research better file formats over mp3? os.rename( -- cgit v1.2.3