#!/usr/bin/env -S guix shell python -- python3 ''' Imports ''' import argparse import logging import sys import glob import os ''' Global variables ''' logger = logging.getLogger('sort_music.py') '''-----------------------------------------''' def parse_arguments(): ''' Parse command line arguments ''' parser = argparse.ArgumentParser() parser.add_argument('-a', '--album', help='EXACT album name shared across designated files.', required=True) parser.add_argument('-f', '--format', help='ncmpcpp format of files. Default is %t_%a_%b_%y. Read the documentation for that program for more information. It MUST include an artist name (%a) and year (%y).', default="%t_%a_%b_%y") parser.add_argument('-v', '--verbose', help='Enable full debug output.', action='store_true') return parser.parse_args() def configure_logs(verbose): ''' Configure program logging level ''' level = logging.DEBUG if verbose else logging.INFO ''' Configure stream handler ''' stream_handler = logging.StreamHandler(sys.stdout) stream_handler.setFormatter(logging.Formatter('%(name)s - %(levelname)s - %(message)s')) ''' For main logger ''' logger.addHandler(stream_handler) logger.setLevel(level) '''-----------------------------------------''' class SortMusic(): def __init__(self, logger, args): self.logger = logger self.album = args.album self.delimiter = args.format[2] self.artist_index = args.format.split(self.delimiter).index('%a') self.year_index = args.format.split(self.delimiter).index('%y') logger.info(f'Starting rename with file extension {self.album} and artist_index {self.artist_index}') self.files = None # list of relevant files in specified directory self.artist = None self.year = None def run(self): self._find_files() self._prompt() self._organize() def _find_files(self): self.files = list(glob.glob(f"*{self.album}*")) if len(self.files) == 0: self.logger.error(f'Files in this album NOT found!') exit(1) self.artist = self.files[0].split(self.delimiter).pop(self.artist_index).rsplit(".",1)[0] # remove file extension (sometimes occurs) self.year = self.files[0].split(self.delimiter).pop(self.year_index).rsplit(".",1)[0] def _prompt(self): self.logger.info(f'Found {len(self.files)} files written by {self.artist} in {self.year}.') quit = input(f'Files will now be organized (y/n)\t').lower() if not quit.startswith('y'): exit(1) def _organize(self): path = f'./{self.artist}/({self.year}) {self.album}' if not os.path.exists(path): logger.info(f'Creating previously missing path {path}') os.makedirs(path) for file in self.files: os.rename(f'./{file}', f'{path}/{file}') '''-----------------------------------------''' def main(): ''' This is the main function ''' args = parse_arguments() configure_logs(args.verbose) ''' Initialize and run ''' sorter = SortMusic(logger, args) sorter.run() if __name__ == '__main__': main()