#!/bin/sh
#author:Wos
#hifi music terminal
#resource from https://hifini.com/
WORK_DIR=$(dirname $0)
HIFIJS=${WORK_DIR}/hifi.js
HTMP="/tmp/hifi.html"
TTMP="/tmp/hifi_title.txt"
LTMP="/tmp/hifi_link.txt"
DOMAIN="https://hifini.com/search-"
KW=$@
PG=1
_KW=$(node -e 'require("'${HIFIJS}'").urlencode("'${KW}'")')
function logo()
{
    echo -e "\033[96;40m"
    base64 -d <<<"H4sIAAAAAAAAA5WOsRHAMAgDe6ZQm4ptUnHHIgwfSTgDGOMzWG+ZbpxoxR5uvdWsoB2FWji5GpW6
HmB2kRo7uAoIzzIsveRRpsnNpv6RGKD0gplHJ/1Y0ixL4/+MNO2SCdumaDX7cjzDyFpV4CbiA1rSlIMUAQAA"|gunzip
    echo -e "     Resource Site:"${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

function getlink(){
LINK=${DOMAIN}${_KW}"-1-"${PG}".htm"
while :;
do
curl -s ${LINK} > ${HTMP}
cat ${HTMP} |grep "<a href=\"thread"|grep "FLAC/MP3-320K"|grep -v -E "Hi-Res|专辑|合集|全部"|sed  -e s/\<em\>//g -e s/\<\\/em\>//g|awk -F\> '{print $2}'|awk -F\< '{print $1}' > ${TTMP}
cat ${HTMP} |grep "<a href=\"thread"|grep "FLAC/MP3-320K"|grep -v -E "Hi-Res|专辑|合集|全部"|sed  -e s/\<em\>//g -e s/\<\\/em\>//g|awk -F\" '{print $2}' > ${LTMP}
if [ -s "${TTMP}" ]; then
    break
fi
done
}

getlink

while :;
do
clear
logo
echo -ne "\033[?25h"
nl ${TTMP}
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 playmode or Search:" input
echo -e "\033[0m"
NUM=$(cat ${TTMP}|wc -l)
echo -ne "\033[?25l"
if [[ $input =~ ^-?[0-9]+$ ]] && (( $input > 0 && $input <= $NUM )); then
    echo -e "\033[93;40m"
    echo -e "    ( m:🔇 9:🔈 0:🔊 q:back, space:⏯  up:⏩)\033[0m"
    LINK2=${DOMAIN%/*}"/"$(sed -n "${input}p" "${LTMP}")""
    curl -s ${LINK2} > ${HTMP}
    LINK3=$(cat ${HTMP}|grep ".m4a"|awk -F\' '{print $2}')
    if [[ -z "${LINK3}" ]]; then
        HIFIARG1=$(cat ${HTMP}|grep "get_music"|awk -F\' '{print $2}')
        if [[ -z "${HIFIARG1}" ]]; then
            break
        fi
        HIFIARG2=$(cat ${HTMP}|grep "get_music"|awk -F\' '{print $4}')
        LINK3=${DOMAIN%/*}"/"${HIFIARG1}$(node -e 'require("'${HIFIJS}'").generateParam("'${HIFIARG2}'")')
    fi
    mpv --loop=inf --really-quiet --vid=no --http-header-fields='Referer: https://hifini.com/' "${LINK3}"
elif [[ "$input" == "sp" ]]; then
    echo -e "\033[93;40m"
    echo -e "    ( m:🔇 9:🔈 0:🔊 q:Next Sound, space:⏯  up:⏩ ^c:back)\033[0m"
    while IFS= read -r line
    do
    LINK2=${DOMAIN%/*}"/"${line}
    curl -s ${LINK2} > ${HTMP}
    LINK3=$(cat ${HTMP}|grep ".m4a"|awk -F\' '{print $2}')
    if [[ -z "${LINK3}" ]]; then
        HIFIARG1=$(cat ${HTMP}|grep "get_music"|awk -F\' '{print $2}')
        if [[ -z "${HIFIARG1}" ]]; then
            continue 
        fi
        HIFIARG2=$(cat ${HTMP}|grep "get_music"|awk -F\' '{print $4}')
        LINK3=${DOMAIN%/*}"/"${HIFIARG1}$(node -e 'require("'${HIFIJS}'").generateParam("'${HIFIARG2}'")')
    fi
    mpv --really-quiet --vid=no --http-header-fields='Referer: https://hifini.com/' "${LINK3}"
    if [ $? != 0 ]; then
        break 
    fi
    done < "${LTMP}"
elif [[ "$input" == "rp" ]]; then
    echo -e "\033[93;40m"
    echo -e "    ( m:🔇 9:🔈 0:🔊 q:Next Sound, space:⏯  up:⏩ ^c:back)\033[0m"
    while IFS= read -r line
    do
    LINK2=${DOMAIN%/*}"/"${line}
    curl -s ${LINK2} > ${HTMP}
    LINK3=$(cat ${HTMP}|grep ".m4a"|awk -F\' '{print $2}')
    if [[ -z "${LINK3}" ]]; then
        HIFIARG1=$(cat ${HTMP}|grep "get_music"|awk -F\' '{print $2}')
        if [[ -z "${HIFIARG1}" ]]; then
            continue    
        fi
        HIFIARG2=$(cat ${HTMP}|grep "get_music"|awk -F\' '{print $4}')
        LINK3=${DOMAIN%/*}"/"${HIFIARG1}$(node -e 'require("'${HIFIJS}'").generateParam("'${HIFIARG2}'")')
    fi
    mpv --really-quiet --vid=no --http-header-fields='Referer: https://hifini.com/' "${LINK3}"
    if [ $? != 0 ]; then
        break 
    fi
    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
    _KW=$(node -e 'require("'${HIFIJS}'").urlencode("'${KW}'")')
    PG=1
    getlink
    continue
fi
done