Skip to content

Instantly share code, notes, and snippets.

@tjluoma
Created April 16, 2020 18:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjluoma/89123d1f37d77509bb0aaa95d71f052c to your computer and use it in GitHub Desktop.
Save tjluoma/89123d1f37d77509bb0aaa95d71f052c to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh -f
# This tells the script to get the $PATH from the ~/.path file if it exists
# otherwise it will use the second $PATH in the "else" clause
if [[ -e "$HOME/.path" ]]
then
source "$HOME/.path"
else
PATH='/usr/local/scripts:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin'
fi
if ((! $+commands[youtube-dl] ))
then
echo "$NAME: 'youtube-dl' is required but not found in $PATH" \
| tee -a "$HOME/Desktop/$NAME.errors.txt"
exit 1
fi
# This is the same file that we created in the Shortcut,
# except that we need to make sure we include the "$HOME/Dropbox" part of it
#
# Obviously, if your Dropbox folder is somewhere else, change this too.
#
# If you change the filename in the Shortcut, change it here too
#
INPUT="$HOME/Dropbox/Action/downie-get.txt"
if [[ ! -e "$INPUT" ]]
then
# What happens if the script is run but the file does not exist?
# We should just quit immediately
echo "$NAME: The input file does not exist."
exit 0
fi
if [[ ! -s "$INPUT" ]]
then
# What happens if the script is run and the file exists but it is
# zero bytes? We should just quit immediately
echo "$NAME: The input file is empty."
exit 0
fi
## Ok, so if we get here, the file exists and is not empty.
# we're going to use this so we can remove the original file ASAP
# in case another URL gets sent by another invocation of the Shortcut
TEMPFILE="$HOME/.Trash/downie-get.$$.$RANDOM.txt"
# This will rename the original file to this temp file in the trash
mv -vf "$INPUT" "$TEMPFILE"
# the 'egrep' line will look for any line that starts with 'http'
# which will also match 'https' URLs, of course
# and then it will process each line that starts with https
# and ignore all of the other lines, including any blank lines.
egrep '^http' "$TEMPFILE" | while read line
do
# I added a few configuration options for youtube-dl but you
# should feel free to add / change / remove.
#
# I put them each on their own line to make it easier to
# add / change / remove options as you wish.
#
# !! Just make sure that each line ends with a '\'
# EXCEPT for the last line which ends in `&`
#
# Note that the last line ends with a `&` which will tell
# youtube-dl to download each URL in the background
# allowing you to download multiple videos at the same time
#
# If you do not wish to use this feature, or if something
# does not seem to be working properly, remove the `&` from
# the last line and try again.
#
youtube-dl \
--output "$HOME/Movies/%(title)s-%(id)s.%(ext)s" \
--restrict-filenames \
--continue \
--no-overwrites \
"$line" &
done
exit 0
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment