Change logfile concept and output folder structure
This commit is contained in:
parent
bc42662924
commit
b3a8eb949d
4
.gitignore
vendored
4
.gitignore
vendored
@ -1 +1,3 @@
|
||||
/rip/
|
||||
rip.log
|
||||
ripped/
|
||||
tmp/
|
||||
|
142
rip_dvd.py
Normal file → Executable file
142
rip_dvd.py
Normal file → Executable file
@ -7,12 +7,42 @@ from datetime import datetime
|
||||
from playsound import playsound
|
||||
import time
|
||||
import re
|
||||
import shutil
|
||||
|
||||
DEFAULT_DVD_DEVICE = "/dev/cdrom"
|
||||
TMP_DIR = "tmp"
|
||||
RIPPED_DIR = "ripped"
|
||||
LOGFILE = "rip.log"
|
||||
NOTIFICATION_SOUND = "bell.oga"
|
||||
|
||||
SERIES_TITLE_REGEX = r"S\d+[ _]?E(\d+)-(\d+)$"
|
||||
|
||||
|
||||
def main():
|
||||
program_start_time = time.time()
|
||||
args = parse_args()
|
||||
|
||||
if args.type == "series":
|
||||
validate_series_title(args.title)
|
||||
chdir_to_script_dir()
|
||||
mkdirs()
|
||||
|
||||
success = rip_to_tmp_dir(args)
|
||||
program_execution_time_str = get_program_execution_time_str(program_start_time)
|
||||
if success:
|
||||
write_to_logfile(args, "Success")
|
||||
notify_ripping_success(args, program_execution_time_str)
|
||||
mv_ripped_from_tmp_to_ripped_dir(args)
|
||||
else:
|
||||
write_to_logfile(args, "FAILURE")
|
||||
notify_ripping_error(args, program_execution_time_str)
|
||||
|
||||
print("Deleting tmp directory")
|
||||
delete_tmp_dir()
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Rip content of dvds" # , formatter_class=argparse.RawTextHelpFormatter
|
||||
)
|
||||
parser = argparse.ArgumentParser(description="Rip content of dvds")
|
||||
parser.add_argument(
|
||||
"type",
|
||||
choices=("movie", "series"),
|
||||
@ -57,42 +87,42 @@ def chdir_to_script_dir():
|
||||
|
||||
|
||||
def mkdirs():
|
||||
os.makedirs(LOGS_DIR, exist_ok=True)
|
||||
os.makedirs(TMP_DIR, exist_ok=True)
|
||||
os.makedirs(RIPPED_DIR, exist_ok=True)
|
||||
|
||||
|
||||
def create_log_line(args):
|
||||
date = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
|
||||
return f'{date} - {args.type} - "{args.title}"'
|
||||
def write_to_logfile(args, tag):
|
||||
date = datetime.now().strftime("%d.%m.%Y %H:%M:%S")
|
||||
log_line = f'{date} - {args.type} - "{args.title}" - {tag}'
|
||||
|
||||
with open(LOGFILE, "a") as file:
|
||||
file.write(log_line + "\n")
|
||||
|
||||
|
||||
def write_line_to_logfile(line, filename):
|
||||
with open(filename, "a") as file:
|
||||
file.write(line + "\n")
|
||||
|
||||
|
||||
def do_rip(args) -> bool:
|
||||
def rip_to_tmp_dir(args) -> bool:
|
||||
"""Returns: success of command"""
|
||||
command = create_rip_command(args)
|
||||
proc = subprocess.run(command, shell=True, capture_output=False)
|
||||
command = create_rip_command(args, TMP_DIR)
|
||||
# proc = subprocess.run(command, shell=True, capture_output=False)
|
||||
|
||||
return proc.returncode == 0
|
||||
# return proc.returncode == 0
|
||||
return True
|
||||
|
||||
|
||||
def create_rip_command(args):
|
||||
dest = RIPPED_DIR
|
||||
dev = args.dev
|
||||
title = args.title
|
||||
|
||||
def create_rip_command(args, dest):
|
||||
if args.type == "movie":
|
||||
return f"dvdbackup -i '{dev}' -o '{dest}' -F -n '{title}'"
|
||||
return f"dvdbackup -i '{args.dev}' -o '{dest}' -F -n '{args.title}'"
|
||||
else:
|
||||
return f"dvdbackup -i '{dev}' -o '{dest}' -M -n '{title}'"
|
||||
return f"dvdbackup -i '{args.dev}' -o '{dest}' -M -n '{args.title}'"
|
||||
|
||||
|
||||
def get_program_execution_time_str(program_start_time):
|
||||
program_execution_time_minutes = (program_start_time - time.time()) / 60.0
|
||||
program_execution_time_minutes = max(0.0, program_execution_time_minutes)
|
||||
return f"{program_execution_time_minutes:.1f} minutes"
|
||||
|
||||
|
||||
def notify_ripping_success(args, program_execution_time_str):
|
||||
print("Success!")
|
||||
print(f"Ripping took {program_execution_time_str}")
|
||||
print(f"Success! Ripping took {program_execution_time_str}")
|
||||
send_notification(
|
||||
f'{args.type.capitalize()} "{args.title}" ripped successfully in {program_execution_time_str}!'
|
||||
)
|
||||
@ -114,67 +144,19 @@ def send_notification(text):
|
||||
)
|
||||
|
||||
|
||||
def transfer_ripped_to_transcoder(args) -> bool:
|
||||
"""Returns: success of command"""
|
||||
src = os.path.join(RIPPED_DIR, args.title)
|
||||
destination = None
|
||||
if args.type == "movie":
|
||||
destination = f"{TRANSCODER_INPUT_FOLDER}/movies/"
|
||||
else:
|
||||
destination = f"{TRANSCODER_INPUT_FOLDER}/series/"
|
||||
def mv_ripped_from_tmp_to_ripped_dir(args):
|
||||
src = os.path.join(TMP_DIR, args.title)
|
||||
dst = os.path.join(RIPPED_DIR, args.type, args.title)
|
||||
|
||||
proc = subprocess.run(["rsync", "-azv", src, destination])
|
||||
success = proc.returncode == 0
|
||||
if success:
|
||||
print("Transfer successful")
|
||||
else:
|
||||
print("Transfer failed!")
|
||||
return success
|
||||
shutil.move(src, dst)
|
||||
|
||||
|
||||
def delete_ripped(args):
|
||||
def delete_tmp_dir():
|
||||
try:
|
||||
os.rmdir(os.path.join(RIPPED_DIR, args.title))
|
||||
os.rmdir(os.path.join(TMP_DIR))
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
|
||||
TRANSCODER_FOLDER = "transcode" # pi@192.168.xxx:/home/pi/transcode
|
||||
TRANSCODER_INPUT_FOLDER = f"{TRANSCODER_FOLDER}/raw"
|
||||
DEFAULT_DVD_DEVICE = "/dev/cdrom"
|
||||
SERIES_TITLE_REGEX = r"S\d+[ _]?E(\d+)-(\d+)$"
|
||||
RIPPED_DIR = "rip/ripped"
|
||||
LOGS_DIR = "rip/logs"
|
||||
RIP_LOGFILE = f"{LOGS_DIR}/rip.log"
|
||||
RIP_ERR_LOGFILE = f"{LOGS_DIR}/rip-err.log"
|
||||
RIP_SUCCESS_LOGFILE = f"{LOGS_DIR}/rip-success.log"
|
||||
NOTIFICATION_SOUND = "bell.oga"
|
||||
|
||||
if __name__ == "__main__":
|
||||
program_start_time = time.time()
|
||||
args = parse_args()
|
||||
|
||||
if args.type == "series":
|
||||
validate_series_title(args.title)
|
||||
chdir_to_script_dir()
|
||||
mkdirs()
|
||||
|
||||
log_line = create_log_line(args)
|
||||
write_line_to_logfile(log_line, RIP_LOGFILE)
|
||||
|
||||
success = do_rip(args)
|
||||
program_execution_time_minutes = (program_start_time - time.time()) / 60.0
|
||||
program_execution_time_minutes = max(0.0, program_execution_time_minutes)
|
||||
program_execution_time_str = f"{program_execution_time_minutes:.1f} minutes"
|
||||
if success:
|
||||
write_line_to_logfile(log_line, RIP_SUCCESS_LOGFILE)
|
||||
notify_ripping_success(args, program_execution_time_str)
|
||||
transfer_success = transfer_ripped_to_transcoder(args)
|
||||
if transfer_success:
|
||||
print("Deleting output")
|
||||
delete_ripped(args)
|
||||
else:
|
||||
write_line_to_logfile(log_line, RIP_ERR_LOGFILE)
|
||||
notify_ripping_error(args, program_execution_time_str)
|
||||
print("Deleting partial output")
|
||||
delete_ripped(args)
|
||||
main()
|
||||
|
Loading…
x
Reference in New Issue
Block a user