about summary refs log tree commit diff stats
path: root/hm
diff options
context:
space:
mode:
Diffstat (limited to 'hm')
-rw-r--r--hm/soispha/pkgs/scripts.nix17
-rwxr-xr-xhm/soispha/pkgs/scripts/wrappers/ytc94
2 files changed, 111 insertions, 0 deletions
diff --git a/hm/soispha/pkgs/scripts.nix b/hm/soispha/pkgs/scripts.nix
index d1a78c29..0b32a9c1 100644
--- a/hm/soispha/pkgs/scripts.nix
+++ b/hm/soispha/pkgs/scripts.nix
@@ -193,6 +193,22 @@
     path = "wrappers";
     dependencies = builtins.attrValues {inherit (pkgs) libvirt;};
   };
+  ytc-scr = write_script {
+    name = "ytc";
+    path = "wrappers";
+    dependencies = builtins.attrValues {
+      inherit
+        (pkgs)
+        jq
+        yt-dlp
+        ytcc
+        csvtool
+        mpv
+        ffmpeg
+        gnused
+        ;
+    };
+  };
   yti-scr = write_script {
     name = "yti";
     path = "wrappers";
@@ -214,5 +230,6 @@ in [
   spodi-scr
   update-sys-scr
   virsh-del-scr
+  ytc-scr
   yti-scr
 ]
diff --git a/hm/soispha/pkgs/scripts/wrappers/ytc b/hm/soispha/pkgs/scripts/wrappers/ytc
new file mode 100755
index 00000000..c7797c16
--- /dev/null
+++ b/hm/soispha/pkgs/scripts/wrappers/ytc
@@ -0,0 +1,94 @@
+#!/usr/bin/env dash
+
+# shellcheck source=/dev/null
+SHELL_LIBRARY_VERSION="1.10.0" . %SHELL_LIBRARY_PATH
+CONCURRENT=4
+OUTPUT_PATH="$XDG_RUNTIME_DIR/ytcc";
+
+col() {
+    echo "$1" | csvtool col "$2" -
+}
+
+play() {
+    dbg "Playing: $1"
+    mpv "$1" --speed=2.7
+    output="$?";
+
+    if [ "$output" -eq 0 ]; then
+        dbg "Removing: $1"
+        rm "$1"
+        dbg "Marking: " "$2"
+        ytcc mark "$2"
+    fi
+    return "$output"
+}
+
+bases="$(ytcc --output json list --attributes url --ids "$@" | jq --raw-output 'map("\(.url),\(.id)") | join("\n")')";
+
+yt_flags="$(mktmp)"
+cat << EOF > "$yt_flags"
+--format bestvideo[height<=?1080]+bestaudio/best
+--embed-chapters
+--progress
+--write-comments
+--extractor-args youtube:max_comments=150,all,100;comment_sort=top
+--write-info-json
+--sponsorblock-mark default
+--sponsorblock-remove sponsor
+--restrict-filenames
+EOF
+
+[ -d "$OUTPUT_PATH" ] || mkdir "$OUTPUT_PATH";
+cd  "$OUTPUT_PATH" || die "(Bug): Was created"
+
+filename_file="$(mktmp)";
+files_to_play="$(mktmp)";
+while read -r base; do
+    url="$(col "$base" 1)";
+    id="$(col "$base" 2)"
+
+    if [ "$old_filename" ]; then
+        echo "$old_filename,$old_id" >> "$files_to_play"
+
+        # Check if the process (pid) exists
+        if ! kill -0 "$pid" >/dev/null; then
+            saved_base="$(head -n 1 "$files_to_play")";
+            sed -i '1d' "$files_to_play";
+            saved_name="$(col "$saved_base" 1)";
+            saved_id="$(col "$saved_base" 2)"
+
+            dbg "Started play for '$saved_name'"
+            play "$saved_name" "$saved_id" &
+            pid="$!"
+        else
+            dbg "Storing for later '$old_filename'"
+        fi
+    fi
+
+    # The sub shell needs to be unquoted, as the arguments may not be treated as one.
+    # shellcheck disable=2046
+    yt-dlp $(cat "$yt_flags") --output "%(channel)s/%(title)s.%(ext)s" "$url" --print-to-file after_move:filepath "$filename_file"
+
+    filename="$(cat "$filename_file")"
+    printf "" > "$filename_file"
+
+    if [ "$old_filename" ]; then
+        while [ "$(wc -l < "$files_to_play")" -gt "$CONCURRENT" ]; do
+            wait "$pid"
+        done
+    fi
+
+    old_filename="$filename";
+    old_id="$id";
+done < "$(tmp echo "$bases")"
+
+echo "$old_filename,$old_id" >> "$files_to_play"
+
+while read -r base; do
+    name="$(col "$base" 1)";
+    id="$(col "$base" 2)"
+
+    dbg "Started play for '$name'"
+    play "$name" "$id"
+done < "$files_to_play"
+# vim: ft=sh