pdownyq/pdown.sh

81 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/bash
#Dependencies:
# yq
# curl
mkdir -p ./{cache,state,Podcasts}
YQ=yq
yqf() {
$YQ "$1" config.yaml
}
dryrun() {
false
}
dryrun || rm -rf ./cache/*
make_fname() {
local name ftype title
ftype=$1; name=$2; title=$3
case $ftype in
"date")
echo "-o $name-$(date --iso-8601).mp3"
;;
"default")
echo "-O"
;;
"title")
sanitized=$(echo "$title" | sed 's/ */-/g' | tr -d -c 'A-Za-z-0-9\n' | grep -o '[^-].*' | cut -c-20)
echo "-o $name-${sanitized}.mp3"
;;
esac
}
download() {
local e name url limit mp3url fname ftype title
e=$1
name=$(yqf "${e}.name");
url=$(yqf "${e}.url")
limit=$(yqf "${e}.limit // 1")
filter=$(yqf "${e}.filter // true")
ftype=$(yqf "${e}.filename_type // \"default\"")
local rss_path="cache/${name}.rss"
local downloads_path="state/${name}.downloads"
dryrun || curl -s -L -o "$rss_path" "$url"
items=".rss.channel.item"
ids=$($YQ -p xml "$items | .[] | select($filter) | path | .[-1]" "$rss_path" |
head -n "$limit")
for id in $ids
do
title=$($YQ -p xml "$items.$id | (.title |= .[0]) | .title" "$rss_path")
mp3url=$($YQ -p xml "$items.$id | .enclosure.+url" "$rss_path")
grep -qF "$mp3url" "$downloads_path" && continue
echo "Downloading: $title - $mp3url"
# mp3path=$(mktemp -p cache "${name}.XXXXXX.mp3")
dryrun && continue
cd cache || return
read -ra title_options < <(make_fname "$ftype" "$name" "$title")
fname=$(curl -s -w "%{filename_effective}" -f -L "${title_options[@]}" "$mp3url")
cd ..
mv {cache,Podcasts}/"$fname"
echo "$mp3url" >> "$downloads_path"
done
}
entries=".entries"
for id in $(yqf "$entries | .[] | path | .[-1]")
do
download "${entries}.$id" &
done
wait