LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 893|回复: 5

初学,发个百度topmp3在线听脚本!

[复制链接]
发表于 2006-10-30 00:46:30 | 显示全部楼层 |阅读模式
#!/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
发表于 2006-10-30 08:51:12 | 显示全部楼层
顶一下~~百度的价值……
回复 支持 反对

使用道具 举报

发表于 2006-10-30 08:51:57 | 显示全部楼层
一起顶
回复 支持 反对

使用道具 举报

发表于 2006-10-30 09:24:47 | 显示全部楼层
如果加上[php]或[code]我就顶
回复 支持 反对

使用道具 举报

发表于 2006-10-31 17:39:57 | 显示全部楼层
好东西,不过,在我这执行有点问题,歌曲列表的字体是乱码的。可是我的终端能正常显示中文的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-11-1 12:48:11 | 显示全部楼层
不知你终端是什么编码的?我是把gbk-- 转为uft-8的!用iconv!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表