summaryrefslogtreecommitdiff
path: root/bin/extra/myts
diff options
context:
space:
mode:
authorRaymaekers Luca <raymaekers.luca@gmail.com>2024-11-29 17:21:07 +0100
committerRaymaekers Luca <raymaekers.luca@gmail.com>2024-11-29 17:21:07 +0100
commit1c632029d7a02742b0cc740b6984ce139429b88b (patch)
treec3ff42411f67d57ef86249723f38a726414ecd8a /bin/extra/myts
parent5464c097a44165473b352c463805a39799888c52 (diff)
checkpoint
Diffstat (limited to 'bin/extra/myts')
-rwxr-xr-xbin/extra/myts65
1 files changed, 65 insertions, 0 deletions
diff --git a/bin/extra/myts b/bin/extra/myts
new file mode 100755
index 0000000..aea4842
--- /dev/null
+++ b/bin/extra/myts
@@ -0,0 +1,65 @@
+#!/bin/sh
+
+# Play a youtube song via yt-dlp, jq, fzf and play it with mpv or mpd.
+
+tsv="/tmp/ytsearch.tsv"
+json="/tmp/ytsearch.json"
+
+# This is the preview code for fzf
+if [ "$1" = "-p" ]; then
+ grep "^$2\s" "$tsv" |
+ cut -f 4,5,6 | tr '\t' '\n' |
+ while read -r channel; do
+ read -r timestamp
+ read -r views
+ printf '\033[2m%s\033[0m\n' "$channel"
+ printf '%s views\n' "$(numfmt --to=iec "$views")"
+ printf '\033[3m%s\033[0m\n' "$(date -d @"$timestamp" +'released on %d/%m/%y at %T')"
+ done
+ exit
+fi
+
+# Whether to pass th --no-video flag to mpv
+if [ "$1" = "-n" ]; then
+ noVideoFlag=1
+ shift
+fi
+
+# Whether to add the url to mpd
+if [ "$1" = "-m" ]; then
+ toMPDFlag=1
+ shift
+fi
+
+q="$@"
+if [ -z "$q" ]; then
+ >&2 printf 'Usage: %s query\n' "$0"
+ exit 1
+fi
+
+>&2 printf 'Searching..\n'
+yt-dlp -j "ytsearch5:$q" | jq > "$json"
+
+jq -r '[.id,.title,.original_url,.channel,.timestamp,.view_count] | @tsv' "$json" > "$tsv"
+
+choice="$(cut -f 1,2,6 "$tsv" |
+ sort -t ' ' -k 3 |
+ cut -f 1,2 |
+ column -t -s ' ' |
+ fzf --with-nth "2.." --preview "$0"' -p {1}')"
+[ "$choice" ] || exit 1
+choice="${choice%% *}"
+url="$(grep "^$choice " "$tsv" | cut -f 3)"
+[ "$url" ] || exit 2
+
+rm -f "$tsv" "$json"
+
+if [ "$toMPDFlag" ]; then
+ >&2 printf 'Adding to mpd..\n'
+ mpc add "$(yt-dlp --prefer-insecure -g -f140 "$url")"
+elif [ "$noVideoFlag" ]; then
+ mpv --no-video "$url"
+else
+ mpv "$url"
+fi
+>&2 printf 'Done.\n'