-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradio
executable file
·111 lines (104 loc) · 3.78 KB
/
radio
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env bash
# vim: set fileencoding=utf-8 :
#======================================================================================================================#
#
# Copyright © 2012 George A Stamoulis ([email protected])
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#======================================================================================================================#
#
# Bash script for listening to internet radio stations via the terminal.
# It is depending on vlc (vlc -I dummy) and cut.
#
# Insert any station you like in the stations file.
# Separate the name and the address by a whitespace and
# don't forget to separate the entries with newlines as well.
#
# Enjoy ;)
#
#======================================================================================================================#
tempdir="$HOME/temp"
radiopid="$tempdir/radio.pid"
stations="/etc/stations.csv"
[[ ! -d $tempdir ]] && mkdir $tempdir
# Killing the process without hassle!
if [ "$1" == "kill" ]; then
if [ -e "$radiopid" ]; then
kill $(cat "$radiopid") &> /dev/null
if [ $? -gt 0 ]; then
echo "Invalid pid. Please kill manyally (or use \"killall\")." 1>&2
exit 2
fi
rm -f "$radiopid"
exit 0
else
echo "Unable to retrieve pid. Please kill manually (or use \"killall\")." 1>&2
exit 2
fi
elif [ "$1" == "killall" ]; then
e=0
for pid in $(ps -eo pid,cmd | grep "vlc -I dummy" | grep -v "grep" | sed s'/^\s*//'g | cut -d' ' -f1); do
kill $pid
let e+=$?
done
if [ $pid ]; then
echo "Some were found."
exit $e
else
echo "Nothing found.." 1>&2
exit 2
fi
fi
if [ $# -lt 1 ]; then # If user doesn't provide a station, we will ask him to choose.
i=0
while read line; do
names[$i]=$(echo $line | cut -d',' -f1)
let i++
done < $stations
PS3="Select station. " #PS3 is the prompt that select uses. We set it to something more relevant than "#?".
select choise in ${names[@]}; do
if [[ -n $choise ]]; then
station=$choise
break
else
echo "Invalid input."
fi
done
elif [ $# -gt 1 ]; then # If user provides more than 1 argument, we exit gracefully!
echo -e "You can listen only one radio station at a time.\nKeep that in mind!" 1>&2
exit 1
else # Finally, user has provided one station at the command line and we assign it to our variable.
station=$1
fi
# We get the address for the requested station.
while read line; do
name=$(echo $line | cut -d',' -f1)
if [ "$name" == "$station" ]; then
address=$(echo $line | cut -d',' -f2)
break
fi
done < $stations
# We assert that we got a valid address from our list and if not we exit.
if [[ -n $address ]]; then
[ -e "$radiopid" ] && kill $(cat "$radiopid") &> /dev/null # We kill previous instance if exists.
vlc -I dummy --quiet $address 0<&- &> /dev/null &
echo $! > "$radiopid"
exit 0
else
echo "Unknown station \"$station\". Please try again." 1>&2
exit 2
fi
#======================================================================================================================#