Post

Shoutcast Streaming from the Command Line

I spend a lot of time working in a terminal, and I like to spend that time listening to music. Since I work primarily in Linux, I was hoping there would be an easy way to merge these two activities … and it turns out there was :)

After searching around, i found a post on the crunchbang forums that provided exactly the script I was looking for.

I modified it a tiny bit to check for dependencies:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/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)

command -v curl > /dev/null 2>&1 || { echo "curl required." >&2; exit 1; }
command -v xsel > /dev/null 2>&1 || { echo "xsel required." >&2; exit 1; }
command -v radiotray > /dev/null 2>&1 || { echo "radiotray required." >&2; exit 1; }

# 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
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.