Fixed NoneType bug and code cleanup

This commit is contained in:
Eric Torres 2018-10-22 10:45:11 -07:00
parent 9e23b0b88a
commit ceb544b7c6

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
"""Download audio using youtube-dl, passing """Download audio using youtube-dl, passing
a specific set of options specified by the user. a specific set of options specified by the user.
===== =====
Usage Usage
===== =====
@ -11,50 +12,51 @@ import argparse
import pathlib import pathlib
import subprocess import subprocess
parser = argparse.ArgumentParser() if __name__ == '__main__':
parser.add_argument('-b', '--batch-dl', parser = argparse.ArgumentParser()
dest='batchfile', parser.add_argument('-b', '--batch-dl',
type=str, dest='batchfile',
help='provide the links from a text file') type=str,
parser.add_argument('-f', '--format', help='provide the links from a text file')
type=str, parser.add_argument('-f', '--format',
default='opus', type=str,
help='the format to use') default='opus',
parser.add_argument('-n', '--filename', help='the format to use')
type=str, parser.add_argument('-n', '--filename',
help='the name of the downloaded file (without extension)') type=str,
parser.add_argument('urls', help='the name of the downloaded file (without extension)')
nargs='*', parser.add_argument('urls',
help='video URLs') nargs='*',
args = parser.parse_args() help='video URLs')
args = parser.parse_args()
default_filename = f"{pathlib.Path.home()}/Music/%(title)s.%(ext)s" default_filename = f"{pathlib.Path.home()}/Music/%(title)s.%(ext)s"
dl_opts = [] dl_opts = []
dl_opts.append('--no-part') dl_opts.append('--no-part')
dl_opts.append('--no-continue') dl_opts.append('--no-continue')
dl_opts.append('--extract-audio') dl_opts.append('--extract-audio')
dl_opts.append(f"--audio-format={args.format}") dl_opts.append(f"--audio-format={args.format}")
dl_opts.append(f"--output={args.filename}") dl_opts.append(f"--output={args.filename}")
# filename handling # filename handling
# -b and -n should not be used together # -b and -n should not be used together
if args.filename and args.batchfile: if args.filename and args.batchfile:
print('Ignoring --batch-dl and --filename') print('Ignoring --batch-dl and --filename')
dl_opts.append(f"--output={default_filename}") dl_opts.append(f"--output={default_filename}")
elif args.filename: elif args.filename:
dl_opts.append(f"--output={pathlib.Path.home()}/Music/{args.filename}.%(ext)s") dl_opts.append(f"--output={pathlib.Path.home()}/Music/{args.filename}.%(ext)s")
else: else:
dl_opts.append(f"--output={default_filename}") dl_opts.append(f"--output={default_filename}")
# URL handling # URL handling
if args.batchfile: if args.batchfile:
dl_opts.append(f"--batch-file={args.batchfile}") dl_opts.append(f"--batch-file={args.batchfile}")
elif len(args.urls) == 0: elif len(args.urls) == 0:
print("URLs are required") print("URLs are required")
exit(2) exit(2)
else: else:
dl_opts += args.urls dl_opts.extend(args.urls)
dl = subprocess.run(['youtube-dl'].extend(dl_opts)) dl = subprocess.run(['youtube-dl'] + dl_opts)