#!/bin/bash
#Copyright (c) 2005 ubuntu.org.cn
#License: GPLv2
#需要安装编码转换器
#sudo apt-get install recode
#mp3的地址
SOURCE="http://list.mp3.baidu.com/list/newhits.html"
#保存mp3的目录
SAVE="${HOME}/mp3100"
#下载重试次数
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所有全角空格去掉
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.*FFFFFF">//g' |\
#删除</a>..."_blank">
sed -e 's/<\/a>.*_blank">/-/g' |\
sed -e 's/<\/a>.*_blank>/-/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 "<td height=\"28\" class=\"d\">" |\
sed -e 's/.*<a href="//g' | sed -e 's/" target=_blank>.*//g' |\
sed '/mid/d' > ${TMP}/down.txt
#size.txt为有效的下载文件大小
iconv -f gbk -t utf8 -c ${TMP}/down.html | grep "M<\/td>" |\
sed -e 's/<td class="t">//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
#得到以下形式 (下载地址 文件大小)
#http://wma2.7t7t.com/pycs/pycs/311/370247.Wma 1.2
#http://snakelmq.hfxyd.com/milk/babybabytellme.mp3 3.2
#http://bbsatt2.ccmove.com.cn/download/1280590/baby%20baby%20tell%20me%28PXC%D6%C6%D7%F7%29.mp3 0.3
#http://vod.music165.com/music/song/new02/wxl_05.07.29/3.Wma 1.6
#http://202.107.247.54/16/0406/94/3.wma 1.6
#排序
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 "mp3$" ${TMP}/temp.txt > ${TMP}/down_mp3.txt
grep -i "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`
echo -e "\e[1;6m\e[1;31m正在下载${name}.mp3\e[1;6m\e[00m"
wget -c --tries=$TRYCOUNT $mp3 -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"
wget -c --tries=$TRYCOUNT $wma -O "${TMP}/${name}.wma"
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
原理是用wget下载下页面来,然后用SED来提取页面,提取到连接后下载
posted on 2006-10-27 15:41
小小程序程序员混口饭吃 阅读(1155)
评论(1) 编辑 收藏 所属分类:
java