#!/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 get_token() { 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) access_token=$(echo "$token_response" | jq -r '.access_token') refresh_token=$(echo "$token_response" | jq -r '.refresh_token') echo "$access_token" > .access_token echo "$refresh_token" > .refresh_token } get_token while true; do # read stream tokens from file stream_tokens=$(cat stream_tokens.txt 2>/dev/null) if [ -z "$stream_tokens" ]; then echo "No stream tokens found. Retrieving streams from fishtank.live..." # get list of live streams stream_list=$(curl -s 'https://www.fishtank.live/api/live-streams' \ -H 'AuthToken: '$(cat .access_token)'' \ --compressed) # extract stream tokens from stream_list stream_tokens=$(echo "$stream_list" | jq -r '.liveStreams[].url') # save stream tokens to file echo "$stream_tokens" > stream_tokens.txt fi # array to store the PIDs of mpv processes declare -a pids while read -r stream_token; do # check if a process with the same PID is already running if [[ " ${pids[@]} " =~ " $$ " ]]; then echo "Already running PID: $$" else # start the stream and save the PID of the mpv process mpv --really-quiet --mute --profile=norm --no-terminal --stream-record=/home/formerwagie/Videos/fishtank/"$(date +%Y-%m-%d_%H-%M-%S)".mkv "https://customer-jwh6wms36w6479.fishtank.live/stream/'"$stream_token"'/'"$stream_token"'.m3u8?token='"$(cat .access_token)"'&sig='"$(cat .stream_token)"'' & pids+=($!) fi done <<< "$stream_tokens" # wait for all processes to finish wait # check if access token has expired if curl --silent -H "Authorization: Bearer $(cat .access_token)" https://www.googleapis.com/oauth2/v1/tokeninfo > /dev/null; then echo "Access token still valid." else echo "Access token expired. Refreshing token..." get_token fi # check if any stream tokens have expired while read -r stream_token; do if ! curl --silent 'https://customer-jwh6wms36w6479.fishtank.live/stream/'"$stream_token"'/'"$stream_token"'.m3u8?token='"$(cat .access_token)"'&sig='"$(cat .stream_token)"'' > /dev/null; then echo "Stream token expired. Reloading stream..." # remove expired stream token sed -i "/$stream_token/d" stream_tokens.txt # remove expired access and stream tokens rm .access_token .stream_token fi done <<< "$stream_tokens"