#!/bin/sh
#https://jav.guru parser 
#copy the video resource link,script will auto parser to play
#author:Wos
function logo()
{
    echo -e "\033[96;40m"
    base64 -d <<<"H4sIAAAAAAAAA61QMQoDMQzb7xXaegel+U2ngPoQP76S7UCGclOTkMiWLOwAIP6wDuDkBVLHhvTD
9M53QVQANL0KWk4bBWKAH0ztgYTR+0HGFqQgaRFCdKGSlpWRutI98ZbwrNp22GCo81g0MGkP2DFy
NAMlnxJMYcOVoBXNBV5suojQVENhy44wva3kf3xmyW8++wuIP4rRtgEAAA=="|gunzip
    echo -e "\033[0m"
}

HTMP=/tmp/javguru.html
HTMP2=/tmp/javguru2.html
HTMP3=/tmp/javguru3.html
UA="User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"
RF="Referer: https://jav.guru/"
PXY="--connect-timeout 5 --retry 3 -x http://127.0.0.1:7891"
PPXY="--http-proxy=http://127.0.0.1:7891"
while :;
do
clear
logo
echo "jav.guru parser"
echo "copy jav.guru resource link,it will auto parser hls link to play"
echo -ne "\033[?25l"
LINK=$(xclip -selection clipboard -o 2>/dev/null)
if [[ $LINK =~ ^https:\/\/jav\.guru\/ ]]; then
    xclip -selection clipboard /dev/null
    curl ${PXY} -H "${UA}" -H "${RF}" -s "${LINK}"|grep -oE 'iframe_url":".*'|awk -F\" '{print $3}' > ${HTMP} 
    echo /dev/null > ${HTMP2} 
    while IFS= read -r line
    do
        echo "$line"|base64 -d >> ${HTMP2} 
        echo "" >> ${HTMP2} 
    done <  ${HTMP} 
    HR=$(cat ${HTMP2}|grep -oE 'hd=.*&'|awk -F\= '{print $2}'|awk -F\& '{print $1}'|rev)
    echo "HR Length:"${#HR}
    LINK="https://jav.guru/searcho/?hr="${HR}
    echo "LINK:"${LINK}
    curl ${PXY} -H "${RF}" -H "${UA}" -i -s ${LINK} > ${HTMP} 
    LINK2=$(cat ${HTMP}|grep location|awk '{print $2}')
    RF="Referer: "${LINK2}
    LINK3=$(echo ${LINK2} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
    echo "LINK3:"${LINK3}
    if [ ${#HR} == 64 ]; then
        LINK3="https://ds2play.com"$(curl ${PXY} -H "${UA}" -s -i "${LINK3}"|grep "location"|awk '{print $2}')
        LINK3=$(echo ${LINK3} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
        echo "LINK3_2:"${LINK3}
    fi
    LINK4="https://ds2play.com/"$(curl ${PXY} -H "${UA}" -s "${LINK3}"|grep -oE 'pass_md5.*'|awk -F\' '{print $1}')
    echo "LINK4:"$LINK4
    curl --connect-timeout 5 --retry 3 -H "${UA}" -s ${LINK3} > ${HTMP} 
    TK=$(cat ${HTMP}|grep -oE 'token=.*'|awk -F\& '{print $1}')
    echo "Token:"${TK}
    LINK5=$(curl ${PXY} -H "${UA}" -H "${RF}" -s "${LINK4}")
    echo "LINK5:"${LINK5}
    if [[ ! $LINK5 =~ ^https:\/\/ ]]; then
        DR=$(cat ${HTMP2}|grep -oE 'dd=.*&'|awk -F\= '{print $2}'|awk -F\& '{print $1}'|rev)
        LINK="https://jav.guru/searcho/?dr="${DR}
        echo "LINK:"$LINK
        curl ${PXY} -H "${RF}" -H "${UA}" -i -s ${LINK} > ${HTMP} 
        LINK2=$(cat ${HTMP}|grep location|awk '{print $2}')
        RF="Referer: "${LINK2}
        LINK3=$(echo ${LINK2} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
        echo "LINK3:"${LINK3}
	curl ${PXY} -H "${UA}" -s "${LINK3}" > ${HTMP3}
	LINK4="https://streamtape.net/get_video?"$(cat ${HTMP3}|grep -E "'botlink'"|grep -oE "id=.*'"|awk -F\' '{print $1}')"&stream=1"
        FN=$(cat ${HTMP3}|grep "og:title"|awk -F\" '{print $4}'|awk -F\. '{print $1}')
        echo "movie name:"${FN}
        echo "LINK4:"${LINK4}
        mpv ${PPXY} --really-quiet --http-header-fields="Referer: ${LINK4}" "${LINK4}"
	echo -ne "\033[?25h"
    else
        random_number=$(< /dev/urandom tr -dc 'A-Za-z0-9' | fold -w 10 | head -n 1)
        TS=$(date +%s%N | cut -b1-13)
        LINK6=$(echo ${LINK5} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
        LINK7=${LINK6}${random_number}"?"${TK}"&expiry="${TS}
        FN=$(cat ${HTMP}|grep -oE '<title>.*</title>'|awk -F\> '{print $2}'|awk '{print $1}')
        echo "movie name:"${FN}
        echo "LINK6:"${LINK7}
        echo -ne "\033[96;40m"
        echo "Ctrl+C:Next Source or Exit"
        echo -ne "\033[0m"
        mpv ${PPXY} --really-quiet --http-header-fields="Referer: ${LINK7}" "${LINK7}"
	if [ $? != 0 ]; then
            DR=$(cat ${HTMP2}|grep -oE 'dd=.*&'|awk -F\= '{print $2}'|awk -F\& '{print $1}'|rev)
            LINK="https://jav.guru/searcho/?dr="${DR}
            echo "LINK:"$LINK
            curl ${PXY} -H "${CK}" -H "${RF}" -H "${UA}" -i -s ${LINK} > ${HTMP} 
            LINK2=$(cat ${HTMP}|grep location|awk '{print $2}')
            RF="Referer: "${LINK2}
            LINK3=$(echo ${LINK2} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
            echo "LINK3:"${LINK3}
	    curl ${PXY} -H "${UA}" -H "${CK2}" -s "${LINK3}" > ${HTMP3}
	    LINK4="https://streamtape.net/get_video?"$(cat ${HTMP3}|grep -E "'botlink'"|grep -oE "id=.*'"|awk -F\' '{print $1}')"&stream=1"
	    FN=$(cat ${HTMP3}|grep "og:title"|awk -F\" '{print $4}'|awk -F\. '{print $1}')
            echo "movie name:"${FN}
            echo "LINK4:"${LINK4}
            mpv ${PPXY} --really-quiet --http-header-fields="Referer: ${LINK4}" "${LINK4}"
	    echo -ne "\033[?25h"
	fi
    fi
else
    sleep 1
    continue
fi
done