#!/bin/sh 
#author:Wos
#universal m3u8 downloader
#the m3u8 link must be a end link,you need manual verify this
UA="User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"
FILTER1="xhcdn|.cdnlz|.lz|yzzy|phncdn|xvideo|xnxx|xluuss|surrit|ijycnd" 
FILTER2="fentvoss|tlkqc"
TEMPPATH=/tmp/m3u8temp/
rm ${TEMPPATH}* 2>/dev/null
M0=${TEMPPATH}"0.m3u8"
M1=${TEMPPATH}"1.m3u8"
M2=${TEMPPATH}"2.m3u8"
M3=${TEMPPATH}"3.m3u8"
M4=${TEMPPATH}"4.m3u8"
PXY=""
PPXY=""

LINK=$(xclip -selection clipboard -o 2>/dev/null) 
if [[ ${LINK} =~ ^https:// ]]; then
    echo -ne "\033[96;40mEnable Proxy(Y/N)?\033[0m"
    read -r input
    if [[ $input == 'y' || $input == "Y" ]]; then
        PXY=" -x http://127.0.0.1:7891"
        PPXY=" --all-proxy=http://127.0.0.1:7891"
    fi

    #normal file download
    echo ${LINK}|grep -E "mp4$|mp4?"|grep -v -E "m3u8$|m3u8?" && aria2c "${LINK}" --max-tries=3 --allow-overwrite=true --file-allocation=none -c -s8 -x8 -k 1M ${PPXY} && exit
    #hls file download
    curl ${PXY} -H "${UA}" -s "${LINK}" > ${M0} || exit
    cat ${M0}|grep "#EXTM3U" >/dev/null || exit
    #Prefix domain name determination
    #Adjust the parameters in the following regular expression according to the type of m3u8
    echo ${LINK}|grep -E "${FILTER1}" && PRELINK=${LINK%/*} || PRELINK=$(echo ${LINK}|cut -d '/' -f 1-3)
    echo ${PRELINK}
    > ${M1}
    cat ${M0}|grep "MAP:URI=\"/key=" && PRELINK=$(echo ${LINK}|cut -d '/' -f 1-3) 
    cat ${M0}|grep "AES-128,URI=\""|grep -v "=\"https" && echo ${PRELINK}/$(cat ${M0}|grep "AES-128,URI="|awk -F\" '{print $2}') >> ${M1}
    cat ${M0}|grep "MAP:URI=\"/key=" && echo ${PRELINK}/$(cat ${M0}|grep "MAP"|awk -F\" '{print $2}') >> ${M1}
    cat ${M0}|grep "MAP:URI=\"https" && cat ${M0}|grep "MAP"|awk -F\" '{print $2}' >> ${M1}
    cat ${M0}|grep "MAP:URI=\""|grep -v "=\"http" && echo ${PRELINK}/$(cat ${M0}|grep "MAP"|awk -F\" '{print $2}') >> ${M1}
    cat ${M0}|grep -v -E '^#|^http'|awk '{print "'${PRELINK}'/"$1}' >> ${M1}
    cat ${M0}|grep -v '^#'|grep '^http'|awk '{print $1}' >> ${M1}
    cat ${M0}|sed  -e '/^#/! s/\(.*\/\)/\/tmp\/m3u8temp\//g' -e '/^#/ s/\(".*\/\)/"\/tmp\/m3u8temp\//g' -e '/^#/! s/.png$/.ts/g' -e '/^#/! s/.jpg$/.ts/g' -e 's/.ts?.*/.ts/g' > ${M2} 
    #Long file name handling
    echo ${LINK}|grep -E "${FILTER2}" && cat ${M0}|sed  -e '/^#/! s/\(.*\/\)/\/tmp\/m3u8temp\//g' -e '/^#/ s/\(".*\/\)/"\/tmp\/m3u8temp\//g' -e '/^#/! s/.png$/.ts/g' -e '/^#/! s/.jpg$/.ts/g' -e 's/.ts?.*/.ts/g' -e '/^#/! s/.*\(.\{13\}\)$/\1/p' -e '/^#/! s/^/\/tmp\/m3u8temp\//g'|uniq > ${M2} 
    echo ${LINK}|grep -E "${FILTER2}" && cat ${M1}|sed -e 's/.*\(.\{13\}\)$/\1/p'  -e 's/^/ out=/g'|grep 'out=' > ${M3}

    echo ${LINK}|grep -E "${FILTER2}" && paste -d, ${M1} ${M3}|tr ',' '\n' > ${M4}

    echo ${LINK}|grep -E "${FILTER2}" && aria2c -i ${M4} --max-tries=3 --allow-overwrite=true --file-allocation=none -c -s8 -x8 -k 1M ${PPXY} -d ${TEMPPATH} 
    #multithread download files
    echo ${LINK}|grep -v -E "${FILTER2}" && aria2c -i ${M1} --max-tries=3 --allow-overwrite=true --file-allocation=none -c -s8 -x8 -k 1M ${PPXY} -d ${TEMPPATH} 
    #combine multi files 
    FN=$(tr -dc 'A-Za-z0-9' </dev/urandom | head -c 5)
    ffmpeg -allowed_extensions ALL -i ${M2} -c copy -absf aac_adtstoasc ${FN}.mp4
    echo ""
    echo -e "File Save As:\033[96;40m${FN}.mp4\033[0m"
fi