1
0

store feeds as JSON

This commit is contained in:
dtb 2022-11-06 01:05:41 -04:00
parent be5c22dbb9
commit bfee05b744

View File

@ -4,6 +4,8 @@ set -e
argv0="$0" argv0="$0"
PIGPEN="$(pig_pen)"
id(){ id(){
youtube-dl --skip-download --print-json --playlist-items 1 "$1" \ youtube-dl --skip-download --print-json --playlist-items 1 "$1" \
| jq '.channel_id' \ | jq '.channel_id' \
@ -11,38 +13,75 @@ id(){
} }
feedurl(){ feedurl(){
printf "https://www.youtube.com/feeds/videos.xml?channel_id=%b\n" "$1" printf "http://www.youtube.com/feeds/videos.xml?channel_id=%b\n" "$1"
} }
list_channels(){ list_channels(){
for file in "$(pig_pen)"/* pscat \
do printf "%s\t%s\n" "$(xq '.feed.title' <"$file")" "$file" [ sh -c "for file in \"$PIGPEN\"/*.xml; \
done do xq '{(.feed.link[0].\"@href\"): .feed.title}' \"\$file\"; \
done 2>/dev/null" ] \
[ sh -c "for file in \"$PIGPEN\"/*.json; \
do jq '{(.feed.link[0].\"@href\"): .feed.title}' \"\$file\"; \
done 2>/dev/null" ] \
[ printf "\n" ] \
| sed \
-e '/{/d' \
-e '/}/d' \
-e 's/^ \"//g' \
-e 's/\": \"/ /g' \
-e 's/\,$//g' \
-e 's/\"$//g'
} }
list_videos(){ list_videos(){
list_channels \ file="$( \
| grep "$1" \ ls "$PIGPEN" \
| cut -f 2 \ | grep "$( \
| xargs xq \ list_channels \
| sed "/$(printf '%b' "$1")/q" \
| tail -n 1 \
| cut -f 1 \
| xargs pig_name
)" \
)"
case "$file" in
*json)
jq \
'reduce .feed.entry[] as $item ( 'reduce .feed.entry[] as $item (
{}; {};
. + { ($item.link."@href"): $item.title } . + { ($item.link."@href"): $item.title }
)' \ )' "$file"
| sed \ ;;
-e '1d' \ *xml)
-e '$d' \ xq \
-e 's/\": \"/ /g' \ 'reduce .feed.entry[] as $item (
-e 's/^ \"//g' \ {};
-e 's/\"\,$//g' # this is really hacky . + { ($item.link."@href"): $item.title }
)' "$file"
;;
*)
printf "No file found!\n" 1>&2
true
;;
esac | sed \
-e '1d' \
-e '$d' \
-e 's/\": \"/ /g' \
-e 's/^ \"//g' \
-e 's/\"\,$//g' # this is really hacky
} }
latin(){ latin(){
xq '.feed.pig' <"$1" case "$1" in
json) jq '.feed.pig' <"$1" ;;
xml) xq '.feed.pig' <"$1" ;;
esac
} }
stow(){ stow(){
pig_fetch "$1" | xq -x "$( name="$PIGPEN/$(pig_name "$1")"
pig_fetch "$1" | xq "$(
date '+%Y-%m-%dT%T' \ date '+%Y-%m-%dT%T' \
| xargs printf ' | xargs printf '
. * { feed: { pig: { . * { feed: { pig: {
@ -51,7 +90,8 @@ stow(){
stow_date: "%b", stow_date: "%b",
version: "0" version: "0"
} } }' "$1" \ } } }' "$1" \
)" >"$(pig_pen)/$(pig_name "$1")" )" >"$name.json"
rm -f "$name.xml"
} }
watch(){ watch(){
@ -82,7 +122,7 @@ case "$1" in
list_channels) list_channels)
! str isvalue "$2" \ ! str isvalue "$2" \
|| usage || usage
list_channels | sort list_channels
;; ;;
list_videos) # do something with youtube-dl? list_videos) # do something with youtube-dl?
str isvalue "$2" \ str isvalue "$2" \
@ -103,19 +143,21 @@ listen)
;; ;;
refresh) refresh)
if ! str isvalue "$2" if ! str isvalue "$2"
then for file in "$(pig_pen)"/* then for file in "$PIGPEN"/*
do latin "$file" \ do stow "$( \
| jq '.feed_url' \ latin "$file" \
| xargs stow & | jq -r '.feed_url' \
)"
done done
else else
while str isvalue "$2" while str isvalue "$2"
do list_channels \ do stow "$( \
| grep "$2" \ latin "$( \
| cut -f 2 \ list_channels \
| xargs latin \ | grep "$2" \
| jq '.feed_url' \ | cut -f 2 \
| xargs stow )" | jq '.feed_url' \
)"
shift shift
done done
fi fi
@ -124,7 +166,7 @@ subscribe)
str isvalue "$2" \ str isvalue "$2" \
|| usage || usage
while str isvalue "$2" while str isvalue "$2"
do stow "$(feedurl "$(id "$2")")" do stow "http://www.youtube.com/feeds/videos.xml?channel_id=$(id "$2")"
shift shift
done done
;; ;;