|
发表于 2005-7-16 21:19:27
|
显示全部楼层
最新更新
[PHP]#!/bin/bash
#################################################################
# #
# 百度MP3歌曲下载工具 #
# Reort bugs to Martian < Martian2008@gmail.com > #
# Last Update 2005/07/17 #
# #
#################################################################
USAGE="`basename $0`:未指定参数
用法: `basename $0` [选项]\n
请尝试使用“`basename $0` -h”查看更多的选项。"
USAGE2="
GetMP3 <martian2008@gmail.com>百度MP3歌曲下载工具
用法: `basename $0` [选项]\n
选项
-a top100|top500|search 选择下载mp3的分类
-b 1-max 下载开始曲目数(default 1)
-e 1-max 下载结束曲目数(default max)
-g number 下载指定曲目(id编号)
-m 交互式选择下载镜像点
-o 覆盖已下载文件
-u 更新 top100/top500 数据
-U 更新下载镜像
-c 清空 search 历史数据
-l 打印音乐数据列表
-s [strings] 查找歌曲
-S [PATH] 自定义文件保存目录
-p number 搜索文件深度 1*30
-f 自定义文件名,只在交互模式下起作用
-t type 指定下载文件类型:mp3 wma swf asf
"
M=0
O=0
B=1
E=0
L=0
G=0
U=0
F=0
SEARCH=""
WORD=""
TYP=""
LYNX="/usr/bin/lynx"
WGET="/usr/bin/wget"
GET_NUM=102
NUM=1
PAGE=2
SOURCE1="http://list.mp3.baidu.com/list/newhits.html"
SOURCE2="http://list.mp3.baidu.com/topso/mp3topsong.html"
SOURCE3="http://mp3.baidu.com/m?f=ms&rn=&tn=baidump3&ct=134217728&word=WORD&submit=%B0%D9%B6%C8%CB%D1%CB%F7&lm=-1"
TEMP1="${HOME}/.Getmp3/tmp_mp3_list"
TEMP2="${HOME}/.Getmp3/tmp_mp3_list2"
TEMP3="${HOME}/.Getmp3/tmp_mp3_down"
TEMP4="${HOME}/.Getmp3/tmp_mp3_search"
SAVE1="${HOME}/mp3_new100"
SAVE2="${HOME}/mp3_top500"
SAVE3="${HOME}/mp3_search"
export LESSCHARSET=latin1
export LC_ALL=zh_CN.gbk
export LANG=zh_CN.gbk
if [ ! -d "${HOME}/.Getmp3" ];then
mkdir -p "${HOME}/.Getmp3"
fi
while getopts a:hmuUob:e:lg:s:S:p:ft:c OPTION;
do
case "$OPTION" in
a) S="$OPTARG" ;;
s) SEARCH="$OPTARG" ;;
h) echo -e "$USAGE2"
exit 0;;
m) M=1;;
o) O=1;;
U) U=1;;
l) L=1;;
t) TYP="`echo $OPTARG|tr [:upper:] [:lower:]`"
;;
f) F=1;;
S) if [ -d "$OPTARG" -a -w "$OPTARG" -a -x "$OPTARG" ];then
SAVE="$OPTARG"
else
echo -en "\\033[0;31m"
echo -e
echo "指定目录{OPTARG} 权限不正确或者该目录不存在"
echo -en "\\033[0;39m"
exit 1;
fi
;;
b)echo "${OPTARG}"|grep -qE '^[0-9]*?[0-9]*$' && B="${OPTARG}" || B=1
;;
e)echo "${OPTARG}"|grep -qE '^[0-9]*?[0-9]*$' && E="${OPTARG}" || E=0
;;
g)echo "${OPTARG}"|grep -qE '^[1-9]*?[0-9]*$' && G="${OPTARG}" || G=0
;;
p)echo "${OPTARG}"|grep -qE '^[0-9]*?[0-9]*$' && PAGE="${OPTARG}" || PAGE=2
;;
c) rm -rf "${TEMP4}"*
echo -en "\\033[0;36m"
echo "Search 数据清除完成"
echo -en "\\033[0;39m"
exit 0
;;
u) rm -rf "${TEMP1}"*
rm -rf "${TEMP2}"*
rm -rf "${TEMP3}"*
echo -en "\\033[0;36m"
echo "开始下载数据"
while [ ! -f "${TEMP1}_top100" ]
do
$LYNX -source ${SOURCE1} >"${TEMP1}_top100"
done
while [ ! -f "${TEMP1}_top500" ]
do
$LYNX -source ${SOURCE2} >"${TEMP1}_top500"
done
cat "${TEMP1}_top500" |grep --after-context=1 "<tr bgcolor=\"#FFFFFF\"><td width=\"20%\"><font color=\"#3333CC\">1." |\
sed -e 's/<\/a>/\n/g' -e 's/<\/td>//g' -e 's/<\/tr><tr>//g' -e 's/<\/tr><tr bgcolor=\"#FFFFFF\">//g'\
-e 's/<td width=\"20%\"><font color=\"#3333CC\">[0-9]*\.<\/font><a href=\"/http:\/\/list.mp3.baidu.com\/topso\//g'\
-e 's/\" target=_blank>/ /g' -e '/^$/d'|sed -e '/<td/d' >"${TEMP2}_top500"
while [ ${GET_NUM} -gt 0 ]
do
cat "${TEMP1}_top100" |grep --after-context=1 "<div align=\"center\"><font color=\"#3333CC\">${NUM}.</font>" | \
grep --after-context=1 "a href" |sed 's/^$//'| sed -e 's/<[^>][^>]*>//' -e 's/<\/td>//' -e 's/^ *//'| \
sed -e 's/<a href="//g' -e 's/" target="_blank">/ /g' -e 's/<\/a>*/ /g' >> "${TEMP2}_top100"
NUM=$((${NUM}+1))
GET_NUM=$((${GET_NUM}-1))
done
echo 数据更新完毕
echo -en "\\033[0;39m"
exit 0
;;
\?) echo -e "$USAGE"
exit 1;;
esac
done
if [ "$#" -eq 0 ] ;then
echo -e "${USAGE}"
exit 1
fi
case "${S}" in
top100) SOURCE="${SOURCE1}"
: ${SAVE:=${SAVE1}}
;;
top500) SOURCE="${SOURCE2}"
: ${SAVE:=${SAVE2}}
;;
search) SOURCE="${SOURCE3}"
: ${SAVE:=${SAVE3}}
if [ ! -n "${SEARCH}" ];then
echo "-a search 参数必须附带 -s 选项"
echo -e
exit 1
else
WORD="`echo "${SEARCH}" | tr -d "\n" | xxd -i | sed -e "s/ 0x/%/g" | tr -d " ,\n"`"
B=1
fi
;;
esac
if [ -z "$SOURCE" ] ;then
echo -e "${USAGE}"
exit 1
fi
TEMP1=${TEMP1}_${S}
TEMP2=${TEMP2}_${S}
TEMP3=${TEMP3}_${S}
TEMP4=${TEMP4}_${WORD}
if [ ! -d "${SAVE}" ];then
echo -en "\\033[0;36m"
echo "创建目录"
echo -en "\\033[0;39m"
mkdir -p ${SAVE}
fi
#clear
echo -e
echo -en "\\033[0;36m"
echo "正在下载/分析数据"
echo -en "\\033[0;39m"
#rm -rf ${TEMP1}
if [ "${S}" != "search" ];then
while [ ! -f ${TEMP1} ]
do
${LYNX} -source ${SOURCE} >${TEMP1}
done
else touch ${TEMP2}
echo "test" >${TEMP2}
fi
#rm -rf ${TEMP2}
if [ "${S}" = "top500" ]; then
while [ ! -f "${TEMP2}" ]
do
cat ${TEMP1} |grep --after-context=1 "<tr bgcolor=\"#FFFFFF\"><td width=\"20%\"><font color=\"#3333CC\">1." |\
sed -e 's/<\/a>/\n/g' -e 's/<\/td>//g' -e 's/<\/tr><tr>//g' -e 's/<\/tr><tr bgcolor=\"#FFFFFF\">//g'\
-e 's/<td width=\"20%\"><font color=\"#3333CC\">[0-9]*\.<\/font><a href=\"/http:\/\/list.mp3.baidu.com\/topso\//g'\
-e 's/\" target=_blank>/ /g' -e '/^$/d'|sed -e '/<td/d' >${TEMP2}
done
elif [ "${S}" = "top100" ]; then
while [ ! -f "${TEMP2}" ]
do
while [ ${GET_NUM} -gt 0 ]
do
cat ${TEMP1} |grep --after-context=1 "<div align=\"center\"><font color=\"#3333CC\">${NUM}.</font>" | \
grep --after-context=1 "a href" |sed 's/^$//'| sed -e 's/<[^>][^>]*>//' -e 's/<\/td>//' -e 's/^ *//'| \
sed -e 's/<a href="//g' -e 's/" target="_blank">/ /g' -e 's/<\/a>*/ /g' >> ${TEMP2}
NUM=$((${NUM}+1))
GET_NUM=$((${GET_NUM}-1))
done
done
fi
unset NUM
: ${NUM:=${B}}
if [ "${G}" -gt 0 ] && [ "${S}" != "search" ];then
NUM="${G}"
E="${G}"
fi
while [ -n "`cat ${TEMP2} |sed -n "${NUM}p"`" ]
do
if [ "${E}" -gt 0 -a "${NUM}" -gt "${E}" ] ;then
if [ "${L}" -eq 1 -o -n "${SEARCH}" ] && [ "${S}" !="search" ] ;then
echo -e "${LIST}" |less
fi
exit 0
fi
PRE=`cat ${TEMP2} |sed -n "${NUM}p"`
PRE=(`echo $PRE`)
if [ "${S}" = "search" ];then
SOURCE="`echo ${SOURCE}|sed -e "s/WORD/${WORD}/g"`"
PRE=(`echo "${SOURCE}" "${SEARCH}"`)
TEMP3="${TEMP4}"
fi
if [ -n "${PRE[0]}" -a -n "${PRE[1]}" ];then
if [ -n "${PRE[3]}" ] ;then
MP3=${PRE[3]}-${PRE[1]}
else
MP3=${PRE[1]}
fi
MP3=`echo ${MP3} |sed 's/\//_/g'`
if [ "${L}" -eq 1 -o -n "${SEARCH}" ] && [ "${S}" != "search" ];then
if [ -n "${SEARCH}" ];then
SEARCH2=` echo "${NUM}. ${MP3}" |grep "${SEARCH}"`
if [ -n "${SEARCH2}" ];then
LIST="${LIST}\n ${SEARCH2}"
fi
unset SEARCH2
NUM=$((${NUM}+1))
continue
fi
LIST="${LIST}\n ${NUM}. ${MP3}"
NUM=$((${NUM}+1))
continue
fi
if [ "${S}" = "search" ];then
echo -en "\\033[0;36m"
echo "正在搜索数据: ${SEARCH}"
else
echo -en "\\033[0;36m"
echo "正在处理数据 ${NUM}. ${MP3}"
fi
if [ "${U}" -eq 1 ];then
rm -rf "${TEMP3}_${NUM}"
fi
while [ ! -f "${TEMP3}_${NUM}" -o ! -s "${TEMP3}_${NUM}" ]
do
${LYNX} -source "${PRE[0]}" > "${TEMP3}_${NUM}"
PAGE_NUM=`cat ${TEMP3}_${NUM} | grep --after-context=1 "<ol><font class=p1>" | sed -e '/<br><br><font class=f9>/d' -e 's/<ol><font class=p1>//g' -e 's/<\/a>/\n/g' -e 's/<\/font>//g'|sed -e '/\[下一页\]/d'|wc -l`
: ${PAGE_NUM:=1}
NEXTURL=`cat ${TEMP3}_${NUM} |grep --after-context=1 "<ol><font class=p1>" | sed -e '/<br><br><font class=f9>/d' -e 's/<ol><font class=p1>//g' -e 's/<\/a>/\n/g' -e 's/<\/font>//g' |grep "[下一页]" |sed -e 's/ \;<a href=//g' -e 's/>\[下一页\]//'`
echo -en "\\033[0;34m"
if [ "${PAGE_NUM}" -eq 0 ];then
echo "没有找到数据"
rm -rf "${TEMP3}_${NUM}"
exit 0
fi
echo "正在处理第1页数据,统计大约有${PAGE_NUM}页,本次最大处理数${PAGE}页"
cat "${TEMP3}_${NUM}" |grep --before-context=0 --after-context=1 "<td class=r1>" | \
sed -e '/<\/script>*/d' -e '/--/d'|sed -e 's/\" target=_blank><font style=color:#e10900>/ /g' -e 's/<\/font><\/td>//g' -e 's/<\/a><br><font color=\"#999999\" class=f10>//g' -e 's/<font style=color:#e10900>[^>][^>]*<[^<]*//g' -e 's/<a href=\"//g' -e 's/ - / /g' -e 's/" target=_blank>/ /g' -e 's/<\/font>//g' > "${TEMP3}_${NUM}"
#sed -e '/<\/script>*/d' -e '/--/d'| \
#sed -e 's/<font[^>][^>].*//g' -e 's/" target=.*//g' -e 's/^.*<a href="//g' >"${TEMP3}_${NUM}"
NUM4=2
while [ "${PAGE}" -ge "${NUM4}" -a "${PAGE_NUM}" -gt 1 -a -n "${NEXTURL}" ]
do
${LYNX} -source "${NEXTURL}" >"${TEMP3}_${NUM}_c"
ret=$?
if [ $ret -ne 0 ]; then
echo -en "\\033[0;31m"
echo "无法获取数据,搜索中止!"
echo -en "\\033[0;31m"
break
fi
PAGE_NUM=`cat "${TEMP3}_${NUM}_c" | grep --after-context=1 "<ol><font class=p1>" | sed -e '/<br><br><font class=f9>/d' -e 's/<ol><font class=p1>//g' -e 's/<\/a>/\n/g' -e 's/<\/font>//g'|sed -e '/\[上一页\]/d'|sed -e '/\[下一页\]/d'|wc -l`
: ${PAGE_NUM:=1}
NEXTURL=`cat "${TEMP3}_${NUM}_c" |grep --after-context=1 "<ol><font class=p1>" | sed -e '/<br><br><font class=f9>/d' -e 's/<ol><font class=p1>//g' -e 's/<\/a>/\n/g' -e 's/<\/font>//g'|sed -e '/\[上一页\]/d' |grep "[下一页]" |sed -e 's/ \;<a href=//g' -e 's/>\[下一页\]//'`
echo "正在处理第${NUM4}页数据,统计大约有${PAGE_NUM}页,本次最大处理数${PAGE}页"
cat "${TEMP3}_${NUM}_c" |grep --before-context=0 --after-context=1 "<td class=r1>" | \
sed -e '/<\/script>*/d' -e '/--/d'|sed -e 's/\" target=_blank><font style=color:#e10900>/ /g' -e 's/<\/font><\/td>//g' -e 's/<\/a><br><font color=\"#999999\" class=f10>//g' -e 's/<font style=color:#e10900>[^>][^>]*<[^<]*//g' -e 's/<a href=\"//g' -e 's/ - / /g' -e 's/" target=_blank>/ /g' -e 's/<\/font>//g' >> "${TEMP3}_${NUM}"
#sed -e '/<\/script>*/d' -e '/--/d'| \
#sed -e 's/<font[^>][^>].*//g' -e 's/" target=.*//g' -e 's/^.*<a href="//g' >>"${TEMP3}_${NUM}"
rm -rf "${TEMP3}_${NUM}_c"
NUM4=$((${NUM4}+1))
done
echo -en "\\033[0;39m"
echo -en "\\033[0;34m"
echo "总共搜索到`cat "${TEMP3}"_"${NUM}" |wc -l` 个下载文件"
done
if [ -n "${TYP}" ]; then
echo "正在搜索指定类型数据"
echo -en "\\033[0;39m"
while [ ! -f "${TEMP3}_${NUM}_${TYP}_${NUM}" -o ! -s "${TEMP3}_${NUM}_${TYP}_${NUM}" ]
do
NUM7=1
while [ -n "`cat "${TEMP3}_${NUM}" | sed -n "${NUM7}p"`" ]
do
TYP2="`cat "${TEMP3}_${NUM}" | sed -n "${NUM7}p" | cut -d\ -f 1`"
TYP2="`basename "${TYP2}" |awk -F. '{print $2}'| tr [:upper:] [:lower:]`"
if [ "${TYP}" = "${TYP2}" ];then
cat "${TEMP3}_${NUM}" | sed -n "${NUM7}p" >> "${TEMP3}_${NUM}_${TYP}_${NUM}"
fi
unset TYP2
NUM7=$((${NUM7}+1))
done
break
done
if [ ! -f "${TEMP3}_${NUM}_${TYP}_${NUM}" -o ! -s "${TEMP3}_${NUM}_${TYP}_${NUM}" ];then
echo "在指定类型中没有找到任何数据,使用全部已知类型."
else
echo "在指定类型中总共搜索到`cat "${TEMP3}_${NUM}_${TYP}_${NUM}" |wc -l`个下载文件"
TEMP3="${TEMP3}_${NUM}_${TYP}"
fi
fi
#交互式下载
if [ "${M}" -eq 1 ];then
NUM3=1
SELECT=""
NUM5=1
while [ -n "`cat "${TEMP3}_${NUM}" |sed -n "${NUM3}p"`" -a "${NUM5}" -le 20 ]
do
unset P2
P=(`cat "${TEMP3}_${NUM}" |sed -n "${NUM3}p"`)
NUM6=1
while [ -n "${P[$NUM6]}" ]
do
P2="${P2}|${P[$NUM6]}"
NUM6=$((${NUM6}+1))
done
SELECT="${SELECT} ${P[0]}|${P2}"
NUM3=$((${NUM3}+1))
NUM5=$((${NUM5}+1))
if [ "${NUM5}" -le 20 -a -n "`cat "${TEMP3}_${NUM}" |sed -n "${NUM3}p"`" ];then
continue
fi
if [ "${NUM3}" -gt 21 ];then
if [ "${NUM3}" -lt 30 ];then
T_NUM3=1
else
T_NUM3=$((${NUM3}-20-${NUM5}+1))
fi
SELECT="${SELECT} 上一页|${T_NUM3}"
fi
if [ "`cat "${TEMP3}_${NUM}" |wc -l`" -gt "${NUM3}" ];then
SELECT="${SELECT} 下一页|${NUM3}"
fi
clear
echo -en "\\033[0;34m"
echo "总共搜索到`cat "${TEMP3}"_"${NUM}" |wc -l` 个下载文件"
echo -en "\\033[0;33m"
echo "歌曲: ${MP3}"
echo "请选择下载镜像"
echo -en "\\033[0;39m"
select COMPONENT in $SELECT
do
if [ -z "${COMPONENT}" ];then
echo "该镜像地址不存在"
echo -en "\\033[0;33m"
echo "请选择下载镜像 按回车继续"
echo -en "\\033[0;39m"
continue
else
DOWN=`echo "${COMPONENT}" | cut -d\| -f 1 `
TYPE=`basename "${DOWN}" |awk -F. '{print $2}'`
if [ -n "`echo "${COMPONENT}" | cut -d\| -f 4 |sed -e 's/\r//g' `" ];then
ATTR="(`echo "${COMPONENT}" | cut -d\| -f 4 |sed -e 's/\r//g' `)"
else
ATTR=""
fi
if [ "${DOWN}" = "下一页" -o "${DOWN}" = "上一页" ];then
NUM3=`echo "${COMPONENT}" | cut -d\| -f 2 `
NUM5=1
unset SELECT
echo -en "\\033[0;33m"
echo "正在跳转到${DOWN}"
echo -en "\\033[0;39m"
break
fi
if [ "${F}" -eq 1 ];then
echo -en "\\033[0;33m"
while :
do
echo "请输入保存的文件名,不需要加扩展名"
read CMD
if [ -n "${CMD}" ];then
echo ${CMD} |grep -qE '^[^\/\~\^\&\*\@\#\$\%\|\!\?\`\.]*$' && break ||echo 文件名错误 && continue
fi
done
echo -en "\\033[0;33m"
fi
echo -en "\\033[0;36m"
echo "开始从镜像站点${REPLY}下载载歌曲 ${NUM}. ${MP3}"
echo -en "\\033[0;33m"
MP3=${CMD:=${MP3}}
echo "文件保存在${SAVE}"
echo -en "\\033[0;39m"
if [ "${O}" -eq 1 ];then
rm -rf "${SAVE}/${MP3}${ATTR}.${TYPE}"
fi
fi
if [ -f "${SAVE}/${MP3}${ATTR}.${TYPE}" ];then
echo -en "\\033[0;31m"
echo "文件: ${SAVE}/${MP3}${ATTR}.${TYPE} 已存在,下载中止"
echo -e
echo -en "\\033[0;39m"
unset TYPE DOWN
break
fi
echo $ATTR
${WGET} -t 1 -N -O "${SAVE}/${MP3}${ATTR}.${TYPE}" ${DOWN}
ret=$?
if [ $ret -eq 0 ]; then
if [ "`file -b "$SAVE/${MP3}${ATTR}.${TYPE}"`" = "empty" ] || \
[ "`file -b "$SAVE/${MP3}${ATTR}.${TYPE}"`" = "HTML document text" ];then
rm -rf ${SAVE}/${MP3}${ATTR}.${TYPE}
echo -en "\\033[0;31m"
echo "下载失败!"
echo -e
echo -en "\\033[0;39m"
echo -en "\\033[0;33m"
echo "请选择下载镜像 按回车继续"
echo -en "\\033[0;39m"
unset TYPE DOWN
continue
fi
break
else
rm -rf ${SAVE}/${MP3}${ATTR}.${TYPE}
echo -en "\\033[0;31m"
echo "下载失败!"
echo -e
echo -en "\\033[0;39m"
echo -en "\\033[0;33m"
echo "请选择下载镜像 按回车继续"
echo -en "\\033[0;39m"
unset TYPE DOWN
continue
fi
done
done
else
NUM2=1
while [ -n "`cat "${TEMP3}_${NUM}" |sed -n "${NUM2}p"`" ]
do
URL=`cat "${TEMP3}_${NUM}"|sed -n "${NUM2}p"| cut -d\ -f 1`
TYPE=`basename "${URL}" |awk -F. '{print $2}'`
echo -en "\\033[0;34m"
echo "总共搜索到`cat "${TEMP3}"_"${NUM}" |wc -l` 个下载文件"
echo -en "\\033[0;36m"
echo "开始下载歌曲 ${NUM}. ${MP3}"
echo -en "\\033[0;33m"
echo "文件保存在: ${SAVE}"
echo -en "\\033[0;39m"
if [ "${O}" -eq 1 ];then
rm -rf "${SAVE}/${MP3}.${TYPE}"
fi
if [ -f "${SAVE}/${MP3}.${TYPE}" ];then
echo -en "\\033[0;31m"
echo "文件: ${SAVE}/${MP3}.${TYPE} 已存在,下载中止"
echo -e
echo -en "\\033[0;39m"
unset URL TYPE
break
fi
${WGET} -t 1 -N -O ${SAVE}/${MP3}.${TYPE} ${URL}
ret=$?
if [ $ret -eq 0 ]; then
unset URL TYPE
break
else
rm -rf ${SAVE}/${MP3}.${TYPE}
echo -en "\\033[0;31m"
echo "下载失败!"
echo -e
echo -en "\\033[0;39m"
unset URL TYPE
fi
NUM2=$((${NUM2}+1))
done
fi
fi
NUM=$((${NUM}+1))
unset PRE MP3
done
if [ "${L}" -eq 1 -o -n "${SEARCH}" ] && [ "${S}" != "search" ];then
echo -e "${LIST}" |less
fi[/PHP] |
|