misc-scripts/twitch-notify

83 lines
2.3 KiB
Plaintext
Raw Normal View History

2023-05-04 23:08:08 -05:00
#!/bin/sh
set -euf
NL='
'
BN="${0##*/}"
# note: use setsid when calling this with exec
# e.g. if you make this a void user-service, do
# exec setsid twitch-notify <channel list>
errecho() { echo "$*" >&2 ; }
fail() { errecho "error: $BN: $*"; exit 1; }
2023-06-13 22:48:00 -05:00
innerclean() {
[ -f "${1:-}" ] && rm -f "$1"
[ -f "${IMG:-}" ] && rm -f "$IMG"
}
2023-05-04 23:08:08 -05:00
clean() {
trap 'exit' INT HUP TERM EXIT
2023-06-13 22:48:00 -05:00
[ -f "${HTML:-}" ] && rm -f "$HTML"
[ -f "${IMG:-}" ] && rm -f "$IMG"
2023-05-04 23:08:08 -05:00
[ -d "${DIR:-}" ] && rm -rf "$DIR"
kill 0
}
# with the sleep 0.5, 2 tries gives like 99.9% accuracy
check() {
2023-06-25 11:27:53 -05:00
tmphtml="$2"
2023-07-25 14:34:16 -05:00
max_tries=2
2023-06-25 11:27:53 -05:00
trap 'innerclean "$tmphtml"' INT HUP TERM EXIT
2023-09-21 09:04:51 -05:00
curl --connect-timeout 10 -s -o "$2" -L "https://twitch.tv/$1"
2023-06-21 17:08:12 -05:00
if [ -n "$(pup 'script[type="application/ld+json"]' < "$2")" ]; then
2023-05-04 23:08:08 -05:00
if [ ! -f "$DIR/$1" ]; then
touch "$DIR/$1"
2023-06-13 22:48:00 -05:00
IMG="$(mktemp -t "twitch-notify-img.XXXX")"
2023-09-21 09:04:51 -05:00
curl --connect-timeout 10 -s -o "$IMG" -L "$(pup 'meta[property="og:image"]' 'attr{content}' < "$2")"
2023-06-13 22:48:00 -05:00
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"
2023-05-04 23:08:08 -05:00
fi
2023-07-25 14:34:16 -05:00
elif [ "$i" -lt "$max_tries" ]; then
2023-05-04 23:08:08 -05:00
i=$((i + 1))
sleep 0.5
check "$1" "$2"
else
[ -f "$DIR/$1" ] && rm "$DIR/$1"
echo "$1 is most likely offline or doesn't exist."
fi
2023-06-25 11:27:53 -05:00
[ -f "$2" ] && rm -f "$2"
2023-05-04 23:08:08 -05:00
}
# janky way to do this but fuck it. easiest one that doesn't get race conditioned
# if a file with their name exists in DIR, they're online
2023-07-25 14:34:16 -05:00
DIR="${XDG_RUNTIME_DIR:-"${TMPDIR:-/tmp}"}/twitch-notify-dir-$USER"
2023-05-04 23:08:08 -05:00
while getopts :t:l OPT; do
case "$OPT" in
t) SLEEP_INTERVAL="$OPTARG" ;; # in seconds, default 180
2023-08-02 16:44:43 -05:00
l) [ -d "$DIR" ] && printf 'ONLINE CHANNELS:\n' && ls -w 1 "$DIR"; exit 0 ;;
2023-05-04 23:08:08 -05:00
*) fail "unknown option: -$OPTARG" ;;
esac
done
shift "$((OPTIND - 1))"
2023-07-25 14:34:16 -05:00
# this mkdir should prevent any "someone's trying to override this" type beats
mkdir -m 700 -- "$DIR" || fail "$DIR already exists! remove it if no daemon is running"
2023-05-04 23:08:08 -05:00
trap 'clean' INT HUP TERM EXIT
while
for name; do
i=0
2023-06-13 22:48:00 -05:00
HTML="$(mktemp -t "twitch-notify-html.XXXX")"
2023-05-04 23:08:08 -05:00
check "$name" "$HTML" &
done
do
2023-06-13 22:48:00 -05:00
sleep "${SLEEP_INTERVAL:-60}"
2023-05-04 23:08:08 -05:00
done &
wait