This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ytlib.sh
895 lines (725 loc) Β· 18.6 KB
/
ytlib.sh
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
#/bin/sh
########################################
# name: ytlib.sh
# desc: library for shelltube (`yt`)
# main: jadedctrl <[email protected]>
# code: hak.xwx.moe/jadedctrl/shelltube
########################################
# --------------------------------------
# generic functions
# STRING --> STRING
# Return the first 'word' (space-delimiter) of a string.
function car {
local string="$1"
echo "$string" \
| awk '{print $1}'
}
# STRING --> STRING
# Return all words after the first word of a string.
function cdr {
local string="$1"
echo "$string" \
| awk '{$1=""; print}' \
| sed 's/^ //'
}
# NUMBER NUMBER --> NUMBER
# Well, subtraction. Y'know...
function subtract {
local operated=$1
local operatee=$2
echo "${1}-${2}" \
| bc
}
# STRING --> NUMBER
# Count how many words are in a string.
function length {
local string="$1"
echo "$string" \
| wc -w
}
# STRING --> NUMBER
# Count how many lines are in a string.
function line_length {
local string="$1"
echo "$string" \
| wc -l
}
# STRING --> NUMBER
# Return the length of a string in chars.
function char_length {
local string="$1"
subtract $(echo "$string" | wc -c) 1
}
# STRING NUMBER [STRING] --> STRING
# pad string $1 out to the minimum length $2 with padding $3 (default: "0")
function min_string_length {
local length="$2"
local string="$1"
if test -z "$3"; then
local padding="0"
else
local padding="$3"
fi
local new="$string"
if test "$length" -le "$(char_length "$string")"; then
echo "$string"
else
while test "$(char_length "$new")" -lt "$length"; do
new="${new}${padding}"
done
echo "$new"
fi
}
# STRING NUMBER --> STRING
# get the first $2 characters in string $1
function char_get {
local number="$1"
local string="$2"
string="$(min_string_length "$string" $number " ")"
echo "$string" \
| grep -o "^$(min_string_length "." $number ".")"
}
# --------------------------------------
# ansi colors and such
# NIL --> STRING
# Print an ANSI "unbold" escape string.
function unbold {
printf "$(tput sgr0)"
}
# STRING --> STRING
# Print an ANSI "bold" escape string.
function bold {
printf "$(tput bold)"
}
# STRING --> STRING
# Format a URL appropriately.
function format_url {
local url="$1"
# playlist/playlist video
if echo "$url" | grep -q "playlist?"; then
echo "$url" \
| sed 's%.*list=%%'
# video
else
echo "$url"
fi
}
# --------------------------------------
# result parsing
# (playlist items, search -items)
# STRING --> STRING
# Pass the raw search HTML, get raw video result-lines
function result_lines {
local search_html="$1"
echo "$search_html" \
| grep "<a href=\"\/watch?v=" \
| grep -v "<span class=\"yt-thumb-simple\"" \
| sed 's/|//g'
#| grep -v "<a href=\"/playlist" \
}
# STRING --> STRING
# Return the video ID of a result_line.
function result_id {
local result_line="$1"
if echo "$result_line" | grep -q "playlist-item"; then
echo "$result_line" \
| sed 's%.*?list=%%' \
| sed 's%".*%%'
elif echo "$result_line" | grep -q "data-title"; then
echo "$result_line" \
| sed 's%.* data-video-id="%%' \
| sed 's%"><td class=".*%%' \
| sed 's%".*%%'
else
echo "$result_line" \
| sed 's%.*<a href="/watch?v=%%' \
| sed 's%".*%%'
fi
}
# STRING --> STRING
# Return the URL of a result-line.
function result_url {
local result_line="$1"
local id="$(result_id "$result_line")"
# playlist ID
if echo "$id" | grep -q "^PL"; then
echo "https://youtube.com/playlist?list=${id}"
# video ID
else # search result
echo "https://youtube.com/watch?v=${id}"
fi
}
# STRING --> STRING
# Return the title of a result-line.
function result_title {
local result_line="$1"
# playlist video
if echo "$result_line" | grep -q "data-title"; then
echo "$result_line" \
| sed 's%.*data-title="%%' \
| sed 's%" data-video-id.*%%' \
| sed 's%".*%%'
# video
else
echo "$result_line" \
| sed 's%.*" title="%%' \
| sed 's%".*%%'
fi
}
# STRING --> STRING
# Return the duration of a result-line.
function result_duration {
local result_line="$1"
# playlist
if echo "$result_line" | grep -q "playlist-item"; then
echo "$result_line" \
| sed 's%.*View full playlist (%%' \
| sed 's% videos.*%%' \
| sed 's%$%v%'
# playlist video
elif echo "$result_line" | grep -q "data-title"; then
echo "00:00"
# video
else
echo "$result_line" \
| sed 's%.*> - Duration: %%' \
| sed 's%\..*%%'
fi
}
# STRING --> STRING
# Return the channel of a result-line.
function result_channel {
local result_line="$1"
# playlist
if echo "$result_line" | grep -q "playlist-item"; then
# playlist with /user/
if echo "$result_line" | grep -q "/user/"; then
echo "$result_line" \
| sed 's%.*/user/%%' \
| sed 's%</a>.*%%' \
| sed 's%.*>%%'
# playlist with /channel/
else
echo "$result_line" \
| sed 's%.*/channel/%%' \
| sed 's%</a>.*%%' \
| sed 's%.*>%%'
fi
# playlist video
elif echo "$result_line" | grep -q "data-title"; then
echo "Someone, bby <3"
# video
else
# video with /channel/
if echo "$result_line" | grep -q "/channel/"; then
echo "$result_line" \
| sed 's%.*href="/channel/%%' \
| sed 's%</a>.*%%' \
| sed 's%.*" >%%'
# video with /user/
elif echo "$result_line" | grep -q "/user/"; then
echo "$result_line" \
| sed 's%.*href="/user/%%' \
| sed 's%".*%%'
fi
fi
}
# STRING --> STRING
# Return the .yt-lockup-meta-info <ul> of a result-line
function result_meta_ul {
local result_line="$1"
echo "$result_line" \
| sed 's%.*<ul class="yt-lockup-meta-info">%%' \
| sed 's%</ul>.*%%'
}
# STRING --> STRING
# Return the "Uploaded..." string of a result-line
function result_uploaded {
local result_line="$1"
local result_meta_ul="$(result_meta_ul "$result_line")"
# playlist
if echo "$result_line" | grep -q "playlist-item"; then
echo "Sometime"
# playlist video
elif echo "$result_line" | grep -q "data-title"; then
echo "Sometime"
# video
else
echo "$result_meta_ul" \
| sed 's%<li>%%' \
| sed 's% ago</li>.*%%'
fi
}
# STRING --> NUMBER
# Return the view-count of a result-line
function result_views {
local result_line="$1"
local result_meta_ul="$(result_meta_ul "$result_line")"
echo "$result_meta_ul" \
| sed 's%.*<li>%%g' \
| sed 's% views</li>.*%%' \
| sed 's%,%%g'
}
# STRING --> STRING
# Return a result's formatted URL
function result_formatted_url {
local result_line="$1"
format_url "$(result_url "$result_line")"
}
# --------------------------------------
# STRING --> STRING
# Format a result-line into an ugly, simple, item URL
function result_format_url {
local result_line="$1"
local url="$(result_formatted_url "$result_line")"
echo "$url"
}
# STRING --> STRING
# Format a result-line into an repulsive, simple, item ID
function result_format_id {
local result_line="$1"
local id="$(result_id "$result_line")"
echo "$id"
}
# STRING --> STRING
# Format a result-line into a mediumly-pretty, one-line string~
function result_format_compact {
local result_line="$1"
local title="$(result_title "$result_line")"
local format_title="$(bold)$(char_get 40 "$title")$(unbold)"
local id="$(result_id "$result_line")"
echo "$format_title | $id"
}
# STRING --> STRING
# Format a result-line into a mediumly-pretty, one-line string~
function result_format_small {
local result_line="$1"
local title="$(result_title "$result_line")"
local url="$(result_formatted_url "$result_line")"
local format_title="$(bold)$(char_get 40 "$title")$(unbold)"
echo "$format_title | $url"
}
# STRING --> STRING
# Format a result-line into a mediumly-pretty, one-line string~
function result_format_medium {
local result_line="$1"
local title="$(result_title "$result_line")"
local duration="$(result_duration "$result_line")"
local url="$(result_formatted_url "$result_line")"
local format_title="$(bold)$(char_get 40 "$title")$(unbold)"
local format_duration="$(char_get 5 "$duration")"
echo "$format_title | $format_duration | $url"
}
# STRING --> STRING
# Format a result-line into a pretty string~
function result_format_big {
local result_line="$1"
local title="$(result_title "$result_line")"
local duration="$(result_duration "$result_line")"
local uploaded="$(result_uploaded "$result_line")"
local channel="$(result_channel "$result_line")"
local url="$(result_formatted_url "$result_line")"
local format_title="$(bold)$(char_get 79 "$title")$(unbold)"
local format_duration="$(char_get 7 "$duration")"
local format_uploaded="$(char_get 8 "$uploaded")"
local format_channel="$(char_get 15 "$channel")"
echo "$format_title"
echo "$format_duration | $format_uploaded | $format_channel | $url"
}
# --------------------------------------
# playlist metadata
# STRING --> STRING
# Get the title of a playlist, from HTML.
function playlist_title {
local html="$1"
video_title "$html"
}
# STRING --> STRING
# Get the video-count of a playlist, from HTML.
function playlist_duration {
local html="$1"
playlist_header_details "$html" \
| sed 's%.*</a></li>%%' \
| sed 's%</li>.*%%' \
| sed 's%<li>%%'
}
# STRING --> STRING
# Get the view-count of a playlist, from HTML.
function playlist_views {
local html="$1"
playlist_header_details "$html" \
| sed 's%.*</a></li>%%' \
| sed 's%</li><li>Last.*%%' \
| sed 's%.*</li><li>%%' \
| sed 's% views%%' \
| sed 's%,%%'
}
# STRING --> STRING
# Get the uploaded date of a playlist, from HTML.
function playlist_uploaded {
local html="$1"
playlist_header_details "$html" \
| sed 's%.*</a></li>%%' \
| sed 's%.*</li><li>%%' \
| sed 's%Last updated on %%' \
| sed 's%</li>.*%%'
}
# STRING --> STRING
# Get the author name of a playlist, from HTML.
function playlist_author_name {
local html="$1"
playlist_header_details "$html" \
| sed 's%</li><li>.*%%' \
| sed 's%.* >%%' \
| sed 's%</.*%%'
}
# STRING --> STRING
# Get the author URL of a playlist, from HTML.
function playlist_author_url {
local html="$1"
local author_name="$(playlist_author_name "$html")"
playlist_header_details "$html" \
| sed 's%'"$author_name"'.*%%' \
| sed 's%.*href="%%' \
| sed 's%".*%%' \
| sed 's%^%https://youtube.com%'
}
# STRING --> STRING
# Return the metadata header for playlist HTML
function playlist_header_details {
local html="$1"
echo "$html" \
| grep "pl-header-details"
}
# --------------------------------------
# video metadata
# STRING --> STRING
# Get the description of a YT video, from HTML.
function video_desc {
local html="$1"
echo "$html" \
| grep "action-panel-details" \
| sed 's/.*<p .*class="" >//' \
| sed 's%</p>.*%%' \
| lynx -stdin -dump \
| sed 's/^ //'
}
# STRING --> STRING
# Get the views of a YT video, from HTML.
function video_views {
local html="$1"
echo "$html" \
| grep "watch-view-count" \
| sed 's/.*"watch-view-count">//' \
| sed 's/ views.*//' \
| sed 's/,//g'
}
# STRING --> STRING
# Get the uploader's name of a YT video, from HTML.
function video_author_name {
local html="$1"
echo "$html" \
| grep "\"name\": " \
| sed 's/.*"name": "//' \
| sed 's/".*//'
}
# STRING --> STRING
# Get the URL to a video uploader's channel, from HTML
function video_author_url {
local html="$1"
local author_name="$(video_author_name "$html")"
local relative_url="$(echo "$html" \
| grep "$author_name" \
| grep "channel" \
| grep "href" \
| sed 's%.*="/%/%' \
| sed 's/".*//')"
echo "https://youtube.com${relative_url}"
}
# STRING --> STRING
# Get the title of a video, from HTML.
function video_title {
local html="$1"
echo "$html" \
| grep "meta name=\"title\"" \
| sed 's/.*content="//' \
| sed 's/".*//' \
| sed 's/ - Youtube//'
}
# --------------------------------------
# STRING STRING --> STRING
# Display results from a string of result_lines and a format-option
function results_display {
local result_lines="$1"
local format="$2"
IFS='
'
for result in $result_lines; do
case "$format" in
"url") result_format_url "$result" ;;
"id") result_format_id "$result" ;;
"compact") result_format_compact "$result" ;;
"small") result_format_small "$result" ;;
"medium") result_format_medium "$result" ;;
"big") result_format_big "$result" ;;
esac
done
}
# --------------------------------------
# playlist usage
# Print playlist usage.
function playlist_list_usage {
echo "usage: yt playlist --list [-UIcsmb] url/id"
exit 2
}
# Print playlist usage.
function playlist_search_usage {
echo "usage: yt playlist --search [-UIcsmb] search_query"
exit 2
}
# Print playlist usage.
function playlist_views_usage {
echo "usage: yt playlist views url/id"
exit 2
}
# Print playlist usage.
function playlist_author_usage {
echo "usage: yt playlist author [-Un] url/id"
exit 2
}
# Print playlist usage.
function playlist_date_usage {
echo "usage: yt playlist date url/id"
exit 2
}
# Print playlist usage.
function playlist_title_usage {
echo "usage: yt playlist title url/id"
exit 2
}
# --------------------------------------
# playlist invocation
# Invoke the playlist --list command
function playlist_list_invocation {
if test "$1" = "-h" -o "$1" = "--help"; then playlist_list_usage; fi
if test -n "$2"; then
local url="$2"
case "$1" in
"-U") local format="url" ;;
"-I") local format="id" ;;
"-c") local format="compact" ;;
"-s") local format="small" ;;
"-m") local format="medium" ;;
"-b") local format="big" ;;
esac
else
local url="$1"
local format="big"
fi
if echo "$url" | grep -qv "youtube.com"; then
url="https://www.youtube.com/playlist?list=${url}"
fi
playlist_html="$(gendl "$url")"
result_lines="$(result_lines "$playlist_html")"
results_display "$result_lines" "$format"
}
# Invoke the playlist --search command
function playlist_search_invocation {
if test "$1" = "-h" -o "$1" = "--help"; then playlist_search_usage; fi
if test -n "$2"; then
local query="$2"
case "$1" in
"-U") local format="url" ;;
"-I") local format="id" ;;
"-c") local format="compact" ;;
"-s") local format="small" ;;
"-m") local format="medium" ;;
"-b") local format="big" ;;
esac
else
local query="$1"
local format="big"
fi
query="$(echo "$query" | sed 's/ /+/g')"
local search_url="https://youtube.com/results?search_query=${query}"
search_url="${search_url}&sp=EgIQAw%253D%253D"
local search_html="$(gendl "$search_url")"
local result_lines="$(result_lines "$search_html")"
results_display "$result_lines" "$format"
}
# Invoke playlist --title
function playlist_title_invocation {
if test "$1" = "-h" -o "$1" = "--help"; then playlist_title_usage; fi
local url="$1"
playlist_title "$(gendl "$url")"
}
# Invoke playlist --desc
function playlist_desc_invocation {
if test "$1" = "-h" -o "$1" = "--help"; then playlist_desc_usage; fi
if echo "$1" | grep "youtube" > /dev/null; then URL="$1"
else
URL="https://www.youtube.com/playlist?list=${1}"
fi
playlist_desc "$(gendl "$URL")"
}
# Invoke playlist --views
function playlist_views_invocation {
case "$1" in
"-h") playlist_views_usage ;;
"--help")
playlist_views_usage ;;
*youtube*)
local url="$1" ;;
*) local url="https://www.youtube.com/playlist?list=${1}" ;;
esac
playlist_views "$(gendl "$url")"
}
# Invoke playlist --date
function playlist_uploaded_invocation {
if test "$1" = "-h" -o "$1" = "--help"; then playlist_date_usage; fi
if echo "$1" | grep "youtube" > /dev/null; then URL="$1"
else
URL="https://www.youtube.com/playlist?list=${1}"
fi
playlist_uploaded "$(gendl "$URL")"
}
# Invoke playlist --author
function playlist_author_invocation {
case "$1" in
"--help")
playlist_author_usage ;;
"-h") playlist_author_usage ;;
"-U") view="url"
url="$2"
;;
"-n") view="name"
url="$2"
;;
*) view="both"
url="$1"
;;
esac
if echo "$url" | grep -qv "youtube"; then
url="https://www.youtube.com/playlist?list=${url}"
fi
html="$(gendl "$url")"
case "$view" in
"url") playlist_author_url "$html" ;;
"name") playlist_author_name "$html" ;;
"both")
echo "$(playlist_author_name "$html") | $(playlist_author_url "$html")"
;;
esac
}
# --------------------------------------
# video usage
# Print video --search usage
function video_search_usage {
echo "usage: yt video search [-UIcsmb] query"
exit 2
}
# Print video --desc usage
function video_desc_usage {
echo "usage: yt video desc url/id"
exit 2
}
# Print video --views usage
function video_views_usage {
echo "usage: yt video views url/id"
exit 2
}
# Print video --author usage
function video_author_usage {
echo "usage: yt video author [-Un] url/id"
exit 2
}
# --------------------------------------
# video invocation
# Invoke video --search
function video_search_invocation {
if test "$1" = "-h" -o "$1" = "--help"; then video_search_usage; fi
if test -n "$2"; then
local query="$2"
case "$1" in
"-U") local format="url" ;;
"-I") local format="id" ;;
"-c") local format="compact" ;;
"-s") local format="small" ;;
"-m") local format="medium" ;;
"-b") local format="big" ;;
esac
else
local query="$1"
local format="big"
fi
query="$(echo "$query" | sed 's/ /+/g')"
local search_url="https://youtube.com/results?search_query=${query}"
search_url="${search_url}&sp=EgIQAQ%253D%253D"
local search_html="$(gendl "$search_url")"
local result_lines="$(result_lines "$search_html")"
results_display "$result_lines" "$format"
}
# Invoke video --title
function video_title_invocation {
if test "$1" = "-h" -o "$1" = "--help"; then video_title_usage; fi
local url="$1"
video_title "$(gendl "$url")"
}
# Invoke video --desc
function video_desc_invocation {
if test "$1" = "-h" -o "$1" = "--help"; then video_desc_usage; fi
if echo "$1" | grep "youtube" > /dev/null; then URL="$1"
else
URL="https://www.youtube.com/watch?v=${1}"
fi
video_desc "$(gendl "$URL")"
}
# Invoke video --date
function video_uploaded_invocation {
if test "$1" = "-h" -o "$1" = "--help"; then video_date_usage; fi
if echo "$1" | grep "youtube" > /dev/null; then URL="$1"
else
URL="https://www.youtube.com/watch?v=${1}"
fi
video_uploaded "$(gendl "$URL")"
}
# Invoke video --views
function video_views_invocation {
case "$1" in
"-h") video_views_usage ;;
"--help")
video_views_usage ;;
*youtube*)
local url="$1" ;;
*) local url="https://www.youtube.com/watch?v=${1}" ;;
esac
video_views "$(gendl "$url")"
}
# Invoke video --author
function video_author_invocation {
case "$1" in
"--help")
video_author_usage ;;
"-h") video_author_usage ;;
"-U") view="url"
url="$2"
;;
"-n") view="name"
url="$2"
;;
*) view="both"
url="$1"
;;
esac
if echo "$url" | grep -qv "youtube"; then
url="https://www.youtube.com/watch?v=${url}"
fi
html="$(gendl "$url")"
case "$view" in
"url") video_author_url "$html" ;;
"name") video_author_name "$html" ;;
"both")
echo "$(video_author_name "$html") | $(video_author_url "$html")"
;;
esac
}