83 lines
1.7 KiB
Bash
Executable File
83 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
|
|
}
|
|
|
|
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"
|
|
|
|
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"
|
|
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
|
|
}
|
|
|
|
set_proxy() {
|
|
local proxy
|
|
proxy="$(yqf .proxy)"
|
|
if [[ -n "$proxy" ]]; then
|
|
export ALL_PROXY="$proxy"
|
|
fi
|
|
}
|
|
|
|
set_proxy
|
|
|
|
entries=".entries"
|
|
for id in $(yqf "$entries | .[] | path | .[-1]")
|
|
do
|
|
download "${entries}.$id" &
|
|
done
|
|
wait
|