2020-12-16 15:46:25 +01:00
|
|
|
#!/bin/zsh
|
2020-12-16 19:30:35 +01:00
|
|
|
# Download YouTube-feeds as if they are podcasts.
|
2020-12-16 15:46:25 +01:00
|
|
|
# Error codes:
|
|
|
|
# 1 = Could not read file or directory.
|
|
|
|
# 2 = Configuration option not set.
|
2020-12-16 18:13:16 +01:00
|
|
|
# 3 = youtube-dl error.
|
2020-12-16 15:46:25 +01:00
|
|
|
|
|
|
|
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."
|
2020-12-16 16:13:57 +01:00
|
|
|
[[ -z "${ytdl_format}" ]] && local ytdl_format="best"
|
2020-12-16 18:55:12 +01:00
|
|
|
[[ -z "${max_videos}" ]] && local max_videos="last"
|
2020-12-16 15:46:25 +01:00
|
|
|
|
2020-12-16 16:52:39 +01:00
|
|
|
mkdir -p "${download_dir}"
|
2020-12-16 21:21:20 +01:00
|
|
|
local files_before="$(find "${download_dir}" -type f)"
|
2020-12-16 15:46:25 +01:00
|
|
|
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" \
|
2020-12-16 16:52:27 +01:00
|
|
|
--output "${download_dir}/%(uploader)s/%(title)s.%(ext)s" \
|
2020-12-16 16:13:57 +01:00
|
|
|
--format "${ytdl_format}" \
|
2020-12-16 18:55:12 +01:00
|
|
|
--playlist-end "${max_videos}" \
|
2020-12-16 18:12:36 +01:00
|
|
|
${=ytdl_extra_args} \
|
2020-12-16 15:46:25 +01:00
|
|
|
"${feed}"
|
2020-12-16 18:56:08 +01:00
|
|
|
# [[ ${?} -eq 0 ]] || die 3 "youtube-dl returned an error."
|
2020-12-16 15:46:25 +01:00
|
|
|
done
|
2020-12-16 21:21:20 +01:00
|
|
|
local files_after="$(find "${download_dir}" -type f)"
|
2020-12-17 12:37:47 +01:00
|
|
|
local time="$(date +'%F_%R')"
|
2020-12-16 17:54:13 +01:00
|
|
|
while read file; do
|
2020-12-16 19:12:55 +01:00
|
|
|
# https://stackoverflow.com/a/8811800/5965450
|
|
|
|
if [[ "${files_before#*${file}}" == "${files_before}" ]]; then
|
2020-12-16 18:20:39 +01:00
|
|
|
echo "${file/${download_dir}\//}" \
|
2020-12-17 12:37:47 +01:00
|
|
|
>> "${download_dir}/New_${time}.m3u"
|
2020-12-16 17:54:13 +01:00
|
|
|
fi
|
|
|
|
done <<<"${files_after}"
|
2020-12-16 16:14:08 +01:00
|
|
|
|
|
|
|
find "${download_dir}" -type f -mtime +"${keep_for_days}" -delete
|
2020-12-16 15:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
main
|