#/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} <-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}" # 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" elif [[ "${url}" =~ nbc.com ]]; then FORMAT="best[width<=?640]" fi if [ -f "${DOWNLOAD_DIR}/${show}.archive" ]; then cp "${DOWNLOAD_DIR}/${show}.archive" "${TMP_DIR}/${show}.archive" 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 "${TMP_DIR}/${show}.archive" \ ${url} # Wait 10 minutes before trying again #sleep 600 if [ -d "${TMP_DIR}/${show}/" ] && [ -n "$( ls -A "${TMP_DIR}/${show}/" )" ]; then mkdir -p "${DOWNLOAD_DIR}/${show}" cp -nr "${TMP_DIR}/${show}"/* "${DOWNLOAD_DIR}/${show}"/ cp "${TMP_DIR}/${show}.archive" "${DOWNLOAD_DIR}/${show}.archive" fi done # Move files from ${DOWNLOAD_DIR}/The Daily Show/NA/ to ${DOWNLOAD_DIR}/The Daily Show/$date if [ -d "${DOWNLOAD_DIR}/${show}"/NA ]; then 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 fi # Remove old shows if [ -d "${DOWNLOAD_DIR}/${show}" ]; then find "${DOWNLOAD_DIR}/${show}/" -mindepth 1 -mtime +${KEEP_DAYS} -delete fi } if [[ -n ${url} ]]; then if [[ "${url}" =~ cc.com ]]; then show="The Daily Show" elif [[ "${url}" =~ cbs.com ]]; then show="The Late Show" elif [[ "${url}" =~ nbc.com ]]; then show="Meet the Press" fi download_show "${url}" "${show}" else #for show in "The Daily Show" "The Late Show" "Meet the Press"; 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 Unlocator/Unotelly DNS #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 ) url=$( wget -qO- http://www.cbs.com/shows/the-late-show-with-stephen-colbert/video/ | grep -Po '"url":.*?[^\\]",' | grep http | perl -pe 's/.*?"url":"(.*?)".*/$1\n/ims' | head -1 ) elif [[ "${show}" =~ Meet ]]; then # Get URL from https://www.nbc.com/meet-the-press/episodes # Note that youtube-dl has a bug on https connections: https://github.com/rg3/youtube-dl/issues/13651 url=$( wget -qO- https://www.nbc.com/meet-the-press/episodes | grep -Po '"permalink":.*?[^\\]",' | perl -pe 's/.*?"permalink":"(.*?)".*/$1\n/ims; s/https/http/g' | head -1 ) fi download_show "${url}" "${show}" done fi