A lot of fixes (see description)

- fix indentation (I used a broken vim config)
- add a -d (dry run) option
- add guarding double quotes at a few places
- use `python3` if no `python` executable exists
- explicitly use bash (instead of posix sh) where it is required
This commit is contained in:
r4 2022-06-11 12:39:16 +02:00
parent 2655890dd4
commit 6ede9a651f
2 changed files with 185 additions and 164 deletions

17
init.sh
View File

@ -1,6 +1,19 @@
#!/usr/bin/env sh
CUSTOM_PYTHON=
if ! which python > /dev/null; then
if which python3 > /dev/null; then
echo ">>> python not found, using python3 instead"
CUSTOM_PYTHON=python3
else
echo ">>> No python executable found, please install python or python3"
exit 1
fi
fi
if [ -e yt-dlc/ ]; then
echo ">>> youtube-dlc already downloaded, delete and redownload? [y/N]"
printf ">>> youtube-dlc already downloaded, delete and redownload? [y/N]: "
read RES
[ "$RES" = "y" ] || [ "$RES" = "Y" ] && rm -rf yt-dlc/
fi
@ -13,3 +26,5 @@ if [ ! -e yt-dlc/ ]; then
fi
echo ">>> Building youtube-dlc"
make -C yt-dlc/ youtube-dlc
[ -n "$CUSTOM_PYTHON" ] &&
sed -i "1c\\#!/usr/bin/env $CUSTOM_PYTHON" yt-dlc/youtube-dlc

View File

@ -1,4 +1,4 @@
#!/usr/bin/env sh
#!/usr/bin/env bash
source "$(dirname "$0")/config.sh"
@ -34,14 +34,15 @@ usage() {
echo " -D - Download episodes in German"
echo " -u - Update episode index (default)"
echo " -U - Skip episode index update"
echo " -d - Dry run: don't download, just print out URLs"
}
unset OPT_SEASON OPT_EPISODE OPT_ALL OPT_EN OPT_LANG OPT_PROGRESS OPT_UPDATE_INDEX
unset OPT_SEASON OPT_EPISODE OPT_ALL OPT_EN OPT_LANG OPT_PROGRESS OPT_UPDATE_INDEX OPT_DRY
OPT_LANG="EN"
OPT_PROGRESS=true
OPT_UPDATE_INDEX=true
while getopts "pPEDuUas:e:h" arg; do
while getopts "pPEDuUdas:e:h" arg; do
case "$arg" in
h)
usage
@ -75,6 +76,9 @@ while getopts "pPEDuUas:e:h" arg; do
U)
unset OPT_UPDATE_INDEX
;;
d)
OPT_DRY=true
;;
?)
usage
exit 1
@ -96,14 +100,14 @@ update_index() {
[ ! -e "$INDEX_FILENAME" ] && echo "$INDEX_INITIAL_URL" > "$INDEX_FILENAME"
echo -ne "\e[32m>>> Updating episode index\e[m"
while true; do
local URL=$(tail -n1 "$INDEX_FILENAME")
local NEWURLS=$(curl -s "$URL" | grep -o "$REGEX_EPISODE_URL" | tr -d "\"" | sed -E "s/^/https:\/\/www.southpark.de/g")
[ "$URL" = $(printf "$NEWURLS" | tail -n1) ] && break
local URL="$(tail -n1 "$INDEX_FILENAME")"
local NEWURLS="$(curl -s "$URL" | grep -o "$REGEX_EPISODE_URL" | tr -d "\"" | sed -E "s/^/https:\/\/www.southpark.de/g")"
[ "$URL" = "$(printf "$NEWURLS" | tail -n1)" ] && break
echo "$NEWURLS" >> "$INDEX_FILENAME"
echo -ne "\e[32m.\e[m"
done
# The awk command removes duplicate lines
local NEW_INDEX=$(awk '!x[$0]++' "$INDEX_FILENAME")
local NEW_INDEX="$(awk '!x[$0]++' "$INDEX_FILENAME")"
printf "$NEW_INDEX" > "$INDEX_FILENAME"
echo
}
@ -142,7 +146,7 @@ monitor_progress() {
local TMP_DIR="$1"
while true; do
[ ! -e "$TMP_DIR" ] && break
printf " Downloaded: %s\r" $(du -bB M "$TMP_DIR" | cut -f1)
printf " Downloaded: %s\r" "$(du -bB M "$TMP_DIR" | cut -f1)"
sleep 0.5
done
}
@ -167,11 +171,12 @@ download_episode() {
local EPISODE_NUMBER="$2"
local OUTFILE="${OUTDIR}/South_Park_S${SEASON_NUMBER}_E${EPISODE_NUMBER}_${OPT_LANG}.mp4"
[ -e "$OUTFILE" ] && echo "Already downloaded Season ${SEASON_NUMBER} Episode ${EPISODE_NUMBER}" && return
local URL=$(get_episode "$SEASON_NUMBER" "$EPISODE_NUMBER")
local URL="$(get_episode "$SEASON_NUMBER" "$EPISODE_NUMBER")"
[ -z "$URL" ] && echo "Unable to download Season ${SEASON_NUMBER} Episode ${EPISODE_NUMBER}; skipping" && return
p_info "Downloading Season $SEASON_NUMBER Episode $EPISODE_NUMBER ($URL)"
if [ -z "$OPT_DRY" ]; then
trap download_interrupt SIGINT
TMPDIR=$(mktemp -d "/tmp/southparkdownloader.XXXXXXXXXX")
TMPDIR="$(mktemp -d "/tmp/southparkdownloader.XXXXXXXXXX")"
[ -n "$OPT_PROGRESS" ] && monitor_progress "$TMPDIR"&
pushd "$TMPDIR" > /dev/null
if ! "$YOUTUBE_DL" "$URL" 2>/dev/null | grep --line-buffered "^\[download\]" | grep -v --line-buffered "^\[download\] Destination:"; then
@ -189,20 +194,21 @@ download_episode() {
ffmpeg -safe 0 -f concat -i "list.txt" -c copy "$OUTFILE" 2>/dev/null
popd > /dev/null
trap - SIGINT
fi
tmp_cleanup
}
# Takes season number as an argument
download_season() {
local SEASON_NUMBER="$1"
local NUM_EPISODES=$(get_num_episodes "$SEASON_NUMBER")
local NUM_EPISODES="$(get_num_episodes "$SEASON_NUMBER")"
for i in $(seq "$NUM_EPISODES"); do
download_episode "$SEASON_NUMBER" "$i"
done
}
download_all() {
local NUM_SEASONS=$(get_num_seasons)
local NUM_SEASONS="$(get_num_seasons)"
for i in $(seq "$NUM_SEASONS"); do
download_season "$i"
done