pdownyq/pdown.sh

102 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/bash
#Dependencies:
# yq https://github.com/mikefarah/yq
# curl
YQ=yq
CONFIG=${1:-./config.yaml}
CACHE_DIR=./cache
STATE_DIR=./state
PODCASTS_DIR=./podcasts
yqf() {
$YQ "$1" "$CONFIG"
}
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_DIR/${name}.rss"
local downloads_path="$STATE_DIR/${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"
pushd "$CACHE_DIR" || return
read -ra title_options < <(make_fname "$ftype" "$name" "$title")
fname=$(curl -s -w "%{filename_effective}" -f -L "${title_options[@]}" "$mp3url")
popd || return
mv "$CACHE_DIR/$fname" "$PODCASTS_DIR"
echo "$mp3url" >> "$downloads_path"
done
}
set_proxy() {
local proxy
proxy="$(yqf .proxy)"
if [[ -n "$proxy" ]]; then
export ALL_PROXY="$proxy"
fi
}
set_proxy
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"
entries=".entries"
for id in $(yqf "$entries | .[] | path | .[-1]")
do
download "${entries}.$id" &
done
wait