|
#!/bin/bash
#Getsong
#baidu topmp3 URL
baseurl="http://list.mp3.baidu.com/list/topmp3.html?id=1"
#baseurl="http://list.mp3.baidu.com/list/newhits.html?id=1#top1"
#some function
#ctrl+c 信号处理函数
function exitprocess()
{
echo -ne '\E[1;34m'"E)xit or C)hoose another song or T)ry other links?"
tput sgr0
read choose
case $choose in
E|e)
exit
;;
t|T)
#发送两个信号 一个用于可用连接,一个选另一首歌
kill -s SIGRTMIN $$
;;
*)
kill -s SIGRTMIN+8 $$
;;
esac
}
#随机选歌函数
#开始监视ctrl+c信号
trap 'exitprocess' 2
#建立临时目录
if [ ! -d $HOME/.song ];then
mkdir $HOME/.song
fi
cd $HOME/.song
#用Wget换取页面回来分析,各个过程分别保存为 song[1-10].txt
wget $baseurl -O song0.txt 2>/dev/null
#filter
#初步过滤
sed 's/[>|<]/"/g' song0.txt |awk -F \" '{for (i=1;i<=NF;i++)\
{if ($i~/%[A-Z][A-Z0-9]/){print $i"\""$(NF-4)}} }' \
|iconv -f gbk -t utf-8 |grep "[^$]" >songU.txt
#页面转为utf-8码,以防显示时乱码
#给歌连接编号,便于存取
awk -F \" '{if ($2!="") {print "*"NR"*",$1,$2}}' \
songU.txt > list.txt
#bfailed选歌标记,下面的bsubfailed为选择另一个可用链接标记
bfailed=0
#列表歌数
list_song=`cat list.txt|wc -l`
let "list_song += 1"
while [ $bfailed = 0 ]
do
#产生随机数
select=$(($RANDOM%$list_song))
#继续过滤
awk '{print $1,$3}' list.txt
#####################################################
#随机选歌
echo
echo -e '\E[1;34m'"随机选歌开始:"'\E[1;31m'"$select";tput sgr0
sleep 2
# echo -n "please select a song: "
# read select
#another url
#过滤
secondurl=`grep "\*$select\*" list.txt |awk '{print $2}'`
songname=`grep "\*$select\*" list.txt |awk '{print $3}'`
#再次换取页面过滤
wget "$secondurl" -O song7.txt 2>/dev/null
#编码,同上
iconv -f gb18030 -t utf-8 song7.txt >song8.txt
# 过滤
awk -F \" '{for (i=1;i<=NF;i++) {if ($i~/[0-9]&lm=16777216$/) \
{print "\""$i"\""}}}' song8.txt >song9.txt
awk -F \" '{ print NR":",$2}' song9.txt >song10.txt
#某个页面符合的歌数
totalsongs=`cat song10.txt |wc -l`
#用颜色字体显示总歌数
echo -ne '\E[1m' "find";tput sgr0
echo -ne '\E[1;35m' $totalsongs ;tput sgr0
echo -e '\E[1m' "records";tput sgr0
bsubfailed=0
while [ $bsubfailed = 0 ]
do
#接收换歌信号
trap "break" SIGRTMIN+8
trap "continue" SIGRTMIN
if [ $totalsongs -lt 1 ];then
break
fi
#随机选歌
let "totalsongs += 1"
selected=$(($RANDOM%$totalsongs))
echo -e "please choose one[1-$totalsongs]:"'\E[1;35m'"$selected"
tput sgr0
sleep 2
# read selected
awk -F $selected: '{print $2}' song10.txt |grep "[^$]" > song11.txt
read last_select < song11.txt
wget "$last_select" -O last.txt 2>/dev/null
#两个时间begin_time,end_time标记,用于某些歌过短而退出链接选择项
begin_time=`date +%s`
#开始播放
if mplayer -ao alsa `grep "embed" last.txt |awk -F \" '{for (i=1;i<=NF;i++)\
{if ($i~/^http/) {print $i}}}'|grep "[^&]"` 2>/dev/null ;then
#结束时间
end_time=`date +%s`
#必要的异常退出检查
if [ $((end_time-begin_time)) -lt 60 ];then
echo -e '\E[1;31m'"connected failed!try again!";tput sgr0
bsubfailed=0
else
bsubfailed=1
bfailed=0
fi
else
echo "$selected faile,choose another one!"
bsubfailed=0
fi
#换链接信号
trap "echo wrong" SIGRTMIN
done
done
#end |
|