#!/bin/bash
#Copyright (c) 2005 ubuntu.org.cn
#License: GPLv2
#原脚本见http://forum.ubuntu.org.cn/viewtopic.php?t=2391&sid=910a60e55961be5f927b82595c2ddc29
#
#由于baidu的问题,导致原脚本不能正确下载,所以由 bones7456 再次修改,欢迎大家使用。
#wget太慢了。脚本默认使用axel多线程下载工具进行下载,如果没有axel,可以执行sudo apt-get install axel进行安装,或者把含有“axel”的两行注释,用下面“wget”的两行代替即可。#mp3的地址
SOURCE=”http://list.mp3.baidu.com/list/newhits.html”
#保存mp3的目录
SAVE=”${HOME}/baidump3″
#下载重试次数
TRYCOUNT=2
#临时目录
TMP=”/tmp/${USER}”
#创建下载目录
if [ ! -d “${SAVE}” ];then
mkdir -p “${SAVE}”
fi
#创建临时下载目录
if [ ! -d “${TMP}” ];then
mkdir -p “${TMP}”
fi
echo “开始下载百度最新100首歌曲列表”
wget -O ${TMP}/mp3.html ${SOURCE}
echo “下载百度最新100首歌曲列表完成。”
#转换网页编码
iconv -f gbk -t utf8 ${TMP}/mp3.html |\
grep “<a href=\”http://mp3.baidu.com/m” |\
#将mp3list.txt所有开头的空格去掉
sed -e ‘s/ *//’ |\
#将mp3list.txt所有开头的tab去掉
sed -e ‘s/\t*//’ |\
#将mp3list.txt所有全角空格去掉
sed -e ‘s/ //g’ |\
#将所有的回车符去掉
sed ‘:a;N;$!ba;s/\n/,/g’ |\
#在td>,后面加上回车符,一行表示一个mp3文件。
sed -e ‘s/,<td/\n<td/g’ |\
sed -e ‘s/td>,/td>\n/g’ |\
#删除<td width=”30%”> <td> </td> <td…FFFFFF”> <p> </p>
sed -e ‘s/<td width=”30%”>//g’ |\
sed -e ‘s/<td>//g’ |\
sed -e ‘s/<\/td>//g’ |\
sed -e ‘s/<p>//g’ |\
sed -e ‘s/<\/p>//g’ |\
sed -e ‘s/<td.*”border”>//g’ |\
#删除</a>…”_blank”>
sed -e ‘s/<\/a>.*_blank”>/-/g’ |\
sed -e ‘s/<\/a>.*_blank>/-/g’ |\
#删除)
sed -e ‘s/<\/a>)/<\/a>/g’ |\
#删除&
sed -e ‘s/\&\;/\//g’ >${TMP}/mp3list.txt
#得到:<a href=”http://mp3.baidu.com/m?tn=baidump3&ct=134217728&lm=-1&li=2&word=Baby%20Baby%20tell%20me%20%CD%F5%D0%C4%C1%E8″ target=”_blank”>Baby ,Baby tell me-王心凌</a>
#取得行号,循环
line=$(awk ‘END{print NR}’ ${TMP}/mp3list.txt)
i=1;
while((i<=line));do
downed=0;
mpline=`awk ‘NR=='”$i”” ${TMP}/mp3list.txt`
url=`echo $mpline | sed -e ‘s/<a href=”//g’ | sed ‘s/\ target.*//g’ | sed ‘s/”//g’ | cat`
name=`echo $mpline | sed -e ‘s/.*_blank”>//g’ | sed -e ‘s/.*_blank>//g’ |\
sed -e ‘s/<\/a>//g’ | sed -e ‘s/\//-/g’ | sed -e ‘s/:/-/g’ | sed -e ‘s/”/’\”/g’ | cat`
#检查是否已经下载过这首歌,如果下载过,放弃
if [ -e “${SAVE}/${name}.mp3” ] || [ -e “${SAVE}/${name}.wma” ]; then
echo -e “\e[1;6m\e[1;31m发现 ${name} 下载过,忽略,继续下一首。\e[1;6m\e[00m”
((i++))
continue;
fi
echo “开始通过 $url 下载 $name”;
wget -O ${TMP}/down.html $url
echo “获取 $name 下载列表完成。”;
#down.txt为有效的下载地址
iconv -f gbk -t utf8 -c ${TMP}/down.html | grep “onclick=\”return ow(this)\”” |\
sed -e ‘s/.*<a href=”//g’ | sed -e ‘s/” target=”_blank”.*//g’ > ${TMP}/down.txt
#size.txt为有效的下载文件大小
iconv -f gbk -t utf8 -c ${TMP}/down.html | grep “M<\/td>” |\
sed -e ‘s/<td>//g’ | sed -e ‘s/ M<\/td>//g’ > ${TMP}/size.txt
#down.txt与size.txt合并而在的down_size.txt文件中字段之间以”`”作为分隔符
paste -d ‘`’ ${TMP}/down.txt ${TMP}/size.txt > ${TMP}/down_size.txt
#排序
sort “+1nr” ${TMP}/down_size.txt > ${TMP}/down_size_sort.txt
#去掉后面的尺寸
sed ‘s/`.*$//’ ${TMP}/down_size_sort.txt > ${TMP}/temp.txt
##### 析取出mp3 的下载地址或 wma的下载地址 ##############
grep -i “word=mp3” ${TMP}/temp.txt > ${TMP}/down_mp3.txt
grep -i “word=wma” ${TMP}/temp.txt > ${TMP}/down_wma.txt
downline_mp3=$(awk ‘END{print NR}’ ${TMP}/down_mp3.txt);
downline_wma=$(awk ‘END{print NR}’ ${TMP}/down_wma.txt);
echo -e “\e[1;6m\e[1;31m发现 ${downline_mp3} 个名为 ${name}.mp3 下载地址。\e[1;6m\e[00m”
echo -e “\e[1;6m\e[1;31m发现 ${downline_wma} 个名为 ${name}.wma 下载地址。\e[1;6m\e[00m”
# 初始化计数器
j=1;
# 优先下载mp3格式的歌曲
while((j<=downline_mp3)); do
mp3=`awk ‘NR=='”$j”” ${TMP}/down_mp3.txt | sed -e ‘s/ /\\ /g’`
echo -e “\e[1;6m\e[1;31m正在下载${name}.mp3\e[1;6m\e[00m”
#echo -e “\e[1;6m\e[1;31m中转页面地址为${mp3}\e[1;6m\e[00m”
wget -O “${TMP}/transit.html” “$mp3”
realURL=`cat ${TMP}/transit.html | grep “<li class=\”li\” style=\”margin-right:10px;\”>” | sed ‘s/.*href=”//’ | sed ‘s/” target=”_blank”>.*//’`
#echo -e “\e[1;6m\e[1;31m真实下载地址为${realURL}\e[1;6m\e[00m”
axel -n 7 -a -o “${TMP}/${name}.mp3” “${realURL}”
#wget太慢了。但是如果没有安装axel,可以把上面一行注释掉,用下面一行代替
#wget -c –tries=$TRYCOUNT $realURL -O “${TMP}/${name}.mp3”
if [ “$?” = 0 ]; then
if [ `file -ib “${TMP}/${name}.mp3” | sed -e ‘s/\/.*//g’` = “audio” ]; then
mv “${TMP}/${name}.mp3” “${SAVE}/${name}.mp3”
downed=1;
break;
else
echo -e “\e[1;6m\e[1;31m下载 ${name}.mp3 文件无效,正在删除重新下载\e[1;6m\e[00m”
rm “${TMP}/${name}.mp3”;
((j++))
fi
else
echo -e “\e[1;6m\e[1;31m下载 ${name}.mp3 文件无效,正在删除重新下载\e[1;6m\e[00m”
rm “${TMP}/${name}.mp3”;
((j++))
fi
done
#如果下载成功继续下其余的歌
#continue用于跳过循环体中的后续命令
if [ “$downed” = 1 ] ; then
((i++))
echo -e “\e[1;7m\e[1;41m下载 $name 成功\e[1;7m\e[00m”
continue;
fi
# 如果没有mp3格式的则下载wma格式的歌
j=1;
while((j<=downline_wma)); do
wma=`awk ‘NR=='”$j”” ${TMP}/down_wma.txt`
echo -e “\e[1;6m\e[1;31m正在下载${name}.wma\e[1;6m\e[00m”
#echo -e “\e[1;6m\e[1;31m中转页面地址为${mp3}\e[1;6m\e[00m”
wget -O “${TMP}/transit.html” “$wma”
realURL=`cat ${TMP}/transit.html | grep “<li class=\”li\” style=\”margin-right:10px;\”>” | sed ‘s/.*href=”//’ | sed ‘s/” target=”_blank”>.*//’`
#echo -e “\e[1;6m\e[1;31m真实下载地址为${realURL}\e[1;6m\e[00m”
axel -n 7 -a -o “${TMP}/${name}.wma” “${realURL}”
#wget太慢了。但是如果没有安装axel,可以把上面一行注释掉,用下面一行代替
#wget -c –tries=$TRYCOUNT $realURL -O “${TMP}/${name}.wma3”
if [ “$?” = 0 ]; then
if [ `file -ib “${TMP}/${name}.wma” | sed -e ‘s/\/.*//g’` = “application” ]; then
mv “${TMP}/${name}.wma” “${SAVE}/${name}.wma”
downed=1;
break;
else
echo -e “\e[1;6m\e[1;31m下载 ${name}.wma 文件无效,正在删除重新下载\e[1;6m\e[00m”
rm “${TMP}/${name}.wma”;
((j++))
fi
else
echo -e “\e[1;6m\e[1;31m下载 ${name}.wma 文件无效,正在删除重新下载\e[1;6m\e[00m”
rm “${TMP}/${name}.wma”;
((j++))
fi
done
((i++))
if [ “$downed” = 1 ] ; then
echo -e “\e[1;7m\e[1;41m下载 $name 成功\e[1;7m\e[00m”
else
echo -e “\e[1;7m\e[1;41m下载 $name 失败\e[1;7m\e[00m”
fi
done
rm -fr ${TMP}
exit 0 |