#!/bin/sh
#author:Wos
#YuTube Music Terminal
HTMP=/tmp/ymusic.html
TTMP=/tmp/ymusic_title.txt
LTMP=/tmp/ymusic_link.txt
DOMAIN="https://invidious.materialio.us/"
PXY="http://127.0.0.1:7891"
UA="user-agent: Mozilla/5.0 (iPad; CPU OS 17_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1"
function logo()
{
    echo -e "\033[96;40m"
    base64 -d <<<"H4sIAAAAAAAAA5VRwQ2EMAz7dwr/AOmkLoRkBsnw58StBLrrgwaoYzsGVGC5+AMW+uhaMczKi5g9
ufMwEwhrNk2n9MIoQqYWcG3EJd/prkvXPjY9e2oy8enckpV2YWRx5o1SfzJNoXf7uwKVF0D8qd0J
Lcc+zvOdPUM/0QuLTEuhxDcnnxPKqrH1AbxY7QvKXr5/yAEAAA=="|gunzip
    echo -e "     Current Instance:"${DOMAIN}
    echo -e "\033[0m"
}

function usage(){
   logo
   echo  "usage:`basename $0` [keyword]"
   echo ""
   exit -1
}

if [ $# == 0 ]; then
   usage
elif [ $# == 1 ] && [ $1 == "-h" ];then
   usage
fi

KW=$@ 
PG=1
function getlink(){
while :;
do
curl -x ${PXY} -H "${UA}" -s ${DOMAIN%%/}"/search?q=music+"${KW// /+}"&page="${PG} > ${HTMP} 
cat ${HTMP} |grep '<a href="/watch'|awk -F\> '{print $3}'|awk -F\< '{print $1}' > ${TTMP} 
cat ${HTMP} |grep '<a href="/watch'|awk -F\" '{print $2}'|awk -F\= '{print $2}' > ${LTMP} 
if [ -s "${TTMP}" ]; then
    break
fi
done
}
getlink
while :; 
do
clear
echo -ne "\033[?25h"
logo
cat ${TTMP}|sed "s/&#39;/\'/g"|nl
echo -e "\033[96;40m"
echo -e "    [^c:Exit,-1:Pre Page,Enter:Next Page,sp:Sequential,rp:Random]"
read -e -r -p "    Selelct Resource ID or Search:" input
echo -ne "\033[0m"
NUM=$(cat ${TTMP}|wc -l)
if [[ $input =~ ^-?[0-9]+$ ]] && (( $input > 0 && $input <= $NUM )); then
    echo -e "\033[93;40m"
    echo -e "    ( m:🔇 9:🔈︎ 0:🔊 q:⏹  space:⏯︎  up:⏩)\033[0m"
    echo -ne "\033[?25l"
    #tag 251 140
    while :;
    do
    VID=$(sed -n "${input}p" "${LTMP}")
    VID2="https://www.youtube.com/watch?v=${VID}"
    yt-dlp --extractor-args "youtube:player_client=tv_embedded,mediaconnect" --proxy "${PXY}" --audio-format best -x -g "${VID2}"|xargs -I {} sh -c 'mpv --http-proxy="'${PXY}'" --really-quiet "{}"'
    if [ $? == 0 ]; then
        break
    fi
    done
elif [[ "$input" == "sp" ]]; then
    echo -e "\033[93;40m"
    echo -e "    ( m:🔇 9:🔈︎ 0:🔊 q:⏹  space:⏯︎  up:⏩)\033[0m"
    echo -ne "\033[?25l"
    while IFS= read -r line
    do
        while :;
	do
        VID2="https://www.youtube.com/watch?v=${line}"
        yt-dlp ---extractor-args "youtube:player_client=tv_embedded,mediaconnect" --proxy "${PXY}" --audio-format best -x -g "${VID2}"|xargs -I {} sh -c 'mpv --http-proxy="'${PXY}'" --really-quiet "{}"'
        if [ $? == 0 ]; then
            break
	fi
        done
    done < ${LTMP} 
elif [[ "$input" == "rp" ]]; then
    echo -e "\033[93;40m"
    echo -e "    ( m:🔇 9:🔈︎ 0:🔊 q:⏹  space:⏯︎  up:⏩)\033[0m"
    echo -ne "\033[?25l"
    while IFS= read -r line
    do
        while :;
	do
        VID2="https://www.youtube.com/watch?v=${line}"
        yt-dlp --extractor-args "youtube:player_client=tv_embedded,mediaconnect" --proxy "${PXY}" --audio-format best -x -g "${VID2}"|xargs -I {} sh -c 'mpv --http-proxy="'${PXY}'" --really-quiet "{}"'
        if [ $? == 0 ]; then
            break
	fi
        done
    done< <(shuf ${LTMP})

elif [[ -z "$input" ]]; then
    PG=$(($PG+1))
    getlink
    continue
elif [[ "$input" == "-1" ]]; then
    if [[ $PG > 1 ]]; then
        PG=$((($PG)-1))
    else
	PG=1
    fi
    getlink
    continue
else
    KW=$input
    PG=1
    getlink
    continue
fi
done