utility-scripts/download_daily_shows.sh

124 lines
3.6 KiB
Bash
Executable File

#/bin/bash
#
# sudo pip install --upgrade youtube-dl && sudo pip3 install --upgrade youtube-dl
#
# If you get youtube-dl permissions errors like
# "ImportError: cannot import name 'main'", run
# sudo find /usr/local -type d -exec chmod a+rx {} + && sudo find /usr/local -type f -exec chmod a+r {} + && sudo find /usr/local/bin -type f -exec chmod a+rx {} +
#
TMP_DIR=/tmp/dailyshow
DOWNLOAD_DIR=/media/nas/video/series
#YOUTUBE_DL="/usr/bin/youtube-dl"
YOUTUBE_DL="python3 /usr/local/bin/youtube-dl"
#KEEP_DAYS=40
KEEP_DAYS=100
ARGS=-qwc
if [ ! -d ${DOWNLOAD_DIR} ]; then
echo "Error: directory not found: ${DOWNLOAD_DIR}"
exit
fi
OPTIND=1 # Reset is necessary if getopts was used previously
while getopts ":u:d" opt; do
case "${opt}" in
d)
ARGS=-vwc
set -x
;;
u)
url="${OPTARG}"
;;
h)
echo "Usage: ${0} <URL> <-d>"
exit 1
;;
esac
done
shift "$((OPTIND-1))" # Shift off the options and optional --.
# Formats:
# python3 /usr/local/bin/youtube-dl -F http://www.cc.com/shows/the-daily-show-with-trevor-noah/full-episodes/
# [info] Available formats for 097b3593-7592-4fe6-95fe-18d5846fead0:
# format code extension resolution note
# 278 mp4 384x216
# 498 mp4 512x288
# 1028 mp4 640x360
# 1528 mp4 768x432
# 2128 mp4 960x540
# 3128 mp4 1280x720
# 5128 mp4 1920x1080 (best)
#FORMAT="vhttp-750"
#FORMAT="rtmp-750"
#FORMAT="rtmp-3500/rtmp2200"
function download_show {
url="$1"
show="$2"
mkdir -p "${TMP_DIR}/${show}"
mkdir -p "${DOWNLOAD_DIR}/${show}"
# Get a chosen video format for each site
if [[ "${url}" =~ cc.com ]]; then
FORMAT="1028/907/498/500"
FORMAT="best[width<=?640]"
elif [[ "${url}" =~ cbs.com ]]; then
FORMAT="rtmp-496-0"
fi
# Try and download twice (resume if partially downloaded)
for i in 1 2; do
${YOUTUBE_DL} ${ARGS} -f ${FORMAT} \
-o "${TMP_DIR}/${show}/%(upload_date)s/%(title)s-%(id)s.%(ext)s" \
--download-archive "${DOWNLOAD_DIR}/${show}.archive" \
${url}
# Wait 10 minutes before trying again
#sleep 600
cp -nr "${TMP_DIR}/${show}"/* "${DOWNLOAD_DIR}/${show}"/
done
# Move files from ${DOWNLOAD_DIR}/The Daily Show/NA/ to ${DOWNLOAD_DIR}/The Daily Show/$date
while IFS= read -r -d '' file; do
dir=$( stat -c %y "$file" | sed -e 's/\([0-9]*\)-\([0-9]*\)-\([0-9]*\).*/\1\2\3/' );
mkdir -p "${DOWNLOAD_DIR}/${show}/${dir}/"
# Keep a copy in /NA/ to avoid re-downloading
cp -n "$file" "${DOWNLOAD_DIR}/${show}/${dir}/"
done < <(find "${DOWNLOAD_DIR}/${show}/NA" -type f -print0)
# Remove old copies in /NA/, since we don't need to
# worry about re-downloading them any more
find "${DOWNLOAD_DIR}/${show}/NA/" -mindepth 1 -mtime +7 -delete
# Remove old shows
find "${DOWNLOAD_DIR}/${show}/" -mindepth 1 -mtime +${KEEP_DAYS} -delete
}
if [[ -n ${url} ]]; then
if [[ "${url}" =~ cc.com ]]; then
show="The Daily Show"
elif [[ "${url}" =~ cbs.com ]]; then
show="The Late Show"
fi
download_show "${url}" "${show}"
else
#for show in "The Daily Show" "The Late Show"; do
for show in "The Daily Show"; do
url=
if [[ "${show}" =~ Daily ]]; then
url=http://www.cc.com/shows/the-daily-show-with-trevor-noah/full-episodes/
elif [[ "${show}" =~ Late ]]; then
# Get URL from http://www.cbs.com/shows/the-late-show-with-stephen-colbert/video/
# Requires Unotelly DNS:
# 197.242.94.51
# 197.189.212.164
url=$( wget -qO- http://www.cbs.com/shows/the-late-show-with-stephen-colbert/video/ | grep -E "/video/[^/]+/" | grep '"url":' | perl -pe 's/.*?"url":"(.*?)".*/$1/ims' | head -1 )
fi
download_show "${url}" "${show}"
done
fi