pdownyq/pdown.sh

102 lines
2.3 KiB
Bash
Raw Normal View History

2022-08-03 17:22:30 -04:00
#!/usr/bin/bash
#Dependencies:
2023-02-14 23:19:52 -05:00
# yq https://github.com/mikefarah/yq
2022-08-03 17:22:30 -04:00
# curl
2022-08-16 23:07:56 -04:00
YQ=yq
2022-08-04 17:52:12 -04:00
2023-02-14 23:19:52 -05:00
CONFIG=${1:-./config.yaml}
CACHE_DIR=./cache
STATE_DIR=./state
PODCASTS_DIR=./podcasts
2022-08-04 17:52:12 -04:00
yqf() {
2023-02-14 23:19:52 -05:00
$YQ "$1" "$CONFIG"
2022-08-03 17:22:30 -04:00
}
2022-08-08 13:23:49 -04:00
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
}
2022-08-04 17:52:12 -04:00
download() {
2022-08-08 13:23:49 -04:00
local e name url limit mp3url fname ftype title
2022-08-04 17:52:12 -04:00
e=$1
2022-08-08 13:23:49 -04:00
name=$(yqf "${e}.name");
2022-08-04 17:52:12 -04:00
url=$(yqf "${e}.url")
2022-08-08 13:23:49 -04:00
limit=$(yqf "${e}.limit // 1")
2022-08-04 17:52:12 -04:00
filter=$(yqf "${e}.filter // true")
2022-08-08 13:23:49 -04:00
ftype=$(yqf "${e}.filename_type // \"default\"")
2022-08-04 17:52:12 -04:00
2023-02-14 23:19:52 -05:00
local rss_path="$CACHE_DIR/${name}.rss"
local downloads_path="$STATE_DIR/${name}.downloads"
2022-08-04 17:52:12 -04:00
2023-01-09 20:46:08 -05:00
curl -s -L -o "$rss_path" "$url"
2022-08-04 17:52:12 -04:00
items=".rss.channel.item"
2022-08-03 17:22:30 -04:00
2022-08-04 17:52:12 -04:00
ids=$($YQ -p xml "$items | .[] | select($filter) | path | .[-1]" "$rss_path" |
head -n "$limit")
for id in $ids
do
2022-08-08 13:23:49 -04:00
title=$($YQ -p xml "$items.$id | (.title |= .[0]) | .title" "$rss_path")
2023-01-09 20:46:08 -05:00
mp3url=$($YQ -p xml "$items.$id | .enclosure.+@url" "$rss_path")
2022-08-08 13:23:49 -04:00
grep -qF "$mp3url" "$downloads_path" && continue
echo "Downloading: $title - $mp3url"
2023-02-14 23:19:52 -05:00
pushd "$CACHE_DIR" || return
2022-12-24 16:31:29 -05:00
read -ra title_options < <(make_fname "$ftype" "$name" "$title")
fname=$(curl -s -w "%{filename_effective}" -f -L "${title_options[@]}" "$mp3url")
2023-02-14 23:19:52 -05:00
popd || return
2022-08-08 13:23:49 -04:00
2023-02-14 23:19:52 -05:00
mv "$CACHE_DIR/$fname" "$PODCASTS_DIR"
2022-08-04 17:52:12 -04:00
echo "$mp3url" >> "$downloads_path"
done
}
2023-02-14 18:10:04 -05:00
set_proxy() {
local proxy
proxy="$(yqf .proxy)"
if [[ -n "$proxy" ]]; then
export ALL_PROXY="$proxy"
fi
}
set_proxy
2023-02-14 23:19:52 -05:00
set_directories() {
local base_dir cache_dir state_dir podcasts_dir
base_dir="$(yqf '.base_dir // ""')"
cache_dir="$(yqf '.cache_dir // ""')"
state_dir="$(yqf '.state_dir // ""')"
podcasts_dir="$(yqf '.podcasts_dir // ""')"
base_dir=${base_dir:-.}
CACHE_DIR=${cache_dir:-$base_dir/cache}
STATE_DIR=${state_dir:-$base_dir/state}
PODCASTS_DIR=${podcasts_dir:-$base_dir/podcasts}
}
set_directories
mkdir -p "$CACHE_DIR" "$STATE_DIR" "$PODCASTS_DIR"
2022-08-04 17:52:12 -04:00
entries=".entries"
for id in $(yqf "$entries | .[] | path | .[-1]")
2022-08-03 17:22:30 -04:00
do
2022-08-08 13:23:49 -04:00
download "${entries}.$id" &
2022-08-03 17:22:30 -04:00
done
2022-08-08 13:23:49 -04:00
wait