Changed dlaudio to python

This commit is contained in:
Eric Torres 2018-10-02 12:29:27 -07:00
parent 058db44db0
commit bb50bb6e78
2 changed files with 60 additions and 168 deletions

60
dlaudio.py Executable file
View File

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

View File

@ -1,168 +0,0 @@
#!/usr/bin/env bash
# Download audio using youtube-dl, passing
# a specific set of options specified by the user
printHelp() {
cat << EOF
Usage: dlaudio [options] [URLs]
Options:
-b, --batch-dl provide the links from a text file
-f, --format the format to use (default: flac)
-h, --help print this help page
-n, --filename the name of the downloaded file (without the extension)
EOF
}
declare -a opts
batchfile=
filename=
declare -r default_filename="--output=${HOME}/Music/%(title)s.%(ext)s"
format=
# error messages
declare -r nobatchfile_error="Error: no batch file entered"
declare -r nofilename_error="Error: no filename entered"
declare -r noformat_error="Error: no format entered"
while true; do
case "${1}" in
"-b"|"--batch-dl")
batchfile="${2}"
case "${batchfile}" in
"")
echo "${nobatchfile_error}" >&2
exit 1
;;
-*)
echo "Not a file: ${batchfile}"
exit 1
;;
*)
[[ ! -f "${batchfile}" ]] && echo "Not a file: ${batchfile}" >&2 && exit 1
;;
esac
shift 2
continue
;;
--batch-dl=*)
batchfile="${1#*=}"
case "${batchfile}" in
"")
echo "${nobatchfile_error}" >&2
exit 1
;;
-*)
echo "Not a file: ${batchfile}" >&2
exit 1
;;
*)
[[ ! -f "${batchfile}" ]] && echo "Not a file: ${batchfile}" && exit 1
;;
esac
shift
continue
;;
"-f"|"--format")
format="${2}"
case "${format}" in
"")
echo "No format entered" >&2
exit 1
;;
-*)
echo "Not a format: ${format}"
exit 1
;;
esac
shift 2
continue
;;
--format=*)
format="${1#*=}"
case "${format}" in
"")
echo "${noformat_error}" >&2
exit 1
;;
-*)
echo "Not a format: ${format}" >&2
exit 1
;;
esac
shift
continue
;;
"-h"|"--help")
# print help page and exit
printHelp
exit
;;
"-n"|"--filename")
filename="${2}"
case "${filename}" in
"")
echo "${nofilename_error}" >&2
exit 1
;;
-*)
echo "Not a filename: ${filename}" >&2
exit 1
;;
esac
shift 2
continue
;;
--filename=*)
filename="${1#*=}"
case "${filename}" in
"")
echo "${nofilename_error}" >&2
exit 1
;;
-*)
echo "Not a filename: ${filename}" >&2
exit 1
;;
esac
shift
continue
;;
"--")
shift
break
;;
-*)
echo "Error, not an option: ${1}" >&2
exit 1
;;
*)
break
esac
done
# default options
opts+=("--no-part")
opts+=("--no-continue")
opts+=("--extract-audio")
opts+=("--audio-format=${format:-flac}")
# filename cannot be used at the same time as batch-dl
if [[ "${filename}" && -z "${batchfile}" ]]; then
opts+=("--output=${HOME}/Music/${filename}.%(ext)s")
elif [[ "${batchfile}" && -z "${filename}" ]]; then
opts+=("${default_filename}")
elif [[ "${batchfile}" && "${filename}" ]]; then
printf '%s\n' "Cannot pass '--batch-dl' and '--filename' together, ignoring"
opts+=("${default_filename}")
else
opts+=("${default_filename}")
fi
if [[ "${batchfile}" ]]; then
youtube-dl "${opts[@]}" --batch-file="${batchfile}"
elif [[ "${*}" ]]; then
youtube-dl "${opts[@]}" "${@}"
else
printf '%s\n' "No arguments entered, cancelling"
fi