From a39f4c9c515d1de170335427c69c84ce1ff482c8 Mon Sep 17 00:00:00 2001 From: Eric Torres Date: Wed, 17 Apr 2019 21:27:50 -0700 Subject: [PATCH] General script cleanup --- dlaudio.py | 94 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 56 insertions(+), 38 deletions(-) diff --git a/dlaudio.py b/dlaudio.py index a15f659..e6b4e16 100644 --- a/dlaudio.py +++ b/dlaudio.py @@ -3,55 +3,73 @@ Dependencies: ============= + * youtube-dl """ import argparse -import pathlib +from pathlib import Path import shutil import subprocess # =========== Constants ========== -YOUTUBE_DL_BIN = shutil.which("youtube-dl") -DEFAULT_FILENAME = f"{pathlib.Path.home()}/Music/%(title)s.%(ext)s" - -# ========== Error Codes ========== -E_NOURLS = 2 - -# ========== Main Script ========== -parser = argparse.ArgumentParser() -parser.add_argument("-b", "--batchfile", help="provide the links from a text file") -parser.add_argument( - "-f", "--format", type=str, default="opus", help="the format to use" -) -parser.add_argument( - "-n", "--filename", type=str, help="downloaded filename (without extension)" -) -parser.add_argument("urls", nargs="*", help="video URLs") -args = parser.parse_args() - -dl_opts = [ - YOUTUBE_DL_BIN, +YOUTUBE_DL_BIN = "/usr/bin/youtube-dl" +DEFAULT_FILENAME = f"{Path.home() / 'Music'}/%(title)s.%(ext)s" +DEFAULT_YOUTUBE_DL_OPTS = ( "--no-part", + "--audio-quality=0", "--no-continue", "--extract-audio", - f"--audio-format={args.format}", -] +) -# filename handling -# if -b is used, DEFAULT_FILENAME must take precedence -if args.filename is not None and args.batchfile is None: - dl_opts.append(f"--output={args.filename}") -else: - dl_opts.append(f"--output={DEFAULT_FILENAME}") +# ----- Error Codes ----- +E_NOURLS = 2 -# URL handling -if args.batchfile is not None: - dl_opts.append(f"--batch-file={args.batchfile}") -elif args.urls is not None: - dl_opts.extend(args.urls) -else: - print("URLs are required") - exit(E_NOURLS) -subprocess.run(dl_opts) +# ========== Functions ========== +def parse_cmdline_arguments(**kwargs): + """Parse command line arguments passed to the script. + All kwargs are passed to ArgumentParser.parse_args(). + + :rtype: argparse.Namespace object + """ + parser = argparse.ArgumentParser() + parser.add_argument("-b", "--batchfile", help="provide the links from a text file") + parser.add_argument( + "-f", "--format", type=str, default="opus", help="the format to use" + ) + parser.add_argument( + "-n", "--filename", type=str, help="downloaded filename (without extension)" + ) + parser.add_argument("urls", nargs="*", help="video URLs") + + return parser.parse_args() + + +# ========== Main Script ========== +if __name__ == "__main__": + args = parse_cmdline_arguments() + + dl_opts = [ + YOUTUBE_DL_BIN, + *DEFAULT_YOUTUBE_DL_OPTS, + f"--audio-format={args.format}", + ] + + # filename handling + # if -b is used, DEFAULT_FILENAME must take precedence + if args.filename is not None and args.batchfile is None: + dl_opts.append(f"--output={args.filename}") + else: + dl_opts.append(f"--output={DEFAULT_FILENAME}") + + # URL handling + if args.batchfile is not None: + dl_opts.append(f"--batch-file={args.batchfile}") + elif args.urls is not None: + dl_opts.extend(args.urls) + else: + print("URLs are required") + exit(E_NOURLS) + + subprocess.run(dl_opts)