Skip to content

Instantly share code, notes, and snippets.

@michd
Created May 18, 2019 09:15
Show Gist options
  • Save michd/fc23fa912e8db19feb47034e6c4477fc to your computer and use it in GitHub Desktop.
Save michd/fc23fa912e8db19feb47034e6c4477fc to your computer and use it in GitHub Desktop.
.bash_profile snippet for listening to Digitally Imported sites' radio streams from the command line
# Usage:
# $ radio <network> <channel-key>
# Where network is a network key as specified in http://api.audioaddict.com/v1/networks
# and where channel-key is the name of a channel as seen in a channel page URL
# The script will ask for a listen key and save it in ~/.listen_key if you don't
# have one on file yet.
radio() {
# Modify playCmd to launch the stream with a different player
local playCmd="mplayer "
local network=$1
local channel=$2
local domain=""
local website=""
local listenKey="-"
local listenKeyFile="$HOME/.listen_key"
if [[ -z "$network" ]]; then
echo "Usage: $ radio <network> <channel-key>"
return 1
fi
if [[ -z "$channel" ]]; then
echo "Usage: $ radio <network> <channel-key>"
return 1
fi
# Note: currently it's hardcoded to use prem1, but ideally it'd pick from
# a random available server instead.
case "$network" in
"di")
domain="prem1.di.fm"
website="di.fm"
;;
"radiotunes")
domain="prem1.radiotunes.com"
website="radiotunes.com"
;;
"jazzradio")
domain="prem1.jazzradio.com"
websites="jazzradio.com"
;;
"rockradio")
domain="prem1.rockradio.com"
website="rockradio.com"
;;
"classicalradio")
domain="prem1.classicalradio.com"
website="classicalradio.com"
;;
*)
echo "Not a valid network specified. Got: $domain"
echo "Available networks: di, radiotunes, jazzradio, rockradio, classicalradio"
return 1
esac
if [[ ! -f $listenKeyFile ]]; then
echo "There is no listen key on file. Please copy the listen key from"
echo "https://www.${website}/settings"
echo "Leave blank to cancel."
read -p 'Listen key: ' listenKey
if [[ -z "$listenKey" ]]; then
echo "Aborting due to blank listen key."
return 1
fi
echo "$listenKey" > "$listenKeyFile"
fi
listenKey=$(cat $listenKeyFile)
local url="http://${domain}:80/${channel}?${listenKey}"
$playCmd $url
}
# Possible future enhancements: autocomplete channel names. Will require some
# API response mangling.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment