nixfiles/musicutil/commands/process_command.py

108 lines
3 KiB
Python
Raw Normal View History

2022-02-06 15:51:42 +00:00
from musicutil.utils.do_replaygain import do_replaygain
2022-02-06 15:20:12 +00:00
from ..types import File
2022-02-04 13:35:01 +00:00
from ..utils.scan_for_music import scan_for_music
from ..utils.load_tag_information import load_tag_information
from ..utils.substitutions import reduce_to_ascii_and_substitute
2022-02-04 13:32:22 +00:00
from copy import deepcopy as deep_copy
from os import rename as rename_file
2022-02-04 13:54:10 +00:00
2022-02-04 13:32:22 +00:00
class ProcessCommandState:
2022-02-04 13:54:10 +00:00
files: list[File] = []
2022-02-06 16:18:14 +00:00
2022-02-06 15:20:12 +00:00
class ProcessCommandArgs:
src: str
dry_run: bool
2022-02-06 15:51:42 +00:00
skip_replaygain: bool
2022-02-06 15:20:12 +00:00
2022-02-06 16:18:14 +00:00
2022-02-06 15:20:12 +00:00
def add_process_command(subparsers):
process_parser = subparsers.add_parser('process')
process_parser.add_argument(
'src',
type=str,
help='src base music directory')
process_parser.add_argument(
'--dry-run', action='store_true')
2022-02-06 15:51:42 +00:00
process_parser.add_argument(
'--skip-replaygain',
action='store_true')
2022-02-06 15:20:12 +00:00
2022-02-06 16:18:14 +00:00
2022-02-06 15:20:12 +00:00
def get_process_args(args) -> ProcessCommandArgs:
command_args = ProcessCommandArgs()
command_args.src = args.src
command_args.dry_run = args.dry_run
2022-02-06 15:51:42 +00:00
command_args.skip_replaygain = args.skip_replaygain
2022-02-06 15:20:12 +00:00
return command_args
2022-02-04 13:54:10 +00:00
2022-02-06 16:18:14 +00:00
2022-02-04 13:54:10 +00:00
class ProcessCommand():
2022-02-06 15:20:12 +00:00
def __init__(self, args: ProcessCommandArgs):
self.args = args
2022-02-04 13:54:10 +00:00
self.state = ProcessCommandState()
def run(self):
print("Renaming")
self.scan_for_music()
self.load_tag_information()
self.rename_files()
2022-02-06 16:18:14 +00:00
if (not self.args.skip_replaygain) and (
not self.args.dry_run):
2022-02-06 15:51:42 +00:00
self.add_replaygain_tags()
2022-02-04 13:54:10 +00:00
def scan_for_music(self):
print("Scanning For Music")
2022-02-06 15:20:12 +00:00
self.state.files = scan_for_music(self.args.src)
2022-02-04 13:54:10 +00:00
def load_tag_information(self):
print("Loading Tag Information")
for file in self.state.files:
tags = load_tag_information(file)
file.tags = tags
def _rename_file(self, file: File) -> File:
filename = file.filename
artist = file.tags.artist.replace("\n", "")
title = file.tags.title.replace("\n", "")
proper_filename = reduce_to_ascii_and_substitute(
f"{artist} - {title}")
if filename != proper_filename:
2022-02-05 12:48:03 +00:00
print(f"Renaming \"{filename}\"", "to " +
2022-02-04 13:54:10 +00:00
f"\"{proper_filename}\"" + "\n")
new_file = deep_copy(file)
new_file.filename = proper_filename
2022-02-06 15:20:12 +00:00
if not self.args.dry_run:
2022-02-04 13:54:10 +00:00
rename_file(
file.join_path_to(),
new_file.join_path_to())
# so that other steps after read the new file and not
# the orig pre-rename file when not dry run
return new_file
else:
return file
def rename_files(self):
print("Renaming files")
for file in self.state.files:
index = self.state.files.index(file)
self.state.files[index] = self._rename_file(
file)
2022-02-06 15:51:42 +00:00
def add_replaygain_tags(self):
print("Adding ReplayGain Tags")
for file in self.state.files:
2022-02-06 16:18:14 +00:00
print(
f"Adding ReplayGain Tags to \"{file.filename}.{file.extension}\"")
2022-02-06 15:51:42 +00:00
2022-02-06 16:18:14 +00:00
do_replaygain(file)