From dac055634c9307da33736996bc08ffef9e0a635f Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 16 Dec 2020 15:46:25 +0100 Subject: [PATCH] Got it working. --- download-yt-feeds.sh | 65 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 download-yt-feeds.sh diff --git a/download-yt-feeds.sh b/download-yt-feeds.sh new file mode 100755 index 0000000..03b5ac1 --- /dev/null +++ b/download-yt-feeds.sh @@ -0,0 +1,65 @@ +#!/bin/zsh +# Download YouTube-feeds like they are podcasts. +# Error codes: +# 1 = Could not read file or directory. +# 2 = Configuration option not set. + +function die() +{ + [[ -n "${2}" ]] && echo "Error: ${2}" >&2 + exit ${1} +} + +# Return path of configuration directory. +function get_config_path() +{ + local path + if [[ -n "${XDG_CONFIG_HOME}" ]]; then + path="${XDG_CONFIG_HOME}/download-yt-feeds" + else + path="${HOME}/.config/download-yt-feeds" + fi + + [[ -d "${path}" ]] || mkdir -p "${path}" + [[ -r "${path}" ]] || die 1 "${path} is not readable." + echo "${path}" +} + +# Return URLs of YouTube-feeds in feedlist. +# All lines that don't start with "http" are ignored. +function read_feedlist() +{ + local feed_path="$(get_config_path)/feedlist" + [[ -r "${feed_path}" ]] || die 1 "${feed_path} is not readable." + + local -a entries + while read line; do + if [[ "${line}" =~ "^http" ]]; then + local -a entry=("${(@s/ /)line}") + entries+=(${entry[1]}) + fi + done <"${feed_path}" + + print -r -- ${(qq)entries} +} + +function main() +{ + local config_path="$(get_config_path)/config" + [[ -r "${config_path}" ]] || die 1 "${config_path} is not readable." + + source "${config_path}" + [[ -z "${download_dir}" ]] && die 2 "download_dir not specified." + [[ -z "${keep_for_days}" ]] && die 2 "keep_for_days not specified." + + local -a feedlist=("${(@Q)${(z)$(read_feedlist)}}") + for feed in ${feedlist}; do + youtube-dl \ + --download-archive "$(get_config_path)/downloaded" \ + --dateafter "now-${keep_for_days}days" \ + --output "${download_dir}/%(title)s.%(ext)s" \ + "${feed}" + done +} + +main