Tudou.class.php 5.4 KB
<?php
class Util_Video_Api_Tudou extends Util_Video_Api
{
    private $jsonurl = "http://www.tudou.com/playlist/service/getPlaylistBigPic.html?lid={id}";
    private $htmlurl = "http://www.tudou.com/playlist/playindex.do?lid={id}";
    private $getflashurl = "http://www.tudou.com/programs/share2.php?lid={id}";
    private $swf_url_1 = "http://www.tudou.com/v/{id}/v.swf";
    const TUDOU_VIDEO_PID = 'c57a52a5393e16ca'; // 土豆视频PID
    private static $tudou_video_api = 'http://api.tudou.com/v3/gw?method=repaste.info.get&appKey=%s&format=json&url=%s'; // 土豆视频接口
    protected function getDomain()
    {
        return "tudou.com";
    }
    public function getData($url)
    {
        if (empty ( $url ))
        {
            throw new Util_Video_ApiException('没有数据');
        }
        $api_url = sprintf ( self::$tudou_video_api, self::TUDOU_VIDEO_PID, urlencode ( $url ) );
        $result = Util_Video_ApiConnect::get($api_url);
        
        if($result == '5001 : 服务错误' || $result=='{}' || $result === false)
        {
            return $this->getViolentData($url);
        }
        else
        {
            $result = json_decode($result);
            $data = new Util_Video_Data($this->id);
            if(@$result->repasteInfo->playlistInfo->outerPlayerUrl && @$result->repasteInfo->playlistInfo->title && @$result->repasteInfo->playlistInfo->playlistPicUrl)
            {
                $data->swf = $result->repasteInfo->playlistInfo->outerPlayerUrl;
                $data->title = htmlspecialchars ( $result->repasteInfo->playlistInfo->title );
                $data->thumb = $result->repasteInfo->playlistInfo->playlistPicUrl;
            }
            else
            {
                $data->swf = $result->repasteInfo->itemInfo->outerPlayerUrl;
                $data->title = htmlspecialchars ( $result->repasteInfo->itemInfo->title );
                $data->thumb = $result->repasteInfo->itemInfo->picUrl;
            }
            return $data;
        }
        
    }
    
    public function getViolentData($url)
    {
        $dataobj = new Util_Video_Data($this->id);
        if(preg_match_all("/programs\/view\/(.*)$/si", $url, $vid, 2))
        {
            $vid = trim(preg_replace("/\/.*$/si", "", preg_replace("/\\.*$/si", "", $vid[0][1])));
            if(empty($vid))
            {
                throw new Util_Video_ApiException('url解析出错');
            }
            $swfurl = str_ireplace("{id}", $vid, $this->swf_url_1);
            $urlarr = get_headers($swfurl);
            $daturl = "";
            if(is_array($urlarr))
            {
                foreach($urlarr as $v)
                {
                    if(preg_match_all("/\s*Location\s*:(.*)/", $v, $date, 2))
                    {
                        $daturl = urldecode(trim($date[0][1]));
                    }
                }
            }
            if(empty($daturl))
            {
                throw new Util_Video_ApiException("url解析出错");
            }
            $pas = parse_url($daturl);
            if(empty($pas['query']))
            {
                throw new Util_Video_ApiException("url解析出错");
            }
            parse_str($pas['query'], $qdata);
            if(isset($qdata['tag']))
            {
                $tag = explode(",", trim($qdata['tag']));
            }
            $dataobj->id = isset($qdata['iid']) ? $qdata['iid'] : 0;
            $dataobj->thumb = isset($qdata['snap_pic']) ? $qdata['snap_pic'] : "";
            $dataobj->time = isset($qdata['totalTime']) ? $qdata['totalTime'] : "";
            $dataobj->tags = isset($tag) ? $tag : array();
            $dataobj->swf = $swfurl;
            $dataobj->title = isset($qdata['title']) ? $qdata['title'] : "";
        }
        else if(preg_match("/([0-9]+)/si", $url))
        {
            $idandswf = $this->getid($url);
            $jsonurl = str_ireplace("{id}", $this->id, $this->jsonurl);
            $dat = Util_Video_ApiConnect::get($jsonurl, array(
                CURLOPT_ENCODING => "gzip"
            ));
            $jsonobj = json_decode($dat);
            $dataobj->thumb = $jsonobj->message;
            $dataobj->time = 0;
            $dataobj->tags = array();
            $dataobj->swf = $idandswf['swf'];
            $dataobj->title = $idandswf['title'];
        }
        else
        {
            throw new Util_Video_ApiException("url解析出错");
        }
        return $dataobj;
    }
    private function getid($url)
    {
        $id = $this->getUrl($url);
        $titledb = Util_Video_ApiConnect::get($url, array(
            CURLOPT_ENCODING => "gzip"
        ));
        if(preg_match_all("/<\s*title\s*>(.*)<\s*\/title\s*>/siU", $titledb, $title, 2))
        {
            $title = $title[0][1];
        }
        if(preg_match_all("/,lid.*=(.*),/isU", $titledb, $lid, 2))
        {
            $id = trim($lid[0][1]);
        }
        $url = str_ireplace("{id}", $id, $this->getflashurl);
        $content = Util_Video_ApiConnect::get($url, array(
            CURLOPT_ENCODING => "gzip"
        ));
        if(preg_match_all("/<\s*embed\s*src=\"(.*)\"/siU", $content, $flashurl, 2))
        {
            $swf = $flashurl[0][1];
        }
        else
        {
            throw new Util_Video_ApiException("解析url出错");
        }
        $this->id = $id;
        return array(
            "swf" => $swf, 
            "id" => $id, 
            "title" => iconv("GBK", "UTF-8", $title)
        );
    }
}

?>