From afe4e5972a3b50756cb5575946389a344d8caf6e Mon Sep 17 00:00:00 2001 From: Jackson Taylor Date: Wed, 10 May 2023 12:57:44 -0400 Subject: Move all file operations to separate module --- file_operations.py | 61 +++++++++++++++++++++++++++++++++++++++++++ jamos | 76 ++++++++---------------------------------------------- 2 files changed, 72 insertions(+), 65 deletions(-) create mode 100644 file_operations.py diff --git a/file_operations.py b/file_operations.py new file mode 100644 index 0000000..9cb74fe --- /dev/null +++ b/file_operations.py @@ -0,0 +1,61 @@ +import errors +import os +from pathlib import Path +import shutil + + +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 save_debug_data(filename, data, debug=True): + if debug: + with open(filename, 'w') as f: + f.write(data) + + +def move_song_to_permanent_location(music_directory, song_data, filename): + try: + # move song to music_directory/artist/album/artist_album_title.ext + # excluding non filesafe characters + artist_for_filename = remove_special_characters_for_filename( + song_data['artists'][0]) + album_for_filename = remove_special_characters_for_filename( + song_data['album']) + title_for_filename = remove_special_characters_for_filename( + song_data['title']) + + song_output_dir = os.path.join(music_directory, + artist_for_filename, + album_for_filename) + + try: + Path(song_output_dir).mkdir(parents=True, exist_ok=True) + except OSError as ex: + raise errors.UnableToCreateAlbumDirectoryError(ex) + + new_filename = '{}_{}_{}.mp3'.format(artist_for_filename, + album_for_filename, + title_for_filename) + + shutil.move(filename, os.path.join(song_output_dir, new_filename)) + except Exception: + print("could not move file to correct directory!") diff --git a/jamos b/jamos index ba1d52d..2f4bfeb 100755 --- a/jamos +++ b/jamos @@ -5,7 +5,6 @@ import glob import json import music_tag import os -from pathlib import Path import requests import shutil import sys @@ -13,6 +12,7 @@ import youtube_dl from ytmusicapi import YTMusic # import musicpd import errors +import file_operations # TODO: set this to false to begin with. for now it's always gonna print # debug information @@ -157,28 +157,6 @@ def create_downloader(music_directory, cookies=None): return youtube_dl.YoutubeDL(audio_options) -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 create_youtube_music_api_object(header_path): return YTMusic(header_path) @@ -227,12 +205,6 @@ def save_album(app, args, album_id): ALBUMS[album_id] = parsed_album -def save_debug_data(filename, data, debug=True): - if debug: - with open(filename, 'w') as f: - f.write(data) - - # TODO: Good place for some fancy yield work? def parse_songs(app, args, all_raw_song_data): all_parsed_songs = [] @@ -263,10 +235,12 @@ def parse_songs(app, args, all_raw_song_data): all_parsed_songs.append(parsed_song) if args.debug_location: - save_debug_data('parsed_album_data.json', json.dumps(ALBUMS), - debug=DEBUG) - save_debug_data('parsed_song_data.json', json.dumps(all_parsed_songs), - debug=DEBUG) + file_operations.save_debug_data('parsed_album_data.json', + json.dumps(ALBUMS), + debug=DEBUG) + file_operations.save_debug_data('parsed_song_data.json', + json.dumps(all_parsed_songs), + debug=DEBUG) return all_parsed_songs @@ -277,8 +251,8 @@ def get_library_songs(app, args, order='a_to_z'): if args.debug_location: path = os.path.join(args.debug_location, 'raw_song_data.json') - save_debug_data(path, json.dumps(all_raw_song_data), - debug=DEBUG) + file_operations.save_debug_data(path, json.dumps(all_raw_song_data), + debug=DEBUG) all_parsed_songs = parse_songs(app, args, all_raw_song_data) @@ -404,35 +378,6 @@ def tag_song(filename, song_data): print("could not tag song!") -def move_song_to_permanent_location(music_directory, song_data, filename): - try: - # move song to music_directory/artist/album/artist_album_title.ext - # excluding non filesafe characters - artist_for_filename = remove_special_characters_for_filename( - song_data['artists'][0]) - album_for_filename = remove_special_characters_for_filename( - song_data['album']) - title_for_filename = remove_special_characters_for_filename( - song_data['title']) - - song_output_dir = os.path.join(music_directory, - artist_for_filename, - album_for_filename) - - try: - Path(song_output_dir).mkdir(parents=True, exist_ok=True) - except OSError as ex: - raise errors.UnableToCreateAlbumDirectoryError(ex) - - new_filename = '{}_{}_{}.mp3'.format(artist_for_filename, - album_for_filename, - title_for_filename) - - shutil.move(filename, os.path.join(song_output_dir, new_filename)) - except Exception: - print("could not move file to correct directory!") - - def download_songs(ytdl, music_directory, songs): for song in songs: try: @@ -449,7 +394,8 @@ def download_songs(ytdl, music_directory, songs): tag_song(filename, song) - move_song_to_permanent_location(music_directory, song, filename) + file_operations.move_song_to_permanent_location(music_directory, + song, filename) except errors.UnableToCreateAlbumDirectoryError as ex: add_to_failed_songs(song, ex) print('Could not download {} - {}, moving on'.format( -- cgit v1.2.3