-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-radio
executable file
·69 lines (64 loc) · 1.5 KB
/
update-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
#!/bin/bash
dir=~/tunes
[[ -e "$dir" ]] || mkdir "$dir"
test_radio()
{
url=$1
valid_radio=false
xxx=$(pwgen -N 1 -s 5)
#this is the common idiom for finding the PID of a subshell without using a bash4 specific feature.
( : ; echo $PPID > /tmp/curl_$xxx.pid; curl "$url" 1>/tmp/curl_$xxx.out 2>/dev/null) &
sleep 1
kill $(cat /tmp/curl_$xxx.pid) 2>/dev/null
sleep .5
ffprobe /tmp/curl_$xxx.out 2>&1 | tail -1 | grep -q Audio && valid_radio=true
rm /tmp/curl_$xxx.pid
$valid_radio && rm /tmp/curl_$xxx.out
}
get_radio()
{
i=0
for url in $@
do
test_radio "$url"
if $valid_radio
then
((i++))
if [[ $i -eq 1 ]]
then
echo "$url" > "$file_name"
else
echo "$url" >> "$file_name"
fi
else
echo "Invalid URL:"
echo "$url"
fi
done
if [[ $i -eq 0 ]]
then
echo "$file_name could not be updated, because all of its urls are invalid."
fi
}
while read station
do
url=$(echo "$station" | cut -d ' ' -f 2)
file_name=$dir/$(echo "$station" | cut -d ' ' -f 1)
if [[ -e "$file_name" ]]
then
music_urls=$(curl -sL "$url" | tr -d '\r' | grep -o 'http[^;]*')
url_sha=$(echo "$music_urls"| shasum | cut -d ' ' -f 1)
file_sha=$(cat "$file_name" | shasum | cut -d ' ' -f 1)
if [[ "$url_sha" == "$file_sha" ]]
then
echo "$file_name is up-to-date"
else
true
echo "updating $file_name"
get_radio $music_urls
fi
else
echo "getting $file_name"
get_radio $music_urls
fi
done < ~/.radio-station-urls