+ -
当前位置:首页 → 问答吧 → 一个从baidu哪里抓歌的Class[垃圾Class]

一个从baidu哪里抓歌的Class[垃圾Class]

时间:2007-11-08

来源:互联网

看源码吧,也是比较垃圾的,至少我是不满意的...通过给歌名从百度哪里读取:歌手,歌曲的专辑,歌词,歌曲的下载地址.
复制PHP内容到剪贴板
PHP代码:

<?php
/**
 * Author:Lazy
 * QQ:8820857
 * Email:o0lazy0o @ gmail.com
 * Version:0.2
 * Copyright:none.
 * 可任意使用但请保留此信息.
 */
require_once('http.php');
class baidu{
    var $Handle;
    var $URL;
    var $Remote;
    var $Song;
    var $Singer;
    var $Album;
    var $Lyric;
    var $LrcLink;
    var $LinkList;

    function baidu(){
        $this->Handle = new http();
    }

    function parse_list($Force=true){
        $Regexp = '/(?:<table.+(?:id=tbs|id=\'tbs\'|id="tbs")[^>]+>)(.*?)<\/table>/is';
        preg_match($Regexp,$this->Remote,$Array);
        $TmpList = strip_tags($Array[1],"<a><td>");
        $Regexp = '/<td[^>]*>(.*?)<\/td>/is';
        preg_match_all($Regexp,$TmpList,$Array);
        $Array = array_pop($Array);
        $Count = count($Array);
        if(empty($this->Singer)){
            for($Foo=0;$Foo<$Count;$Foo=$Foo+10){
                $Tmp = str_replace(' ','',trim(strip_tags($Array[$Foo+2])));
                $Temp = str_replace(' ','',trim(strip_tags($Array[$Foo+3])));
                $Singer[md5($Tmp)]['Count']++;
                $Album[md5($Temp)]['Count']++;
                $Singer[md5($Tmp)]['Singer'] = $Tmp;
                $Album[md5($Temp)]['Album'] = $Temp;
            }
            sort($Singer);
            sort($Album);
            $Tmp = array_pop($Singer);
            $Temp = array_pop($Album);
            $this->Singer = $Tmp['Singer'];
            $this->Album = $Temp['Album'];
        }
        for($Foo=0;$Foo<$Count;$Foo=$Foo+10){
            $Tmp = str_replace(' ','',trim(strip_tags($Array[$Foo+2])));
            $Temp = trim(strip_tags($Array[$Foo+1]));
            if($Force && $this->Singer == $Tmp && strtoupper($Temp) == strtoupper($this->Song)){
                $Return[]=array('Album'=>str_replace(' ','',trim(strip_tags($Array[$Foo+3]))),'Lyric'=>$Array[$Foo+5],'Song'=>$Array[$Foo+1]);
            }else{
                if($this->Singer == $Tmp){
                    $Return[]=array('Album'=>str_replace(' ','',trim(strip_tags($Array[$Foo+3]))),'Lyric'=>$Array[$Foo+5],'Song'=>$Array[$Foo+1]);
                }
            }
        }
        return $Return;
    }

    function parse_lyric($URL){
        $LinkType = "href";
        $Regexp = '/[\\s]?(?:'.$LinkType.')[\\s]?=[\\s]?("(?:.+?)"|\'(?:.+?)\'|(?:.+?)[\\s>]?)(?:[\\s]+.*?)?>(.*?)<\/a>/sim';
        preg_match($Regexp,$URL,$Array);
        $URL = trim($Array[1]," '\"\t\n\r\0\x0B");
        $Remote = $this->Handle->get($URL);
        $Regexp = '/href=(.*\\.lrc)[^>]*>/i';
        preg_match($Regexp,$Remote,$Array);
        $this->LrcLink = trim($Array[1]," '\"\t\n\r\0\x0B");
        if(empty($this->LrcLink)){
            $this->LrcLink=false;
            $this->Lyric=false;
        }else{
            $Remote = $this->Handle->get($this->LrcLink);
            $this->Lyric = trim(preg_replace('/\[[^\]]+\]/','',$Remote));
        }
    }

    function parse_song($List,$Force=true){
        $LinkType = "href";
        $Regexp = '/[\\s]?(?:'.$LinkType.')[\\s]?=[\\s]?("(?:.+?)"|\'(?:.+?)\'|(?:.+?)[\\s>]?)(?:[\\s]+.*?)?>(.*?)<\/a>/sim';
        foreach($List as $Var){
            preg_match($Regexp,$Var['Song'],$Array);
            $URL = trim($Array[1]," '\"\t\n\r\0\x0B");
            $Remote = $this->Handle->get($URL);
            preg_match($Regexp,$Remote,$Array);
            $URL = trim($Array[1]," '\"\t\n\r\0\x0B");
            $this->LinkList[]=$URL;
            if($this->Handle->head($URL)==true and $Force){
                $this->LinkList[]=$URL;
            }elseif($Force==false){
                $this->LinkList[]=$URL;
            }else{
                continue;
            }
        }
    }

    function get($Song,$Singer=null,$Force=true,$ForceHead=true){
        $this->Song = trim($Song);
        $this->Singer = trim($Singer);
        if(empty($this->Song)){ exit('Must Fill The Song Name'); }
        //http://mp3.baidu.com/m?f=ms&rn=&tn=baidump3&ct=134217728&word=%C1%E3%CA%B1%C1%E3%B7%D6&lm=-1
        $BaseURL = "http://mp3.baidu.com/m?f=ms&rn=&tn=baidump3&ct=134217728&word={$this->Song}&lm=-1";
        $CacheFile = md5($BaseURL).'.cache';
        $this->Remote = $this->Handle->get($BaseURL);
//        if(!file_exists($CacheFile)){
//            $this->Remote = $this->Handle->get($BaseURL);
//            $Handle = fopen('./data/'.md5($BaseURL).'.cache','wb+');
//            fwrite($Handle,$this->Remote);
//            fclose($Handle);
//        }else{
//            $this->Remote = file_get_contents($CacheFile);
//        }

        $List = $this->parse_list();
        $this->parse_lyric($List[0]['Lyric']);
        $this->parse_song($List,$ForceHead);
        return array('Song'=>$this->Song,'Singer'=>$this->Singer,'Album'=>$this->Album,'Lyirc'=>$this->Lyric,'LrcLink'=>$this->LrcLink,'LinkList'=>$this->LinkList);
    }
}
//$Demo = new baidu();
//var_dump($T->$Demo('断点'));
?>

--------------------------------------------------------------------------------------------------------------------------
顺便发下求职信息,有哪家公司招初级phper的请联系我:[email protected]

[ 本帖最后由 lazy 于 2007-11-7 21:51 编辑 ]

作者: lazy   发布时间: 2007-11-07

其中的http.php
见http://www.phpchina.com/bbs/view ... mp;extra=#pid305665

作者: lazy   发布时间: 2007-11-07

我们这招人
有兴趣的话可以发简历到
[email protected]  注明:PHP

作者: beluckly   发布时间: 2007-11-07