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

EDIT 2015/10/29 This thread is now continued on the BunsenLabs forums.
EDIT 2015/9/8 Don't use the version in this top post - scroll down for version 3.2

A little script that searches the shoutcast directory for keyword(s) and sends your chosen station url to radiotray, or any other music player. You'll need Bash 4 (standard with #! Statler), curl, optionally radiotray, and xsel to keep the url for pasting into another app. Run this in a terminal:

#!/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

Notes

1) Shoutcast had a nice simple web api, but now you need to register and get a key to use it. The browser search page is riddled with javascript but the end result is a page url of the form 'http://www.shoutcast.com/Internet-Radio/keyword' so you can bypass all the obfuscation and just request that page. smile Then downloading it with curl it's possible to extract the station names and urls with awk. EDIT - shoutcast's interface has changed again. Version 3.2 below works OK.

2) Bash now has the very nice feature of associative arrays which mean you can index the elements by arbitary strings instead of numbers. So we can use the station names as array indices and store the urls as elements. This makes it a lot easier to make a menu with "select".

3) Radiotray lets you send urls to a running instance with 'radiotray url' and it will switch from what it's already playing to the new station. If you then open Configure Radios...>Add  you can paste in the new url with a middle-click (write in the name yourself) to get the station in your menu.

edit 140923 Radiotray 0.7.2-1 comes with Debian Wheezy and seems to work fine.

Any bug reports or enhancement requests are most welcome!

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


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
#! forum moderator    BunsenLabs

Offline

Help fund CrunchBang, donate to the project!

#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)


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
#! forum moderator    BunsenLabs

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

hello, i've been tweaking your script a little and i wonder: the part that extracts the info from shoutcast, is it still up-to-date?
it works really nicely, but there's some fluctuation (i don't always get the same search results) and some keywords that i would expect to throw hundreds of results (like 'pop') give only a handful.

but, here's my mod:

#!/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

a few notes:
i'm using mplayer instead of radiotray, so you have both the search function and the audio playback in one terminal. i think it's quite nice, esp. with the colors, almost reminds me of my beloved, bygone shellfm... :cry:

i'm perfectly happy with pressing ctrl+c to end playback. if you think it's inelegant, tweak some more!

i'm particularly proud of the mplayer command line. it looks horribly long, but the most important thing is the one long worm that comes after -msglevel: mplayer can output info while playing back, and with this you can tell it to the dot which info you want, and which not. so all options that have =-1 show nothing at all.
you can actually use that as a template; it was a bit of work to copy-paste all that over from mplayers man pages.
see the screenshot.
i find "ID_FILENAME" particularly helpful: i can copy that into a playlist that opens with mocp!
apart frm that mplayers terminal output is tee'd into a file, and i made a oneliner to put into my conky to display current song:

${if_existing /home/<username>/.mplayer/mplog}MPLAYER.
${font proliferationless:size=80}$color${execi 6 tac .mplayer/mplog|grep -m 1 ICY | awk -F'[=|;]' '{print $2}' | sed 's/^.\(.*\).$/\1/' | fold -s -w 29}$endif

edit: this conky snip is really inelegant and too long, i think it could be done with 1 sed or awk command. i'd be happy about suggestions as how to improve that.

originally i also wanted to include some kind of rudimentary history function, but the associative arrays are a bit too much for me...

i can do really nice searches with this, i like it a lot. "prog" or "hip" or "pop" give expected results, but i can also just try something out, like "green". nice.

edit: oops, forgot the screenshot. scrot_26_05_13_1369583111.jpg

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

ohnonot wrote:

hello, i've been tweaking your script a little and i wonder: the part that extracts the info from shoutcast, is it still up-to-date?
it works really nicely, but there's some fluctuation (i don't always get the same search results)

Yes, I found that too, but as far as I know the interface is OK. You can open 'http://www.shoutcast.com/Internet-Radio/keyword' in a browser and see basically the same results. However, for some reason they appear in a different order when fetched by curl. I've tried setting a different user-agent but it has no effect.

and some keywords that i would expect to throw hundreds of results (like 'pop') give only a handful.

The browser window displays the first 10 or so results with a "show more" button which uses javascript to pull down more. Javascript in an automated web scraper is very difficult...


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
#! forum moderator    BunsenLabs

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

ohnonot wrote:

i definitely got less than 10 for 'pop'.

Yes, try the web interface with a browser. I got "Showing 6 out of 15673 resultsresults for 'pop'."


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
#! forum moderator    BunsenLabs

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

ohnonot wrote:

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!

same issue here.
this worked well for me when i used it years ago. did the shoutcast format change?
i found shoutcurses (http://shoutcurses.tappir.com) for now. maybe it'll work again.  neutral time will tell...

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.


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
#! forum moderator    BunsenLabs

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

New Version - Shoutcast and radionomy searches combined, recording with streamripper and options to use mplayer, mpg123 or radiotray

EDIT 141003 See version 3.1 below which deals with the new Shoutcast and Radionomy web interfaces. Description and usage stay the same as this though.

This new version does a search of Shoutcast and then Radionomy on your searchterms, and combines the results in one menu. You can set the player to mplayer or mpg123 as well as radiotray. (Check the top of the script.) If you use one of the first two players it will run in the background as a "slave", controlled by commands sent via a FIFO, from the script or keyboard shortcut etc. Of course Radiotray is controlled from its sytray icon.

Default player is mpg123 - nice and fast, though mplayer might use less CPU - and default behaviour is keep_player=true so you can close the script and terminal and the music will continue. Rerun the script if you want to change the station or shut down the player. Record the stream with streamripper if you want.

KEYBOARD SHORTCUTS:
Ctrl+C to exit normally
Ctrl+\ to terminate and close player
Ctrl+Z to start recording current station

#!/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)


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
#! forum moderator    BunsenLabs

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

Shoutcast changed their web interface a while ago, breaking that part of the script. This is now fixed, and a bonus is that you now get more results. There's also a --help option. Otherwise usage is the same as post #12 : (edit 141003 Version 3.1 to handle new Radionomy interface.)

#!/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)


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
#! forum moderator    BunsenLabs

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.


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
#! forum moderator    BunsenLabs

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

John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
#! forum moderator    BunsenLabs

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

This thread is now continued on the BunsenLabs forums.


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
#! forum moderator    BunsenLabs

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: acrobat

Debian Logo