From 6f337ee548e08064325741271b36ca5197d88d87 Mon Sep 17 00:00:00 2001
From: Soispha <soispha@vhack.eu>
Date: Mon, 1 Jan 2024 22:42:02 +0100
Subject: fix(hm/pkgs/scr/ytc): Allow titles with commas in them

---
 hm/soispha/pkgs/scripts.nix          |  1 +
 hm/soispha/pkgs/scripts/wrappers/ytc | 12 ++++++++----
 2 files changed, 9 insertions(+), 4 deletions(-)

(limited to 'hm')

diff --git a/hm/soispha/pkgs/scripts.nix b/hm/soispha/pkgs/scripts.nix
index e9c70966..a4cab3d4 100644
--- a/hm/soispha/pkgs/scripts.nix
+++ b/hm/soispha/pkgs/scripts.nix
@@ -206,6 +206,7 @@
         mpv
         ffmpeg
         gnused
+        gawk
         ;
     };
   };
diff --git a/hm/soispha/pkgs/scripts/wrappers/ytc b/hm/soispha/pkgs/scripts/wrappers/ytc
index ce7f9ba9..7e806449 100755
--- a/hm/soispha/pkgs/scripts/wrappers/ytc
+++ b/hm/soispha/pkgs/scripts/wrappers/ytc
@@ -6,7 +6,7 @@ CONCURRENT=4
 OUTPUT_PATH="/tmp/ytcc";
 
 col() {
-    echo "$1" | csvtool col "$2" -
+    echo "$1" | csvtool -t ';' col "$2" -
 }
 
 play() {
@@ -23,7 +23,11 @@ play() {
     return "$output"
 }
 
-bases="$(ytcc --output json list --attributes url --ids "$@" | jq --raw-output 'map("\(.url),\(.id)") | join("\n")')";
+escape() {
+    echo "$1" | awk '{gsub(/;/, ","); print}'
+}
+
+bases="$(ytcc --output json list --attributes url --ids "$@" | jq --raw-output 'map("\(.url);\(.id)") | join("\n")')";
 
 yt_flags="$(mktmp)"
 cat << EOF > "$yt_flags"
@@ -47,7 +51,7 @@ while read -r base; do
     id="$(col "$base" 2)"
 
     if [ "$old_filename" ]; then
-        echo "$old_filename,$old_id" >> "$files_to_play"
+        echo "$(escape "$old_filename");$old_id" >> "$files_to_play"
 
         # Check if the process (pid) exists
         dbg "PID is '$pid'"
@@ -84,7 +88,7 @@ while read -r base; do
 done < "$(tmp echo "$bases")"
 
 wait "$pid"
-echo "$old_filename,$old_id" >> "$files_to_play"
+echo "$(escape "$old_filename");$old_id" >> "$files_to_play"
 
 while read -r base; do
     name="$(col "$base" 1)";
-- 
cgit 1.4.1