Made a bash script that checks if a streamer is online, any advice?

So, this time I was able to pull this off with by asking chatGPT a couple questions but other than that I was able to do this all on my own! It also took me forever to find a website that sent the online/offline data as part of the page, I also reused code from my ISO downloader, if anyone would like I would be happy to share it (I think I may have also asked for help with it at one point).

#!/bin/bash

wget "https://twitchtracker.com/$1" -O 'streamer.html'

if grep -q 'LIVE</span>' 'streamer.html'; then
  if ! grep -q "$(date '+%j')" 'stream_state.txt'; then
    if [ "$(notify-send "$1 is online!" "https://www.twitch.tv/$1/" -u CRITICAL -a "$1-detector" -A 'Open Stream' -A 'Nope')" -eq '0' ]; then
      xdg-open "https://www.twitch.tv/$1/"
      rm 'stream_state.txt'
      echo "$(date '+%j')" >> 'stream_state.txt'
    fi
  fi
  rm 'streamer.html'
else
  echo 'No stream, exiting.'
fi

@40476 use ollama locally? Anyway, I have ollama/open-webui running in kubernetes on a local machine and it came up with;

#!/bin/bash

# Replace this variable with your Twitch stream key
STREAM_KEY="your_twitch_stream_key"

# Function to check if the Twitch stream is online
check_twitch_status() {
  local status_code
  local json_output

  # Make an HTTP request to get the current status of the stream
  response=$(curl -s "https://api.twitch.tv/helix/streams?key=${TWITCH_API_KEY}&id=$STREAM_KEY")

  if [ $? -ne 0 ]; then
    echo "Error: Failed to contact Twitch API." >&2
    exit 1
  fi

  # Parse the JSON output and get the 'status' property
  json_output=$(echo "${response}" | jq -r '.data[].status')

  # Output whether the stream is online or offline
  if [[ "${json_output}" == "live" ]]; then
    echo "Stream is currently online."
  else
    echo "Stream is currently offline."
  fi
}

# Set your Twitch API key here
TWITCH_API_KEY="your_twitch_api_key"

# Call the function to check the status of the stream
check_twitch_status

You need to have jq installed, not tested as don’t use twitch…

Well, I dont have the the hardware or a twitch account/API key so I had to rely on something already available, I do on changing wget to curl as I believe it is built into most Linux distributions, that would also remove the streamer.html file but then it gets deleted anyway.

I made this for my brother who I set up a Linux system for various uses (only uses it for web browsing (skibidi toilet and discord meme compilations - both of which make no sense to me)).

My plan is to add a crontab entry for every 5 minutes on the channel ‘bushwack18’ since he find this guy funny apparently. And, if I may include, my finely tuned tastes are intelligently biased towards more interesting streamers, particularly [nope] and [nope]

And here is an updated version, still have not changed wget to curl but i did fix a bug and make the program a little less annyoing as well a a check to prevent two programs from checking the same stream at the same time

#!/bin/bash
sloc="$(dirname "$(readlink -f $0)")"
if ! grep -q "busy" "$sloc/$1prog_state.txt"; then
  echo "busy" >> "$sloc/$1prog_state.txt"
  wget "https://twitchtracker.com/$1" -O "$sloc/streamer.html"
  if grep -q 'LIVE</span>' "$sloc/streamer.html"; then
    if ! grep -q "$(date '+%j')" "$sloc/$1stream_state.txt"; then
      if [ "$(notify-send "$1 is online!" "https://www.twitch.tv/$1/" -u CRITICAL -a "$1-detector" -A 'Open Stream' -A 'Nope')" -eq '0' ]; then
        xdg-open "https://www.twitch.tv/$sloc/$1/"
      fi
      rm "$sloc/$1stream_state.txt"
      echo "$(date '+%j')" >> "$sloc/$1stream_state.txt"
      fi
  else
    echo 'No stream, exiting.'
  fi
  rm "$sloc/streamer.html"
fi
rm "$sloc/$1prog_state.txt"

fixed a bug where twitch would open with the path to the script (me is genius, paste things everywhere)

#!/bin/bash
sloc="$(dirname "$(readlink -f $0)")"
if ! grep -q "busy" "$sloc/$1prog_state.txt"; then
  echo "busy" >> "$sloc/$1prog_state.txt"
  wget "https://twitchtracker.com/$1" -O "$sloc/streamer.html"
  if grep -q 'LIVE</span>' "$sloc/streamer.html"; then
    if ! grep -q "$(date '+%j')" "$sloc/$1stream_state.txt"; then
      if [ "$(notify-send "$1 is online!" "https://www.twitch.tv/$1/" -u CRITICAL -a "$1-detector" -A 'Open Stream' -A 'Nope')" -eq '0' ]; then
        xdg-open "https://www.twitch.tv/$1/"
      fi
      rm "$sloc/$1stream_state.txt"
      echo "$(date '+%j')" >> "$sloc/$1stream_state.txt"
      fi
  else
    echo 'No stream, exiting.'
  fi
  rm "$sloc/streamer.html"
fi
rm "$sloc/$1prog_state.txt"

more concurrent execution bugs fixed (trying to make it work with cron)

#!/bin/bash
sloc="$(dirname \"$(readlink -f $0)\")"
if ! grep -q "busy" "$sloc/$1prog_state.txt"; then
  echo "busy" >> "$sloc/$1prog_state.txt"
  wget "https://twitchtracker.com/$1" -O "$sloc/$1streamer.html"
  if grep -q 'LIVE</span>' "$sloc/$1streamer.html"; then
    if ! grep -q "$(date '+%j')" "$sloc/$1stream_state.txt"; then
      if [ "$(notify-send "$1 is online!" "https://www.twitch.tv/$1/" -u CRITICAL -a "$1-detector" -A 'Open Stream' -A 'Nope')" -eq '0' ]; then
        xdg-open "https://www.twitch.tv/$1/"
      fi
      rm "$sloc/$1stream_state.txt"
      echo "$(date '+%j')" >> "$sloc/$1stream_state.txt"
      fi
  else
    echo 'No stream, exiting.'
  fi
  rm "$sloc/$1streamer.html"
fi
rm "$sloc/$1prog_state.txt"

the backslashes in the beginning broke something somehow

#!/bin/bash
sloc="$(dirname "$(readlink -f $0)")"
if ! grep -q "busy" "$sloc/$1prog_state.txt"; then
  echo "busy" >> "$sloc/$1prog_state.txt"
  wget "https://twitchtracker.com/$1" -O "$sloc/$1streamer.html"
  if grep -q 'LIVE</span>' "$sloc/$1streamer.html"; then
    if ! grep -q "$(date '+%j')" "$sloc/$1stream_state.txt"; then
      if [ "$(notify-send "$1 is online!" "https://www.twitch.tv/$1/" -u CRITICAL -a "$1-detector" -A 'Open Stream' -A 'Nope')" -eq '0' ]; then
        xdg-open "https://www.twitch.tv/$1/"
      fi
      rm "$sloc/$1stream_state.txt"
      echo "$(date '+%j')" >> "$sloc/$1stream_state.txt"
      fi
  else
    echo 'No stream, exiting.'
  fi
  rm "$sloc/$1streamer.html"
fi
rm "$sloc/$1prog_state.txt"