-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathyoutube_to_podcast.bash
More file actions
executable file
·1442 lines (1344 loc) · 59.5 KB
/
youtube_to_podcast.bash
File metadata and controls
executable file
·1442 lines (1344 loc) · 59.5 KB
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
#############################
## Issues ##
#############################
# If you experience any issues, please let me know here:
# https://github.com/goose-ws/bash-scripts
# These scripts are purely a passion project of convenience for myself, so pull requests welcome :)
#############################
## About ##
#############################
# This script will convert a particular YouTube playlist or channel page to a podcast
#############################
## Changelog ##
#############################
# 2025-05-24
# Initial commit
#############################
## Installation ##
#############################
# 1. Download the script .bash file somewhere safe
# 2. Download the script .env file somewhere safe
# 3. Edit the .env file to your liking
# 4. Set the script to run on an hourly cron job, or whatever your preference is
#############################
## Sanity checks ##
#############################
if ! [ -e "/bin/bash" ]; then
echo "This script requires Bash"
exit 255
fi
if [[ -z "${BASH_VERSINFO[0]}" || "${BASH_VERSINFO[0]}" -lt "4" ]]; then
echo "This script requires Bash version 4 or greater"
exit 255
fi
depsArr=("awk" "basename" "chmod" "curl" "date" "ffprobe" "md5sum" "mimetype" "mkdir" "printf" "qrencode" "realpath" "sha256sum" "shuf" "sqlite3" "stat" "xmllint" "yq" "yt-dlp")
depFail="0"
for i in "${depArr[@]}"; do
if [[ "${i:0:1}" == "/" ]]; then
if ! [[ -e "${i}" ]]; then
echo "${i}\\tnot found"
depFail="1"
fi
else
if ! command -v "${i}" > /dev/null 2>&1; then
echo "${i}\\tnot found"
depFail="1"
fi
fi
done
if [[ "${depFail}" -eq "1" ]]; then
echo "Dependency check failed"
exit 255
fi
# Local variables
realPath="$(realpath "${0}")"
scriptName="$(basename "${0}")"
lockFile="${realPath%/*}/.${scriptName}.lock"
# URL of where the most updated version of the script is
updateURL="https://raw.githubusercontent.com/goose-ws/bash-scripts/main/youtube_to_podcast.bash"
# For ease of printing messages
lineBreak="$(printf "\r\n\r\n")"
scriptStart="$(($(date +%s%N)/1000000))"
#############################
## Lockfile ##
#############################
if [[ -e "${lockFile}" ]]; then
if kill -s 0 "$(<"${lockFile}")" > /dev/null 2>&1; then
echo "Lockfile present, refusing to run"
exit 0
else
echo "Removing stale lockfile for PID $(<"${lockFile}")"
fi
fi
echo "${$}" > "${lockFile}"
#############################
## Standard Functions ##
#############################
function printOutput {
case "${1}" in
0) logLevel="[reqrd]";; # Required
1) logLevel="[error]";; # Errors
2) logLevel="[warn] ";; # Warnings
3) logLevel="[info] ";; # Informational
4) logLevel="[verb] ";; # Verbose
5) logLevel="[DEBUG]";; # Debug
esac
if [[ "${1}" -le "${outputVerbosity}" ]]; then
echo "${0##*/} :: $(date "+%Y-%m-%d %H:%M:%S") :: ${logLevel} ${2}"
fi
if [[ "${1}" -le "1" ]]; then
errorArr+=("${2}")
fi
}
function removeLock {
if rm -f "${lockFile}"; then
if ! [[ "${1}" == "silent" ]]; then
printOutput "4" "Lockfile removed"
fi
else
printOutput "1" "Unable to remove lockfile"
fi
}
function badExit {
removeLock
if [[ -z "${2}" ]]; then
printOutput "0" "Received signal: ${1}"
exit "255"
else
if [[ "${telegramErrorMessages,,}" =~ ^(yes|true)$ ]]; then
sendTelegramMessage "<b>${0##*/}</b>${lineBreak}${lineBreak}Error Code ${1}:${lineBreak}${2}" "${telegramErrorChannel}"
fi
printOutput "1" "${2} [Error code: ${1}]"
exit "1"
fi
}
function cleanExit {
if [[ "${1}" == "silent" ]]; then
removeLock "--silent"
else
if [[ "${#errorArr[@]}" -ne "0" ]]; then
printOutput "1" "=== Error Log ==="
for i in "${errorArr[@]}"; do
printOutput "1" "${i}"
done
fi
printOutput "3" "Script executed in $(timeDiff "${scriptStart}")"
removeLock
fi
exit 0
}
function callCurlGet {
# URL to call should be ${1}
if [[ -z "${1}" ]]; then
badExit "1" "No input URL provided for GET"
fi
curlOutput="$(curl -skL -m 15 "${1}" 2>&1)"
curlExitCode="${?}"
if [[ "${curlExitCode}" -eq "28" ]]; then
printOutput "2" "Curl timed out, waiting 10 seconds then trying again"
sleep 10
curlOutput="$(curl -skL "${1}" 2>&1)"
curlExitCode="${?}"
fi
if [[ "${curlExitCode}" -ne "0" ]]; then
printOutput "1" "Curl returned non-zero exit code ${curlExitCode}"
while read -r i; do
printOutput "1" "Output: ${i}"
done <<<"${curlOutput}"
badExit "2" "Bad curl output"
fi
}
function callCurlDownload {
# URL to call should be $1, output should be $2
if [[ -z "${1}" ]]; then
badExit "5" "No input URL provided for download"
elif [[ -z "${2}" ]]; then
badExit "6" "No output path provided for download"
fi
printOutput "5" "Issuing curl command [curl -skL -m 15 \"${1}\" -o \"${2}\"]"
curlOutput="$(curl -skL -m 15 "${1}" -o "${2}" 2>&1)"
curlExitCode="${?}"
if [[ "${curlExitCode}" -eq "28" ]]; then
printOutput "2" "Curl download call returned exit code 28 -- Waiting 5 seconds and reattempting download"
sleep 5
curlOutput="$(curl -skL -m 15 "${1}" -o "${2}" 2>&1)"
curlExitCode="${?}"
fi
if [[ "${curlExitCode}" -ne "0" ]]; then
printOutput "1" "Curl download call returned non-zero exit code ${curlExitCode}"
while read -r i; do
printOutput "1" "Output: ${i}"
done <<<"${curlOutput}"
badExit "7" "Bad curl output"
fi
}
function randomSleep {
# ${1} is minumum seconds, ${2} is maximum
# If no min/max set, min=5 max=30
if [[ -z "${1}" ]]; then
sleepTime="$(shuf -i 5-30 -n 1)"
else
sleepTime="$(shuf -i "${1}"-"${2}" -n 1)"
fi
printOutput "4" "Pausing for ${sleepTime} seconds before continuing"
sleep "${sleepTime}"
}
function throttleDlp {
if ! [[ "${throttleMin}" -eq "0" && "${throttleMax}" -eq "0" ]]; then
printOutput "4" "Throttling after yt-dlp download call"
randomSleep "${throttleMin}" "${throttleMax}"
fi
}
function timeDiff {
# Start time should be passed as ${1}
# End time can be passed as ${2}
# If no end time is defined, will use the time the function is called as the end time
# Time should be provided via: startTime="$(($(date +%s%N)/1000000))"
if [[ -z "${1}" ]]; then
echo "No start time provided"
return 1
else
startTime="${1}"
fi
if [[ -z "${2}" ]]; then
endTime="$(($(date +%s%N)/1000000))"
fi
if [[ "$(( ${endTime:0:10} - ${startTime:0:10} ))" -le "5" ]]; then
printf "%sms\n" "$(( endTime - startTime ))"
else
local T="$(( ${endTime:0:10} - ${startTime:0:10} ))"
local D="$((T/60/60/24))"
local H="$((T/60/60%24))"
local M="$((T/60%60))"
local S="$((T%60))"
(( D > 0 )) && printf '%dd' "${D}"
(( H > 0 )) && printf '%dh' "${H}"
(( M > 0 )) && printf '%dm' "${M}"
(( D > 0 || H > 0 || M > 0 ))
printf '%ds\n' "${S}"
fi
}
function msToTime {
if [[ "${1}" -le "5" ]]; then
printf "%sms\n" "$(( endTime - startTime ))"
else
local T="$(( ${1} / 1000 ))"
local D="$((T/60/60/24))"
local H="$((T/60/60%24))"
local M="$((T/60%60))"
local S="$((T%60))"
(( D > 0 )) && printf '%dd' "${D}"
(( H > 0 )) && printf '%dh' "${H}"
(( M > 0 )) && printf '%dm' "${M}"
(( D > 0 || H > 0 || M > 0 ))
printf '%ds\n' "${S}"
fi
}
function sendTelegramMessage {
# Message to send should be passed as function positional parameter #1
# We can pass an "Admin channel" as positional parameter #2 for the case of sending error messages
callCurlGet "https://api.telegram.org/bot${telegramBotId}/getMe"
if ! [[ "$(yq -p json ".ok" <<<"${curlOutput}")" == "true" ]]; then
printOutput "1" "Telegram bot API check failed"
else
printOutput "4" "Telegram bot API key authenticated [$(yq -p json ".result.username" <<<"${curlOutput}")]"
for chanId in "${telegramChannelId[@]}"; do
if [[ -n "${2}" ]]; then
chanId="${2}"
fi
callCurlGet "https://api.telegram.org/bot${telegramBotId}/getChat?chat_id=${telegramChannelId}"
if [[ "$(yq -p json ".ok" <<<"${curlOutput}")" == "true" ]]; then
printOutput "4" "Telegram channel authenticated [$(yq -p json ".result.title" <<<"${curlOutput}")]"
msgEncoded="$(rawurlencode "${1}")"
callCurlGet "https://api.telegram.org/bot${telegramBotId}/sendMessage?chat_id=${chanId}&parse_mode=html&text=${msgEncoded}"
# Check to make sure Telegram returned a true value for ok
if ! [[ "$(yq -p json ".ok" <<<"${curlOutput}")" == "true" ]]; then
printOutput "1" "Failed to send Telegram message:"
printOutput "1" ""
while read -r i; do
printOutput "1" "${i}"
done < <(yq -p json "." <<<"${curlOutput}")
printOutput "1" ""
else
printOutput "4" "Telegram message sent successfully"
fi
else
printOutput "1" "Telegram channel check failed"
fi
if [[ -n "${2}" ]]; then
break
fi
done
fi
}
#############################
## Unique Functions ##
#############################
function sqDb {
# Validate it
if [[ -z "${idCount}" ]]; then
# We're the first entry
idCount="1"
elif [[ "${idCount}" =~ ^[0-9]+$ ]]; then
# Expected outcome
(( idCount++ ))
elif ! [[ "${idCount}" =~ ^[0-9]+$ ]]; then
printOutput "1" "Data [${idCount}] failed to validate as an INTEGER"
return 1
else
badExit "3" "Impossible condition"
fi
# Execute the command
if sqOutput="$(sqlite3 "${sqliteDb}" "${1}" 2>&1)"; then
if [[ -n "${sqOutput}" ]]; then
echo "${sqOutput}"
fi
else
sqlite3 "${sqliteDb}" "INSERT INTO error_log (TIME, COMMAND, RESULT, OUTPUT) VALUES ('$(date)', '${1//\'/\'\'}', 'Failure', '${sqOutput//\'/\'\'}');"
if [[ -n "${sqOutput}" ]]; then
echo "${sqOutput}"
fi
return 1
fi
}
function clean_lf {
echo "${1///}"
}
function rawurlencode {
local string="${1}"
local strlen="${#string}"
local encoded=""
local pos c o
for (( pos=0 ; pos<strlen ; pos++ )); do
c="${string:$pos:1}"
case "${c}" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'${c}"
esac
encoded+="${o}"
done
echo "${encoded}"
}
function html_encode {
local input="${1}"
# Order is important: & must be encoded first.
input="${input//&/\&}"
input="${input//</\<}"
input="${input//>/\>}"
input="${input//\"/\"}"
input="${input//\'/\'}" # or '
input="${input//“/\"}" # Left double quote
input="${input//”/\"}" # Right double quote
echo "${input}"
}
function cdata_encode {
local input
input="$(html_encode "${1}")"
# Convert URLs to clickable links using sed
input=$(sed -E 's|(http[s]?://[^[:space:]]+)|<a href="\1">\1</a>|g' <<< "${input}")
# Convert double newlines to paragraph tags
input="${input//$'\n\n'/<\/p><p>}"
# Convert single newlines to line breaks
#input="${input//$'\n'/<br \/>$'\n'}"
input="${input//$'\n'/<br \/>}"
# Break the </p><p> in to two lines
#input="${input//<\/p><p>/<\/p>$'\n'<p>}"
# Wrap the content in paragraph tags
echo "<p>${input}</p>"
}
function generate_uuid {
# Generate SHA-256
local hash
hash="$(sha256sum <<<"${1}")"
# Format the hash into a UUID-like format from the first 32 characters
echo "${hash:0:8}-${hash:8:4}-4${hash:13:3}-${hash:17:4}-${hash:21:12}"
}
function ytApiCall {
if [[ -z "${1}" ]]; then
printOutput "1" "No API endpoint passed for YouTube API call"
return 1
fi
callCurlGet "https://www.googleapis.com/youtube/v3/${1}&key=${ytApiKey}"
# Check for a 400 or 403 error code
errorCode="$(yq -p json ".error.code" <<<"${curlOutput}")"
if [[ "${errorCode}" == "403" || "${errorCode}" == "400" ]]; then
if [[ "${errorCode}" == "403" ]]; then
badExit "4" "API key exhaused, unable to preform API calls."
elif [[ "${errorCode}" == "400" ]]; then
badExit "5" "API key appears to be invalid"
fi
else
(( apiCallsYouTube++ ))
# Account for unit cost
if [[ "${1%%\?*}" == "videos" ]]; then
# Costs 5 units
totalUnits="$(( totalUnits + 5 ))"
totalVideoUnits="$(( totalVideoUnits + 5 ))"
elif [[ "${1%%\?*}" == "captions" ]]; then
# Costs 50 units
totalUnits="$(( totalUnits + 50 ))"
totalCaptionsUnits="$(( totalCaptionsUnits + 50 ))"
elif [[ "${1%%\?*}" == "channels" ]]; then
# Costs 8 units
totalUnits="$(( totalUnits + 8 ))"
totalChannelsUnits="$(( totalChannelsUnits + 8 ))"
elif [[ "${1%%\?*}" == "playlists" ]]; then
# Costs 3 units
totalUnits="$(( totalUnits + 3 ))"
totalPlaylistsUnits="$(( totalPlaylistsUnits + 3 ))"
fi
fi
}
function sponsorApiCall {
callCurlGet "https://sponsor.ajay.app/api/${1}" "goose's bash script - contact [github <at> goose <dot> ws] for any concerns or questions"
(( apiCallsSponsor++ ))
}
function generate_qr {
# Check if the URL is provided
if [[ -z "${1}" ]]; then
printOutput "1" "No URL passed to generate QR code"
return 1
fi
# Get terminal width and height
term_width=$(tput cols)
term_height=$(tput lines)
# Minimum dimensions required for a larger QR code (59x31)
min_width=58
min_height=30
# Check if the terminal is large enough
if [[ "${term_width}" -lt "${min_width}" ]] || [[ "${term_height}" -lt "${min_height}" ]]; then
printOutput "1" "Terminal is too small to generate QR code (Minimum size required: ${min_width}x${min_height})"
return 1
fi
# Generate the QR code
printOutput "3" "QR code for URL [${1}]"
while read -r line; do
printOutput "3" "${line}"
done < <(qrencode -t ANSIUTF8 <<<"${1}")
}
#############################
## Signal Traps ##
#############################
trap "badExit SIGINT" INT
trap "badExit SIGQUIT" QUIT
#############################
## Positional parameters ##
#############################
# We can run the positional parameter options without worrying about lockFile
case "${1,,}" in
"-h"|"--help")
echo "-h --help Displays this help message"
echo ""
echo "-u --update Self update to the most recent version"
exit 0
;;
"-u"|"--update")
oldStartLine="0"
while read -r i; do
if [[ "${i}" == "## Changelog ##" ]]; then
oldStartLine="1"
elif [[ "${oldStartLine}" -eq "1" ]]; then
oldStartLine="2"
elif [[ "${oldStartLine}" -eq "2" ]]; then
oldStartLine="${i}"
break
fi
done < "${0}"
if curl -skL "${updateURL}" -o "${0}"; then
if chmod +x "${0}"; then
printOutput "1" "Update complete"
newStartLine="0"
while read -r i; do
if [[ "${newStartLine}" -eq "2" ]]; then
if [[ "${i}" == "${oldStartLine}" ]]; then
break
fi
if [[ "${i:2:1}" =~ ^[0-9]$ ]]; then
changelogArr+=(" ${i#\#}")
else
changelogArr+=(" - ${i#\#}")
fi
elif [[ "${newStartLine}" -eq "1" ]]; then
newStartLine="2"
elif [[ "${i}" == "## Changelog ##" ]]; then
newStartLine="1"
fi
done < <(curl -skL "${updateURL}")
printOutput "1" "Changelog:"
for i in "${changelogArr[@]}"; do
printOutput "1" "${i}"
done
cleanExit
else
badExit "6" "Update downloaded, but unable to \`chmod +x\`"
fi
else
badExit "7" "Unable to download Update"
fi
;;
esac
#############################
## Initiate .env file ##
#############################
if [[ -e "${realPath%/*}/${scriptName%.bash}.env" ]]; then
source "${realPath%/*}/${scriptName%.bash}.env"
else
badExit "8" "Error: \"${realPath%/*}/${scriptName%.bash}.env\" does not appear to exist"
fi
#############################
## Update check ##
#############################
if [[ "${updateCheck,,}" =~ ^(yes|true)$ ]]; then
newest="$(curl -skL "${updateURL}" | md5sum | awk '{print $1}')"
current="$(md5sum "${0}" | awk '{print $1}')"
if ! [[ "${newest}" == "${current}" ]]; then
printOutput "0" "A newer version is available"
# If our ${TERM} is dumb, we're probably running via cron, and should push a message to Telegram, if allowed
if [[ "${TERM,,}" == "dumb" ]]; then
if [[ "${telegramErrorMessages}" =~ ^(yes|true)$ ]]; then
sendTelegramMessage "[${0##*/}] An update is available" "${telegramErrorChannel}"
fi
fi
else
printOutput "3" "No new updates available"
fi
fi
#############################
## Payload ##
#############################
# If we're being invoked by cron, sleep for up to ${cronSleep}
if [[ -t 1 ]]; then
printOutput "5" "Script spawned by interactive terminal"
else
printOutput "4" "Script spawned by cron with PID [${$}]"
if [[ "${cronSleep}" =~ ^[0-9]+$ ]]; then
sleepTime="$(( RANDOM % cronSleep ))"
printOutput "3" "Sleeping for [${sleepTime}] seconds before continuing"
sleep "${sleepTime}"
fi
fi
# Make sure our URL is a playlist ID or a channel ID, not a channel @username
printOutput "3" "################ Validating source URL ################"
printOutput "5" "Source URL [${sourceUrl}]"
id="${sourceUrl#http:\/\/}"
id="${id#https:\/\/}"
id="${id#m\.}"
id="${id#www\.}"
if [[ "${id:0:8}" == "youtu.be" ]]; then
# I think these short URL's can only be a file ID?
badExit "9" "Found single file ID, please provide a channel or playlist"
elif [[ "${id:12:6}" == "shorts" ]]; then
# This is a file ID for a short
badExit "10" "Found short file ID, please provide a channel or playlist"
elif [[ "${id:0:8}" == "youtube." ]]; then
# This can be a file ID (normal, live, or short), a channel ID, a channel name, or a playlist
if [[ "${id:12:1}" == "@" ]]; then
printOutput "4" "Found username"
# It's a username
ytId="${id:13}"
ytId="${ytId%\&*}"
ytId="${ytId%\?*}"
ytId="${ytId%\/*}"
# We have the "@username", we need the channel ID
# Try using yt-dlp as an API First
printOutput "4" "Calling yt-dlp to obtain channel ID from channel handle [@${ytId}]"
channelId="$(yt-dlp -J --playlist-items 0 "https://www.youtube.com/@${ytId}")"
channelId="$(yq -p json ".channel_id" <<<"${channelId}")"
if ! [[ "${channelId}" =~ ^[0-9A-Za-z_-]{23}[AQgw]$ ]]; then
# We don't, let's try the official API
printOutput "3" "Calling API to obtain channel ID from channel handle [@${ytId}]"
ytApiCall "channels?forHandle=@${ytId}&part=snippet"
apiResults="$(yq -p json ".pageInfo.totalResults" <<<"${curlOutput}")"
# Validate it
if [[ -z "${apiResults}" ]]; then
badExit "11" "API lookup for channel ID of handle [${ytId}] returned blank results output (Bad API call?)"
elif [[ "${apiResults}" =~ ^[0-9]+$ ]]; then
# Expected outcome
true
elif ! [[ "${apiResults}" =~ ^[0-9]+$ ]]; then
badExit "12" "API lookup for channel ID of handle [${ytId}] returned non-integer results [${apiResults}]"
else
badExit "13" "Impossible condition"
fi
if [[ "${apiResults}" -eq "0" ]]; then
badExit "14" "API lookup for source parsing returned zero results"
fi
if [[ "$(yq -p json ".pageInfo.totalResults" <<<"${curlOutput}")" -eq "1" ]]; then
channelId="$(yq -p json ".items[0].id" <<<"${curlOutput}")"
# Validate it
if ! [[ "${channelId}" =~ ^[0-9A-Za-z_-]{23}[AQgw]$ ]]; then
badExit "15" "Unable to validate channel ID for [@${ytId}]"
fi
else
badExit "16" "Unable to isolate channel ID for [${sourceUrl}]"
fi
fi
printOutput "2" " __ __ _ _ "
printOutput "2" " \ \ / / (_) | |"
printOutput "2" " \ \ /\ / /_ _ _ __ _ __ _ _ __ __ _| |"
printOutput "2" " \ \/ \/ / _\` | '__| '_ \| | '_ \ / _\` | |"
printOutput "2" " \ /\ / (_| | | | | | | | | | | (_| |_|"
printOutput "2" " \/ \/ \__,_|_| |_| |_|_|_| |_|\__, (_)"
printOutput "2" " __/ | "
printOutput "2" " |___/ "
printOutput "1" "Channel usernames are less reliable than channel ID's, as usernames can be changed, but ID's can not."
printOutput "1" "To have this source indexed, please replace your source URL:"
printOutput "1" " ${sourceUrl}"
printOutput "1" "with its channel ID URL:"
printOutput "1" " https://www.youtube.com/channel/${channelId}"
printOutput "2" " "
badExit "17" "Refusing to index source with a non-constant URL"
elif [[ "${id:12:8}" == "watch?v=" ]]; then
# It's a file ID
badExit "18" "Found single file ID, please provide a channel or playlist"
elif [[ "${id:12:7}" == "channel" ]]; then
# It's a channel ID
itemType="channel"
channelId="${id:20:24}"
printOutput "3" "Validated channel ID [${channelId}]"
elif [[ "${id:12:8}" == "playlist" ]]; then
# It's a playlist
itemType="playlist"
if [[ "${id:26:2}" == "WL" ]]; then
# Watch later
plId="${id:26:2}"
elif [[ "${id:26:2}" == "LL" ]]; then
# Liked videos
plId="${id:26:2}"
elif [[ "${id:26:2}" == "PL" ]]; then
# Public playlist?
plId="${id:26:34}"
fi
printOutput "3" "Validated playlist ID [${plId}]"
fi
else
badExit "19" "Unable to parse input [${id}]"
fi
# If there is no sqlite db, create one
sqliteDb="${realPath%/*}/.${scriptName}.db"
if ! [[ -e "${sqliteDb}" ]]; then
printOutput "3" "############### Initializing database #################"
startTime="$(($(date +%s%N)/1000000))"
newDb="true"
sqlite3 "${sqliteDb}" "CREATE TABLE files( \
ID INTEGER PRIMARY KEY AUTOINCREMENT, \
YTID TEXT, \
TITLE TEXT, \
TIMESTAMP INTEGER, \
THUMBNAIL TEXT, \
DESC TEXT, \
TYPE TEXT, \
STATUS TEXT, \
ERROR TEXT, \
PATH TEXT, \
DURATION INTERGER, \
SB_AVAILABLE TEXT, \
UPDATED INTEGER);"
printOutput "3" "Main database initialized"
sqlite3 "${sqliteDb}" "CREATE TABLE podcast_info( \
ID INTEGER PRIMARY KEY AUTOINCREMENT, \
TITLE TEXT, \
DESC TEXT, \
IMAGE TEXT, \
UPDATED INTEGER);"
printOutput "3" "Information table initialized"
sqlite3 "${sqliteDb}" "CREATE TABLE error_log( \
ID INTEGER PRIMARY KEY AUTOINCREMENT, \
TIME TEXT, \
RESULT TEXT, \
COMMAND TEXT, \
OUTPUT TEXT);"
printOutput "3" "Error log initialized"
printOutput "3" "Database initialization complete [Took $(timeDiff "${startTime}")]"
fi
# If we're dealing with a new database, get our playlist/channel info
if [[ "${newDb}" == "true" ]]; then
printOutput "3" "########### Initializing channel information ##########"
startTime="$(($(date +%s%N)/1000000))"
if [[ "${itemType}" == "channel" ]]; then
# API call
printOutput "5" "Calling API for channel info [${channelId}]"
ytApiCall "channels?id=${channelId}&part=snippet,statistics,brandingSettings"
elif [[ "${itemType}" == "playlist" ]]; then
# API call
printOutput "5" "Calling API for playlist info [${plId}]"
ytApiCall "playlists?part=snippet&id=${plId}"
fi
apiResults="$(yq -p json ".pageInfo.totalResults" <<<"${curlOutput}")"
# Validate it
if [[ -z "${apiResults}" ]]; then
badExit "20" "No data provided to validate integer"
elif [[ "${apiResults}" =~ ^[0-9]+$ ]]; then
# Expected outcome
true
elif ! [[ "${apiResults}" =~ ^[0-9]+$ ]]; then
badExit "21" "Data [${apiResults}] failed to validate as an integer"
else
badExit "22" "Impossible condition"
fi
if [[ "${apiResults}" -eq "0" ]]; then
badExit "23" "API lookup for channel info returned zero results"
fi
# Get the channel name
podName="$(yq -p json ".items[0].snippet.title" <<<"${curlOutput}")"
# Validate it
if [[ -z "${podName}" ]]; then
badExit "24" "No channel name returned from API lookup for channel ID [${channelId}]"
fi
printOutput "5" "Channel name [${podName}]"
# Get the channel description
podDesc="$(yq -p json ".items[0].snippet.description" <<<"${curlOutput}")"
if [[ -z "${podDesc}" || "${podDesc}" == "null" ]]; then
# No channel description set
printOutput "5" "No channel description set"
podDesc="${sourceUrl}"
else
printOutput "5" "Channel description found [${#podDesc} characters]"
podDesc="${podDesc}${lineBreak}-----${lineBreak}${sourceUrl}"
fi
# Get the channel image URL
podImage="$(yq -p json ".items[0].snippet.thumbnails | to_entries | sort_by(.value.height) | reverse | .0 | .value.url" <<<"${curlOutput}")"
printOutput "5" "Channel image URL [${podImage}]"
# Store them in the database
if sqDb "INSERT INTO podcast_info (TITLE, UPDATED) VALUES ('${podName//\'/\'\'}', $(date +%s));"; then
printOutput "4" "Successfully initailized source in database"
else
badExit "25" "Failed to initialize source in database"
fi
if sqDb "UPDATE podcast_info SET DESC = '${podDesc//\'/\'\'}', UPDATED = $(date +%s);"; then
printOutput "4" "Successfully updated description in database"
else
badExit "26" "Failed to update description in database"
fi
if sqDb "UPDATE podcast_info SET IMAGE = '${podImage//\'/\'\'}', UPDATED = $(date +%s);"; then
printOutput "4" "Successfully updated image in database"
else
badExit "27" "Failed to update image in database"
fi
printOutput "3" "Channel information successfully initailized [Took $(timeDiff "${startTime}")]"
else
dbCount="$(sqDb "SELECT COUNT(1) FROM podcast_info;")"
if [[ "${dbCount}" -ne "1" ]]; then
badExit "28" "Found [${dbCount}] source items in podcast_info table -- Database corruption"
fi
podName="$(sqDb "SELECT TITLE FROM podcast_info;")"
podDesc="$(sqDb "SELECT DESC FROM podcast_info;")"
podImage="$(sqDb "SELECT IMAGE FROM podcast_info;")"
fi
# Make sure we have a valid workDir
workDir="${workDir%/}/${podName//[^a-zA-Z0-9._-]/_}"
printOutput "5" "Setting work directory to [${workDir}]"
if ! [[ -d "${workDir}" ]]; then
if mkdir -p "${workDir}"; then
printOutput "5" "Workdir [${workDir}] created"
callCurlDownload "${podImage}" "${workDir}/cover.jpg"
else
badExit "29" "Failed to create work directory [${workDir}]"
fi
else
printOutput "5" "Verified work directory [${workDir}]"
fi
# Get the list of file ID's we're working with from the source URL
printOutput "3" "################# Retrieving file ID's ################"
startTime="$(($(date +%s%N)/1000000))"
if ! readarray -t vidIds < <(yt-dlp --flat-playlist --no-warnings --print "%(id)s" "${sourceUrl}" 2>&1); then
badExit "30" "Failed to pull file ID list for source URL [${sourceUrl}]"
else
printOutput "3" "Pulled [${#vidIds[@]}] file ID's from source [Took $(timeDiff "${startTime}")]"
fi
# Number them
printOutput "3" "############## Assigning episode numbers ##############"
startTime="$(($(date +%s%N)/1000000))"
declare -A epNum
epNumDigit="${#vidIds[@]}"
for ytId in "${vidIds[@]}"; do
epNum["_${ytId}"]="${epNumDigit}"
(( epNumDigit-- ))
done
printOutput "3" "Done [Took $(timeDiff "${startTime}")]"
if [[ "${keepLimit}" -ne "0" ]]; then
currentPos="0"
fi
printOutput "3" "################# Processing file ID's ################"
startTime="$(($(date +%s%N)/1000000))"
downloadQueue="0"
for ytId in "${vidIds[@]}"; do
(( currentPos++ ))
# Check and see if the file ID is already in the database or not
dbCount="$(sqDb "SELECT COUNT(1) FROM files WHERE YTID = '${ytId//\'/\'\'}';")"
if [[ "${dbCount}" -eq "0" ]]; then
printOutput "3" "Processing file ID [${ytId}]"
# It is not, do we need to worry about skipping it?
if [[ "${keepLimit}" -ne "0" && "${downloadQueue}" -ge "${keepLimit}" ]]; then
printOutput "4" "Skipping file ID [${ytId}] in position [${currentPos}] given limit of [${keepLimit}] files to keep"
# Add it to the database as a skipped item
if sqDb "INSERT INTO files (YTID, STATUS, ERROR, UPDATED) VALUES ('${ytId//\'/\'\'}', 'skipped', 'Skipping file ID given limit of [${keepLimit}] files to keep', $(date +%s));"; then
printOutput "5" "Added file ID [${ytId}] to database"
else
badExit "31" "Failed to add file ID [${ytId}] to database"
fi
continue
fi
# If we've gotten this far, we can keep it, add it to the database
# Call the YouTube Data API for info on the video
unset dbCount vidTitle vidTitleClean channelId uploadDate uploadEpoch uploadYear vidDesc vidType vidStatus vidError sponsorCurl
# Get the video info
printOutput "5" "Calling API for video info [${ytId}]"
ytApiCall "videos?id=${ytId}&part=snippet,liveStreamingDetails"
# Check to make sure we got a result
apiResults="$(yq -p json ".pageInfo.totalResults" <<<"${curlOutput}")"
# Validate it
if [[ -z "${apiResults}" ]]; then
printOutput "1" "No data provided to validate integer"
return 1
elif [[ "${apiResults}" =~ ^[0-9]+$ ]]; then
# Expected outcome
true
elif ! [[ "${apiResults}" =~ ^[0-9]+$ ]]; then
printOutput "1" "Data [${apiResults}] failed to validate as an integer"
return 1
else
badExit "32" "Impossible condition"
fi
if [[ "${apiResults}" -eq "0" ]]; then
printOutput "1" "file ID [${ytId}] API lookup failed"
continue
elif [[ "${apiResults}" -eq "1" ]]; then
# Get the video title
vidTitle="$(yq -p json ".items[0].snippet.title" <<<"${curlOutput}")"
# Get the video description
# Blank if none set
vidDesc="$(yq -p json ".items[0].snippet.description" <<<"${curlOutput}")"
# Get the upload date and time
uploadDate="$(yq -p json ".items[0].snippet.publishedAt" <<<"${curlOutput}")"
# Get the video type (Check to see if it's a live broadcast)
vidType="$(yq -p json ".items[0].snippet.liveBroadcastContent" <<<"${curlOutput}")"
# Get the broadcast start time (Will only return value if it's a live broadcast)
broadcastStart="$(yq -p json ".items[0].liveStreamingDetails.actualStartTime" <<<"${curlOutput}")"
# Get the maxres thumbnail URL
thumbUrl="$(yq -p json ".items[0].snippet.thumbnails | to_entries | .[-1].value.url" <<<"${curlOutput}")"
else
badExit "33" "Impossible condition"
fi
# Get the video title
if [[ -z "${vidTitle}" ]]; then
printOutput "1" "File ID [${ytId}] API lookup returned blank result for title"
continue
fi
printOutput "5" "Video title [${vidTitle}]"
# Put it in the database
if sqDb "INSERT INTO files (YTID, TITLE, UPDATED) VALUES ('${ytId//\'/\'\'}', '${vidTitle//\'/\'\'}', $(date +%s));"; then
printOutput "5" "Added file ID [${ytId}] with title [${vidTitle}] to database"
else
badExit "34" "Failed to add file ID [${ytId}] with title [${vidTitle}] to database"
fi
# Get the upload timestamp
if [[ -z "${uploadDate}" ]]; then
printOutput "1" "Upload date lookup failed for video [${ytId}]"
return 1
fi
# Convert the date to a Unix timestamp
uploadEpoch="$(date --date="${uploadDate}" "+%s")"
if ! [[ "${uploadEpoch}" =~ ^[0-9]+$ ]]; then
printOutput "1" "Unable to convert upload date [${uploadDate}] to unix epoch timestamp [${uploadEpoch}]"
return 1
fi
printOutput "5" "Upload timestamp [${uploadEpoch}]"
# Put it in the database
if sqDb "UPDATE files SET TIMESTAMP = ${uploadEpoch}, UPDATED = $(date +%s) WHERE YTID = '${ytId//\'/\'\'}';"; then
printOutput "5" "Updated epoch timestamp [${uploadEpoch}] for file ID [${ytId}] in database"
else
badExit "35" "Failed to update epoch timestamp [${uploadEpoch}] for file ID [${ytId}] in database"
fi
# Get the video thumbnail
printOutput "5" "Thumbnail URL [${thumbUrl}]"
# Put it in the database
if sqDb "UPDATE files SET THUMBNAIL = '${thumbUrl//\'/\'\'}', UPDATED = $(date +%s) WHERE YTID = '${ytId//\'/\'\'}';"; then
printOutput "5" "Updated thumbnail URL [${thumbUrl}] for file ID [${ytId}] in database"
else
badExit "36" "Failed to update thumbnail URL [${thumbUrl}] for file ID [${ytId}] in database"
fi
# Get the video description
if [[ "${vidDesc}" == " " ]]; then
unset vidDesc
fi
if [[ -z "${vidDesc}" ]]; then
printOutput "5" "No video description"
vidDesc="https://www.youtube.com/watch?v=${ytId}"
else
printOutput "5" "Video description present [${#vidDesc} characters]"
vidDesc="${vidDesc}${lineBreak}-----${lineBreak}https://www.youtube.com/watch?v=${ytId}"
fi
# Put it in the database
if sqDb "UPDATE files SET DESC = '${vidDesc//\'/\'\'}', UPDATED = $(date +%s) WHERE YTID = '${ytId//\'/\'\'}';"; then
printOutput "5" "Updated description [${#vidDesc} characters] for file ID [${ytId}] in database"
else
badExit "37" "Failed to update description [${#vidDesc} characters] for file ID [${ytId}] in database"
fi
# Get the video type (Regular / Short / Live)
if [[ -z "${vidType}" ]]; then
printOutput "1" "Video type lookup for file ID [${ytId}] returned blank result"
continue
elif [[ "${vidType}" == "none" || "${vidType}" == "not_live" || "${vidType}" == "was_live" ]]; then
# Not currently live
# Check to see if it's a previous broadcast
if [[ -z "${broadcastStart}" ]]; then
# This should not be blank, it should be 'null' or a date/time
printOutput "1" "Broadcast start time lookup for file ID [${ytId}] returned blank result [${broadcastStart}]"
continue
elif [[ "${broadcastStart}" == "null" || "${vidType}" == "not_live" ]]; then
# It doesn't have one. Must be a short, or a regular video.
# Use our bullshit to find out
httpCode="$(curl -m 15 -s -I -o /dev/null -w "%{http_code}" "https://www.youtube.com/shorts/${ytId}")"
if [[ "${httpCode}" == "000" ]]; then
# We're being throttled
printOutput "2" "Throttling detected"
randomSleep "5" "15"
httpCode="$(curl -m 15 -s -I -o /dev/null -w "%{http_code}" "https://www.youtube.com/shorts/${ytId}")"
fi
if [[ -z "${httpCode}" ]]; then
printOutput "1" "Curl lookup to determine video type returned blank result [${httpCode}]"
continue
elif [[ "${httpCode}" == "200" ]]; then
# It's a short
printOutput "4" "Determined video to be a short"
vidType="short"
elif [[ "${httpCode}" == "303" ]]; then
# It's a regular video
printOutput "4" "Determined video to be a standard video"
vidType="normal"
elif [[ "${httpCode}" == "404" ]]; then
# No such video exists
printOutput "1" "Curl lookup returned HTTP code 404 for file ID [${ytId}]"
continue
else
printOutput "1" "Curl lookup to determine file ID [${ytId}] type returned unexpected result [${httpCode}]"
continue
fi
elif [[ "${broadcastStart}" =~ ^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]Z$ || "${vidType}" == "was_live" ]]; then
printOutput "4" "file ID [${ytId}] detected to be a past live broadcast"
vidType="waslive"
else
printOutput "Broadcast start time lookup returned unexpected result [${broadcastStart}]"
continue
fi
elif [[ "${vidType}" == "live" || "${vidType}" == "is_live" || "${vidType}" == "upcoming" ]]; then
# Currently, or going to be, live
printOutput "2" "file ID [${ytId}] detected to be a live broadcast"
vidType="live"
else