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
|
from playsound import playsound
|
||||||
import time
|
import time
|
||||||
import re
|
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():
|
def parse_args():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(description="Rip content of dvds")
|
||||||
description="Rip content of dvds" # , formatter_class=argparse.RawTextHelpFormatter
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"type",
|
"type",
|
||||||
choices=("movie", "series"),
|
choices=("movie", "series"),
|
||||||
@ -57,42 +87,42 @@ def chdir_to_script_dir():
|
|||||||
|
|
||||||
|
|
||||||
def mkdirs():
|
def mkdirs():
|
||||||
os.makedirs(LOGS_DIR, exist_ok=True)
|
os.makedirs(TMP_DIR, exist_ok=True)
|
||||||
os.makedirs(RIPPED_DIR, exist_ok=True)
|
os.makedirs(RIPPED_DIR, exist_ok=True)
|
||||||
|
|
||||||
|
|
||||||
def create_log_line(args):
|
def write_to_logfile(args, tag):
|
||||||
date = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
|
date = datetime.now().strftime("%d.%m.%Y %H:%M:%S")
|
||||||
return f'{date} - {args.type} - "{args.title}"'
|
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):
|
def rip_to_tmp_dir(args) -> bool:
|
||||||
with open(filename, "a") as file:
|
|
||||||
file.write(line + "\n")
|
|
||||||
|
|
||||||
|
|
||||||
def do_rip(args) -> bool:
|
|
||||||
"""Returns: success of command"""
|
"""Returns: success of command"""
|
||||||
command = create_rip_command(args)
|
command = create_rip_command(args, TMP_DIR)
|
||||||
proc = subprocess.run(command, shell=True, capture_output=False)
|
# proc = subprocess.run(command, shell=True, capture_output=False)
|
||||||
|
|
||||||
return proc.returncode == 0
|
# return proc.returncode == 0
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def create_rip_command(args):
|
def create_rip_command(args, dest):
|
||||||
dest = RIPPED_DIR
|
|
||||||
dev = args.dev
|
|
||||||
title = args.title
|
|
||||||
|
|
||||||
if args.type == "movie":
|
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:
|
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):
|
def notify_ripping_success(args, program_execution_time_str):
|
||||||
print("Success!")
|
print(f"Success! Ripping took {program_execution_time_str}")
|
||||||
print(f"Ripping took {program_execution_time_str}")
|
|
||||||
send_notification(
|
send_notification(
|
||||||
f'{args.type.capitalize()} "{args.title}" ripped successfully in {program_execution_time_str}!'
|
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:
|
def mv_ripped_from_tmp_to_ripped_dir(args):
|
||||||
"""Returns: success of command"""
|
src = os.path.join(TMP_DIR, args.title)
|
||||||
src = os.path.join(RIPPED_DIR, args.title)
|
dst = os.path.join(RIPPED_DIR, args.type, args.title)
|
||||||
destination = None
|
|
||||||
if args.type == "movie":
|
|
||||||
destination = f"{TRANSCODER_INPUT_FOLDER}/movies/"
|
|
||||||
else:
|
|
||||||
destination = f"{TRANSCODER_INPUT_FOLDER}/series/"
|
|
||||||
|
|
||||||
proc = subprocess.run(["rsync", "-azv", src, destination])
|
shutil.move(src, dst)
|
||||||
success = proc.returncode == 0
|
|
||||||
if success:
|
|
||||||
print("Transfer successful")
|
|
||||||
else:
|
|
||||||
print("Transfer failed!")
|
|
||||||
return success
|
|
||||||
|
|
||||||
|
|
||||||
def delete_ripped(args):
|
def delete_tmp_dir():
|
||||||
try:
|
try:
|
||||||
os.rmdir(os.path.join(RIPPED_DIR, args.title))
|
os.rmdir(os.path.join(TMP_DIR))
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
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__":
|
if __name__ == "__main__":
|
||||||
program_start_time = time.time()
|
main()
|
||||||
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)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user