SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2011-10-05 05:35:10

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Shoutcast and Radionomy tuner for mplayer, mpg123 or radiotray

#!/bin/bash
# search shoutcast and send url to radiotray or another player
# needs Bash 4, curl, [radiotray], [xsel to send url to X selection for pasting]
# (comment out line 53 "printf '%s'..." if you don't use xsel)

# choose player (& options if necessary): radio url will be sent to it.
radioplay() {
    radiotray "$1"
#    mplayer -playlist "$1" # replace 'mplayer -playlist' to taste, $1 will hold the url
#    exec mplayer -playlist "$1" # add 'exec' if you'd rather launch player and leave script
}                               

# start up radiotray in background if it's not already running
# Comment out this line if you don't use radiotray.
pgrep radiotray >/dev/null || ( radiotray >/dev/null 2>&1 & )

##########################################################################
while true 
do
echo "Please enter keyword(s)"
read keyword
keyword="${keyword// /%20}" # escape spaces for url
results=$( curl -s "http://www.shoutcast.com/Internet-Radio/$keyword" |awk '
BEGIN {
    RS="<div class=\"dirlist\">"
    FS=">"
}
NR < 2 {next}
{url = name = $2
sub(/^.*title=\"/,"",name)
sub(/\".*$/,"",name)
sub(/^.*href=\"/,"",url)
sub(/\".*$/,"",url)
print url,name }
' )
[[ $results ]] || { echo "Sorry, no results for $keyword"; continue;}

unset list
declare -A list # make associative array
while read url name # read in awk's output
do
    list["$name"]="$url"
done <<< "$results"

PS3='Please enter the number of your choice > '
while true
do
    select station in "${!list[@]}" 'Search Again' Quit
    do
        [[ $station = 'Search Again' ]] && break 2
        [[ $station = Quit ]] && { echo 'Goodbye...'; exit; } 
        [[ $station ]] && {
        printf '%s' "${list[$station]}" | xsel --input #--clipboard  # can paste url
        radioplay "${list[$station]}" 
        break
        }
    done
echo "
Last station chosen was $station ( ${list[$station]} )
" 
done

done # closes loop started at line 18
exit

Last edited by johnraff (2015-10-29 07:27:32)

Offline

Be excellent to each other!

#2 2011-10-05 06:55:53

Bonster
New Member
Registered: 2011-10-05
Posts: 4

Re: Shoutcast and Radionomy tuner for mplayer, mpg123 or radiotray

Good Script Thanks

only wish there was an extra options to search again instead of restarting the script to do different searches

Offline

#3 2011-10-05 09:55:16

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Shoutcast and Radionomy tuner for mplayer, mpg123 or radiotray

Hi Bonster - I was wondering about that myself. It wouldn't be hard to add a "redo" option to the menu, but I thought the script started up pretty fast anyway and most of the waiting time is while curl gets the results from shoutcast, so you might just as well run the script again.

Maybe I'll add it anyway. smile
     OK now added.

...and welcome to the Crunchbang Forums!

Last edited by johnraff (2011-10-05 10:49:08)

Offline

#4 2011-10-05 12:01:35

dubois
The Old Codger
From: Missoula, Montana
Registered: 2010-09-03
Posts: 2,238

Re: Shoutcast and Radionomy tuner for mplayer, mpg123 or radiotray

Hmm.  I just did a Debian 6.0.2.1-i386-netinst on my 1015PEM.  Radiotray was on my todo list for today.  Hmm.

Offline

#5 2011-10-05 17:03:01

Bonster
New Member
Registered: 2011-10-05
Posts: 4

Re: Shoutcast and Radionomy tuner for mplayer, mpg123 or radiotray

@johnraff

Cool now is perfect Thanks cool

Offline

#6 2013-05-26 15:54:32

ohnonot
...again
Registered: 2012-05-22
Posts: 2,205

Re: Shoutcast and Radionomy tuner for mplayer, mpg123 or radiotray

#!/bin/bash
# search shoutcast and send url to radiotray or another player
# needs Bash 4, curl, [radiotray], [xsel to send url to X selection for pasting]
# (comment out line 53 "printf '%s'..." if you don't use xsel)

radioplay() {
mplayer -msglevel global=-1:cplayer=-1:vo=-1:ao=-1:demuxer=4:ds=-1:demux=-1:header=-1:avsync=-1:autoq=-1:cfgparser=-1:decaudio=-1:decvideo=-1:seek=-1:win32=-1:open=-1:dvd=-1:parsees=-1:lirc=-1:stream=-1:cache=-1:xacodec=-1:tv=-1:osdep=-1:spudec=-1:playtree=-1:input=-1:vfilter=-1:osd=-1:network=5:cpudetect=-1:codeccfg=-1:vobsub=-1:subreader=-1:afilter=-1:muxer=-1:identify=4:ass=-1:statusline=-1:fixme=-1 -msgmodule -msgcolor -prefer-ipv4 -noautosub -novideo -volume 90 -playlist "$1" | tee -a $HOME/.mplayer/mplog
}                               
##########################################################################
echo "Shoutshell will search shoutcast radiostations and play them."
while true 
do
echo "Please enter keyword(s)"
read keyword
keyword="${keyword// /%20}" # escape spaces for url
results=$( curl -s "http://www.shoutcast.com/Internet-Radio/$keyword" |awk '
BEGIN {
    RS="<div class=\"dirlist\">"
    FS=">"
}
NR < 2 {next}
{url = name = $2
sub(/^.*title=\"/,"",name)
sub(/\".*$/,"",name)
sub(/^.*href=\"/,"",url)
sub(/\".*$/,"",url)
print url,name }
' )
[[ $results ]] || { echo "Sorry, no results for $keyword"; continue;}

unset list
declare -A list # make associative array
while read url name # read in awk's output
do
    list["$name"]="$url"
done <<< "$results"

PS3='Please enter the number of your choice > '
while true
do
    select station in "${!list[@]}" 'Search Again' Quit
    do
        [[ $station = 'Search Again' ]] && break 2
        [[ $station = Quit ]] && { echo 'Goodbye...'; exit; } 
        [[ $station ]] && {
       # printf '%s' "${list[$station]}" | xsel --input #--clipboard  # can paste url
	echo Playing: "${list[$station]}" 
        radioplay "${list[$station]}" 
	rm -f $HOME/.mplayer/mplog
	break
        }
    done
echo "
Last station chosen was $station ( ${list[$station]} )
" 

done

done # closes loop started at line 18
exit

Last edited by ohnonot (2013-05-26 19:48:37)

Offline

#7 2013-05-29 05:31:49

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Shoutcast and Radionomy tuner for mplayer, mpg123 or radiotray

Offline

#8 2013-05-29 16:10:18

ohnonot
...again
Registered: 2012-05-22
Posts: 2,205

Re: Shoutcast and Radionomy tuner for mplayer, mpg123 or radiotray

ok, thanks for checking.
and no, i definitely got less than 10 for 'pop'.
i will monitor via web page every now and then and check back if i deduct sth.
great script!

Offline

#9 2013-05-31 16:12:13

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Shoutcast and Radionomy tuner for mplayer, mpg123 or radiotray

Offline

#10 2013-08-18 13:55:36

georgethomas
New Member
Registered: 2013-08-18
Posts: 1

Re: Shoutcast and Radionomy tuner for mplayer, mpg123 or radiotray

Last edited by georgethomas (2013-08-25 20:26:26)

Offline

#11 2013-08-21 17:14:40

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Shoutcast and Radionomy tuner for mplayer, mpg123 or radiotray

The script still seems to be working for me as it did before, so I'm not sure if Shoutcast have changed anything. The results list pretty much matches what the web page shows, except for the "more" button.

Offline

#12 2013-10-15 13:12:19

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Shoutcast and Radionomy tuner for mplayer, mpg123 or radiotray

#!/bin/bash
# shoutcast_radionomy_search.sh
# search shoutcast and radionomy,
# send url to radiotray, mpg123, mplayer or another player
# send url to streamripper to record
#
# needs curl, [radiotray, dbus | mpg123 | mplayer], streamripper, [xsel]
# xsel enables pasting from the X selection (to a config file etc.)
# Comment out line 246 "printf '%s'..." if you don't use it.
#
# KEYBOARD SHORTCUTS:
# Ctrl+C to exit normally
# Ctrl+\ to terminate and close player
# Ctrl+Z to start recording current station (handles SIGTSTP)

##### choose from radiotray, mpg123 or mplayer #####
player=mpg123

# Set this to something other than 'true'
# to have audio player exit with script.
# Otherwise player will continue till closed separately.
# Even with 'keep_player=true', if script is stopped with Ctrl+\
# then player will exit too.
keep_player=true

##### code to record a radio stream (url is $1) in a new terminal #####
# Add your own options to streamripper's command line,
# edit ~/.config/streamripper/streamripper.ini,
# change urxvt to another terminal
# or use a different command altogether.
recorder() {
    ( setsid urxvt -e streamripper "$1" >/dev/null 2>&1 & )
}

# where to put player control fifo
# (radiotray doesn't use this)
rpipe=/tmp/radio_pipe

##########################################################################
case $player in

##### RADIOTRAY SETTINGS #####
radiotray)
required_commands='curl streamripper radiotray'
start_player() {
    if pgrep radiotray >/dev/null
    then
        echo "$player is already running"
    else
        ( radiotray >/dev/null 2>&1 & )
    fi
}
radioplay() {
    radiotray $1
}
cleanup() { # run just before exit
    [[ $player_ok = true ]] && [[ $keep_player = true ]] && {
        echo "$player will continue to play.
You can control it from the system tray icon
or run the script again to choose another station."
        sleep 4
        return
    }
    pkill radiotray && echo "Closed radiotray."
}
;;
##### END RADIOTRAY #####

##### MPLAYER SETTINGS #####
mplayer)
required_commands='curl streamripper mplayer'
player_regex="^mplayer .*-input file=$rpipe"
launch_player() {
    [[ -p $rpipe ]] || { mkfifo "$rpipe" || error_exit "cannot make fifo $rpipe"; }
    ( setsid sh -c "mplayer -really-quiet -idle -slave -input file=$rpipe; rm -f $rpipe;" >/dev/null 2>&1 & )
    sleep 4 & launching_player=$!
}
load_url() {
    echo "loadlist $1" >"$rpipe"
}
;;&
##### END MPLAYER #####

##### MPG123 SETTINGS #####
mpg123)
required_commands='curl streamripper mpg123'
player_regex="^mpg123 .*--fifo $rpipe"
launch_player() { # mpg123 will make fifo if necessary
    ( setsid sh -c "mpg123 --remote --fifo $rpipe; rm -f $rpipe;" >/dev/null 2>&1 & )
    (sleep 2; echo 'silence' >"$rpipe") & launching_player=$!
}
load_url() {
    echo "loadlist 1 $1" >"$rpipe"
}
;;&
##### END MPG123 #####

##### COMMON TO MPLAYER AND MPG123 #####
mplayer|mpg123)
start_player() {
    if pgrep -f "$player_regex" >/dev/null
    then
        echo "$player is already running"
        [[ -p $rpipe ]] || error_exit "fifo missing $rpipe"
        (:>"$rpipe") & test_pipe=$!
        (sleep 2; kill $test_pipe 2>/dev/null && kill -s SIGPIPE $selfpid) &
    else
        launch_player
    fi
}
radioplay() {
    wait $launching_player
    [[ -p $rpipe ]] || error_exit "fifo missing $rpipe"
    pgrep -f "$player_regex" >/dev/null || error_exit "$player not running"
    load_url "$1"
}
cleanup() { # run just before exit
    [[ -p $rpipe ]] || { player_ok=false; echo "Script error: fifo $rpipe does not exist." >&2 ;}
    pgrep -f "$player_regex" >/dev/null || { player_ok=false; echo "Script error: $player not running" >&2 ;}
    [[ $player_ok = true ]] && {
        [[ $keep_player = true ]] && {
            echo "$player will continue to play.
You can stop it with the command:
echo quit >$rpipe
or run the script again to choose another station."
            sleep 4
            return
        }
        echo "closing $player..."
        echo 'quit' >"$rpipe" # try to close player nicely
        sleep 2 # time for player to quit
    }
    pkill -f "$player_regex" && echo "$player close forced."
    echo "removing $rpipe"
    rm -f "$rpipe" # in case it has become a normal file
}
;;
##### END COMMON TO MPLAYER AND MPG123 #####

*)
echo "$0: chosen player $player has not been configured.
Please check line 17 of the script" >&2
exit 1
;;
esac

##########################################################################
selfpid=$$
player_ok=true
error_exit() {
    echo "Script error: $1" >&2
    player_ok=false
    exit 1
}
trap 'cleanup' EXIT
trap 'echo " Exit script
Goodbye..."; exit' SIGHUP SIGINT
trap 'echo " Exit script
($player will be shut down)
Goodbye..."; keep_player=false; exit' SIGQUIT
trap 'error_exit "script terminated"' SIGTERM
trap 'error_exit "broken pipe"' SIGPIPE
trap 'recorder "${playing_url%.m3u}"' SIGTSTP

missing_commands=
for i in $required_commands
do
    hash $i || missing_commands+=" $i"
done
[[ $missing_commands ]] && error_exit "This script requires the following commands: $missing_commands
Please install the packages containing the missing commands
and rerun the script."

start_player

unset playing_name playing_url
while true
do
echo "Please enter keyword(s)"
read keyword
keyword="${keyword// /%20}" # escape spaces for url
results_sh=$( curl -s "http://www.shoutcast.com/Internet-Radio/$keyword" |awk '
BEGIN {
    RS="<div class=\"dirlist\">"
    FS=">"
}
NR < 2 {next}
{url = name = $2
sub(/^.*title=\"/,"",name)
sub(/\".*$/,"",name)
sub(/^.*href=\"/,"",url)
sub(/\".*$/,"",url)
print url,name }
' )
results_ra=$( curl -s "http://www.radionomy.com/en/search?q=$keyword" |awk '
BEGIN {
    RS="<h2 class=\"radio-title-list\"><a href=\"/en/radio/"
    FS="</a></h2>"
}
NR < 2 {next}
{url = name = $1
sub(/^.*>/,"",name)
sub(/\/index\".*$/,"",url)
url="http://listen.radionomy.com/" url ".m3u"
print url,name }
' )

if [[ $results_sh ]] && [[ $results_ra ]]
then
    results="$results_sh"$'\n'"$results_ra"
elif [[ $results_sh ]]
then
    echo "No results for $keyword on radionomy"
    results="$results_sh"
elif [[ $results_ra ]]
then
    echo "No results for $keyword on shoutcast"
    results="$results_ra"
else
    echo "Sorry, no results for $keyword"
    continue
fi

unset list
declare -A list # make associative array
while read -r url name # read in awk's output
do
    list["$name"]="$url"
done <<< "$results"

PS3='Please enter the number of your choice > '
while true
do
    menu=("${!list[@]}")
    [[ $playing_name && $playing_url ]] && menu+=("RECORD \"$playing_name\"")
    select station in "${menu[@]}" 'SEARCH AGAIN' QUIT
    do
        [[ $station = "RECORD \"$playing_name\"" ]] && {
            recorder "${playing_url%.m3u}" # streamripper won't take m3u urls
            break
        }
        [[ $station = 'SEARCH AGAIN' ]] && break 2
        [[ $station = QUIT ]] && { echo 'Goodbye...'; exit; }
        [[ $station ]] && {
# comment out next line if you don't use xsel
            printf '%s' "${list[$station]}" | xsel --input #--clipboard  # can paste url
            radioplay "${list[$station]}"
            playing_name=$station
            playing_url=${list[$station]}
            break
        }
    done
echo "
Station last chosen was \"$playing_name\" ( $playing_url )
"
done

done # closes loop started at line 178
exit

Last edited by johnraff (2014-10-03 04:47:56)

Offline

#13 2013-10-15 21:14:18

DapperMe17
#! CrunchBanger
From: Todd's Gang
Registered: 2012-10-19
Posts: 221

Re: Shoutcast and Radionomy tuner for mplayer, mpg123 or radiotray

Really nice work John. Thanks for sharing!

smile

Offline

#14 2013-10-17 10:56:09

ohnonot
...again
Registered: 2012-05-22
Posts: 2,205

Re: Shoutcast and Radionomy tuner for mplayer, mpg123 or radiotray

ooooh-yes!
thanks from me too!

Offline

#15 2014-09-23 13:32:54

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Shoutcast and Radionomy tuner for mplayer, mpg123 or radiotray

#!/bin/bash
# shoutcast_radionomy_search.sh
# search shoutcast and radionomy,
# send url to radiotray, mpg123, mplayer or another player
# send url to streamripper to record
#
# version 3.1
#
# needs curl, [radiotray, dbus | mpg123 | mplayer], streamripper, [xsel], [perl]
# xsel enables pasting from the X selection (to a config file etc.)
# Comment out line 288 "printf '%s'..." if you don't use it.
# perl is used to urlencode the query.
# Comment out line 246 and uncomment line 245 to escape spaces only
# if your system doesn't have perl.
#
# KEYBOARD SHORTCUTS:
# Ctrl+C to exit normally
# Ctrl+\ to terminate and close player
# Ctrl+Z to start recording current station (handles SIGTSTP)

##### choose from radiotray, mpg123 or mplayer #####
player=radiotray
#player=mpg123
#player=mplayer

# Set this to something other than 'true'
# to have audio player exit with script.
# Otherwise player will continue till closed separately.
# Even with 'keep_player=true', if script is stopped with Ctrl+\
# then player will exit too.
keep_player=true

##### code to record a radio stream (url is $1) in a new terminal #####
# Add your own options to streamripper's command line,
# edit ~/.config/streamripper/streamripper.ini,
# change urxvt to another terminal
# or use a different command altogether.
recorder() {
    ( setsid urxvt -e streamripper "$1" >/dev/null 2>&1 & )
}

# where to put player control fifo
# (radiotray doesn't use this)
rpipe=/tmp/radio_pipe

HELP="This is an interactive script to query the Shoutcast and Radionomy listings,
put the results in a menu,
and load the chosen radio station in radiotray, mpg123 or mplayer.
There is also an option to record with streamripper.

If you exit the script and leave mpg123 or mplayer running,
you can close either of them with the command:
echo quit >$rpipe

KEYBOARD SHORTCUTS:
Ctrl+C to exit normally
Ctrl+\ to terminate and close player
Ctrl+Z to start recording current station (handles SIGTSTP)"

##########################################################################

case $1 in
--help|-h)
    echo "$HELP"
    exit
    ;;
esac

case $player in

##### RADIOTRAY SETTINGS #####
radiotray)
required_commands='curl streamripper radiotray'
start_player() {
    if pgrep radiotray >/dev/null
    then
        echo "$player is already running"
    else
        ( setsid radiotray >/dev/null 2>&1 & )
    fi
}
radioplay() {
    radiotray "$1"
}
cleanup() { # run just before exit
    [[ $player_ok = true ]] && [[ $keep_player = true ]] && {
        echo "$player will continue to play.
You can control it from the system tray icon
or run the script again to choose another station."
        sleep 4
        return
    }
    pkill radiotray && echo "Closed radiotray."
    sleep 4
}
;;
##### END RADIOTRAY #####

##### MPLAYER SETTINGS #####
mplayer)
required_commands='curl streamripper mplayer'
player_regex="^mplayer .*-input file=$rpipe"
launch_player() {
    [[ -p $rpipe ]] || { mkfifo "$rpipe" || error_exit "cannot make fifo $rpipe"; }
    ( setsid sh -c "mplayer -really-quiet -idle -slave -input file=$rpipe; rm -f $rpipe;" >/dev/null 2>&1 & )
    sleep 4 & launching_player=$!
}
load_url() {
    echo "loadlist $1" >"$rpipe"
}
;;&
##### END MPLAYER #####

##### MPG123 SETTINGS #####
mpg123)
required_commands='curl streamripper mpg123'
player_regex="^mpg123 .*--fifo $rpipe"
launch_player() { # mpg123 will make fifo if necessary
    ( setsid sh -c "mpg123 --remote --fifo $rpipe; rm -f $rpipe;" >/dev/null 2>&1 & )
    (sleep 2; echo 'silence' >"$rpipe") & launching_player=$!
}
load_url() {
    echo "loadlist 1 $1" >"$rpipe"
}
;;&
##### END MPG123 #####

##### COMMON TO MPLAYER AND MPG123 #####
mplayer|mpg123)
start_player() {
    if pgrep -f "$player_regex" >/dev/null
    then
        echo "$player is already running"
        [[ -p $rpipe ]] || error_exit "fifo missing $rpipe"
        (:>"$rpipe") & test_pipe=$!
        (sleep 2; kill $test_pipe 2>/dev/null && kill -s SIGPIPE $selfpid) &
    else
        launch_player
    fi
}
radioplay() {
    wait $launching_player
    [[ -p $rpipe ]] || error_exit "fifo missing $rpipe"
    pgrep -f "$player_regex" >/dev/null || error_exit "$player not running"
    load_url "$1"
}
cleanup() { # run just before exit
    [[ -p $rpipe ]] || { player_ok=false; echo "Script error: fifo $rpipe does not exist." >&2 ;}
    pgrep -f "$player_regex" >/dev/null || { player_ok=false; echo "Script error: $player not running" >&2 ;}
    [[ $player_ok = true ]] && {
        [[ $keep_player = true ]] && {
            echo "$player will continue to play.
You can stop it with the command:
echo quit >$rpipe
or run the script again to choose another station."
            sleep 4
            return
        }
        echo "closing $player..."
        echo 'quit' >"$rpipe" # try to close player nicely
        sleep 2 # time for player to quit
    }
    pkill -f "$player_regex" && echo "$player close forced."
    echo "removing $rpipe"
    rm -f "$rpipe" # in case it has become a normal file
}
;;
##### END COMMON TO MPLAYER AND MPG123 #####

*)
echo "$0: chosen player $player has not been configured.
Please check line 17 of the script" >&2
exit 1
;;
esac

##########################################################################
selfpid=$$
player_ok=true
error_exit() {
    echo "Script error: $1" >&2
    player_ok=false
    exit 1
}
trap 'cleanup' EXIT
trap 'echo " Exit script
Goodbye..."; exit' SIGHUP SIGINT
trap 'echo " Exit script
($player will be shut down)
Goodbye..."; keep_player=false; exit' SIGQUIT
trap 'error_exit "script terminated"' SIGTERM
trap 'error_exit "broken pipe"' SIGPIPE
trap 'recorder "${playing_url%.m3u}"' SIGTSTP

missing_commands=
for i in $required_commands
do
    hash $i || missing_commands+=" $i"
done
[[ $missing_commands ]] && error_exit "This script requires the following commands: $missing_commands
Please install the packages containing the missing commands
and rerun the script."

query_shoutcast() {
    curl -s --data "query=$1" "http://www.shoutcast.com/Search/UpdateSearch" | awk '
    BEGIN {
        RS="},{"
    }
    {
        url = name = $0
        if($0=="[]") {exit}
        sub(/^.*\"ID\":/,"",url)
        sub(/,.*$/,"",url)
        url = "http://yp.shoutcast.com/sbin/tunein-station.pls?id=" url
        sub(/^.*\"Name\":\"/,"",name)
        sub(/\".*$/,"",name)
        print url,name
    }
    '
}
query_radionomy() {
    curl -sL "http://www.radionomy.com/en/search?q=$1" |awk '
    BEGIN {
        RS="<h2 class=\"radio-title-list\"><a href=\"/en/radio/"
        FS="</a></h2>"
    }
    NR < 2 {next}
    {
        url = name = $1
        sub(/^.*>/,"",name)
        sub(/\/index\".*$/,"",url)
        url="http://listen.radionomy.com/" url ".m3u"
        print url,name
    }
    '
}

start_player

unset playing_name playing_url
while true
do
echo "Please enter keyword(s)"
read keyword
#keyword_esc="${keyword// /%20}" # escape spaces for url
keyword_esc=$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$keyword")
results_sh=$( query_shoutcast "$keyword_esc" )
results_ra=$( query_radionomy "$keyword_esc" )

if [[ $results_sh ]] && [[ $results_ra ]]
then
    results="$results_sh"$'\n'"$results_ra"
elif [[ $results_sh ]]
then
    echo "No results for $keyword on radionomy"
    results="$results_sh"
elif [[ $results_ra ]]
then
    echo "No results for $keyword on shoutcast"
    results="$results_ra"
else
    echo "Sorry, no results for $keyword"
    continue
fi

unset list
declare -A list # make associative array
while read -r url name # read in awk's output
do
    list["$name"]="$url"
done <<< "$results"

PS3='Please enter the number of your choice > '
while true
do
    menu=("${!list[@]}")
    [[ $playing_name && $playing_url ]] && menu+=("RECORD \"$playing_name\"")
    select station in "${menu[@]}" 'SEARCH AGAIN' QUIT
    do
        [[ $station = "RECORD \"$playing_name\"" ]] && {
            recorder "${playing_url%.m3u}" # streamripper won't take m3u urls
            break
        }
        [[ $station = 'SEARCH AGAIN' ]] && break 2
        [[ $station = QUIT ]] && { echo 'Goodbye...'; exit; }
        [[ $station ]] && {
# comment out next line if you don't use xsel
            printf '%s' "${list[$station]}" | xsel --input #--clipboard  # can paste url
            radioplay "${list[$station]}"
            playing_name=$station
            playing_url=${list[$station]}
            break
        }
    done
echo "
Station last chosen was \"$playing_name\" ( $playing_url )
"
done # closes loop started at line 274

done # closes loop started at line 241

exit

Last edited by johnraff (2014-10-03 04:45:22)

Offline

#16 2014-10-03 04:46:40

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Shoutcast and Radionomy tuner for mplayer, mpg123 or radiotray

Since that last post Radionomy have also changed their web interface a bit. Luckily, this time it only requires curl to follow a redirect to the new search results page, which means adding a -L option. Script now edited.

Offline

#17 2015-09-08 03:19:38

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Shoutcast and Radionomy tuner for mplayer, mpg123 or radiotray

Radionomy changed their interface again. Here's version 3.2.
I've just tested it with radiotray - if you hit problems with mplayer or mpg123, please post.

#!/bin/bash
# shoutcast_radionomy_search.sh
# search shoutcast and radionomy,
# send url to radiotray, mpg123, mplayer or another player
# send url to streamripper to record
#
# version 3.2
#
# needs curl, [radiotray, dbus | mpg123 | mplayer], streamripper, [xsel], [perl]
# xsel enables pasting from the X selection (to a config file etc.)
# Comment out line 288 "printf '%s'..." if you don't use it.
# perl is used to urlencode the query.
# Comment out line 246 and uncomment line 245 to escape spaces only
# if your system doesn't have perl.
#
# KEYBOARD SHORTCUTS:
# Ctrl+C to exit normally
# Ctrl+\ to terminate and close player
# Ctrl+Z to start recording current station (handles SIGTSTP)

##### choose from radiotray, mpg123 or mplayer #####
player=radiotray
#player=mpg123
#player=mplayer

# Set this to something other than 'true'
# to have audio player exit with script.
# Otherwise player will continue till closed separately.
# Even with 'keep_player=true', if script is stopped with Ctrl+\
# then player will exit too.
keep_player=true

##### code to record a radio stream (url is $1) in a new terminal #####
# Add your own options to streamripper's command line,
# edit ~/.config/streamripper/streamripper.ini,
# change urxvt to another terminal
# or use a different command altogether.
recorder() {
    ( setsid urxvt -e streamripper "$1" >/dev/null 2>&1 & )
}

# where to put player control fifo
# (radiotray doesn't use this)
rpipe=/tmp/radio_pipe

HELP="This is an interactive script to query the Shoutcast and Radionomy listings,
put the results in a menu,
and load the chosen radio station in radiotray, mpg123 or mplayer.
There is also an option to record with streamripper.

If you exit the script and leave mpg123 or mplayer running,
you can close either of them with the command:
echo quit >$rpipe

KEYBOARD SHORTCUTS:
Ctrl+C to exit normally
Ctrl+\ to terminate and close player
Ctrl+Z to start recording current station (handles SIGTSTP)"

##########################################################################

case $1 in
--help|-h)
    echo "$HELP"
    exit
    ;;
esac

case $player in

##### RADIOTRAY SETTINGS #####
radiotray)
required_commands='curl streamripper radiotray'
start_player() {
    if pgrep radiotray >/dev/null
    then
        echo "$player is already running"
    else
        ( setsid radiotray >/dev/null 2>&1 & )
    fi
}
radioplay() {
    radiotray "$1"
}
cleanup() { # run just before exit
    [[ $player_ok = true ]] && [[ $keep_player = true ]] && {
        echo "$player will continue to play.
You can control it from the system tray icon
or run the script again to choose another station."
        sleep 4
        return
    }
    pkill radiotray && echo "Closed radiotray."
    sleep 4
}
;;
##### END RADIOTRAY #####

##### MPLAYER SETTINGS #####
mplayer)
required_commands='curl streamripper mplayer'
player_regex="^mplayer .*-input file=$rpipe"
launch_player() {
    [[ -p $rpipe ]] || { mkfifo "$rpipe" || error_exit "cannot make fifo $rpipe"; }
    ( setsid sh -c "mplayer -really-quiet -idle -slave -input file=$rpipe; rm -f $rpipe;" >/dev/null 2>&1 & )
    sleep 4 & launching_player=$!
}
load_url() {
    echo "loadlist $1" >"$rpipe"
}
;;&
##### END MPLAYER #####

##### MPG123 SETTINGS #####
mpg123)
required_commands='curl streamripper mpg123'
player_regex="^mpg123 .*--fifo $rpipe"
launch_player() { # mpg123 will make fifo if necessary
    ( setsid sh -c "mpg123 --remote --fifo $rpipe; rm -f $rpipe;" >/dev/null 2>&1 & )
    (sleep 2; echo 'silence' >"$rpipe") & launching_player=$!
}
load_url() {
    echo "loadlist 1 $1" >"$rpipe"
}
;;&
##### END MPG123 #####

##### COMMON TO MPLAYER AND MPG123 #####
mplayer|mpg123)
start_player() {
    if pgrep -f "$player_regex" >/dev/null
    then
        echo "$player is already running"
        [[ -p $rpipe ]] || error_exit "fifo missing $rpipe"
        (:>"$rpipe") & test_pipe=$!
        (sleep 2; kill $test_pipe 2>/dev/null && kill -s SIGPIPE $selfpid) &
    else
        launch_player
    fi
}
radioplay() {
    wait $launching_player
    [[ -p $rpipe ]] || error_exit "fifo missing $rpipe"
    pgrep -f "$player_regex" >/dev/null || error_exit "$player not running"
    load_url "$1"
}
cleanup() { # run just before exit
    [[ -p $rpipe ]] || { player_ok=false; echo "Script error: fifo $rpipe does not exist." >&2 ;}
    pgrep -f "$player_regex" >/dev/null || { player_ok=false; echo "Script error: $player not running" >&2 ;}
    [[ $player_ok = true ]] && {
        [[ $keep_player = true ]] && {
            echo "$player will continue to play.
You can stop it with the command:
echo quit >$rpipe
or run the script again to choose another station."
            sleep 4
            return
        }
        echo "closing $player..."
        echo 'quit' >"$rpipe" # try to close player nicely
        sleep 2 # time for player to quit
    }
    pkill -f "$player_regex" && echo "$player close forced."
    echo "removing $rpipe"
    rm -f "$rpipe" # in case it has become a normal file
}
;;
##### END COMMON TO MPLAYER AND MPG123 #####

*)
echo "$0: chosen player $player has not been configured.
Please check line 17 of the script" >&2
exit 1
;;
esac

##########################################################################
selfpid=$$
player_ok=true
error_exit() {
    echo "Script error: $1" >&2
    player_ok=false
    exit 1
}
trap 'cleanup' EXIT
trap 'echo " Exit script
Goodbye..."; exit' SIGHUP SIGINT
trap 'echo " Exit script
($player will be shut down)
Goodbye..."; keep_player=false; exit' SIGQUIT
trap 'error_exit "script terminated"' SIGTERM
trap 'error_exit "broken pipe"' SIGPIPE
trap 'recorder "${playing_url%.m3u}"' SIGTSTP

missing_commands=
for i in $required_commands
do
    hash $i || missing_commands+=" $i"
done
[[ $missing_commands ]] && error_exit "This script requires the following commands: $missing_commands
Please install the packages containing the missing commands
and rerun the script."

query_shoutcast() {
    curl -s --data "query=$1" "http://www.shoutcast.com/Search/UpdateSearch" | awk '
    BEGIN {
        RS="},{"
    }
    {
        url = name = $0
        if($0=="[]") {exit}
        sub(/^.*\"ID\":/,"",url)
        sub(/,.*$/,"",url)
        url = "http://yp.shoutcast.com/sbin/tunein-station.pls?id=" url
        sub(/^.*\"Name\":\"/,"",name)
        sub(/\".*$/,"",name)
        print url,name
    }
    '
}

query_radionomy() {
    curl -sL "http://www.radionomy.com/en/search/index?query=$1" |awk '
    BEGIN {
        RS="class=\"browseRadioWrap\">"
        FS="<div class=\"radioPlay\">"
    }
    NR < 2 {next}
    {
        name = $1
        sub(/^.*class=\"radioName\">/,"",name)
        sub(/<\/p>.*$/,"",name)
        url = $2
        sub(/^.*&quot;mp3&quot;: &quot;/,"",url)
        sub(/&quot;.*$/,"",url)
        print url,name
    }
    '
}

start_player

unset playing_name playing_url
while true
do
echo "Please enter keyword(s)"
read keyword
#keyword_esc="${keyword// /%20}" # escape spaces for url
keyword_esc=$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$keyword")
results_sh=$( query_shoutcast "$keyword_esc" )
results_ra=$( query_radionomy "$keyword_esc" )

if [[ $results_sh ]] && [[ $results_ra ]]
then
    results="$results_sh"$'\n'"$results_ra"
elif [[ $results_sh ]]
then
    echo "No results for $keyword on radionomy"
    results="$results_sh"
elif [[ $results_ra ]]
then
    echo "No results for $keyword on shoutcast"
    results="$results_ra"
else
    echo "Sorry, no results for $keyword"
    continue
fi

unset list
declare -A list # make associative array
while read -r url name # read in awk's output
do
    list["$name"]="$url"
done <<< "$results"

PS3='Please enter the number of your choice > '
while true
do
    menu=("${!list[@]}")
    [[ $playing_name && $playing_url ]] && menu+=("RECORD \"$playing_name\"")
    select station in "${menu[@]}" 'SEARCH AGAIN' QUIT
    do
        [[ $station = "RECORD \"$playing_name\"" ]] && {
            recorder "${playing_url%.m3u}" # streamripper won't take m3u urls
            break
        }
        [[ $station = 'SEARCH AGAIN' ]] && break 2
        [[ $station = QUIT ]] && { echo 'Goodbye...'; exit; }
        [[ $station ]] && {
# comment out next line if you don't use xsel
            printf '%s' "${list[$station]}" | xsel --input #--clipboard  # can paste url
            radioplay "${list[$station]}"
            playing_name=$station
            playing_url=${list[$station]}
            break
        }
    done
echo "
Station last chosen was \"$playing_name\" ( $playing_url )
"
done # closes loop started at line 278

done # closes loop started at line 245

exit

Offline

#18 2015-10-29 07:27:54

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Shoutcast and Radionomy tuner for mplayer, mpg123 or radiotray

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.
Server: bleh

Debian Logo