-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundbash
executable file
·205 lines (185 loc) · 4.77 KB
/
soundbash
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env bash
# Checks if a page is a single track page or not.
#
# Input:
# $1 - URL of the page
# Return: 0 if the page is a single track page, 1 otherwise
is_single_track_page() {
wget -O - -- "$1" 2>/dev/null | grep '<meta content="soundcloud:sound"' >/dev/null 2>&1
}
# Extracts all stream URLs from a given page.
#
# Input:
# $1 - URL of the page
# Output:
# stdout - streaming URLs separated by newlines
get_stream_URLs() {
wget -O - -- "$1" 2>/dev/null | sed -ne '/window\.SC\.bufferTracks\.push/{s/.*uri":"\([^"]*\)".*streamUrl":"\([^"?]*\).*/\2?\1/;p;}'
}
# Extracts the page count for a page.
#
# Input:
# $1 - URL of the page
# Output:
# stdout - number of pages or an empty string if there aren’t multiple pages
get_page_count() {
local count="$(wget -O - -- "$1" 2>/dev/null | grep -o 'page=[0-9]*' | cut -d= -f 2 | sort -g | tail -n 1)"
if ! (( count >= 1 )); then
count=1
fi
echo "$count"
}
# Shows a description of the script and a list of parameters
#
# Output:
# stdout - short description and a list of parameters
show_help() {
cat <<EOF
Usage: soundbash soundcloud_link
This utility extracts all SoundCloud streaming URLs from a given page. It should work with pretty much everything (track URLs, user favorites, searches).
The streaming URL contains the track path as a parameter. Prepend the part after the quotation mark with http://soundcloud.com to get the track URL.
Parameters:
-h -? print this help and exit.
-m number limits the output to a maximum of number pages. Use 0 or a negative number to fetch all results. Default: 10
-l URL extract the streaming URLs from the given link.
-u user extract the streaming URLs from the track page of the given user
-f user extract the streaming URLs from the favorites page of the given user
-g group extract the streaming URLs from a group
EOF
}
# Extracts the streaming URL from a single track page.
#
# Input:
# $1 - URL
# Output:
# stdout - the streaming URL
get_single_track_URL() {
get_stream_URLs "$1" | head -n 1
}
# Extracts streaming URLs from a multi track page. A maximum number of pages can be specified.
#
# Input:
# $1 - URL
# $2 - maximum number of pages. Pass nothing, a number <= 0 or a string to get all pages.
# Output:
# stdout - the streaming URLs from the page
get_multi_track_URLs() {
count="$(get_page_count "$1")"
if ! (( count >= 1 )); then
count=1
fi
if (( "$2" >= 1 && "$2" < count )); then
count="$2"
fi
for (( i=1; i <= count; i++ )); do
get_stream_URLs "${1}?page=${i}"
done
}
# Extracts streaming URLs from a page. If a single track page is given, only the URL for the track is returned, otherwise as many track pages as
# specified are returned.
#
# Input:
# $1 - URL
# $2 - maximum number of pages to extract streaming URLs from
# Output:
# stdout - the streaming URL(s) for the track(s)
handle_url() {
if is_single_track_page "$1"; then
get_single_track_URL "$1"
else
get_multi_track_URLs "$1" "$2"
fi
}
# Extracts streaming URLs from the tracks page of a given user.
#
# Input:
# $1 - user name
# $2 - maximum number of pages to extract
# Output:
# stdout - the streaming URLs
handle_user() {
get_multi_track_URLs "http://soundcloud.com/${1}/tracks" "$2"
}
# Extracts streaming URLs from the favorites page of a given user.
#
# Input:
# $1 - user name
# $2 - maximum number of pages to extract
# Output:
# stdout - the streaming URLs
handle_favorites() {
get_multi_track_URLs "http://soundcloud.com/${1}/favorites" "$2"
}
# Extracts streaming URLs from a given group.
#
# Input:
# $1 - the group name
# $2 - maximum number of pages to extract
# Output:
# stdout - the streaming URLs
handle_group() {
get_multi_track_URLs "http://soundcloud.com/groups/${1}/tracks" "$2"
}
max_pages=10
while (( $# > 0 )); do
if [[ "${1:0:1}" == "-" ]]; then
params="${1:1}"
shift
while (( ${#params} > 0 )); do
case "${params:0:1}" in
h|\?)
show_help
exit 0
;;
m)
if (( $# < 1 )); then
echo "-m needs a parameter" >&2
exit 1
fi
max_pages="$1"
shift
;;
u)
if (( $# < 1 )); then
echo "-u needs a parameter" >&2
exit 1
fi
handle_user "$1" "$max_pages"
shift
;;
f)
if (( $# < 1 )); then
echo "-f needs a parameter" >&2
exit 1
fi
handle_favorites "$1" "$max_pages"
shift
;;
g)
if (( $# < 1 )); then
echo "-g needs a parameter" >&2
exit 1
fi
handle_group "$1" "$max_pages"
shift
;;
l)
if (( $# < 1 )); then
echo "-l needs a parameter" >&2
exit 1
fi
handle_url "$1" "$max_pages"
shift
;;
*)
echo "Unknown parameter: ${params:0:1}" >&2
exit 1
;;
esac
params="${params:1}"
done
else
handle_url "$1" "$max_pages"
shift
fi
done