twitch-notify: good stuff

This commit is contained in:
yosh 2023-06-13 22:48:00 -05:00
parent 1570b01983
commit 2ecadef2a9
1 changed files with 19 additions and 7 deletions

View File

@ -11,8 +11,14 @@ BN="${0##*/}"
errecho() { echo "$*" >&2 ; }
fail() { errecho "error: $BN: $*"; exit 1; }
innerclean() {
[ -f "${1:-}" ] && rm -f "$1"
[ -f "${IMG:-}" ] && rm -f "$IMG"
}
clean() {
trap 'exit' INT HUP TERM EXIT
[ -f "${HTML:-}" ] && rm -f "$HTML"
[ -f "${IMG:-}" ] && rm -f "$IMG"
[ -d "${DIR:-}" ] && rm -rf "$DIR"
kill 0
}
@ -20,14 +26,21 @@ clean() {
# with the sleep 0.5, 2 tries gives like 99.9% accuracy
MAX_TRIES=2
check() {
trap 'innerclean '"$2" INT HUP TERM
curl -s -o "$2" -L "https://twitch.tv/$1"
if [ "$(pup 'script[type="application/ld+json"]' < "$2")" ]; then
if [ ! -f "$DIR/$1" ]; then
touch "$DIR/$1"
IMG="$(mktemp)"
IMG="$(mktemp -t "twitch-notify-img.XXXX")"
curl -s -o "$IMG" -L "$(pup 'meta[property="og:image"]' 'attr{content}' < "$2")"
CODE="$(notify-send -A "https://twitch.tv/$1" -t 10000 -u normal -i "$IMG" "YO! DICKHEAD!$NL$1 is online!")"
[ "${CODE:-999}" -eq 0 ] && xdg-open "https://twitch.tv/$1"
TITLE="$(pup 'script[type="application/ld+json"] text{}' < "$2" | jq -r '.[0].description')"
CODE="$(notify-send -A "watch" -A "copy" -t 10000 -u normal -i "$IMG" "$1 is online!$NL$NL\"$TITLE\"")"
case "${CODE:-999}" in
0) xdg-open "https://twitch.tv/$1" ;;
1) printf '%s' "https://twitch.tv/$1" | xclip -sel clipboard ;;
*) : ;;
esac
rm -f "$IMG"
fi
elif [ "$i" -lt "$MAX_TRIES" ]; then
i=$((i + 1))
@ -37,8 +50,7 @@ check() {
[ -f "$DIR/$1" ] && rm "$DIR/$1"
echo "$1 is most likely offline or doesn't exist."
fi
[ -f "${HTML:-}" ] && rm -f "$HTML"
[ -f "${IMG:-}" ] && rm -f "$IMG"
[ -f "${2:-}" ] && rm -f "$2"
}
# janky way to do this but fuck it. easiest one that doesn't get race conditioned
@ -59,10 +71,10 @@ trap 'clean' INT HUP TERM EXIT
while
for name; do
i=0
HTML="$(mktemp)"
HTML="$(mktemp -t "twitch-notify-html.XXXX")"
check "$name" "$HTML" &
done
do
sleep "${SLEEP_INTERVAL:-180}"
sleep "${SLEEP_INTERVAL:-60}"
done &
wait