#!/bin/bash # REQUIREMENTS # curl - sudo apt install curl # jq - sudo apt install jq # mpv - sudo apt install mpv # INSTRUCTIONS # FILL IN refresh_token (only once) # How to get it; # https://files.catbox.moe/e3bg5m.png refresh_token="APZUo0TVNv21VWf6ogcIIu3S43wXVJl8d-kA3f6ERL0Jf9WbiVKTJPRJnUkw8U5G5wnon-7Xp6fB12Nf4Di8j6r9zx6iROUH88RjtACd__-FL1CmPKY9CxOi70ReUzP-WMCTXDzmz2o9WgZLzhFJuygIgutimc9tmliOvZZ515vqxaN6u51gFQoFvwqFj58mGLcOXgTGO9CDHrIQxFWs-UXQ-tUCTpavVg" if [ -z "$refresh_token" ] then echo "Add refresh_token" exit fi # use the new refresh_token instead of above if [ -f .refresh_token ]; then refresh_token=$(cat .refresh_token) fi # get your access and refresh tokens token_response=$(curl -s 'https://securetoken.googleapis.com/v1/token?key=AIzaSyBOOpV21k6o3cJc56-4uRNb0jDMzIxShMY' \ -X POST --data-raw 'grant_type=refresh_token&refresh_token='$refresh_token'' --compressed) echo "$token_response" | jq -r '.access_token' > .access_token echo "$token_response" | jq -r '.refresh_token' > .refresh_token while true; do get_feeds=$(curl -s 'https://www.fishtank.live/api/live-streams' \ -H 'AuthToken: '$(cat .access_token)'' \ --compressed | jq -r '.liveStreams | .[] | "\(.name)|\(.url)|\(.expiryTime)"') # array to store the PIDs of mpv processes and the corresponding stream tokens and expiry times declare -A streams while read -r line; do stream_name=$(echo "$line" | cut -d '|' -f1) stream_token=$(echo "$line" | cut -d '|' -f2) expiry_time=$(echo "$line" | cut -d '|' -f3) # check if the stream token has expired if [ "$expiry_time" -lt "$(date +%s)" ]; then echo "Stream '$stream_name' has expired, reloading..." # overwrite the .stream_token and .access_token files echo "$token_response" | jq -r '.access_token' > .access_token echo "$stream_token" > .stream_token # start the stream and save the PID of the mpv process along with the stream token and expiry time mpv --really-quiet --mute --profile=norm --no-terminal --stream-record=/home/formerwagie/Videos/fishtank/"${stream_name}_$(date +%Y-%m-%d_%H-%M-%S)".mkv "https://customer-jwh6wms36w6479b4.cloudflarestream.com/$ mpv_pid=$! streams["$mpv_pid"]=$stream_token streams["${mpv_pid}_expiry"]=$expiry_time fi done <<< "$get_feeds" # iterate through the mpv processes and check if they're still running for pid in "${!streams[@]}"; do if ! kill -0 "$pid" > /dev/null 2>&1; then echo "Stream with PID $pid has ended, removing..." unset streams["$pid"] unset streams["${pid}_expiry"] fi done sleep 10