Back
Featured image of post 喜马拉雅下载脚本

喜马拉雅下载脚本

抓了极客下载的API,写了个脚本自用 主要是MAC要批量下载太蛋疼了

#!/usr/bin/env bash
#Author zsnmwy
#v1.1 fix https error
judge(){
	if [[ $? -eq 0 ]];then
		echo -e "$1 完成"
		sleep 1
	else
		echo -e "$1 失败"
		exit 1
	fi
}
url=$1
check_url=$(echo $url | grep "http://www.ximalaya.com")
check_url_https=$(echo $url | grep "https://www.ximalaya.com")
if  [ -n $check_url_https ]
then
    check_url=$(echo $url | sed s/https/http/)
    url=$check_url
fi
if [ "$check_url" = "" ]
then
    echo "请输入正确的喜马拉雅地址"
    echo "
例子

下载单个节目请使用:
http://www.ximalaya.com/youshengshu/249504/2482208
http://www.ximalaya.com/8889234/sound/42857655/
http://www.ximalaya.com/8889234/sound/42857655

专辑不超过200集请使用:
http://www.ximalaya.com/renwen/5939130/
http://www.ximalaya.com/39200626/album/3385980
http://www.ximalaya.com/39200626/album/3385980/

专辑超过200集请使用以下链接形式下载:
http://www.ximalaya.com/shangye/3385980/p1/
http://www.ximalaya.com/shangye/3385980/p2/
http://www.ximalaya.com/shangye/3385980/p3/
"
    exit
else
    echo -e "\n\n输入的地址的为 $1"
fi

api_url="http://jikexiazai.cn/get_xmly.php?url=$url"

echo -e "\n"
echo -e "API请求地址为 $api_url \n\n"

data=$(curl -s $api_url) 
#echo -e "$data"

length_data=$(echo -e "$data" | awk '{print NR}'| tail -n1)
#echo $length_data

if [ $length_data -eq 0 ] || [ $length_data -eq 1 ]
then
    echo "没有找到下载地址,请确保输入的地址正确"
    exit
elif [ $length_data -eq 2 ]
then
    echo "找到一个下载地址,准备下载"
elif [ $length_data -gt 2 ]
then
    file_number=$(expr $length_data / 2)
    echo "找到 $file_number 个下载地址,准备下载"
fi

name_data=$(echo -e "$data" | sed -n '1~2p')

echo -e "\n\n\n\n"
echo -e "准备下载这些文件  按回车以继续下载 \n\n"
echo -e "$name_data"
echo -e "\n\n准备下载这些文件  按回车以继续下载 \n\n"
read -p "按回车以继续 不想下载这些文件请按 Ctrl + C    "
echo -e "\n\n"


link_data=$(echo -e "$data" | sed -n '0~2p')
#echo -e "$link_data"

#echo -e "\n\n\n"


length_link=$(echo -e "$link_data" | awk '{print NR}'| tail -n1)

#echo "########### NAME #############"

#OLDIFS=$IFS
#IFS='\n'
i=0
k=1

for link in $link_data
do
    #echo $i
    a[$i]=$(echo -e "$name_data" | sed -n ${k}p)
    #echo ${i} ${a[$i]}
    i=$(expr $i + 1)
    k=$(expr $k + 1)
done

#echo ${a[*]}
#echo "########### NAME #############"

#echo "###########LINK#############"
i=0
for link in $link_data
do
    #echo $i
    b[$i]="$link"
    #echo ${b[$i]}
    i=$(expr $i + 1)
    #echo $i
done
#echo "###########LINK#############"

i=0
for link in $link_data
do
    echo "准备下载 ${a[$i]}"
    echo "这是第 $(expr $i + 1) 个 , 总共 $file_number 个"
    file_type=$(basename "${b[$i]}" | cut -d "." -f2)
    curl -o "$(echo ${a[$i]}).$(echo $file_type)" "$(echo ${b[$i]})"

    judge "下载 ${a[$i]}"

    if [ $i == $(expr $length_link - 1) ]
    then
        echo "全部下载完成"
        exit
    fi
    echo "暂停十秒"
    sleep 10
    i=$(expr $i + 1)
    echo -e "\n\n\n\n"
done
comments powered by Disqus