#!/bin/sh [ "$#" -eq 0 ] && { echo "Usage: ytlol [options] Options: None Play first result as audio (default) -f Search & pick result -v Play as video -s Skip title/link message (not faster) -u Update default Invidious instance for searches "; exit 1; } inv="https://invidious.nerdvpn.de" skp=0 vid=0 fzf=0 grepz() { grep -Eo "youtube.com/watch\?v=.{11}" | uniq | sed 's|^|https://|'; } link() { grepz | while read -r l; do echo "$(yt-dlp --get-title --get-filename --get-duration -o "%(uploader)s" "$l" 2>/dev/null | tr '\n' '\t')$l" | awk -F'\t' '{printf "%-105s%-55s%-15s%s\n", $1, $2, $3, $4}'; done; } play() { [ "$skp" -eq 0 ] && yt-dlp --get-title "$1" 2>/dev/null && echo "Playing: $1" & $([ "$vid" -eq 1 ] && echo "mpv --no-pause --save-position-on-quit" || echo "mpv --no-video") "$1" || { echo "Error: Failed to play video. Try running 'ytlol -u'."; exit 1; }; } while getopts "vfsu" opt; do case $opt in v) vid=1 ;; f) fzf=1 ;; s) skp=1 ;; u) [ -w "$0" ] || { echo "Error: You cannot perform this operation unless you are root."; exit 1; } curl -s "https://raw.githubusercontent.com/iv-org/documentation/master/docs/instances.md" | grep "\* \[" | grep -Ev "(Onion|Eepsite|Cloudflare|yewtu\.be)" | awk -F'[][]' '{print "https://"$2}' | while read -r i; do curl -s "${i}/search?q=gnu" | grep -qE "youtube.com/watch\?v=.{11}" && echo "Instance found: $i" && sed -i "s%inv=\"[^\"]*\"%inv=\"$i\"%g" "$0" && break done; exit 1 ;; esac done shift $((OPTIND-1)) [ -z "$1" ] && { echo "Error: No search query."; exit 1; } [ $fzf -eq 1 ] && { query=$(printf '%s' "$*" | tr ' ' '+') for page in {1..1}; do curl -s "$inv/search?q=$query&page=$page" | grep ').*?(?=)' | sed 's/ - YouTube//')" $cha; done curl -s "$inv/search?q=$query&page=$page" | link done | fzf --exact --reverse | awk '{print $NF}' | while read -r url; do case $url in *"/channel/"*) curl -s $(echo $url | sed "s|https://www.youtube.com|$inv|g") | link | fzf --exact --reverse | awk '{print $NF}' | while read -r v; do play $v; done ;; *"/watch?v"*) play $url ;; esac done } || play $(curl -s "$inv/search?q=$(printf '%s' "$*" | tr ' ' '+')" | grepz | head -n 1) || echo "No results or error retrieving link."