dot_testing/sh/yt

104 lines
2.0 KiB
Bash
Executable File

#!/bin/sh
[ -z "${YT_DATFILE}" ] && YT_DATFILE="${HOME}/.local/share/ytdat"
[ -z "${YT_CACHEDIR}" ] && YT_CACHEDIR="${HOME}/.local/share/ytcache"
[ -z "${YT_TORIFY}" ] && YT_TORIFY=""
ver=0.1
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
=> [i]d [url] - Show internal channel id
=> [c]lear - Clear cache
List file: export YT_DATFILE=<your file here>
Cache directory: export YT_CACHEDIR=<your dir here>
Torify wrapper: export YT_TORIFY=<your torify here>
"
}
cache () {
touch ${YT_DATFILE}
mkdir -p ${YT_CACHEDIR}
}
err () {
printf "err: %s\n" ${1}
[ -z "${2}" ] && exit 1
exit ${2}
}
sync () {
cache
for i in $(cat $YT_DATFILE | tr '\n' ' '); do
${YT_TORIFY} curl -s \
https://www.youtube.com/feeds/videos.xml?channel_id=$i\
> ${YT_CACHEDIR}/$i
done
}
id () {
${YT_TORIFY} curl "${1}" -s | \
grep 'youtube/www\.youtube\.com/channel/.\{24\}' -o | \
awk -F'/' '{print $NF}' | \
sed 1q
}
display () {
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" | "
}
add () { # $1: name of channel id
cache
printf "%s\n" $1 >> ${YT_DATFILE}
}
del () { # $1: line number of channel id
cache
sed -i "${1}d" ${YT_DATFILE}
}
cclear () {
rm -r ${YT_CACHEDIR}
}
case $1 in
"s"*)
sync
exit 0
;;
"a"*)
[ $# -eq 2 ] && add $2 || \
err "two args required"
exit 0
;;
"d"*)
[ $# -eq 2 ] && del $2 || \
err "two args required"
exit 0
;;
"h"*)
info
exit 0
;;
"i"*)
[ $# -eq 2 ] && id $2 || \
err "two args required"
exit 0
;;
"c"*)
cclear
exit 0
;;
*)
display
exit 0
;;
esac
exit 0