Scripting an alert for when a YouTuber is live
A script to check if a YouTuber is live. When given a YouTuber’s homepage as an argument it’ll output live
, offline
or an error description.
Inspired by an alternative script here
Is a YouTuber live?
sudo touch /usr/local/bin/ytcheckiflive
sudo chmod +x /usr/local/bin/ytcheckiflive
sudo nano /usr/local/bin/ytcheckiflive
# Paste the following and save:
#!/usr/bin/env bash
# Usage: ytcheckiflive <YouTuber's homepage URL>
# Example: ytcheckiflive https://www.youtube.com/channel/UCfp-lNJy4QkIGnaEE6NtDSg
Err(){
printf 'ERROR: %s\n' "$2" 1>&2
[ $1 -gt 0 ] && exit $1
}
# Check for curl dependency
[ -z $( which curl ) ] && Err 1 "'curl' not found."
# Get YouTuber's homepage
Data=$( curl -s "$1" )
# This string should be present if the URL is correct
MatchForGood='{"runs":[{"text":'
# This string should be present if the YouTuber is live
MatchForLive='{"iconType":"LIVE"}'
while read Line; do
[ -z $IsGood ] && [[ $Line == *$MatchForGood* ]] && IsGood=1
[ -z $IsLive ] && [[ $Line == *$MatchForLive* ]] && IsLive=1
[ -n "$IsGood" ] && [ -n "$IsLive" ] && break;
done <<< "$Data"
[ -z "$IsGood" ] && Err 1 "Not a recognized channel"
if [ -n "$IsLive" ]; then
printf 'live'
else
printf 'offline'
fi
# See if they're live using their YouTube homepage:
ytcheckiflive https://www.youtube.com/channel/UCfp-lNJy4QkIGnaEE6NtDSg
Turning it into an alert
If the above script is called at 5 minute intervals with a list of YouTubers it can be used to produce an alert if one of them goes live.
Below is an example script intended to be added to a user’s crontab, it’ll use a temp file to enforce only one alert per “live” YouTuber session and uses notify-send
to let the user know.
mkdir -p ~/.local/bin/
touch ~/.local/bin/ytalertiflive
chmod u+x ~/.local/bin/ytalertiflive
nano ~/.local/bin/ytalertiflive
# Paste the following and save:
#!/usr/bin/env sh
Err(){
notify-send "ERROR: $2"
[ $1 -gt 0 ] && exit $1
}
# Check for ytcheckiflive dependency
[ -z $( which ytcheckiflive ) ] && Err 1 "'ytcheckiflive' not found."
AlertIfLive(){
Name=$1
# Get the YouTuber's live status from ytcheckiflive (script above)
Status=$( ytcheckiflive "$2" )
ExitCode=$?
[ $ExitCode -gt 0 ] && Err "$ExitCode" "$Status"
if [ "$Status" = 'offline' ]; then
# If applic. remove the live state so there's an alert when they're live next
[ -f "$HOME/.cache/ytislive_$Name" ] && rm "$HOME/.cache/ytislive_$Name"
elif [ "$Status" = 'live' ]; then
if [ ! -f "$HOME/.cache/ytislive_$Name" ]; then
# Set the live state so there's only one alert for this "live" session
touch "$HOME/.cache/ytislive_$Name"
# Alert the user
notify-send -t 0 "YouTuber $Name is LIVE"
fi
else
Err 1 "Incompatible status"
fi
}
# List of YouTubers to check
# Syntax: AlertIfLive <Name of YouTuber> <Homepage of YouTuber>
# Example:
AlertIfLive TerminalForLife https://www.youtube.com/channel/UCfp-lNJy4QkIGnaEE6NtDSg
AlertIfLive DLN https://www.youtube.com/c/DestinationLinuxNetwork
Running the script every 5 minutes with chron
crontab -e # (don't use sudo)
# Append the following and save to apply changes...
# Don't forget to change USER_HERE to the name of the user
# Check for live YouTubers every 5 minutes
*/5 * * * * PATH='/home/USER_HERE/.local/bin:/usr/local/bin:/usr/bin:/bin'; /home/USER_HERE/.local/bin/ytalertiflive