-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikimusic
executable file
·438 lines (364 loc) · 12.1 KB
/
wikimusic
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#!/bin/bash
prog="$(echo "$0" | sed -e 's_./__g' -e 's/\/usbi//g' )"
red='\033[0;31m'
nc='\033[0m'
cyan='\033[1;36m'
bold='\033[0;1m'
underline='\033[0;4m'
tmpdir="/tmp/wikimusic"
base_pages_url="https://en.wikipedia.org/wiki/Wikipedia:List_of_sound_files/"
pages=('A' 'Baa%E2%80%93Bac' 'Bac%E2%80%93Baz' 'Bba%E2%80%93Bee' 'Bef%E2%80%93Bzz' 'C' 'D%E2%80%93E' 'F%E2%80%93G' 'H' 'I%E2%80%93L' 'M' 'N%E2%80%93Q' 'R' 'S' 'T%E2%80%93Z')
check_tmp()
{
[[ ! -d "$tmpdir" ]] && mkdir -p $tmpdir
}
tmp_rm()
{
rm -Rf $tmpdir
}
error()
{
echo -e "$red"
echo "$error_msg"
echo -e "$nc"
}
playlist_links()
{
for i in ${pages[@]}
do
echo "$base_pages_url$i?action=raw"
done
}
random_playlist_link()
{
check_tmp
playlist_links > $tmpdir/r_sel_playlist
cat $tmpdir/r_sel_playlist | \
sort -R | \
head -n$((1+$RANDOM%15)) | \
tail -n1
tmp_rm
}
selected_playlist_link()
{
[[ -z "$usr_inp" ]] && random_playlist_link && exit 0
[[ "$usr_inp" -eq 0 ]] || [[ "$usr_inp" < 0 ]] && random_playlist_link && exit 0
check_tmp
playlist_links > $tmpdir/sel_playlist
cat $tmpdir/sel_playlist | \
head -n$usr_inp | \
tail -n1
tmp_rm
}
page_selection_menu()
{
echo -e "$underline[Select Playlist]$nc"
num=1
for i in ${pages[@]}
do
echo -e "$num)$bold Playlist-($i) $nc" | sed 's/%E2%80%93/-/g'
num="$(($num+1))"
done
echo -e "$nc\c"
}
menu_input()
{
read -p "==> " -r usr_inp
selected_playlist_link || random_playlist_link
}
fetch_playlist()
{
check_tmp
curl -s "$url" --compressed | \
grep -o "File:.*" | \
sed "s/|namespace=0}}//g" | \
sed 's/[[:space:]]/_/g' | \
sed -e 's/File_/File:/g' -e 's/.ogg_/.ogg/g' \
-e 's_File:_https://en.wikipedia.org/wiki/File:_g' | \
cat -n | \
sed -e 's/^[ 0-9\t]*//g' | cat -n | sed 's/^[ \t]*//g' > $tmpdir/song_lists
}
display_info()
{
total_songs="$(cat $tmpdir/song_lists | wc -l)"
[[ -z "$total_songs" ]] && error_msg="Unable To Fetch Links!" && error && exit
selected_page="$( echo "$url" | \
sed 's@https://en.wikipedia.org/wiki/Wikipedia:List_of_sound_files/@@g' | \
sed "s/?action=raw//g" | sed 's/%E2%80%93/-/g')"
echo -e "$red\c"
echo -e "Total [$total_songs] Songs In Playlist-[$selected_page]$nc"
[[ ! -z "$selected_link" ]] && echo "URL : $selected_link"
}
display_options()
{
display_info
echo "==> a [Randomly Play Loop]"
echo "==> c [Play a random song from the selected Playlist-($selected_page)]"
echo "==> r [Play a song randomly from any playlist]"
echo "==> s [Select Manually]"
echo "==> m [Main Menu]"
echo "==> q [Quit]"
}
check_bat()
{
[[ -f "/usr/bin/bat" ]] && CAT="bat --force-colorization --file-name=Music-List" || CAT="less"
echo $CAT
}
create_log(){
mkdir -p $HOME/.cache/wikimusic
echo "$selected_link" >> $HOME/.cache/wikimusic/songs_history
echo "$(Selectedtitle)" >> $HOME/.cache/wikimusic/songs_titles
sed -i '/^[[:space:]]*$/d' $HOME/.cache/wikimusic/songs_titles
}
Selectedtitle()
{
selected_title="$(head -n$select_manual $tmpdir/song_lists | tail -n1 | awk '{print $2}' | \
sed 's_https://en.wikipedia.org/wiki/File:__g' | \
sed -e 's/.ogg//g' -e 's/^_//g' -e 's/_/ /g' )"
echo "$selected_title"
}
manual_selection(){
(cat $tmpdir/song_lists | \
sed 's_https://en.wikipedia.org/wiki/File:__g' | \
sed -e 's/.ogg//g' -e 's/^_//g' -e 's/_/ /g' | \
$( check_bat ))
read -r -p "==> " select_manual
case $select_manual in
q)
error_msg="Exiting!"
error
exit
;;
"")
ifr="-Random"
(pkill -f wikimusic_mpv & > /dev/null 2>&1)
rand="$((1+$RANDOM%$(wc -l $tmpdir/song_lists | awk '{print $1}')))"
selected_link="$(head -n$rand $tmpdir/song_lists | tail -n1 | awk '{print $2}')"
select_manual="$rand"
echo -e "$cyan\c"
echo -e "Playing$ifr [$(Selectedtitle)]$nc"
(nohup mpv --really-quiet --title="wikimusic_mpv" "$selected_link" & > /dev/null 2>&1 && disown > /dev/null 2>&1) > /dev/null 2>&1
create_log
;;
*)
if [ "$select_manual" -gt "$(wc -l $tmpdir/song_lists | awk '{print $1}')" ];then
rand="$((1+$RANDOM%$(wc -l $tmpdir/song_lists | awk '{print $1}')))"
selected_link="$(head -n$rand $tmpdir/song_lists | tail -n1 | awk '{print $2}')"
select_manual="$rand"
ifr="-Randomly"
fi
selected_link="$(head -n$select_manual $tmpdir/song_lists | tail -n1 | awk '{print $2}')"
(pkill -f wikimusic_mpv & > /dev/null 2>&1)
echo -e "$cyan\c"
echo -e "Playing$ifr [$(Selectedtitle)]$nc"
(nohup mpv --really-quiet --title="wikimusic_mpv" "$selected_link" & > /dev/null 2>&1 && disown > /dev/null 2>&1) > /dev/null 2>&1
create_log
;;
esac
}
song_list_menu(){
while true
do
display_options
read -n1 -s options
case $options in
r)
url=$( random_playlist_link )
fetch_playlist
rand="$((1+$RANDOM%$(wc -l $tmpdir/song_lists | awk '{print $1}')))"
select_manual="$rand"
selected_link="$(head -n$rand $tmpdir/song_lists | tail -n1 | awk '{print $2}')"
(pkill -f wikimusic_mpv & > /dev/null 2>&1)
echo -e "$cyan\c"
echo -e "Selected Randomly : Playing [$(Selectedtitle)]$nc"
(nohup mpv --really-quiet --title="wikimusic_mpv" "$selected_link" & > /dev/null 2>&1 && disown > /dev/null 2>&1) > /dev/null 2>&1
create_log
song_list_menu
;;
a)
echo "==> q [Change Current Song]"
echo "==> qq [Exit From Loop]"
echo "==> qqq [Exit]"
echo "==> qm [Main Menu]"
(pkill -f wikimusic_mpv & > /dev/null 2>&1)
while true
do
url=$( random_playlist_link )
fetch_playlist
rand="$((1+$RANDOM%$(wc -l $tmpdir/song_lists | awk '{print $1}')))"
select_manual="$rand"
selected_link="$(head -n$rand $tmpdir/song_lists | tail -n1 | awk '{print $2}')"
select_manual="$((1+$RANDOM%$total_songs))"
display_info
echo -e "$cyan\c"
echo -e "Selected Randomly : Playing [$(Selectedtitle)] From Playlist [$selected_page]$nc"
mpv --really-quiet --aid=2 --title="wikimusic_mpv" "$selected_link"
create_log
read -t 0.25 -n1 -s keypress
case $keypress in
q)
break
;;
m)
initial_menu
;;
esac
done
;;
s)
manual_selection
song_list_menu
;;
c)
rand="$((1+$RANDOM%$(wc -l $tmpdir/song_lists | awk '{print $1}')))"
select_manual="$rand"
selected_link="$(head -n$rand $tmpdir/song_lists | tail -n1 | awk '{print $2}')"
(pkill -f wikimusic_mpv & > /dev/null 2>&1)
echo -e "$cyan\c"
echo -e "Selected Random : Playing [$(Selectedtitle)] From Playlist [$selected_page]$nc"
(nohup mpv --really-quiet --title="wikimusic_mpv" "$selected_link" & > /dev/null 2>&1 && disown > /dev/null 2>&1) > /dev/null 2>&1
create_log
song_list_menu
;;
q)
(pkill -f wikimusic_mpv & > /dev/null 2>&1)
error_msg="Exiting!"
error
tmp_rm
exit
;;
m)
initial_menu
;;
*)
echo "[$options] : No Such Option Available!"
song_list_menu
;;
esac
done
}
initial_menu()
{
page_selection_menu
url=$(menu_input)
fetch_playlist
selected_link=""
song_list_menu
}
show_history()
{
while true
do
[[ ! -f "$HOME/.cache/wikimusic/songs_history" ]] && echo "No History Yet!" && exit
[[ ! -f "$HOME/.cache/wikimusic/songs_titles" ]] && echo "No History Yet!" && exit
clear
echo -e "$bold$cyan$BANNER$nc"
echo -e "\n$underline[Last-History]$nc\n"
cat -n $HOME/.cache/wikimusic/songs_titles | tail -7
echo -e "\n==> m [Main Menu]"
echo "==> s [Show-List]"
echo "==> q [Quit]"
read -n1 -s number
[[ -z "$number" ]] && number="$((1+$RANDOM%$(wc -l $HOME/.cache/wikimusic/songs_titles | awk '{print $1}')))"
case $number in
q)
(pkill -f wikimusic_mpv & > /dev/null 2>&1)
error_msg="Exiting!"
error
exit
;;
m)
initial_menu
break
exit 0
;;
s)
(cat $HOME/.cache/wikimusic/songs_titles | sed 's_https://en.wikipedia.org/wiki/File:__g' | sed -e 's/.ogg//g' -e 's/^_//g' -e 's/_/ /g' | $( check_bat ) )
read -p "==> " playin_from_his
[[ "$playin_from_his" = "q" ]] && \
(pkill -f wikimusic_mpv & > /dev/null 2>&1) && \
error_msg="Exiting!" && \
error && break && exit 0
[[ -z "$playin_from_his" ]] && \
echo "==> [Playing Random From History]" && \
playin_from_his="$((1+$RANDOM%$(wc -l $HOME/.cache/wikimusic/songs_titles \
| awk '{print $1}')))"
(pkill -f wikimusic_mpv & > /dev/null 2>&1)
selected="$(cat $HOME/.cache/wikimusic/songs_history | head -n$playin_from_his | tail -n1 )"
selected_title="$(cat $HOME/.cache/wikimusic/songs_titles | head -n$playin_from_his | tail -n1 | \
sed 's_https://en.wikipedia.org/wiki/File:__g' | \
sed -e 's/.ogg//g' -e 's/^_//g' -e 's/_/ /g' )"
echo "==> Playing [$selected_title]"
URL_BANNER="URL: $selected"
(nohup mpv --really-quiet --title="wikimusic_mpv" $selected & > /dev/null 2>&1 && disown > /dev/null 2>&1) > /dev/null 2>&1
BANNER="Now Playing [$selected_title]"
create_log
;;
*)
(pkill -f wikimusic_mpv & > /dev/null 2>&1)
selected="$(cat $HOME/.cache/wikimusic/songs_history | head -n$number | tail -n1 )"
selected_title="$(cat $HOME/.cache/wikimusic/songs_titles | head -n$number | tail -n1 | \
sed 's_https://en.wikipedia.org/wiki/File:__g' | \
sed -e 's/.ogg//g' -e 's/^_//g' -e 's/_/ /g' )"
echo "==> Playing [$selected_title]"
URL_BANNER="URL: $selected"
(nohup mpv --really-quiet --title="wikimusic_mpv" $selected & > /dev/null 2>&1 && disown > /dev/null 2>&1) > /dev/null 2>&1
BANNER="Now Playing [$selected_title]"
create_log
;;
esac
done
}
show_help()
{
cat<<END
$prog [USAGE]: [OPTIONS]
$prog -r - Play Random Songs Until You Stop
$prog -H - Show History
$prog -h, $prog --help - Show Help
Example:
$prog
Note: The Application Provides an interactiva Menu!
[MISC]
Cache Directory : $HOME/.cache/wikimusic
TMP_DIRECTORY : /tmp/wikimusic
END
}
when_you_dont_know_coding(){
echo "==> q [Change Current Song]"
echo "==> qq [Exit]"
echo "==> qm [Main Menu]"
(pkill -f wikimusic_mpv & > /dev/null 2>&1)
while true
do
url=$( random_playlist_link )
fetch_playlist
rand="$((1+$RANDOM%$(wc -l $tmpdir/song_lists | awk '{print $1}')))"
select_manual="$rand"
selected_link="$(head -n$rand $tmpdir/song_lists | tail -n1 | awk '{print $2}')"
select_manual="$((1+$RANDOM%$(wc -l $tmpdir/song_lists | awk '{print $1}')))"
display_info
echo -e "$cyan\c"
echo -e "Selected Randomly : Playing [$(Selectedtitle)] From Playlist [$selected_page]$nc"
mpv --really-quiet --aid=2 --title="wikimusic_mpv" "$selected_link"
create_log
read -t 0.30 -n1 -s keypress
case $keypress in
q)
(pkill -f wikimusic_mpv & > /dev/null 2>&1)
error_msg="Exiting!"
error
break
;;
m)
initial_menu
break && exit 0
;;
esac
done
}
[[ -z "$1" ]] && initial_menu
[[ "$1" = "-r" ]] && when_you_dont_know_coding
[[ "$1" = "-H" ]] && show_history
[[ "$1" = "-h" ]] || [[ "$1" = "--help" ]] && show_help
[[ "$1" = "-v" ]] || [[ "$1" = "--version" ]] && echo " $prog : v0"