Skip to content

Instantly share code, notes, and snippets.

@NullNoname
Last active May 11, 2017 11:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NullNoname/5886332a31b2b406e2da to your computer and use it in GitHub Desktop.
Save NullNoname/5886332a31b2b406e2da to your computer and use it in GitHub Desktop.
#!/bin/bash
# LinuxBean Radio Tuner (Unofficial English Translation)
# Depends on: yad (for GUI), jq (for parsing SHOUTcast output), nkf (for converting SHOUTcast output's charset)
# xsel (Optional, for copying info to the clipboard), streamripper (Optional, for recording the stream)
# Original code:
# https://bitbucket.org/aztake/triforium/src/79c136729aeba19b9306ae82c9cbd712324c30cf/usr+local+bin/bean_radio?at=master
#====================================================================================================
# Double-click the list to copy the info
#====================================================================================================
if [ "$1" == "--dclk" ] ; then
info="[$3] $2"
echo "${info}" | xsel -i -b &
exit 0
fi
#====================================================================================================
# Preparation
#====================================================================================================
BR="
"
# Set cache folder
cachedir="${HOME}/.cache/bean_radio"
if [ ! -d "${cachedir}" ] ; then
mkdir -p "${cachedir}"
fi
if [ ! -f "${cachedir}/cookie.txt" ] ; then
cd "${cachedir}"
rm -f *
cd - >/dev/null
fi
# Detect the default app for playing the radio station
if [ $(pgrep pcmanfm | wc -l) -gt 0 ] ; then
desktop="$(cat ${HOME}/.local/share/applications/mimeapps.list | grep -m 1 audio/mpeg | cut -d= -f2 | cut -d\; -f1)"
elif which xdg-mime > /dev/null 2>&1 ; then
desktop="$(xdg-mime query default audio/mpeg)"
else
desktop=""
fi
if [ -f "${HOME}/.local/share/applications/${desktop}" ] ; then
cmd=$(cat "${HOME}/.local/share/applications/${desktop}" | \
grep "^Exec=" | cut -d= -f2 | sed -e "s|%\S\+||igm" -e "s|^\s*||igm" -e "s|\s*$||igm")
elif [ -f "/usr/local/share/applications/${desktop}" ] ; then
cmd=$(cat "/usr/local/share/applications/${desktop}" | \
grep "^Exec=" | cut -d= -f2 | sed -e "s|%\S\+||igm" -e "s|^\s*||igm" -e "s|\s*$||igm")
elif [ -f "/usr/share/applications/${desktop}" ] ; then
cmd=$(cat "/usr/share/applications/${desktop}" | \
grep "^Exec=" | cut -d= -f2 | sed -e "s|%\S\+||igm" -e "s|^\s*||igm" -e "s|\s*$||igm")
elif which gogglesmm > /dev/null 2>&1 ; then
cmd=gogglesmm
elif which radiotray > /dev/null 2>&1 ; then
cmd=radiotray
elif which audacious > /dev/null 2>&1 ; then
cmd=audacious
elif which clementine > /dev/null 2>&1 ; then
cmd=clementine
elif which rhythmbox > /dev/null 2>&1 ; then
cmd=rhythmbox
elif which guayadeque > /dev/null 2>&1 ; then
cmd=guayadeque
elif which xnoise > /dev/null 2>&1 ; then
cmd=xnoise
elif which gmusicbrowser > /dev/null 2>&1 ; then
cmd=gmusicbrowser
elif which qmmp > /dev/null 2>&1 ; then
cmd=qmmp
elif which xdg-open > /dev/null 2>&1 ; then
cmd=xdg-open
fi
#====================================================================================================
# Subroutines for showing the progress meter
#====================================================================================================
# Get the Genre List
_genres()
{
echo 0
local i
local genres
local genres_name
local genres_id
local name
local id
local linecount
rm -f "${cachedir}/genres.txt"
touch "${cachedir}/genres.txt"
wget -q \
--no-check-certificate \
--keep-session-cookies \
--save-cookies="${cachedir}/cookie.txt" \
http://www.shoutcast.com/Home -O- | \
grep loadStationsByGenre > "${cachedir}/genres.html"
genres_name=$(cat "${cachedir}/genres.html" | sed \
-e 's|^\s\+<li><[^>]\+>| |igm' \
-e 's|^\s\+<a[^>]\+>||igm' \
-e 's|^\([^<]\+\).\+|\1|igm')
# genres_id=$(cat "${cachedir}/genres.html" | \
# grep -o "loadStationsByGenre.[0-9]\+" | \
# grep -o "[0-9]\+$")
linecount=$(echo "${genres_name}" | wc -l)
echo $linecount
# Convert the spliter characters to Carriage Return
_IFS="$IFS";IFS="$BR"
i=1
while [ $linecount -gt $i ] ; do
name=$(echo "${genres_name}" | sed -n "${i}p")
#id=$(echo "${genres_id}" | sed -n "${i}p")
id=$(echo "${name}" | sed -e 's/^\s\+//igm' -e 's/^\s\+/%20/igm')
genres="${genres}${BR}${name}${BR}${id}"
i=$(expr $i + 1)
done
echo "${genres}" | tail -n +2 > "${cachedir}/genres.txt"
# Revert the Carriage Return
IFS="$_IFS"
rm -f "${cachedir}/genres.html"
}
# Get the Station List
_stations()
{
echo 0
local str
local act
local json
local i
local linecount
local parsed
local stations
if [ "$1" == "--search" ] ; then
str="station=$(echo $2 | nkf -wMQ | tr = %)"
act="Search/UpdateAdvancedSearch"
elif [ "$1" == "--genre" ] ; then
#str="genreid=${2}"
str="genrename=${2}"
act="Home/BrowseByGenre"
fi
json=$(wget -q \
--no-check-certificate \
--keep-session-cookies \
--save-cookies="${cachedir}/cookie.txt" \
--post-data "${str}" http://www.shoutcast.com/${act} -O- | \
sed -e 's|\"ID\":\([0-9]\+\)|\"ID\":\"http://yp.shoutcast.com/sbin/tunein-station.pls\?id=\1\"|igm')
linecount=$(echo "${json}" | jq ".[].ID" | wc -l)
if [ ${linecount} -gt 0 ] ; then
linecount=$(expr ${linecount} - 1)
fi
i=0
if [ $linecount -gt 50 ] ; then
linecount=50
fi
while [ $linecount -ge $i ] ; do
parsed=$(echo "${json}" | jq ".[$i].ID, .[$i].Name, .[$i].Genre, .[$i].Listeners, .[$i].Bitrate, .[$i].Format, .[$i].CurrentTrack")
stations="${stations}${BR}${parsed}"
if [ $? -gt 0 ] ; then
break
fi
i=$(expr $i + 1)
done
echo "${stations}" | tail -n +2 | sed -e 's|^"||igm' -e 's|"$||igm' > "${cachedir}/stations.txt"
# Revert the Carriage Return
IFS="$_IFS"
rm -f "${cachedir}/search.json"
}
#====================================================================================================
# Start the main job
#====================================================================================================
while true ; do
keyword=""
genre=""
station=""
keyword=$(
yad \
--title="linuxBean Radio Tuner" \
--on-top --center --borders=10 --width=600 \
--window-icon=radiotray --image=radiotray \
--text " This tool will search the internet radio stations from SHOUTcast.\n To browse stations from the Genre list, click the OK button without entering the search keyword." \
--form --field=""
)
if [ $? -gt 0 ] ; then
exit 1
fi
keyword=$(echo "${keyword}" | cut -d\| -f1)
if [ "${keyword}" == "" ] ; then
if [ ! -f "${cachedir}/genres.txt" ] || [ $(expr $(date +%s) - $(stat -c %Y "${cachedir}/genres.txt")) -gt 604800 ] ; then
_genres | \
yad --title="Connecting..." --on-top --center \
--window-icon=radiotray --image=radiotray \
--text "Receiving the Genre list..." --progress --pulsate --auto-close
fi
genre=$(
cat "${cachedir}/genres.txt" | \
yad \
--title="Genre" \
--on-top --center --width=200 --height=480 --window-icon=radiotray \
--text="Please select a genre.\nTo return to the search dialog, click the Cancel button." --list \
--column=Genre --column=ID --search-column=1 --hide-column=2 | \
cut -d\| -f2
)
if [ "${genre}" == "" ] ; then
continue
fi
_stations --genre "${genre}" | \
yad --title="Connecting..." --on-top --center \
--window-icon=radiotray --image=radiotray \
--text "Receiving the radio station list..." --progress --pulsate --auto-close
else
_stations --search "${keyword}" | \
yad --title="Searching..." --on-top --center \
--window-icon=radiotray --image=radiotray \
--text "Receiving the radio station list..." --progress --pulsate --auto-close
fi
while true ; do
station=$(
cat "${cachedir}/stations.txt" | \
yad \
--title="linuxBean Radio Tuner" \
--on-top --center --width=600 --height=400 --window-icon=radiotray \
--text="Please select a radio station you want to play. Click OK to play it. Click Cancel to return to the search dialog.\nIf you double-click the list, the station name and URL will be copied to the clipboard." \
--list --search-column=2 --hide-column=1 \
--dclick-action="$0 --dclk %s" \
--column="URL" --column="Stations" --column="Genre" --column="Listeners:NUM" --column="kbps:NUM" --column="Type" --column="Now Playing"
)
if [ $? -gt 0 ] ; then
break
fi
station=$(echo "${station}" | cut -d\| -f1)
echo "${cmd} \"${station}\""
if [ "${station}" == "" ] ; then
break
else
nohup ${cmd} "${station}" > /dev/null 2>&1 &
fi
if which streamripper > /dev/null 2>&1 ; then
yad \
--title="streamripper" \
--on-top --center --borders=10 \
--window-icon=radiotray --image=radiotray \
--text " Do you want to record this radio station?\n\n OK: Start the recording and return to the station list\n Cancel: Return to the station list without recording"
if [ $? -gt 0 ] ; then
continue
else
cd "${HOME}" > /dev/null 2>&1
nohup x-terminal-emulator -e streamripper ${station} > /dev/null 2>&1 &
cd - > /dev/null 2>&1
continue
fi
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment