-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmagic-tape.sh
executable file
·1055 lines (1020 loc) · 57.6 KB
/
magic-tape.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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# Need Bash version 4.2 or higher to have support for 'declare'
bash_version_min="4.2"
if [ -z "$BASH_VERSION" ] || [[ "${BASH_VERSION%%[^0-9.]*}" < "$bash_version_min" ]]; then
echo "Error: This script requires Bash version $bash_version_min or higher."
exit 1
fi
# Preserve bash features in subshells (eg. fzf)
SHELL="$(command -v bash)"
#┏┳┓┏━┓┏━╸╻┏━╸ ╺┳╸┏━┓┏━┓┏━╸
#┃┃┃┣━┫┃╺┓┃┃ ╺━╸ ┃ ┣━┫┣━┛┣╸
#╹ ╹╹ ╹┗━┛╹┗━╸ ╹ ╹ ╹╹ ┗━╸
#A script written by Christos Angelopoulos in March 2023 under GNU GENERAL PUBLIC LICENSE
#
function load_colors(){
if [[ $COLORED_MESSAGES == "yes" ]];
then
Yellow="\e[33m"
Green="\e[32m"
Red="\e[31m"
Magenta="\e[35m"
Cyan="\e[36m"
Gray="\e[38;5;242m"
bold="\x1b[1m"
normal="\e[m"
else
Yellow="";
Green="";
Red="" ;
Magenta="" ;
Cyan="" ;
Gray="" ;
bold="" ;
normal=""
fi;
}
function notify() {
[ "${NOTIFICATION_ENABLED:-yes}" == "no" ] && return 0;
command -v notify-send >/dev/null && {
notify-send -t $1 -i "$2" "magic-tape" "$3"
return 0
}
[ -n "$KITTY_PID" ] && [ $kitty_version -gt 35 ] && {
case "$(uname -s)" in
# FIXME: For some reason `-p` does not work on macOS
Darwin) kitty @ kitten notify -s silent -e $(($1 / 1000))s "magic-tape" "$3" ;;
*) kitty @ kitten notify -s silent -e $(($1 / 1000))s -p "$2" "magic-tape" "$3" ;;
esac
return 0
}
command -v osascript >/dev/null && {
osascript -e "display notification \"$1\" with title \"magic-tape\""
return 0
}
return 1
}
function copy_to_clipboard() {
command -v xclip >/dev/null && { echo "$1" | xclip -sel clip; return 0; }
command -v pbcopy >/dev/null && { echo "$1" | pbcopy; return 0; }
return 1
}
function load_config() {
if [[ -e "$HOME/.config/magic-tape/magic-tape.conf" ]]
then
source "$HOME/.config/magic-tape/magic-tape.conf"
load_colors
if [[ $LIST_LENGTH -gt 99 ]];then LIST_LENGTH=99;fi;
ROFI_FORMAT='rofi -dmenu -l 20 -width 40 -i -p ';
FZF_FORMAT="fzf --preview-window=0 --color='gutter:-1' --reverse --tiebreak=begin --border=rounded +m --info=hidden --header-first --prompt=";
DMENU_FORMAT="dmenu -fn 13 -nb '#2E3546' -sb '#434C5E' -l 20sc -i -p "
if [[ $PREF_SELECTOR == "rofi" ]]
then PREF_SELECTOR=$rofi_format
elif [[ $PREF_SELECTOR == "fzf" ]]
then PREF_SELECTOR=$fzf_format
elif [[ $PREF_SELECTOR == "dmenu" ]]
then PREF_SELECTOR=$dmenu_format;fi;
else
notify 9000 "$SHARE_DIR"/magic-tape.png "Exiting magic-tape"
echo -e "\e[31mConfigurations not loaded correctly.Please make sure that:\n- You have installed the script correctly using the install.sh.\n- All the variables have assigned acceptable values.$normal"
exit
fi
}
function search_filter ()
{
FILT_PROMPT="";
FILT_PROMPT="$(echo -e "No Duration Filter\n☕ Duration up to 4 mins\n☕☕ Duration between 4 and 20 mins\n☕☕☕ Duration longer than 20 mins\n📋 Search for playlist"|eval "$PREF_SELECTOR""\"Select Filter \"")";
case $FILT_PROMPT in
"No Duration Filter") FILTER="&sp=EgQQARgE";
;;
"☕ Duration up to 4 mins") FILTER="&sp=EgQQARgB";
;;
"☕☕ Duration between 4 and 20 mins") FILTER="&sp=EgQQARgD";
;;
"☕☕☕ Duration longer than 20 mins") FILTER="&sp=EgQQARgC";
;;
"📋 Search for playlist") FILTER="&sp=EgQQAxgE";
;;
*)FILTER="&sp=EgQQARgE";
;;
esac
}
function new_subscription ()
{
C=${C// /+};C=${C//\'/%27};
repeat_channel_search=1;
ITEM=1;
FEED="/results?search_query="$C"&sp=EgIQAg%253D%253D";
while [ $repeat_channel_search -eq 1 ];
do fzf_header="$(echo ${FEED^^}|sed 's/&SP=.*$//;s/^.*SEARCH_QUERY=/search: /;s/[\/\?=&+]/ /g') channels: $ITEM to $(($ITEM + $(($LIST_LENGTH - 1))))";
ITEM0=$ITEM;
echo -e "${Gray}Downloading $FEED...${normal}";
echo -e "$db\n$ITEM\n$ITEM0\n$FEED\n$fzf_header">$HOME/.cache/magic-tape/history/last_action.txt;
yt-dlp --cookies-from-browser $PREF_BROWSER --flat-playlist --playlist-start $ITEM --playlist-end $(($ITEM + $(($LIST_LENGTH - 1)))) -j "https://www.youtube.com$FEED">$HOME/.cache/magic-tape/json/channel_search.json
echo -e "${Gray}Completed $FEED${normal}";
jq '.channel_id' $HOME/.cache/magic-tape/json/channel_search.json|sed 's/"//g'>$HOME/.cache/magic-tape/search/channels/ids.txt;
jq '.title' $HOME/.cache/magic-tape/json/channel_search.json|sed 's/"//g'>$HOME/.cache/magic-tape/search/channels/titles.txt;
jq '.description' $HOME/.cache/magic-tape/json/channel_search.json|sed 's/"//g'>$HOME/.cache/magic-tape/search/channels/descriptions.txt;
jq '.channel_follower_count' $HOME/.cache/magic-tape/json/channel_search.json|sed 's/"//g'>$HOME/.cache/magic-tape/search/channels/subscribers.txt;
jq '.thumbnails[1].url' $HOME/.cache/magic-tape/json/channel_search.json|sed 's/"//g'>$HOME/.cache/magic-tape/search/channels/img_urls.txt;
cat /dev/null>$HOME/.cache/magic-tape/search/channels/thumbnails.txt;
i=1;
while [ $i -le $(cat $HOME/.cache/magic-tape/search/channels/ids.txt|wc -l) ];
do echo "url = \"https:""$(sed -n "${i}p" $HOME/.cache/magic-tape/search/channels/img_urls.txt)\"">>$HOME/.cache/magic-tape/search/channels/thumbnails.txt;
echo "output = \"$HOME/.cache/magic-tape/jpg/$(sed -n "${i}p" $HOME/.cache/magic-tape/search/channels/ids.txt).jpg\"">>$HOME/.cache/magic-tape/search/channels/thumbnails.txt;
((i++));
done;
echo -e "${Gray}Downloading channel thumbnails...${normal}";
curl -s -K $HOME/.cache/magic-tape/search/channels/thumbnails.txt 2>/dev/null&echo -e "${Gray}Background downloading channel thumbnails...${normal}";
if [ $ITEM -gt 1 ];then echo "Previous Page">>$HOME/.cache/magic-tape/search/channels/titles.txt;fi;
if [ $(cat $HOME/.cache/magic-tape/search/channels/ids.txt|wc -l) -ge $LIST_LENGTH ];then echo "Next Page">>$HOME/.cache/magic-tape/search/channels/titles.txt;fi;
echo "Abort Selection">>$HOME/.cache/magic-tape/search/channels/titles.txt;
CHAN=" $(cat -n $HOME/.cache/magic-tape/search/channels/titles.txt|sed 's/^. *//g' |fzf\
--info=hidden \
--layout=reverse \
--height=100% \
--prompt="Select Channel: " \
--header="$fzf_header" \
--preview-window=left,50%\
--bind=right:accept \
--expect=shift-left,shift-right\
--tabstop=1 \
--no-margin \
+m \
-i \
--exact \
--preview='hght=$(($FZF_PREVIEW_COLUMNS /3));\
if [[ "$IMAGE_SUPPORT" == "kitty" ]];then clear_image;fi;\
i=$(echo {}|sed "s/\\t.*$//g");\
echo $i>$HOME/.cache/magic-tape/search/channels/index.txt;\
if [[ "$IMAGE_SUPPORT" == "ueberz"* ]];then ll=0; while [ $ll -le $hght ];do echo "";((ll++));done;fi;\
if [[ "$IMAGE_SUPPORT" == "kitty" ]]&&[[ $kitty_version -le 21 ]];then ll=0; while [ $ll -le $hght ];do echo "";((ll++));done;fi;\
TITLE="$(sed -n "${i}p" $HOME/.cache/magic-tape/search/channels/titles.txt)";\
if [[ "$TITLE" == "Previous Page" ]];then draw_preview 1 1 $FZF_PREVIEW_COLUMNS $hght "$SHARE_DIR"/previous.png;\
elif [[ "$TITLE" == "Next Page" ]];then draw_preview 1 1 $FZF_PREVIEW_COLUMNS $hght "$SHARE_DIR"/next.png;\
elif [[ "$TITLE" == "Abort Selection" ]];then draw_preview 1 1 $FZF_PREVIEW_COLUMNS $hght "$SHARE_DIR"/abort.png;\
else draw_preview 1 1 $FZF_PREVIEW_COLUMNS $hght "$HOME""/.cache/magic-tape/jpg/""$(sed -n "${i}p" $HOME/.cache/magic-tape/search/channels/ids.txt)"".jpg";fi;\
ll=1; echo -ne "\x1b[38;5;241m"; while [ $ll -le $FZF_PREVIEW_COLUMNS ];do echo -n -e "─";((ll++));done;echo -e "$normal";\
echo -e "\n""$Yellow""$TITLE""$normal"|fold -w $FZF_PREVIEW_COLUMNS -s;\
if [[ $TITLE != "Abort Selection" ]]&&[[ $TITLE != "Next Page" ]]&&[[ $TITLE != "Previous Page" ]];\
then SUBS="$(sed -n "${i}p" $HOME/.cache/magic-tape/search/channels/subscribers.txt)";\
echo -e "\n"$Green"Subscribers: ""$Cyan""$SUBS""$normal";\
ll=1; echo -ne "\x1b[38;5;241m"; while [ $ll -le $FZF_PREVIEW_COLUMNS ];do echo -n -e "─";((ll++));done;echo -e "$normal";\
DESCRIPTION="$(sed -n "${i}p" $HOME/.cache/magic-tape/search/channels/descriptions.txt)";\
echo -e "\n\x1b[38;5;250m$DESCRIPTION"$normal""|fold -w $FZF_PREVIEW_COLUMNS -s; \
fi;')";
clear_image;
i=$(cat $HOME/.cache/magic-tape/search/channels/index.txt);
NAME=$(sed -n "${i}p" $HOME/.cache/magic-tape/search/channels/titles.txt);
if [[ $CHAN == " " ]]; then echo "ABORT!"; NAME="Abort Selection";clear;fi;
echo -e "${Gray}Channel Selected: $NAME${normal}";
if [ $ITEM -ge $LIST_LENGTH ]&&[[ $CHAN == *"shift-left"* ]]; then NAME="Previous Page";fi;
if [ $ITEM -le $LIST_LENGTH ]&&[[ $CHAN == *"shift-left"* ]]; then NAME="Abort Selection";fi;
#if [[ -n $PREVIOUS_PAGE ]]&&[[ $CHAN == *"shift-left"* ]]; then NAME="Previous Page";fi;
if [[ $CHAN == *"shift-right"* ]]; then NAME="Next Page";fi;
if [[ $NAME == "Next Page" ]];then ITEM=$(($ITEM + $LIST_LENGTH));fi;
if [[ $NAME == "Previous Page" ]];then ITEM=$(($ITEM - $LIST_LENGTH));fi;
if [[ $NAME == "Abort Selection" ]];then repeat_channel_search=0;fi;
if [[ "$NAME" != "Abort Selection" ]]&&[[ "$NAME" != "Next Page" ]]&&[[ "$NAME" != "Previous Page" ]];
then SUB_URL="$(sed -n "${i}p" $HOME/.cache/magic-tape/search/channels/ids.txt)";
repeat_channel_search=0;
echo -e " ${Green}You will subscribe to this channel:\n${Yellow}$NAME${normal}\nProceed?(Y/y)"; read -N 1 pr;echo -e "\n";
if [[ $pr == Y ]] || [[ $pr == y ]];
then notification_img="$HOME/.cache/magic-tape/jpg/""$(sed -n "${i}p" $HOME/.cache/magic-tape/search/channels/ids.txt)"".jpg";
if [ -n "$(grep -i $SUB_URL $HOME/.cache/magic-tape/subscriptions/subscriptions.txt)" ];
then notify $NOTIFICATION_DURATION "$notification_img" "You are already subscribed to $NAME ";
else echo "$SUB_URL"" ""$NAME">>$HOME/.cache/magic-tape/subscriptions/subscriptions.txt;
notify $NOTIFICATION_DURATION "$notification_img" "You have subscribed to $NAME ";
mv "$notification_img" $HOME/.cache/magic-tape/subscriptions/jpg/"$SUB_URL.jpg";
echo -e "${Red}NOTICE: ${Yellow}In order for this action to take effect in YouTube, you need to subscribe manually from a browser as well.\nDo you want to do it now? (Y/y)${normal}"|fold -w 75 -s;
read -N 1 pr2;echo -e "\n";
if [[ $pr2 == Y ]] || [[ $pr2 == y ]];then $LINK_BROWSER "https://www.youtube.com/channel/"$SUB_URL&echo "Opened $LINK_BROWSER";fi;
fi;
fi;
fi;
done;
}
function channel_feed ()
{
big_loop=1;
ITEM=1;
ITEM0=$ITEM;
if [[ "$P" == "@"* ]];then FEED="/""$P""/videos";else FEED="/channel/""$P""/videos";fi
while [ $big_loop -eq 1 ];
do fzf_header="channel: "$channel_name" videos $ITEM to $(($ITEM + $(($LIST_LENGTH - 1))))";
get_feed_json;
get_data;
small_loop=1;
while [ $small_loop -eq 1 ];
do select_video ;
if [[ "$TITLE" == "Next Page" ]]||[[ "$TITLE" == "Previous Page" ]];then small_loop=0;fi;
if [[ "$TITLE" == "Abort Selection" ]];then small_loop=0;big_loop=0;fi;
if [[ "$TITLE" != "Abort Selection" ]]&&[[ "$TITLE" != "Next Page" ]]&&[[ "$TITLE" != "Previous Page" ]];then select_action;fi;
done;
done;
}
function like_video ()
{
LIKE="$(tac $HOME/.cache/magic-tape/history/watch_history.txt|sed 's/^.*https:\/\/www\.youtube\.com/https:\/\/www\.youtube\.com/g'|cut -d' ' -f2-|eval "$PREF_SELECTOR""\"❤️ Select video to like \"")";
if [[ -z "$LIKE" ]];
then empty_query;
else echo -e "❤️ Add\n${Yellow}"$LIKE"${normal}\nto Liked Videos?(Y/y))";
read -N 1 alv;echo -e "\n";
if [[ $alv == Y ]] || [[ $alv == y ]];
then if [[ -z "$(grep "$LIKE" $HOME/.cache/magic-tape/history/liked.txt)" ]];
then echo "$(grep "$LIKE" $HOME/.cache/magic-tape/history/watch_history.txt|head -1)" >> $HOME/.cache/magic-tape/history/liked.txt;
notify $NOTIFICATION_DURATION "$SHARE_DIR"/magic-tape.png "❤️ Video added to Liked Videos.";
else notify $NOTIFICATION_DURATION "$SHARE_DIR"/magic-tape.png "❤️ Video already added to Liked Videos.";
fi;
fi;alv="";
fi;
}
function import_subscriptions()
{
echo -e "${Red}Your magic-tape subscriptions will be synced with your YouTube ones.Before initializing this function, make sure you are logged in in your YT account, and you have set up your preferred browser.\nProceed? (Y/y)${normal}"|fold -w 75 -s;
read -N 1 impsub ;echo -e "\n";
if [[ $impsub == "Y" ]] || [[ $impsub == "y" ]];
then echo -e "${Gray}Downloading subscriptions data...${normal}";
new_subs=subscriptions_$(date +%F).json;
yt-dlp --cookies-from-browser $PREF_BROWSER --flat-playlist -j "https://www.youtube.com/feed/channels">$HOME/.cache/magic-tape/json/$new_subs;
echo -e "${Green}Download Complete.${normal}";
jq '.id' $HOME/.cache/magic-tape/json/$new_subs|sed 's/"//g'>$HOME/.cache/magic-tape/search/channels/channel_ids.txt;
jq '.title' $HOME/.cache/magic-tape/json/$new_subs|sed 's/"//g'>$HOME/.cache/magic-tape/search/channels/channel_names.txt;
jq '.thumbnails[1].url' $HOME/.cache/magic-tape/json/$new_subs|sed 's/"//g'>$HOME/.cache/magic-tape/search/channels/image_urls.txt;
cat /dev/null>$HOME/.cache/magic-tape/search/channels/thumbnails.txt;
cp $HOME/.cache/magic-tape/subscriptions/subscriptions.txt $HOME/.cache/magic-tape/subscriptions/subscriptions-$(date +%F).bak.txt;
cat /dev/null>$HOME/.cache/magic-tape/subscriptions/subscriptions.txt;
i=1;
while [ $i -le $(cat $HOME/.cache/magic-tape/search/channels/channel_ids.txt|wc -l) ];
do echo "$(sed -n "${i}p" $HOME/.cache/magic-tape/search/channels/channel_ids.txt) $(sed -n "${i}p" $HOME/.cache/magic-tape/search/channels/channel_names.txt)">>$HOME/.cache/magic-tape/subscriptions/subscriptions.txt;
img_path="$HOME/.cache/magic-tape/subscriptions/jpg/$(sed -n "${i}p" $HOME/.cache/magic-tape/search/channels/channel_ids.txt).jpg";
if [ ! -f "$img_path" ];
then echo "url = \"https:$(sed -n "${i}p" $HOME/.cache/magic-tape/search/channels/image_urls.txt)\"">>$HOME/.cache/magic-tape/search/channels/thumbnails.txt;
echo "output = \"$img_path\"">>$HOME/.cache/magic-tape/search/channels/thumbnails.txt;
fi;
((i++));
done;
echo -e "${Gray}Downloading thumbnails...${normal}";
curl -s -K $HOME/.cache/magic-tape/search/channels/thumbnails.txt 2>/dev/null;
echo -e "${Gray}Thumbnail download complete.${normal}";
echo -e "${Green}Your magic-tape subscriptions are now updated.\nA backup copy of your old subscriptions is kept in\n${Yellow}$HOME/.cache/magic-tape/subscriptions/subscriptions-$(date +%F).bak.txt${normal}\n${Green}Press any key to continue: ${normal}";
read -N 1 imp2;clear;mv $HOME/.cache/magic-tape/json/$new_subs $HOME/.local/share/Trash/files/;
fi;
}
function draw_line(){
ll=1; echo -ne ${Gray}; while [ $ll -le $cols ];do echo -n -e "─";((ll++));done;echo -e "$normal"
}
function print_mpv_video_shortcuts()
{
if [[ "$SHOW_MPV_KEYBINDINGS" == 'yes' ]]
then
draw_line
echo -e "\n${Yellow}${bold}SHORTCUTS${normal}\n"
echo -e " ${Gray}╭─────┬──────────╮ ╭─────┬─────────────╮";
echo -e " ${Gray}│${Magenta}${bold} ␣ ${Gray}│${normal}${Cyan} Pause ${Gray}│ │${Magenta}${bold} f ${Gray}│${normal}${Cyan} Fullscreen ${Gray}│";
echo -e " ${Gray}├─────┼──────────┤ ├─────┼─────────────┤";
echo -e " ${Gray}│${Magenta}${bold} 9 0 ${Gray}│${normal}${Cyan} Volume ${Gray}│ │${Magenta}${bold} [ ] ${Gray}│${normal}${Cyan} Play Speed ${Gray}│";
echo -e " ${Gray}├─────┼──────────┤ ├─────┼─────────────┤";
echo -e " ${Gray}│${Magenta}${bold} m ${Gray}│${normal}${Cyan} Mute ${Gray}│ │${Magenta}${bold} 1 2 ${Gray}│${normal}${Cyan} Contrast ${Gray}│";
echo -e " ${Gray}├─────┼──────────┤ ├─────┼─────────────┤";
echo -e " ${Gray}│${Magenta}${bold} ← → ${Gray}│${normal}${Cyan} Skip 10\"${Gray} │ │${Magenta}${bold} 3 4 ${Gray}│${normal}${Cyan} Brightness${Gray} │";
echo -e " ${Gray}├─────┼──────────┤ ├─────┼─────────────┤";
echo -e " ${Gray}│${Magenta}${bold} ↑ ↓ ${Gray}│${normal}${Cyan} Skip 60\"${Gray} │ │${Magenta}${bold} 7 8 ${Gray}│${normal}${Cyan} Saturation${Gray} │";
echo -e " ${Gray}├─────┼──────────┤ ├─────┼─────────────┤";
echo -e " ${Gray}│${Magenta}${bold} , . ${Gray}│${normal}${Cyan} Frame ${Gray}│ │${Magenta}${bold} q ${Gray}│${normal}${Red} Quit ${Gray}│";
echo -e " ${Gray}╰─────┴──────────╯ ╰─────┴─────────────╯";
fi
}
function misc_menu ()
{
while [ "$db2" != "q" ] ;
do db2="$(echo -e "${Yellow}${bold}┏┳┓╻┏━┓┏━╸ ┏┳┓┏━╸┏┓╻╻ ╻${normal}\n${Yellow}${bold}┃┃┃┃┗━┓┃ ┃┃┃┣╸ ┃┗┫┃ ┃${normal}\n${Yellow}${bold}╹ ╹╹┗━┛┗━╸ ╹ ╹┗━╸╹ ╹┗━┛${normal}\n${Yellow}${bold}P ${normal}${Cyan}to SET UP PREFERENCES!${normal}\n${Yellow}${bold}Y to UPDATE yt-dlp!${normal}\n${Yellow}${bold}l ${normal}${Red}to LIKE a video.${normal}\n${Yellow}${bold}L ${normal}${Red}to UNLIKE a video.${normal}\n${Yellow}${bold}I ${normal}${Green}to import subscriptions from YouTube.${normal}\n${Yellow}${bold}n ${normal}${Green}to subscribe to a new channel.${normal}\n${Yellow}${bold}u ${normal}${Green}to unsubscribe from a channel.${normal}\n${Yellow}${bold}H ${normal}${Magenta}to clear watch history.${normal}\n${Yellow}${bold}S ${normal}${Magenta}to clear search history.${normal}\n${Yellow}${bold}T ${normal}${Magenta}to clear thumbnail cache.${normal}\n${Yellow}${bold}q${normal} ${Cyan}to quit this menu.${normal}"|fzf \
--preview-window=0 \
--disabled \
--reverse \
--ansi \
--tiebreak=begin \
--border=rounded \
+i \
+m \
--color='gutter:-1' \
--nth=1 \
--info=hidden \
--header-lines=3 \
--prompt="Enter:" \
--header-first \
--expect=A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,1,2,3,4,5,6,7,8,9,0 )";
db2="$(echo $db2|awk '{print $1}')";
case $db2 in
"P") eval $PREF_EDITOR $HOME/.config/magic-tape/magic-tape.conf;load_config;if [[ $IMAGE_SUPPORT == "ueberzugpp" ]];then trap exit_upp HUP INT QUIT TERM EXIT ERR ABRT ;clean_upp; fi;clear_image;
;;
"Y")clear;echo -e "${Red}${bold}NOTICE: ${Yellow}${bold}Updating yt-dlp with this option works only when you have installed it using pip.\nProceed? (Y/y)${normal}";
read -N 1 pipytdlp;echo -e "\n";if [[ $pipytdlp == Y ]] || [[ $pipytdlp == y ]]; then python3 -m pip install -U "yt-dlp[default]";pipytdlp="";fi; echo -e "${Gray}Press any key to return${normal}";read -N 1 xxx;
;;
"I") clear;
import_subscriptions;
;;
"n") clear;
echo -e "🔎 Enter keyword/keyphrase for a channel to search for: \n\n";
read C;
if [[ -z "$C" ]];
then empty_query;
else new_subscription;
fi;
;;
"u") clear;U="$(cat $HOME/.cache/magic-tape/subscriptions/subscriptions.txt|cut -d' ' -f2-|eval "$PREF_SELECTOR""\"❌ Unsubscribe from channel \"")";
if [[ -z "$U" ]]; then empty_query;
else echo "$U";
echo -e "${Red}${bold}Unsubscribe from this channel:\n"${Yellow}$U"${normal}\nProceed? (Y/y))";
read -N 1 uc;echo -e "\n";
if [[ $uc == Y ]] || [[ $uc == y ]];
then notification_img=""$SHARE_DIR"/magic-tape.png";
sed -i "/$U/d" $HOME/.cache/magic-tape/subscriptions/subscriptions.txt;
echo -e "${Green}${bold}Unsubscribed from $U ]${normal}";
notify $NOTIFICATION_DURATION "$notification_img" "You have unsubscribed from $U";
echo -e "${Red}${bold}NOTICE: ${Yellow}${bold}In order for this action to take effect in YouTube, you need to unsubscribe manually from a browser as well.\nDo you want to do it now? (Y/y)${normal}"|fold -w 75 -s;
read -N 1 uc2;echo -e "\n";
if [[ $uc2 == Y ]] || [[ $uc2 == y ]];then $LINK_BROWSER "https://www.youtube.com/feed/channels"&echo "Opened $PREF_BROWSER";fi;
fi;
fi;uc="";uc2="";
;;
"H") clear;echo -e "${Green}Clear ${Yellow}${bold}watch history?${normal}(Y/y))";
read -N 1 cwh;echo -e "\n";
if [[ $cwh == Y ]] || [[ $cwh == y ]];
then cat /dev/null > $HOME/.cache/magic-tape/history/watch_history.txt;
notify $NOTIFICATION_DURATION "$SHARE_DIR"/magic-tape.png "Watch history cleared.";
fi;cwh="";
;;
"S") clear;echo -e "${Green}Clear ${Yellow}${bold}search history?${normal}(Y/y))";
read -N 1 csh;echo -e "\n";
if [[ $csh == Y ]] || [[ $csh == y ]];
then cat /dev/null > $HOME/.cache/magic-tape/history/search_history.txt;
notify $NOTIFICATION_DURATION "$SHARE_DIR"/magic-tape.png "Search history cleared.";
fi;csh="";
;;
"T") clear;echo -e "${Green}Clear ${Yellow}${bold}thumbnail cache?${normal}(Y/y))";
read -N 1 ctc;echo -e "\n";
if [[ $ctc == Y ]] || [[ $ctc == y ]];
then mv $HOME/.cache/magic-tape/jpg/* $HOME/.local/share/Trash/files/
notify $NOTIFICATION_DURATION "$SHARE_DIR"/magic-tape.png "Thumbnail cache cleared.";
fi;ctc="";
;;
"l") clear;like_video;
;;
"L") clear;UNLIKE="$(tac $HOME/.cache/magic-tape/history/liked.txt|sed 's/^.*https:\/\/www\.youtube\.com/https:\/\/www\.youtube\.com/g'|cut -d' ' -f2-|eval "$PREF_SELECTOR""\"❌ Select video to unlike \"")";
if [[ -z "$UNLIKE" ]]; then empty_query;
else echo -e "${Red}${bold}Unlike video\n${Yellow}"$UNLIKE"?${normal}\n(Y/y))";
read -N 1 uv;echo -e "\n";
if [[ $uv == Y ]] || [[ $uv == y ]];
then notification_img=""$SHARE_DIR"/magic-tape.png";
#UNLIKE="$(echo "$UNLIKE"|awk '{print $1}'|sed 's/^.*\///')";
sed -i "/$UNLIKE/d" $HOME/.cache/magic-tape/history/liked.txt;
notify $NOTIFICATION_DURATION "$notification_img" "❌ You have unliked $UNLIKE";
fi;
fi;uv="";
;;
"q") clear;
;;
*)echo -e "\n😕${Yellow}${bold}$db2${normal} ${Green}is an invalid key, please try again.${normal}\n"; sleep $TERMINAL_MESSAGE_DURATION;clear;
;;
esac
done
db2="";
}
################# UBERZUGPP ###################
function exit_upp () {
db2="q"
PLAY=" "
db="q"
CHAN=" "
ueberzugpp cmd -s "$SOCKET" -a exit >/dev/null 2>&1
#killall ueberzugpp>/dev/null 2>&1
# killall -9 -g magic-tape.sh>/dev/null 2>&1
kill -9 $magic_tape_pid>/dev/null 2>&1
}
clean_upp() {
ueberzugpp cmd -s "$SOCKET" -a exit>/dev/null 2>&1&&ueberzugpp layer --no-stdin --silent --use-escape-codes --pid-file /tmp/.magic_tape_upp
UB_PID=$(cat /tmp/.magic_tape_upp)
SOCKET=/tmp/ueberzugpp-"$UB_PID".socket
}
function draw_upp () {
ueberzugpp cmd -s $SOCKET -i fzfpreview -a add -x "0" -y "0" --max-width $3 --max-height $4 -f $5
}
################# UBERZUG ######################
declare -r -x UEBERZUG_FIFO_MAGIC_TAPE="$(mktemp --dry-run )"
function start_ueberzug {
mkfifo "${UEBERZUG_FIFO_MAGIC_TAPE}"
<"${UEBERZUG_FIFO_MAGIC_TAPE}" \
ueberzug layer --parser bash --silent &
# prevent EOF
3>"${UEBERZUG_FIFO_MAGIC_TAPE}" \
exec
}
function finalise {
3>&- \
exec
&>/dev/null \
rm "${UEBERZUG_FIFO_MAGIC_TAPE}"
&>/dev/null \
kill $(jobs -p)
}
######################################################
function clear_image (){
if [[ "$IMAGE_SUPPORT" == "kitty" ]];then kitty icat --transfer-mode file --clear 2>/dev/null;fi;
if [[ "$IMAGE_SUPPORT" == "ueberzug" ]];then finalise;start_ueberzug;fi;
if [[ "$IMAGE_SUPPORT" == "ueberzugpp" ]];then clean_upp;fi;
}
function draw_uber {
#sample draw_uber 35 35 90 3 /path/image.jpg
>"${UEBERZUG_FIFO_MAGIC_TAPE}" declare -A -p cmd=( \
[action]=add [identifier]="preview" \
[x]="$1" [y]="$2" \
[width]="$3" [height]="$4" \
[scaler]=fit_contain [scaling_position_x]=10 [scaling_position_y]=10 \
[path]="$5")
}
function draw_preview {
#sample draw_preview 90 3 35 35 /path/image.jpg
if [[ "$IMAGE_SUPPORT" == "kitty" ]]
then
if [[ $kitty_version -le 22 ]]
then
kitty icat --transfer-mode file --place $3x$4@$1x$2 --scale-up "$5"
else
# kitty icat --transfer-mode=file --use-window-size $(($3-5)),$(($3/2)),480,360 --unicode-placeholder --align=left --scale-up "$5" 2>/dev/null
kitty icat --clear --transfer-mode=memory --unicode-placeholder --stdin=no --align=left --place="$3x$4@0x0" "$5" | sed '$d' | sed $'$s/$/\e[m/'
fi
fi
# if [[ "$IMAGE_SUPPORT" == "kitty" ]];then kitty icat --transfer-mode file --place $3x$4@$1x$2 --scale-up --unicode-placeholder "$5";fi;
if [[ "$IMAGE_SUPPORT" == "ueberzugpp" ]];then thumb_ratio=$(identify $5|awk '{print$3}');thumb_w="${thumb_ratio%x*}";thumb_h="${thumb_ratio##*x}";if [[ $thumb_w -ge $thumb_h ]];then max_x=$(($3*5/3));max_h=$(($thumb_h*$max_x/$thumb_w));else max_h="$(($hght))";max_x=$(($max_h*$thumb_h/$thumb_w-1));fi;draw_upp $1 $2 $max_x $max_h $5;fi;
if [[ "$IMAGE_SUPPORT" == "ueberzug" ]];then draw_uber $1 $2 $3 $4 $5;fi;
if [[ "$IMAGE_SUPPORT" == "chafa" ]];then chafa --format=symbols -c full -s $3 $5;fi;
}
function get_feed_json ()
{
echo -e "${Gray}Downloading $FEED...${normal}";
echo -e "$db\n$ITEM\n$ITEM0\n$FEED\n$fzf_header">$HOME/.cache/magic-tape/history/last_action.txt;
#if statement added to fix json problem. If the problem re-appears, uncomment the if statement, and comment following line
#if [ $db == "f" ]||[ $db == "t" ]||[ $db == "y" ];then LIST_LENGTH=$(($LIST_LENGTH * 2 ));else LIST_LENGTH="$(grep 'LIST_LENGTH:' $HOME/.config/magic-tape/magic-tape.conf|sed 's/^#.*//g;s/^.*: //')";fi;
###LIST_LENGTH="$(grep 'LIST_LENGTH:' $HOME/.config/magic-tape/magic-tape.conf|sed 's/^#.*//g;s/^.*: //')";
yt-dlp --cookies-from-browser $PREF_BROWSER --flat-playlist --extractor-args youtubetab:approximate_date --playlist-start $ITEM0 --playlist-end $(($ITEM0 + $(($LIST_LENGTH - 1)))) -j "https://www.youtube.com$FEED">$HOME/.cache/magic-tape/json/video_search.json;
echo -e "${Gray}Completed $FEED.${normal}";
#correct back LIST_LENGTH value(fix json problem);
#if [ $db == "f" ]||[ $db == "t" ];then LIST_LENGTH=$(($LIST_LENGTH / 2 ));fi;
}
function get_data ()
{
#fix json problem first seen Apr 12 2023, where each item in the json file takes two lines, not one. While and until this stands, this one-liner corrects the issue. Also LIST_LENGTH=$(($LIST_LENGTH * 2 )) in get_feed_json function, exactly because of this issue
#if [ $db == "f" ]||[ $db == "t" ];then even=2;while [ $even -le $(cat $HOME/.cache/magic-tape/json/video_search.json|wc -l) ];do echo "$(head -$even $HOME/.cache/magic-tape/json/video_search.json|tail +$even)">>$HOME/.cache/magic-tape/json/video_search_temp.json;even=$(($even +2));done;mv $HOME/.cache/magic-tape/json/video_search_temp.json $HOME/.cache/magic-tape/json/video_search.json;fi;
jq '.id' $HOME/.cache/magic-tape/json/video_search.json|sed 's/\\"/⁆/g;s/"//g;s/⁆/"/g'>$HOME/.cache/magic-tape/search/video/ids.txt;
jq '.title' $HOME/.cache/magic-tape/json/video_search.json|sed 's/\\"/⁆/g;s/"//g;s/⁆/"/g'>$HOME/.cache/magic-tape/search/video/titles.txt;
jq '.duration_string' $HOME/.cache/magic-tape/json/video_search.json|sed 's/\\"/⁆/g;s/"//g;s/⁆/"/g'>$HOME/.cache/magic-tape/search/video/lengths.txt;
jq '.url' $HOME/.cache/magic-tape/json/video_search.json|sed 's/\\"/⁆/g;s/"//g;s/⁆/"/g'>$HOME/.cache/magic-tape/search/video/urls.txt;
jq '.timestamp' $HOME/.cache/magic-tape/json/video_search.json>$HOME/.cache/magic-tape/search/video/timestamps.txt;
jq '.description' $HOME/.cache/magic-tape/json/video_search.json>$HOME/.cache/magic-tape/search/video/descriptions.txt;
jq '.view_count' $HOME/.cache/magic-tape/json/video_search.json|sed 's/\\"/⁆/g;s/"//g;s/⁆/"/g'>$HOME/.cache/magic-tape/search/video/views.txt;
jq '.channel_id' $HOME/.cache/magic-tape/json/video_search.json|sed 's/\\"/⁆/g;s/"//g;s/⁆/"/g'>$HOME/.cache/magic-tape/search/video/channel_ids.txt;
jq '.channel' $HOME/.cache/magic-tape/json/video_search.json|sed 's/\\"/⁆/g;s/"//g;s/⁆/"/g'>$HOME/.cache/magic-tape/search/video/channel_names.txt;
jq '.thumbnails[0].url' $HOME/.cache/magic-tape/json/video_search.json|sed 's/\\"/⁆/g;s/"//g;s/⁆/"/g'|sed 's/\.jpg.*$/\.jpg/g'>$HOME/.cache/magic-tape/search/video/image_urls.txt;
jq '.live_status' $HOME/.cache/magic-tape/json/video_search.json>$HOME/.cache/magic-tape/search/video/live_status.txt;
epoch="$(jq '.epoch' $HOME/.cache/magic-tape/json/video_search.json|head -1)";
Y_epoch="$(date --date=@$epoch +%Y|sed 's/^0*//')";
M_epoch="$(date --date=@$epoch +%m|sed 's/^0*//')";
D_epoch="$(date --date=@$epoch +%j|sed 's/^0*//')";
if [[ $db == "c" ]];
then jq '.playlist_uploader' $HOME/.cache/magic-tape/json/video_search.json|sed 's/"//g'>$HOME/.cache/magic-tape/search/video/channel_names.txt;
jq '.playlist_uploader_id' $HOME/.cache/magic-tape/json/video_search.json|sed 's/"//g'>$HOME/.cache/magic-tape/search/video/channel_ids.txt;
fi;
cat /dev/null>$HOME/.cache/magic-tape/search/video/thumbnails.txt;
cat /dev/null>$HOME/.cache/magic-tape/search/video/shared.txt;
i=1;
while [ $i -le $(cat $HOME/.cache/magic-tape/search/video/titles.txt|wc -l) ];
do img_path="$HOME/.cache/magic-tape/jpg/img-$(sed -n "${i}p" $HOME/.cache/magic-tape/search/video/ids.txt).jpg";
if [ ! -f "$img_path" ];
then echo "url = \"$(sed -n "${i}p" $HOME/.cache/magic-tape/search/video/image_urls.txt)\"">>$HOME/.cache/magic-tape/search/video/thumbnails.txt;
echo "output = \"$img_path\"">>$HOME/.cache/magic-tape/search/video/thumbnails.txt;
cp "$SHARE_DIR"/wait.png $HOME/.cache/magic-tape/jpg/img-$(sed -n "${i}p" $HOME/.cache/magic-tape/search/video/ids.txt).jpg
fi;
### parse approx date
timestamp="$(sed -n "${i}p" $HOME/.cache/magic-tape/search/video/timestamps.txt)";
if [[ "$timestamp" != "null" ]];then
now=$(date +%s)
diff=$((now - $timestamp))
days=$((diff / 86400))
hours=$(((diff % 86400) / 3600))
minutes=$(((diff % 3600) / 60))
d=$(date -d @$timestamp '+%Y-%m-%d')
if [ $diff -lt 60 ]; then
approximate_date="$minutes minute(s) ago"
elif [ $diff -lt 3600 ]; then
[ $minutes -eq 1 ] && approximate_date="$d (1 minute ago)" || approximate_date="$d ($minutes minutes ago)"
elif [ $days -eq 0 ]; then
[ $hours -eq 1 ] && approximate_date="$d (1 hour ago)" || approximate_date="$d ($hours hours ago)"
elif [ $days -eq 1 ]; then approximate_date="$d (yesterday)"
elif [ $days -lt 7 ]; then approximate_date="$d ($days days ago)"
elif [ $days -lt 30 ]; then
weeks=$((days / 7))
[ $weeks -eq 1 ] && approximate_date="$d (1 week ago)" || approximate_date="$d ($weeks weeks ago)"
elif [ $days -lt 365 ]; then
months=$((days / 30))
[ $months -eq 1 ] && approximate_date="1 month ago" || approximate_date="$months months ago"
else
years=$((days / 365))
[ $years -eq 1 ] && approximate_date="1 year ago" || approximate_date="$years years ago"
fi
else approximate_date="$(sed -n "${i}p" $HOME/.cache/magic-tape/search/video/live_status.txt|sed 's/_/ /g;s/"//g')";
fi;
echo $approximate_date>>$HOME/.cache/magic-tape/search/video/shared.txt;
((i++));
done;
echo -e "${Gray}Downloading thumbnails...${normal}";
curl -s -K $HOME/.cache/magic-tape/search/video/thumbnails.txt 2>/dev/null& echo -e "${Gray}Background thumbnails download.${normal}";
if [ $ITEM -gt 1 ];then echo "Previous Page">>$HOME/.cache/magic-tape/search/video/titles.txt;fi;
if [ $(cat $HOME/.cache/magic-tape/search/video/ids.txt|wc -l) -ge $LIST_LENGTH ];then echo "Next Page">>$HOME/.cache/magic-tape/search/video/titles.txt;fi;
echo "Abort Selection">>$HOME/.cache/magic-tape/search/video/titles.txt;
}
function select_video ()
{
PLAY="";
PLAY=" $(cat -n $HOME/.cache/magic-tape/search/video/titles.txt|sed 's/^. *//g' |fzf\
--info=hidden \
--layout=reverse \
--height=100% \
--prompt="Select video: " \
--header="$fzf_header" \
--preview-window=left,40% \
--tabstop=1 \
--no-margin \
--bind=right:accept \
--expect=shift-left,shift-right \
+m \
-i \
--exact \
--preview='
hght=$(($FZF_PREVIEW_COLUMNS /3));\
if [[ "$IMAGE_SUPPORT" == "kitty" ]];then clear_image;fi;\
i=$(echo {}|cut -b -2);echo $i>$HOME/.cache/magic-tape/search/video/index.txt;\
if [[ "$IMAGE_SUPPORT" == "ueberz"* ]];then ll=0; while [ $ll -le $hght ];do echo "";((ll++));done;fi;\
if [[ "$IMAGE_SUPPORT" == "kitty" ]]&&[[ $kitty_version -le 21 ]];then ll=0; while [ $ll -le $hght ];do echo "";((ll++));done;fi;\
TITLE="$(sed -n "${i}p" $HOME/.cache/magic-tape/search/video/titles.txt)";\
channel_name="$(sed -n "${i}p" $HOME/.cache/magic-tape/search/video/channel_names.txt)";\
channel_jpg="$(sed -n "${i}p" $HOME/.cache/magic-tape/search/video/channel_ids.txt)"".jpg";\
if [[ "$TITLE" == "Previous Page" ]];then draw_preview 1 1 $FZF_PREVIEW_COLUMNS $hght "$SHARE_DIR"/previous.png;\
elif [[ "$TITLE" == "Next Page" ]];then draw_preview 1 1 $FZF_PREVIEW_COLUMNS $hght "$SHARE_DIR"/next.png;\
elif [[ "$TITLE" == "Abort Selection" ]];then draw_preview 1 1 $FZF_PREVIEW_COLUMNS $hght "$SHARE_DIR"/abort.png;\
else draw_preview 1 1 $FZF_PREVIEW_COLUMNS $hght "$HOME/.cache/magic-tape/jpg/img-""$(sed -n "${i}p" $HOME/.cache/magic-tape/search/video/ids.txt)"".jpg";\
fi;\
ll=1; echo -ne "\x1b[38;5;241m"; while [ $ll -le $FZF_PREVIEW_COLUMNS ];do echo -n -e "─";((ll++));done;echo -n -e "$normal";\
echo -e "\n"$Yellow"$TITLE"$normal"" |fold -w $FZF_PREVIEW_COLUMNS -s ; \
ll=1; echo -ne "\x1b[38;5;241m"; while [ $ll -le $FZF_PREVIEW_COLUMNS ];do echo -n -e "─";((ll++));done;echo -n -e "$normal";\
if [[ $TITLE != "Abort Selection" ]]&&[[ $TITLE != "Previous Page" ]]&&[[ $TITLE != "Next Page" ]];\
then LENGTH="$(sed -n "${i}p" $HOME/.cache/magic-tape/search/video/lengths.txt)";\
SHARED="$(sed -n "${i}p" $HOME/.cache/magic-tape/search/video/shared.txt)";\
if [[ $SHARED == "is upcoming" ]];then echo -e "\n""$Green""Shared: ""$Red""$SHARED""$normal";else echo -e "\n""$Green""Shared: ""$Cyan""$SHARED""$normal";fi;\
if [[ $LENGTH != "null" ]];then echo -e "$Green""Length: ""$Cyan""$LENGTH""$normal";fi;\
VIEWS="$(sed -n "${i}p" $HOME/.cache/magic-tape/search/video/views.txt)";\
if [[ $VIEWS != "null" ]];then printf "${Green}Views :${Cyan} %'\''d\n" $VIEWS;fi;\
if [[ $db != "c" ]];\
then ll=1; echo -ne "\x1b[38;5;241m"; while [ $ll -le $FZF_PREVIEW_COLUMNS ];do echo -n -e "─";((ll++));done;echo -n -e "$normal";\
echo -e "\n"$Green"Channel: "$Yellow"$channel_name" |fold -w $FZF_PREVIEW_COLUMNS -s;\
fi;\
DESCRIPTION="$(sed -n "${i}p" $HOME/.cache/magic-tape/search/video/descriptions.txt)";\
if [[ $DESCRIPTION != "null" ]];
then ll=1; echo -ne "\x1b[38;5;241m"; while [ $ll -le $FZF_PREVIEW_COLUMNS ];do echo -n -e "─";((ll++));done;echo -n -e "$normal";\
echo -e "\n\x1b[38;5;250m$DESCRIPTION"$normal""|fold -w $FZF_PREVIEW_COLUMNS -s; \
fi;
fi;')";
clear_image;
i=$(cat $HOME/.cache/magic-tape/search/video/index.txt);
notification_img="$HOME/.cache/magic-tape/jpg/img-"$(sed -n "${i}p" $HOME/.cache/magic-tape/search/video/ids.txt)".jpg";
play_now="$(sed -n "${i}p" $HOME/.cache/magic-tape/search/video/urls.txt)";
TITLE=$(sed -n "${i}p" $HOME/.cache/magic-tape/search/video/titles.txt);
channel_name="$(sed -n "${i}p" $HOME/.cache/magic-tape/search/video/channel_names.txt)";
channel_id="$(sed -n "${i}p" $HOME/.cache/magic-tape/search/video/channel_ids.txt)";
if [ $ITEM -ge $LIST_LENGTH ]&&[[ $PLAY == *"shift-left"* ]]; then TITLE="Previous Page";fi;
if [ $ITEM -le $LIST_LENGTH ]&&[[ $PLAY == *"shift-left"* ]]; then TITLE="Abort Selection";fi;
if [[ $PLAY == *"shift-right"* ]]; then TITLE="Next Page";fi;
if [[ $TITLE == "Next Page" ]];
then ITEM=$(($ITEM + $LIST_LENGTH));
#change implemented when the 2-lines-per-item-in-the-json-file issue appeared
#if [[ $db == "f" ]]||[[ $db == "t" ]]; then ITEM0=$(($ITEM0 + $LIST_LENGTH * 2));else ITEM0=$ITEM;fi;
ITEM0=$ITEM;
fi;
if [[ $TITLE == "Previous Page" ]];
then ITEM=$(($ITEM - $LIST_LENGTH));
#change implemented when the 2-lines-per-item-in-the-json-file issue appeared
#if [[ $db == "f" ]]||[[ $db == "t" ]]; then ITEM0=$(($ITEM0 - $LIST_LENGTH * 2));else ITEM0=$ITEM;fi;
ITEM0=$ITEM;
fi;
if [[ $TITLE == "Abort Selection" ]];then big_loop=0;fi;
if [[ $PLAY == " " ]]; then echo "ABORT!"; TITLE="Abort Selection";big_loop=0;clear;fi;
PLAY="";
}
function download_video ()
{
cd "$HOME""$DOWNLOAD_DIRECTORY";
echo -e "${Gray}Directory: ""$HOME""$DOWNLOAD_DIRECTORY";
echo -e "${Gray}Downloading $play_now${normal}...]";
notify $NOTIFICATION_DURATION "$SHARE_DIR"/download.png "Video Downloading: $TITLE";
yt-dlp "$play_now";
notify $NOTIFICATION_DURATION "$SHARE_DIR"/magic-tape.png "Video Downloading of $TITLE is now complete.";
echo -e "${Green}Video Downloading of${Yellow}${bold} $TITLE ${Green}is now complete.${normal}";
sleep $TERMINAL_MESSAGE_DURATION;
cd ;
clear;
}
function download_audio ()
{
cd "$HOME""$DOWNLOAD_DIRECTORY";
echo -e "${Gray}Directory: ""$HOME""$DOWNLOAD_DIRECTORY";
echo -e "${Gray}Downloading audio of $play_now...${normal}";
notify $NOTIFICATION_DURATION "$SHARE_DIR"/download.png "Audio Downloading: $TITLE";
yt-dlp --extract-audio --audio-quality 0 --embed-thumbnail "$play_now";
notify $NOTIFICATION_DURATION "$SHARE_DIR"/magic-tape.png "Audio Downloading of $TITLE is now complete.";
echo -e "${Green}Audio Downloading of${Yellow}${bold} $TITLE ${Green}is now complete.${normal}";
sleep $TERMINAL_MESSAGE_DURATION;
cd ;
clear;
}
function print_pinned_comment()
{
pindex="$(jq '.comments[].is_pinned' ~/.cache/magic-tape/comments/comments.info.json|grep -n "true")"
if [[ -n "$pindex" ]]
then
pindex="${pindex/:*/}"
((pindex--))
echo -e "${Red}🖈 ${Yellow}$(jq -r ".comments[$pindex].author" $HOME/.cache/magic-tape/comments/comments.info.json) ${Gray}wrote $(jq -r ".comments[$pindex]._time_text" $HOME/.cache/magic-tape/comments/comments.info.json): $(jq -r ".comments[$pindex].like_count" $HOME/.cache/magic-tape/comments/comments.info.json|sed 's/^/🖒 /g;s/🖒 0//g') ${Red}🖈\n${Green}> $(jq -r ".comments[$pindex].text" $HOME/.cache/magic-tape/comments/comments.info.json)"
fi
}
function load_comments()
{
if [[ $COMMENTS_TOGGLE == "yes" ]]
then
cols=$(tput cols)
echo "0">$HOME/.cache/magic-tape/comments/switch.txt
echo -e "${Gray}""Loading description & comments... Please wait."
if [[ $COMMENTS_SORT != "old" ]]
then
yt-dlp -q --write-comments --no-download --extractor-args "youtube:max_comments=$COMMENTS_MAX,all,$COMMENT_REPLIES_MAX;comment_sort=$COMMENTS_SORT" --write-info-json "$play_now" -o $HOME/.cache/magic-tape/comments/comments>/dev/null 2>&1;
com_line=0
com_count="$(jq '.comment_count' $HOME/.cache/magic-tape/comments/comments.info.json)"
draw_line; draw_line
echo -e "\n${Yellow}${bold}DESCRIPTION${normal}\n\n${Green}$(jq '.description' $HOME/.cache/magic-tape/comments/comments.info.json)"
draw_line; draw_line
echo -e "\n${Yellow}${bold}COMMENTS${normal}${Gray} ($com_count)\n\nView $COMMENTS_SORT comments first:\n"
if [[ $com_count -eq 0 ]];then draw_line;fi
if [[ $PINNED_COMMENTS == "first" ]]
then
print_pinned_comment;
fi
while [ $com_line -le $((com_count-1)) ]
do
if [[ $(cat $HOME/.cache/magic-tape/comments/switch.txt) == 0 ]] #check if needs to break load_comments fn, and
then
if [[ "$(jq ".comments[$com_line].is_pinned" $HOME/.cache/magic-tape/comments/comments.info.json)" == "false" ]]
then
echo -e "${Gray}$((com_line+1)) ${Yellow}$(jq -r ".comments[$com_line].author" $HOME/.cache/magic-tape/comments/comments.info.json) ${Gray}wrote $(jq -r ".comments[$com_line]._time_text" $HOME/.cache/magic-tape/comments/comments.info.json): $(jq -r ".comments[$com_line].like_count" $HOME/.cache/magic-tape/comments/comments.info.json|sed 's/^/🖒 /g;s/🖒 0//g')\n> "$Cyan"$(jq -r ".comments[$com_line].text" $HOME/.cache/magic-tape/comments/comments.info.json)"
((com_line++))
draw_line
else
((com_line++))
fi
else break;
fi
done
if [[ $PINNED_COMMENTS == "last" ]]
then
print_pinned_comment
fi
else
yt-dlp -q --write-comments --no-download --extractor-args "youtube:max_comments=all,all,$COMMENT_REPLIES_MAX;comment_sort=new" --write-info-json "$play_now" -o $HOME/.cache/magic-tape/comments/comments>/dev/null 2>&1;
com_count="$(jq '.comment_count' $HOME/.cache/magic-tape/comments/comments.info.json)"
draw_line; draw_line
echo -e "\n${Yellow}${bold}DESCRIPTION${normal}\n\n${Green}$(jq '.description' $HOME/.cache/magic-tape/comments/comments.info.json)"
draw_line; draw_line
if [[ $com_count -gt $COMMENTS_MAX ]];then com_last=$((com_count-COMMENTS_MAX));else com_last=0;fi
echo -e "\n${Yellow}${bold}COMMENTS${normal}${Gray} ($((com_count-com-last)))\n\nView $COMMENTS_SORT comments first:\n"
if [[ $com_count -eq 0 ]];then draw_line;fi
if [[ $PINNED_COMMENTS == "first" ]]
then
print_pinned_comment;
fi
com_line=$((com_count-1))
c_i=0
while [ $com_line -ge $com_last ]
do
if [[ $(cat $HOME/.cache/magic-tape/comments/switch.txt) == 0 ]] #check if needs to break load_comments fn
then
if [[ "$(jq ".comments[$com_line].is_pinned" $HOME/.cache/magic-tape/comments/comments.info.json)" == "false" ]]
then
echo -e "${Gray}$((c_i+1)) ${Yellow}$(jq -r ".comments[$com_line].author" $HOME/.cache/magic-tape/comments/comments.info.json) ${Gray}wrote $(jq -r ".comments[$com_line]._time_text" $HOME/.cache/magic-tape/comments/comments.info.json): $(jq -r ".comments[$com_line].like_count" $HOME/.cache/magic-tape/comments/comments.info.json|sed 's/^/🖒 /g;s/🖒 0//g')\n${Gray}> "$Cyan"$(jq -r ".comments[$com_line].text" $HOME/.cache/magic-tape/comments/comments.info.json)"
((com_line--))
((c_i++))
draw_line
else
((com_line--))
fi
else break;
fi
done
if [[ $PINNED_COMMENTS == "last" ]]
then
print_pinned_comment
fi
###################
fi
fi
if [[ $(cat $HOME/.cache/magic-tape/comments/switch.txt) == 0 ]];then print_mpv_video_shortcuts;fi
}
function message_audio_video ()
{
echo -e "${Gray}Playing: $play_now\nTitle : $TITLE\nChannel: $channel_name${normal}";
if [[ -n "$play_now" ]] && [[ -n "$TITLE" ]] && [[ -z "$(tail -1 $HOME/.cache/magic-tape/history/watch_history.txt|grep "$play_now" )" ]];
then echo "$channel_id"" ""$channel_name"" ""$play_now"" ""$TITLE">>$HOME/.cache/magic-tape/history/watch_history.txt;
#echo "{\"url\": \"$play_now\", \"title\": \"$TITLE\", \"channel\": \"$channel_name\", \"channel_id\": \"$channel_id\"}">>$HOME/.cache/magic-tape/history/watch_history.json;
fi;
notify $NOTIFICATION_DURATION "$notification_img" "Playing: $TITLE";
}
function select_action ()
{
clear;
ACTION="$(echo -e "Play ⭐ Video 144p\nPlay ⭐ ⭐ Video 360p\nPlay ⭐ ⭐ ⭐ Video 720p\nPlay ⭐ ⭐ ⭐ ⭐ Best Video\nPlay ⭐ ⭐ ⭐ ⭐ Best Audio\nDownload Video 🔽\nDownload Audio 🔽\nLike Video ❤️\nBrowse Feed of channel "$channel_name" 📺\nSubscribe to channel "$channel_name" 📋\nOpen in browser 🌐\nCopy link 🔗\nQuit ❌"|eval "$PREF_SELECTOR"\"Select action\")";
case $ACTION in
"Play ⭐ Video 144p") message_audio_video;load_comments&mpv --msg-level=all=no --ytdl-raw-options=format="bv*[height<=144]+ba/b[height<=360] / wv*+ba/w" "$play_now";play_now="";TITLE="";
;;
"Play ⭐ ⭐ Video 360p") message_audio_video;load_comments&mpv --msg-level=all=no --ytdl-raw-options=format="18/bv*[height<=360]+ba/b[height<=480] / wv*+ba/w" "$play_now";play_now="";TITLE="";
;;
"Play ⭐ ⭐ ⭐ Video 720p") message_audio_video;load_comments&mpv --msg-level=all=no --ytdl-raw-options=format="22/bv*[height<=720]+ba/b[height<=720] / wv*+ba/w" "$play_now";play_now="";TITLE="";
;;
"Play ⭐ ⭐ ⭐ ⭐ Best Video") message_audio_video;load_comments&mpv --msg-level=all=no "$play_now";play_now="";TITLE="";
;;
"Play ⭐ ⭐ ⭐ ⭐ Best Audio") message_audio_video;load_comments&mpv --ytdl-raw-options=format=ba "$play_now";play_now="";TITLE="";
;;
"Download Video 🔽") clear;download_video;echo -e "\n${Green}Video Download complete.\n${normal}";
;;
"Download Audio 🔽") clear;download_audio;echo -e "\n${Green}Audio Download complete.${normal}\n";
;;
"Like Video ❤️") clear;
if [[ -z "$(grep "$play_now" $HOME/.cache/magic-tape/history/liked.txt)" ]];
then echo "$channel_id"" ""$channel_name"" ""$play_now"" ""$TITLE">>$HOME/.cache/magic-tape/history/liked.txt;
notify $NOTIFICATION_DURATION "$SHARE_DIR"/magic-tape.png "❤️ Video added to Liked Videos.";
else notify $NOTIFICATION_DURATION "$SHARE_DIR"/magic-tape.png "❤️ Video already added to Liked Videos.";
fi;
;;
"Browse Feed of channel"*) clear;db="c"; P="$channel_id";
channel_feed;
;;
"Subscribe to channel"*) clear;
if [ -n "$(grep $channel_id $HOME/.cache/magic-tape/subscriptions/subscriptions.txt)" ];
then notify $NOTIFICATION_DURATION $HOME/.cache/magic-tape/subscriptions/jpg/$channel_id".jpg" "You are already subscribed to $channel_name ";
else C=${channel_name// /+};C=${C//\'/%27};
if [[ "$C" == "null" ]]; then notify $NOTIFICATION_DURATION "$SHARE_DIR"/magic-tape.png "❌ You cannot subscribe to this channel (null)";
else echo -e "${Gray}Downloading data of $channel_name${normal}${Green} channel...${normal}";
yt-dlp --cookies-from-browser $PREF_BROWSER --flat-playlist --playlist-start 1 --playlist-end 10 -j "https://www.youtube.com/results?search_query="$C"&sp=EgIQAg%253D%253D"|grep "$channel_id">$HOME/.cache/magic-tape/json/channel_search.json;
channel_thumbnail_url="$(jq '.thumbnails[1].url' $HOME/.cache/magic-tape/json/channel_search.json|sed 's/"//g')";
echo -e "${Gray}Dowloading thumbnail of $channel_name channel...${normal}";
curl -s -o $HOME/.cache/magic-tape/subscriptions/jpg/$channel_id".jpg" "https:""$channel_thumbnail_url" 2>/dev/null;
echo -e "${Gray}Done.${normal}";
echo "$channel_id"" ""$channel_name">>$HOME/.cache/magic-tape/subscriptions/subscriptions.txt;
notify $NOTIFICATION_DURATION $HOME/.cache/magic-tape/subscriptions/jpg/$channel_id".jpg" "You have subscribed to $channel_name ";
echo -e "${Red}${bold}NOTICE: ${Yellow}${bold}In order for this action to take effect in YouTube, you need to subscribe manually from a browser as well.\nDo you want to do it now? (Y/y)${normal}"|fold -w 75 -s;
read -N 1 sas;echo -e "\n";
if [[ $sas == Y ]] || [[ $sas == y ]];then $LINK_BROWSER "https://www.youtube.com/channel/"$channel_id&echo -e "${Gray}Opened $PREF_BROWSER${normal}";fi;
fi;
fi;
;;
"Open in browser 🌐")clear;notify $NOTIFICATION_DURATION "$SHARE_DIR"/magic-tape.png "🌐 Opening video in browser..."& $LINK_BROWSER "$play_now";
;;
"Copy link 🔗") clear; copy_to_clipboard "$play_now" && notify $NOTIFICATION_DURATION "$SHARE_DIR"/magic-tape.png "🔗 Link copied to clipboard.";
;;
"Quit ❌") clear;
;;
*)echo -e "\n😕${Yellow}${bold}$db${normal} ${Green}is an invalid key, please try again.${normal}\n"; sleep $TERMINAL_MESSAGE_DURATION;clear;
;;
esac
ACTION="";
echo "1">$HOME/.cache/magic-tape/comments/switch.txt #sends signal to stop load_comments function
sleep .5
}
function empty_query ()
{
clear;
echo "😕 Selection cancelled...";
sleep $TERMINAL_MESSAGE_DURATION;
}
###############################################################################
SHARE_DIR=$HOME/.local/share/magic-tape/png
magic_tape_pid==$(ps -e|grep magic-tape.sh|tail -2|head -1|awk '{print $1}')
kitty_version=$(kitty -v|awk '{print $2}'|sed 's/0.//;s/\..*//')
export -f draw_preview draw_uber clear_image start_ueberzug finalise clean_upp draw_upp
load_config
export IMAGE_SUPPORT UEBERZUG_FIFO_MAGIC_TAPE SOCKET Green Yellow Red Magenta Cyan bold normal $FZF_PREVIEW_COLUMNS $FZF_PREVIEW_LINES SHARE_DIR kitty_version
#trap exit_upp HUP INT QUIT TERM EXIT ERR ABRT
db=""
load_config
if [[ $IMAGE_SUPPORT == "ueberzugpp" ]];then trap exit_upp HUP INT QUIT TERM EXIT ERR ABRT ;clean_upp; fi
clear_image
while [ "$db" != "q" ]
do db="$(echo -e "${Yellow}${bold}┏┳┓┏━┓┏━╸╻┏━╸ ╺┳╸┏━┓┏━┓┏━╸${normal}\n${Yellow}${bold}┃┃┃┣━┫┃╺┓┃┃ ╺━╸ ┃ ┣━┫┣━┛┣╸ ${normal}\n${Yellow}${bold}╹ ╹╹ ╹┗━┛╹┗━╸ ╹ ╹ ╹╹ ┗━╸${normal} \n ${Yellow}${bold}f ${normal}${Red}to browse Subscriptions Feed.${normal} \n ${Yellow}${bold}y ${normal}${Red}to browse YT algorithm Feed. ${normal} \n ${Yellow}${bold}t ${normal}${Red}to browse Trending Feed.${normal} \n ${Yellow}${bold}s${normal} ${Green}to Search for a key word/phrase.${normal} \n ${Yellow}${bold}r ${normal}${Green}to Repeat previous action.${normal} \n ${Yellow}${bold}c ${normal}${Green}to select a Channel Feed.${normal} \n ${Yellow}${bold}l ${normal}${Magenta}to browse your Liked Videos.${normal} \n ${Yellow}${bold}h ${normal}${Magenta}to browse your Watch History.${normal} \n ${Yellow}${bold}j ${normal}${Magenta}to browse your Search History.${normal} \n ${Yellow}${bold}m ${normal}${Cyan}for Miscellaneous Menu.${normal} \n ${Yellow}${bold}q ${normal}${Cyan}to Quit.${normal}"|fzf \
--preview-window=0 \
--disabled \
--color='gutter:-1' \
--reverse \
--ansi \
--tiebreak=begin \
--border=rounded \
+i \
+m \
--nth=1 \
--info=hidden \
--header-lines=3 \
--prompt="Enter:" \
--header-first \
--expect=A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,1,2,3,4,5,6,7,8,9,0 )"
db="$(echo $db|awk '{print $1}')"
case $db in
"f") clear;
big_loop=1;
ITEM=1;
ITEM0=1;
FEED="/feed/subscriptions";
while [ $big_loop -eq 1 ];
do fzf_header="$(echo ${FEED^^}|sed 's/[\/\?=]/ /g') videos $ITEM to $(($ITEM + $(($LIST_LENGTH - 1))))";
get_feed_json;
get_data;
small_loop=1;
while [ $small_loop -eq 1 ];
do select_video ;
if [[ "$TITLE" == "Next Page" ]]||[[ "$TITLE" == "Previous Page" ]];then small_loop=0;fi;
if [[ "$TITLE" == "Abort Selection" ]];then small_loop=0;big_loop=0;fi;
if [[ "$TITLE" != "Abort Selection" ]]&&[[ "$TITLE" != "Next Page" ]]&&[[ "$TITLE" != "Previous Page" ]];then select_action;fi;
done;
done;
clear;clear_image;
;;
"y") clear;
big_loop=1;
ITEM=1;
ITEM0=1;
FEED="";
while [ $big_loop -eq 1 ];
do fzf_header="YT algorithm suggestions, videos $ITEM to $(($ITEM + $(($LIST_LENGTH - 1))))";
get_feed_json;
get_data;
small_loop=1;
while [ $small_loop -eq 1 ];
do select_video ;
if [[ "$TITLE" == "Next Page" ]]||[[ "$TITLE" == "Previous Page" ]];then small_loop=0;fi;
if [[ "$TITLE" == "Abort Selection" ]];then small_loop=0;big_loop=0;fi;
if [[ "$TITLE" != "Abort Selection" ]]&&[[ "$TITLE" != "Next Page" ]]&&[[ "$TITLE" != "Previous Page" ]];then select_action;fi;
done;
done;
clear;
;;
"t") clear;
big_loop=1;
ITEM=1;
ITEM0=1;
FEED="/feed/trending";
while [ $big_loop -eq 1 ];
do fzf_header="$(echo ${FEED^^}|sed 's/[\/\?=]/ /g') videos $ITEM to $(($ITEM + $(($LIST_LENGTH - 1))))";
get_feed_json;
get_data;
small_loop=1;
while [ $small_loop -eq 1 ];
do select_video ;
if [[ "$TITLE" == "Next Page" ]]||[[ "$TITLE" == "Previous Page" ]];then small_loop=0;fi;
if [[ "$TITLE" == "Abort Selection" ]];then small_loop=0;big_loop=0;fi;
if [[ "$TITLE" != "Abort Selection" ]]&&[[ "$TITLE" != "Next Page" ]]&&[[ "$TITLE" != "Previous Page" ]];then select_action;fi;
done;
done;
clear;
;;
"s") clear;
echo -e "🔎 Enter keyword/keyphrase to search for: \n\n";
read P;
if [[ -z "$P" ]];
then empty_query;
else P=${P// /+};
echo "$P">>$HOME/.cache/magic-tape/history/search_history.txt;
search_filter;
big_loop=1;
ITEM=1;
ITEM0=1;
FEED="/results?search_query=""$P""$FILTER";
while [ $big_loop -eq 1 ];
do fzf_header="$(echo "$FILT_PROMPT"|sed 's/ .*/ /')""$(echo ${FEED^^}|sed 's/&SP=.*$//;s/^.*SEARCH_QUERY=/search: /;s/[\/\?=&+]/ /g') videos: $ITEM to $(($ITEM + $(($LIST_LENGTH - 1))))";
get_feed_json;
get_data;
small_loop=1;
while [ $small_loop -eq 1 ];
do select_video ;
if [[ "$TITLE" == "Next Page" ]]||[[ "$TITLE" == "Previous Page" ]];then small_loop=0;fi;
if [[ "$TITLE" == "Abort Selection" ]];then small_loop=0;big_loop=0;fi;
if [[ "$TITLE" != "Abort Selection" ]]&&[[ "$TITLE" != "Next Page" ]]&&[[ "$TITLE" != "Previous Page" ]];then select_action;fi;
done;
done;
fi;
clear;
;;
"r") clear;
db="$(head -1 $HOME/.cache/magic-tape/history/last_action.txt)";
ITEM="$(sed -n "2p" $HOME/.cache/magic-tape/history/last_action.txt)";
ITEM0="$(sed -n "3p" $HOME/.cache/magic-tape/history/last_action.txt)";
FEED="$(sed -n "4p" $HOME/.cache/magic-tape/history/last_action.txt)";
fzf_header="$(sed -n "5p" $HOME/.cache/magic-tape/history/last_action.txt)";
big_loop=1;
first=1;
while [ $big_loop -eq 1 ];
do if [ $first -eq 0 ];then fzf_header="$(echo ${FEED^^}|sed 's/&SP=.*$//;s/^.*SEARCH_QUERY=/search: /;s/[\/\?=]/ /g') videos $ITEM to $(($ITEM + $(($LIST_LENGTH - 1))))";get_feed_json;get_data;fi;
small_loop=1;
while [ $small_loop -eq 1 ];
do select_video ;
first=0;
if [[ "$TITLE" == "Next Page" ]]||[[ "$TITLE" == "Previous Page" ]];then small_loop=0;fi;
if [[ "$TITLE" == "Abort Selection" ]];then small_loop=0;big_loop=0;fi;
if [[ "$TITLE" != "Abort Selection" ]]&&[[ "$TITLE" != "Next Page" ]]&&[[ "$TITLE" != "Previous Page" ]];then select_action;fi;
done;
first=0;
done;
clear;
;;
"c") clear;channel_search="";
if [[ -f $HOME/.cache/magic-tape/subscriptions/subscriptions.txt ]];
then channel_search=yes;
else echo -e " ${Yellow}There are no subscriptions locally imported from YouTube. Do you wish to import your subscriptions now?(Y/y)";
read -N 1 import_subs_prompt ;echo;
if [[ $import_subs_prompt == Y ]]||[[ $import_subs_prompt == y ]];
then import_subscriptions;channel_search=yes;
else channel_search=no;
fi;
fi;
if [[ $channel_search == yes ]];
then channel_name="$(cat $HOME/.cache/magic-tape/subscriptions/subscriptions.txt|cut -d' ' -f2-|eval "$PREF_SELECTOR""\"🔎 Select channel \"")";
echo -e "${Gray}Selected channel: $channel_name"${normal};
if [[ -z "$channel_name" ]];
then empty_query;
else P="$(grep "$channel_name" $HOME/.cache/magic-tape/subscriptions/subscriptions.txt|head -1|awk '{print $1}')";
channel_feed;
fi;
fi;
;;
"h") clear;
TITLE="$(echo -e "ABORT SELECTION\n""$(tac $HOME/.cache/magic-tape/history/watch_history.txt|sed 's/^.*https:\/\/www\.youtube\.com/https:\/\/www\.youtube\.com/g'|cut -d' ' -f2-)"|eval "$PREF_SELECTOR""\"🔎 Select previous video \"")";
if [[ "$TITLE" == "ABORT SELECTION" ]]||[[ -z "$TITLE" ]];
then empty_query;
else HISTORY_TITLE="$(echo "$TITLE"|sed 's/[][}{*\]//g')";