Save Songs from Internet 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env bash
 
thispid=$$
save=false
prevsave=true
play=false
prevplay=true
quit=false
 
if [ ! -d /tmp/radio/ ]
then
    mkdir /tmp/radio/
fi
cd /tmp/radio/
 
streamripper http://www.radioswisspop.ch/live/mp3.m3u -r -s -k 0 &
 
del_or_save ()
{
    todo=$1
    while true
    do
        if [ $todo == "del" ]
        then
            rm *.mp3 &> /dev/null
        elif [ $todo == "save" ]
        then
            if [ -e *.mp3 ]
            then
                mv *.mp3 ~/Music/ &> /dev/null
                todo="del"
                echo "finished"
            fi
        else
            exit 1
        fi
        sleep 10
    done
}
coproc del_or_save "del"
 
while true
do
 
    if $quit
    then
        break
    fi
 
    if [ $save != $prevsave ]
    then
        if $save
        then
            kill $COPROC_PID &> /dev/null
            rm *.mp3 &> /dev/null
            coproc del_or_save "save"
        else
            kill $COPROC_PID &> /dev/null
            coproc del_or_save "del"
        fi
    fi
 
    if [ $play != $prevplay ]
    then
        if $play
        then
            mplayer http://localhost:8000 &> /dev/null &
        else
            pkill -f "mplayer http://localhost:8000"
        fi
    fi
 
    # set status variables
    prevsave=$save
    prevplay=$play
    read -s -n 1 -p "enter s for save, p to start/stop playback, or q for quit. " answer
 
    # if coproc said something (i.e. "finished"), set save to false
    # because the coproc switched to false already
    read -t 0.1 -u ${COPROC[0]} coproc_reply
    if [ ! -z $coproc_reply ]
    then
        save=false
        prevsave=false
    fi
 
    if [ -z $answer ]
    then
        continue
    elif [ $answer == "q" ]
    then
        echo ""
        echo "quit"
        quit=true
    elif [ $answer == "s" ]
    then
        if $save
        then
            echo ""
            echo "song will not be saved"
            echo ""
            save=false
        else
            echo ""
            echo "song will be saved"
            echo ""
            save=true
        fi
    elif [ $answer == "p" ]
    then
        if $play
        then
            echo ""
            echo "stop playback"
            echo ""
            play=false
        else
            echo ""
            echo "start playback"
            echo ""
            play=true
        fi
    else
        continue
    fi
 
done
 
kill $COPROC_PID &> /dev/null
pkill -P $thispid &> /dev/null
rm ./incomplete/*.* &> /dev/null
cd /tmp/
rm -rf /tmp/radio/

Leave a Reply