Last active
September 3, 2024 19:49
-
-
Save savitojs/ca6fd566d7bd890ce8568311c284a79d to your computer and use it in GitHub Desktop.
Get Live Gurbani Channels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
GREEN='\033[0;32m' | |
RED='\033[0;31m' | |
YELLOW='\033[0;33m' | |
BLUE='\033[0;34m' | |
NC='\033[0m' # No Color | |
CHECK_SYMBOL='\u2713' | |
X_SYMBOL='\u2A2F' | |
# | |
# https://gitlab.com/-/snippets/1936196 | |
# | |
function execute_and_wait() { | |
eval $1 >/tmp/execute-and-wait.log 2>&1 & | |
pid=$! | |
delay=0.05 | |
frames=('\u280B' '\u2819' '\u2839' '\u2838' '\u283C' '\u2834' '\u2826' '\u2827' '\u2807' '\u280F') | |
echo "$pid" >"/tmp/.spinner.pid" | |
# Hide the cursor | |
tput civis | |
index=0 | |
framesCount=${#frames[@]} | |
while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do | |
printf "${YELLOW}${frames[$index]}${NC} ${GREEN}$2${NC}" | |
let index=index+1 | |
if [ "$index" -ge "$framesCount" ]; then | |
index=0 | |
fi | |
printf "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" | |
sleep $delay | |
done | |
# Wait for the command to finish and capture its exit status | |
wait $! | |
exitCode=$? | |
if [ "$exitCode" -eq "0" ]; then | |
printf "${CHECK_SYMBOL} ${2} \b\n" | |
else | |
printf "${X_SYMBOL} ${2} \b\n" | |
fi | |
# Restore the cursor | |
tput cnorm | |
return $exitCode | |
} | |
execute_and_wait "curl -s https://play.sikhnet.com/api/radio -o /tmp/radio_data.json" "Fetching Live Radio data from Sikhnet.com" | |
result=$? | |
if [ $result -ne 0 ]; then | |
echo "Failed to fetch data. Please check the log at /tmp/execute-and-wait.log." | |
exit 1 | |
else | |
echo "" | |
echo -e "${BLUE}Title\tChannel\tOnline/Offline\tLocation\tURL${NC}" | column -t -s $'\t' | |
fi | |
jq -r '.[] | select(.isLive == true) | "\(.title)\t\(.channel)\t\(.isOnline)\t\(.location)\t\(.resource)"' /tmp/radio_data.json | while IFS=$'\t' read -r title channel isOnline location url; do | |
if [ "$isOnline" = "true" ]; then | |
online_status="${GREEN}Online${NC}" | |
else | |
online_status="${RED}Offline${NC}" | |
fi | |
echo -e "$title\t$channel\t$online_status\t$location\t$url" | |
done | column -t -s $'\t' | |
rm /tmp/radio_data.json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Screencast.from.2024-08-31.23-40-32.mp4