scripts/regain

92 lines
3.5 KiB
Plaintext
Raw Normal View History

2023-10-02 11:20:49 +01:00
#!/usr/bin/env python3
# regain - search folders below working directory and call regainer on each album found
# Copyright Frederick Boniface 2023 - fredboniface.co.uk
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import subprocess
import logging
import argparse
FORMATS = ["flac", "m4a", "aac", "alac", "mp3", "m3a", "ogg", "opus", "oga"]
PATH_TO_REGAINER = "./regainer.py"
def parse_arguments():
parser = argparse.ArgumentParser(description='Call regainer.py on subdirectories containing music files')
parser.add_argument('--debug', action='store_true', help='Enable debug logging')
return parser.parse_args()
def configureLogging(debug_mode = False):
log_level = logging.DEBUG if debug_mode else logging.INFO
logging.basicConfig(level=log_level, format='%(asctime)s - %(levelname)s - %(message)s')
def getWorkingDirectory():
return os.getcwd()
def find_files_by_extension(workdir):
file_lists = []
for root, _, files in os.walk(workdir):
matching_files = [os.path.join(root, file) for file in files if file.endswith(tuple(FORMATS))]
if matching_files:
file_lists.append(matching_files)
return file_lists
def countFiles(file_list_of_lists):
count = 0
for item in file_list_of_lists:
count = count + len(item)
return count
if __name__ == "__main__":
args = parse_arguments()
configureLogging(args.debug)
workdir = getWorkingDirectory()
logging.info(f"regain.py WorkDir: {workdir}")
logging.debug("Debug logs enabled")
file_lists = find_files_by_extension(workdir)
logging.debug(f"File lists: {file_lists}")
logging.info(f"Files for processing: {countFiles(file_lists)}")
cmd = ["python", PATH_TO_REGAINER, "-f"]
for sublist in file_lists:
cmd.append("-a")
for item in sublist:
cmd.append(item)
logging.debug(f"Running command: {cmd}")
logging.info("Running regainer.py")
if args.debug:
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
output = result.stdout
error = result.stderr
# Handle the captured output as needed
print("Output:", output)
print("Error:", error)
else:
result = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
logging.debug(f"regainer.py has exited with code {result.returncode}")
if result.returncode != 0:
print("You can re-run with --debug to view the output from regainer")
else:
print("Files successfully tagged")