#!/bin/sh

query="$(playerctl -i firefox metadata --format '{{ artist }} - {{ title }}')"

echo "Searching YouTube for:"
echo "  $query"
echo

yt-dlp "ytsearch1:$query" \
  --print "━━━━━━━━━━━━━━━━━━━━━━━" \
  --print "Title:     %(title)s" \
  --print "Artist:    %(artist|Unknown)s" \
  --print "Track:     %(track|Unknown)s" \
  --print "Channel:   %(uploader)s" \
  --print "Duration:  %(duration_string)s" \
  --print "Views:     %(view_count)s" \
  --print "URL:       %(webpage_url)s" \
  --print "━━━━━━━━━━━━━━━━━━━━━━━"

echo
read -p "Download this? [y/N] " confirm

if [[ "$confirm" =~ ^[Yy]$ ]]; then
    yt-dlp --embed-metadata -x --audio-format aac -f bestaudio \
    "ytsearch1:$query" \
    -o "~/Music/%(artist)s - %(title)s.%(ext)s"
else
    echo "Aborted. You resisted entropy."
fi
