-
Notifications
You must be signed in to change notification settings - Fork 18
/
mpvc-fzf
executable file
·533 lines (471 loc) · 16.9 KB
/
mpvc-fzf
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#!/usr/bin/env sh
#
# @file mpvc-fzf
# @description mpvc fzf, mpv terminal user interface
# @author gmt4 <gmt4 at github.com> (c) Copyright 2022 GPLv2+
# @url github.com/gmt4/mpvc
# SPDX-License-Identifier: GPL-2.0-or-later
#
PROGNAME=${0##*/}
PROGDIR=${0%/*}
PROGVERSION="v1.5"
PROGAUTHOR=gmt4
PROGURL="https://github.com/gmt4/mpvc"
set -euf
quiet() { "$@" > /dev/null 2>&1; }
mpvcfzf_warn() { echo "$@" >&2; }
mpvcfzf_awk0() { awk '{print $NF}'; }
mpvcfzf_awk1() { awk '{print $1}'; }
mpvc() { "$PROGDIR/mpvc" "$@"; } # mpvc-web?
# Set defaults
mpvcfzf_defaults()
{
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-"$HOME/.config"}
MPVC_CONFIG_DIR="${MPVC_CONFIG_DIR:-$XDG_CONFIG_HOME/mpvc}"
MPVC_CONFIG="${MPVC_CONFIG:-$MPVC_CONFIG_DIR/mpvc.conf}"
MPVC_YTDL_ARCHIVE=${MPVC_YTDL_ARCHIVE:-}
MPVC_YTDL_OUTPUT=${MPVC_YTDL_OUTPUT:-}
MPVC_TERM=${MPVC_TERM:-xterm}
MPVC_FZF_SOMAFM_FILE="${MPVC_FZF_SOMAFM_FILE:-$MPVC_CONFIG_DIR/somafm-channels.json}"
MPVC_FZF_RADIOAPI_FILE="${MPVC_FZF_RADIOAPI_FILE:-$MPVC_CONFIG_DIR/radioapi-stations.json}"
#mpvc -q
}
mpvcfzf_config()
{
mpvcfzf_defaults
if [ ! -d "$MPVC_CONFIG_DIR" ]; then mkdir -p "$MPVC_CONFIG_DIR"; fi
# if present load user config
if [ -r "$MPVC_CONFIG" ]; then . "$MPVC_CONFIG"; fi
if ! quiet command -v fzf; then
mpvcfzf_warn "$PROGNAME: Warning: No fzf, Install to get fuzzy media search & play support!"
sleep 1
fi
}
mpvcfzf_curl()
{
curl -fsSL -A 'Mozilla/5.0' --retry 1 "$@"
}
mpvcfzf_getvideo_id()
{
echo "${1##*/}"
}
mpvcfzf_isearch_once()
{
mpvcfzf_isearch "$@" | awk 'NR==2 {sub(" +", " # "); print}'
}
mpvcfzf_isearch()
{
url_host="${url_host:-yewtu.be}"
url_sort="sort=relevance" # relevance views date
url_query="q=$(for f in $@; do printf "%s+" "$f"; done)"
url_search="https://${url_host}/search?$url_query&$url_sort"
echo "$url_search"
mpvcfzf_curl "$url_search&page=1" "$url_search&page=2" |
awk '
/^.*<p dir="auto">/ {
gsub("^.*<p dir=\"auto\">",""); gsub("</p>.*$",""); vtitle=$0
}
/<p class="video-data" dir="auto">/ {
gsub("^ *<p class=\"video-data\" dir=\"auto\">",""); gsub(" views",""); gsub("</p>",""); shared=$0
}
/^ *<p class="length">/ {
gsub("^ *<p class=\"length\">",""); gsub("</p>",""); vlength=$0
}
/^ *<a title="Audio mode/ {
gsub("^ *<a title=\"Audio mode\" href=\"",""); gsub("\">","");
gsub("/watch\\?v=", "https://yewtu.be/"); gsub("&listen=1","");
printf "%s %4s %7s %s\n", $0, shared, vlength, vtitle
}
END {
if (NR == 0)
{
print "Error: No results found, maybe yewtu.be is down, try switching to a different instance with:"
print " url_host=invidious.projectsegfau.lt mpvc-fzf"
}
}
'
}
mpvcfzf_related_search()
{
id=${1:-$(mpvc get filename)}
id=$(mpvcfzf_getvideo_id "$id")
output=$(mpvcfzf_curl "https://yewtu.be/redirect?referer=/watch?v=$id")
echo "$output" |
awk '
/<div class="video-card-row">/ { c++; next }
c && c-- {
sub("^ *<a href=\"/watch\\?v=","https://yewtu.be/");
sub("&listen=false\"><p dir=\"auto\">"," ");
sub("</p></a>$","");
print $0
}'
}
mpvcfzf_related_play()
{
mpvcfzf_related_search "$1" |
mpvcfzf_preview --prompt="(similar) " |
mpvcfzf_awk1
}
mpvcfzf_lyrics()
{
id=${1:-$(mpvc get filename)}
id=$(mpvcfzf_getvideo_id "$id")
output=$(mpvcfzf_curl "https://yewtu.be/redirect?referer=/watch?v=$id")
echo "$output" |
awk -v id=$id '
c==0 && /<meta name="description"/ {c=1}
c==1 && /^[Ll][Yy][Rr][Ii][Cc][Ss]/ {c=2}
c==2 && /<meta name="keywords"/ {c=0;exit}
c==2 {print}
END {if (c==1) print "Lyrics: No lyrics found for",id}
'
}
mpvcfzf_lyrics_ddg()
{
echo "https://ddg.gg?ia=lyrics&iax=lyrics&q=lyrics $(mpvc -f '%title%')"
}
mpvcfzf_nextvideo()
{
id=$(mpvcfzf_getvideo_id "$1")
output=$(mpvcfzf_curl "https://yewtu.be/watch?v=$id")
next_video=$(echo "$output" | awk '/next_video/ {gsub("[\",]",""); print $2}')
next_url="https://yewtu.be/$next_video"
echo "$next_url"
}
mpvcfzf_icache_ytdlro() { mpvc getr ytdl-raw-options; }
mpvcfzf_icache_archive() {
if [ -n "$MPVC_YTDL_ARCHIVE" ]; then
echo "$MPVC_YTDL_ARCHIVE"
else
mpvcfzf_icache_ytdlro | jq -r ".data.download-archive"
fi
}
mpvcfzf_icache_output() { mpvcfzf_icache_ytdlro | jq -r ".data.output"; }
mpvcfzf_icache_output_dir() {
if [ -n "$MPVC_YTDL_OUTPUT" ]; then
echo "$MPVC_YTDL_OUTPUT"
else
MPVC_YTDL_OUTPUT=$(mpvcfzf_icache_output)
MPVC_YTDL_OUTPUT=${MPVC_YTDL_OUTPUT%/*}
eval echo $MPVC_YTDL_OUTPUT
fi
}
mpvcfzf_icache()
{
# check for media present in local cache
#ytdl_archive="$(mpvcfzf_icache_archive)"
ytdl_output="$(mpvcfzf_icache_output_dir)"
ytdl_dir="$ytdl_output"
if [ ! -n "$ytdl_output" -o ! -n "$ytdl_output" ]; then
#quiet mpvc get ytdl-raw-options || cat
mpvcfzf_warn "$PROGNAME: Error cache not enabled: undefined ytdl-raw-options"
if cat; then return; fi
fi
while read -r line
do
url=$(echo "$line" | awk '/https:/ { gsub("watch\\?v=",""); print $1 }')
vid=$(basename "$url")
file=$(find $ytdl_dir/ 2>/dev/null | awk "/$vid/ { print; exit }")
if [ -n "$url" -a -e "$file" ]; then
echo "$file"
else
echo "$line"
fi
done
}
mpvcfzf_fetch()
{
if [ $# -lt 1 ]; then mpvcfzf_usage; fi
ytdl_opts=""
ytdlp=$(command -v youtube-dl || command -v yt-dlp)
if [ ! -x "$ytdlp" ]; then
mpvcfzf_warn "$PROGNAME: Warning: No yt-dlp, Install to get ytdl support!"
fi
mpvc -q --mpv
ytdl_json=$(mpvc getr ytdl-raw-options)
if [ -z "$ytdl_json" ]; then
mpvcfzf_warn "$PROGNAME: Error ytdl-raw-options not set. Check docs/mpv.conf"
return
fi
for option in $(echo "$ytdl_json" | jq -r ".data | keys | .[]")
do
value=$(echo "$ytdl_json" | jq -r ".data.\"$option\"")
if [ -n "$value" ]
then
ytdl_opts="$ytdl_opts --$option=$value"
else
ytdl_opts="$ytdl_opts --$option"
fi
done
"$ytdlp" $ytdl_opts ${YTDL_OPTS:-} "$@"
}
mpvcfzf_fetch_remove()
{
if [ -z "$1" ]; then
mpvcfzf_warn "$PROGNAME: Error missing ytdl-video-id to remove"
return
fi
ytdl_id="$1"
ytdl_json=$(mpvc getr ytdl-raw-options)
ytdl_archive=$(echo $ytdl_json | jq -r '.data."download-archive"')
ytdl_output=$(echo $ytdl_json | jq -r ".data.output")
ytdl_archive=$(eval echo $ytdl_archive)
ytdl_dir=$(dirname $ytdl_output)
ytdl_dir=$(eval echo $ytdl_dir)
if [ -z "$ytdl_json" -o ! -r "$ytdl_archive" -o ! -n "$ytdl_output" ]; then
mpvcfzf_warn "$PROGNAME: Error cache not enabled: undefined ytdl-raw-options"
return
fi
rm -i "$(find "$ytdl_dir" | grep "$ytdl_id")"
sed -i "/$ytdl_id/ s|^|#|" "$ytdl_archive"
}
mpvcfzf_preview()
{
MPVC_FZF_BIND_HELP="C-x:list C-v:yt-dlp C-\\:local C-s:chapter C-t:add C-o:play C-r:reload C-spc:toggle"
# --bind='ctrl-d:execute(mpvc {q})'
# --bind='left:execute-silent(mpvc -q seek -20 &)' \
# --bind='right:execute-silent(mpvc -q seek +20 &)' \
# fzf keys: ctrl + [dorstxv\]
fzf \
--ansi \
--no-sort \
--multi \
\
--layout=reverse \
--header="$PROGNAME help: $MPVC_FZF_BIND_HELP" \
--bind='ctrl-space:execute-silent(mpvc toggle &)' \
--bind='ctrl-t:execute-silent(mpvc add {+1} &)' \
--bind='ctrl-s:execute(mpvc-fzf -c)' \
--bind='ctrl-x:execute(mpvc-fzf -f {q})' \
--bind='ctrl-v:execute(mpvc-fzf -p {q})' \
--bind='ctrl-\:execute(mpvc-fzf -l)' \
\
--preview-window='up:5' \
--preview="mpvc" \
"$@"
}
mpvcfzf_iplay()
{
mpvcfzf_isearch "$1" |
mpvcfzf_preview --prompt="(youtube) " |
mpvcfzf_awk1
}
mpvcfzf_local()
{
find . -type f,d | sort |
mpvcfzf_preview --prompt="(local) " |
mpvc load
}
mpvcfzf_playlist()
{
len=$(mpvc gets playlist-count)
(if [ -z "$len" ]; then echo; else mpvc fullplaylist; fi) |
mpvcfzf_preview --prompt="(playlist) " \
--bind='ctrl-o:execute-silent(mpvc play {1})' \
--bind='ctrl-r:reload(mpvc fullplaylist)'
}
mpvcfzf_chapterlist()
{
len=$(mpvc gets chapter-list/count)
( if [ -z "$len" ]; then echo; else mpvc chapter-list-nr; fi) |
mpvcfzf_preview --prompt="(chapter) " \
--bind='ctrl-o:execute-silent(mpvc set chapter {1})' \
--bind='ctrl-r:reload(mpvc chapter-list-nr)'
}
mpvcfzf_stashlist()
{
mpvc stash list | mpvcfzf_awk0 |
mpvcfzf_preview --prompt="(stash) " \
--bind='ctrl-o:execute-silent(mpvc stash apply {+} &)' \
--bind='ctrl-r:reload(mpvc stash list | mpvc-fzf -0)'
}
mpvcfzf_ddg_play()
{
mpvcfzf_ddg "$@" |
mpvcfzf_preview --prompt="(ddg) " |
mpvcfzf_awk1 |
mpvc load
}
mpvcfzf_urlget()
{
url=${1:-https://localhost:8443/ytdl-archive/}
mpvcfzf_curl "$url" |
awk '/<a href=".*">.*<\/a>/ { h=$0;sub("^.*href=\"","",h);sub("\".*$","",h);print h }'
}
mpvcfzf_urlbrowser()
{
url=${1:-https://localhost:8443/ytdl-archive/}
mpvcfzf_urlget "$url" |
mpvcfzf_preview --prompt="(urlbrowse) " |
awk -v url="$url" '{ print url$0 }'
}
mpvcfzf_equalizer()
{
MPVC_FZF_BIND_HELP="C-x:list C-v:yt-dlp C-\\:local C-s:chapter C-t:add C-o:play C-r:reload C-spc:toggle"
mpvc-equalizer preset |
mpvcfzf_preview --prompt="(equalizer) " \
--header="$PROGNAME help: $MPVC_FZF_BIND_HELP" \
--bind='ctrl-x:execute(mpvc-equalizer dec {1} &)' \
--bind='ctrl-v:execute(mpvc-equalizer inc {1} &)' \
--bind='ctrl-space:execute-silent(mpvc-equalizer reset)' \
--bind='ctrl-o:execute-silent(mpvc-equalizer preset {-1} | mpvc-equalizer load)' \
--bind='ctrl-s:reload(mpvc-equalizer bars)' \
--bind='ctrl-r:reload(mpvc-equalizer preset)' \
"$@"
}
mpvcfzf_ddg()
{
url_search="https://duckduckgo.com/html?q=$*&iax=videos"
echo "$url_search"
mpvcfzf_curl "$url_search" |
awk '
function urldecode(url)
{
for (y=0; y < 127; y++)
{
if (y != 37)
gsub(sprintf("%%%02x|%%%02X", y, y), y==38 ? "\\&" : sprintf("%c", y), url);
gsub(/%25/, "%", url);
}
return url
}
/result__a/ {
gsub("<a rel=\"nofollow\" class=\"result__a\" href=\"//duckduckgo.com/l/\\?uddg=", "");
gsub("&", "\\&");
gsub("rut=[0-9a-f]+","");
gsub("\">", " ");
gsub("</a>","");
gsub("%3A",":"); gsub("%2F","/"); gsub("%3F","?"); gsub("%3D","=");
gsub("^ *","");
#print urldecode($0)
print
}'
}
mpvcfzf_somafm()
{
chan=${1:-}
if [ -n "$chan" ]; then
mpvcfzf_somafm_search | awk -F';' -v chan="$chan" '{ if(match($0,chan))print $1}' | mpvc load
else
mpvcfzf_somafm_search | column -s ';' -t | mpvcfzf_preview --prompt="(somafm) "
fi
}
mpvcfzf_somafm_search()
{
url="https://somafm.com/channels.json"
file="$MPVC_FZF_SOMAFM_FILE"
[ ! -s "$file" ] && mpvcfzf_curl -o "$file" "$url"
jq -r --argjson quality 3 '
.channels | sort_by(.listeners | tonumber) | reverse | .[] |
(.playlists[$quality]|.url) + ";" + .listeners + " ls "+.genre+";"+ .title' "$file"
}
mpvcfzf_radioapi()
{
chan=${1:-}
if [ -n "$chan" ]; then
mpvcfzf_radioapi_search | awk -F';' -v chan="$chan" '{ if(match($0,chan))print $1}' | mpvc load
else
mpvcfzf_radioapi_search | column -s ';' -t | mpvcfzf_preview --prompt="(radioapi) "
fi
}
mpvcfzf_radioapi_search()
{
url="https://nl1.api.radio-browser.info/json/stations/topvote"
file="$MPVC_FZF_RADIOAPI_FILE"
[ ! -s "$file" ] && mpvcfzf_curl --retry 2 -d hidebroken=true -d limit=4096 -o "$file" "$url"
jq -r -c '.[] | select( (.url|length) < 80 ) | (.url+";"+(.votes|tostring)+" "+.name)' "$file"
}
mpvcfzf_now_geturls() { mpvc saven; }
mpvcfzf_now_getvids() { echo "$@" | awk '/youtube|yewtu/ { sub("^.*/",""); sub(".*=",""); sub("^","\\&v="); printf $0; }'; }
mpvcfzf_now()
{
date=$(date -Idate)
now="https://gmt4.github.io/mpvc/now/"
urls=$(mpvcfzf_now_geturls)
vids=$(mpvcfzf_now_getvids "$urls")
list=$(mpvc fullplaylist)
echo "$now?t=$date$vids"
echo
awk -v u="$urls" -v l="$list" 'END{ split(u, au,"\n"); split(l, al,"\n"); for(u in au) {print au[u], al[u]}; }' /dev/null
}
mpvcfzf_launch_term()
{
if quiet command -v "$MPVC_TERM"; then "$MPVC_TERM" -e "$0" "$@"; fi;
}
mpvcfzf_version()
{
echo "usage: $PROGNAME opts # @version $PROGVERSION (c) $PROGAUTHOR $PROGURL"
}
mpvcfzf_usage()
{
echo "usage: $PROGNAME opts # @version $PROGVERSION (c) $PROGAUTHOR $PROGURL"
echo " -b|browse : Browse the provided ytdl-archive URL with fzf"
echo " -c|chapters : Start fzf to manage the current mpv chapterlist"
echo " -d|dir : Set the WD to the media directory given as argument"
echo " -e|eqz : Start fzf to manage the equalizer settings"
echo " -f|playlist : Start fzf to manage the current mpv playist"
echo " -g|fetch : Fetch the given YT URL, and store locally"
echo " -G|Fetch : Search on Invidious, fetch, and store locally"
echo " -i|lyrics : Search given media lyrics on Invidious"
echo " -k|dplay : Search & play DuckDuckGo videos"
echo " -K|dsearch : Search DuckDuckGo videos"
echo " -l|local : Search & play local media"
echo " -s|search : Search on Invidious"
echo " -p|splay : Search & play media found using Invidious"
echo " -y|related : Search related media on Invidious"
echo " -Y|Related : Search & play related media using Invidious"
echo " -x|launch : Starts $PROGNAME in a new $MPVC_TERM (config \$MPVC_TERM) [combine -x with other opts]"
echo " -v|version : Return the $PROGNAME version."
echo " now : Return a shareable URL to the "now listening" playlist"
echo " somafm : Search & play SomaFM channels"
echo " radioapi : Search & play Radio-Browser API channels"
echo "*tips: If unsure about where to begin, start: $PROGNAME -p 'kupla mirage'"
exit;
}
main()
{
mpvcfzf_config
if [ $# -lt 1 ]; then mpvcfzf_usage; fi
for arg in "$@";
do
arg1="${2:-}"
case "$arg" in
-0|awk0) shift; mpvcfzf_awk0; return ;;
-1|awk1) shift; mpvcfzf_awk1; return ;;
-a|stash) shift; mpvcfzf_stashlist; return ;;
-b|browse) shift; mpvcfzf_urlbrowser "$arg1"; return ;;
-B|urlget) shift; mpvcfzf_urlget "$arg1"; return ;;
-c|chapters)shift; mpvcfzf_chapterlist; return ;;
-C|cache) shift; mpvcfzf_icache; return;;
-d|dir) shift; MPVC_DIR="$arg1"; cd "$MPVC_DIR" || return;;
-e|eqz) shift; mpvcfzf_equalizer; return ;;
-f|playlist)shift; mpvcfzf_playlist; return ;;
-F|preview) shift; mpvcfzf_preview "$@"; return ;;
-g|fetch) shift; mpvcfzf_fetch "$arg1"; return ;;
-G|Fetch) shift; mpvcfzf_fetch $(mpvcfzf_iplay "$1"); return ;;
-i|lyrics) shift; mpvcfzf_lyrics "$arg1"; return ;;
-L|Lyrics) shift; mpvcfzf_lyrics_ddg; return ;;
-k|dplay) shift; mpvcfzf_ddg_play "$arg1"; return ;;
-K|dsearch) shift; mpvcfzf_ddg "$arg1"; return ;;
-h|help) shift; mpvcfzf_usage; return ;;
-l|local) shift; mpvcfzf_local; return ;;
-n|next) shift; mpvcfzf_nextvideo "$arg1"; return ;;
-o|osearch) shift; mpvcfzf_isearch_once "$arg1" ;; # lucky
-O|Osearch) shift; mpvcfzf_isearch_once "$arg1" | mpvcfzf_awk1 | mpvc load ;; # lucky
-#O) mpvcfzf_icache_output_dir ;;
-p|splay) shift; mpvcfzf_iplay "$arg1" | mpvc load; return ;;
-P|Splay) shift; mpvcfzf_iplay "$arg1" | mpvc loadc; return ;;
-r|remove) shift; mpvcfzf_fetch_remove "$arg1"; return ;;
-s|search) shift; mpvcfzf_isearch "$arg1"; return;;
-x|launch) shift; mpvcfzf_launch_term "$@"; exit ;;
-y|related) shift; mpvcfzf_related_search "$arg1"; return ;;
-Y|Related) shift; mpvcfzf_related_play "$arg1"; return ;;
-v|version) shift; mpvcfzf_version; return ;;
now) shift; mpvcfzf_now; return ;;
somafm) shift; mpvcfzf_somafm "${1:-}"; return ;;
radioapi) shift; mpvcfzf_radioapi "${1:-}"; return ;;
#*) shift; mpvcfzf_usage; return ;;
esac
done
}
main "$@"