dot_testing/scripts/yt

104 lines
2.0 KiB
Plaintext
Raw Normal View History

#!/bin/sh
2021-02-15 12:02:09 -06:00
[ -z "${YT_DATFILE}" ] && YT_DATFILE="${HOME}/.local/share/ytdat"
[ -z "${YT_CACHEDIR}" ] && YT_CACHEDIR="${HOME}/.local/share/ytcache"
2021-04-26 10:59:12 -05:00
[ -z "${YT_TORIFY}" ] && YT_TORIFY=""
2021-02-15 12:02:09 -06:00
ver=0.1
2021-02-15 12:02:09 -06:00
info () {
printf %s "\
yt - youtube tool
=> [s]ync - Sync RSS feeds
=> [a]dd [id] - Add a channel to the list
=> [d]el [id] - Remove a channel from the list
=> [h]elp - Show this help
2021-03-06 21:40:23 -06:00
=> [i]d [url] - Show internal channel id
2021-04-26 10:59:12 -05:00
=> [c]lear - Clear cache
2021-02-15 12:02:09 -06:00
2021-03-06 21:40:23 -06:00
List file: export YT_DATFILE=<your file here>
Cache directory: export YT_CACHEDIR=<your dir here>
Torify wrapper: export YT_TORIFY=<your torify here>
2021-02-15 12:02:09 -06:00
"
}
2021-03-06 21:40:23 -06:00
cache () {
touch ${YT_DATFILE}
mkdir -p ${YT_CACHEDIR}
}
err () {
printf "err: %s\n" ${1}
2021-05-11 20:54:47 -05:00
[ -z "${2}" ] && exit 1
2021-03-06 21:40:23 -06:00
exit ${2}
}
sync () {
2021-03-06 21:40:23 -06:00
cache
for i in $(cat $YT_DATFILE | tr '\n' ' '); do
2021-03-06 21:40:23 -06:00
${YT_TORIFY} curl -s \
https://www.youtube.com/feeds/videos.xml?channel_id=$i\
> ${YT_CACHEDIR}/$i
done
}
2021-03-06 21:40:23 -06:00
id () {
${YT_TORIFY} curl "${1}" -s | \
grep 'youtube/www\.youtube\.com/channel/.\{24\}' -o | \
awk -F'/' '{print $NF}' | \
sed 1q
}
display () {
2021-03-06 21:40:23 -06:00
cache
tmp1=$(mktemp)
tmp2=$(mktemp)
for i in $(ls $YT_CACHEDIR | tr '\n' ' '); do
grep \<media:title\> ${YT_CACHEDIR}/$i | cut -c 17- | \
rev | cut -c 15- | rev >> $tmp1
grep 'link rel' ${YT_CACHEDIR}/$i | grep 'watch' | \
cut -c31- | rev | cut -c4- | rev >> $tmp2
done
cat $tmp1 $tmp2 | pr -2t -s" | "
}
2020-12-20 11:50:47 -06:00
add () { # $1: name of channel id
2021-03-06 21:40:23 -06:00
cache
printf "%s\n" $1 >> ${YT_DATFILE}
2020-12-20 11:50:47 -06:00
}
del () { # $1: line number of channel id
2021-03-06 21:40:23 -06:00
cache
sed -i "${1}d" ${YT_DATFILE}
2020-12-20 11:50:47 -06:00
}
2021-04-26 10:59:12 -05:00
cclear () {
rm -r ${YT_CACHEDIR}
}
case $1 in
2021-03-06 21:40:23 -06:00
"s"*)
sync
2021-03-06 21:40:23 -06:00
exit 0
;;
2021-03-06 21:40:23 -06:00
"a"*)
[ $# -eq 2 ] && add $2 || \
err "two args required"
exit 0
;;
2021-03-06 21:40:23 -06:00
"d"*)
[ $# -eq 2 ] && del $2 || \
err "two args required"
exit 0
;;
2021-03-06 21:40:23 -06:00
"h"*)
info
2021-03-06 21:40:23 -06:00
exit 0
;;
"i"*)
[ $# -eq 2 ] && id $2 || \
err "two args required"
exit 0
;;
2021-04-26 10:59:12 -05:00
"c"*)
cclear
exit 0
;;
*)
display
2021-03-06 21:40:23 -06:00
exit 0
;;
esac
exit 0