Youku.class.php 4.52 KB
<?php
class Util_Video_Api_Youku extends Util_Video_Api
{
    private $jsonurl = "http://v.youku.com/player/getPlayList/VideoIDS/{id}/timezone/+08/version/5/source/out?password=&ran={rand}&n=";
    private $htmlurl = "http://v.youku.com/v_show/{id}.html";
    private $review = "http://v.youku.com/v_vpcommentContent/id_{id}_time_{time}_page_{page}.html?__rt=1&__ro=videobodycommentlist";
    //优酷网
    const YOUKU_VIDEO_PID = 'XMjA5Ng=='; // 优酷视频PID
    private static $youku_video_api = 'http://api.youku.com/api_ptvideoinfo?pid=%s&id=%s'; // 优酷视频接口
    
    public function getData($url)
    {
        if(empty($url))
        {
            throw new Util_Video_ApiException("没有数据");
        }
        $api_url = sprintf ( self::$youku_video_api, self::YOUKU_VIDEO_PID, $url );       
        $result = Util_Video_ApiConnect::get($api_url);
        $data = new Util_Video_Data($this->id);
        if ($result) 
        {
            $vinfo = ( array ) simplexml_load_string ( $result, 'SimpleXMLElement', LIBXML_NOCDATA );
            $vinfo = ( array ) $vinfo ['item'];
            
            if (isset($vinfo['code']) && $vinfo ['code'] == 2)
            {
                throw new Util_Video_ApiException("已删除");
            }
            if(isset($vinfo ['title']) && isset($vinfo ['shareswf']))
            {
                $data->swf = $vinfo ['shareswf'];
                $data->title = htmlspecialchars ( $vinfo ['title'] );
                $data->thumb = $vinfo ['imagelink'];
            }
            else
            {
                return $this->getViolentData($url);
            }
        } 
        else 
        {
            return $this->getViolentData($url);
        }
        if(empty($data))
        {
            throw new Util_Video_ApiException("没有数据");
        }
        return $data;
        
    }
    
    /**
     * 暴力破解
     */
    private function getViolentData($url)
    {
        $jsonurl = str_ireplace("{rand}", rand(100, 999), $this->jsonurl);
        $idandswf = $this->getid($url);
        $jsonurl = str_ireplace("{id}", $this->id, $jsonurl);
        $dat = Util_Video_ApiConnect::get($jsonurl);
        $jsonobj = json_decode($dat);
        if(empty($jsonobj))
        {
            throw new Util_Video_ApiException("没有数据");
        }
        $dataobj = new Util_Video_Data($this->id);
        $dataobj->swf = $idandswf['swf'];
        $dataobj->thumb = $jsonobj->data[0]->logo;
        $dataobj->time = $jsonobj->data[0]->seconds;
        $dataobj->title = $jsonobj->data[0]->title;
        $dataobj->source = '来自于youku.com';
        return $dataobj;
    }
    
    private function getid($url)
    {
        $url = $this->getUrl($url);
        if(!preg_match("/^http:\/\/.*/i", $url))
        {
            $url = str_ireplace("{id}", $url, $htmlurl);
        }
        $content = Util_Video_ApiConnect::get($url);
        if(preg_match_all("/<embed\s*src=\"(.*)\"/siU", $content, $flashurl, 2))
        {
            $swf = $flashurl[0][1];
            $urlid = preg_replace("/\/.*$/si", "", preg_replace("/^.*sid\//si", "", $swf));
        }
        else
        {
            throw new Util_Video_ApiException('数据解析失败');
        }
        $this->id = $urlid;
        return array(
                        "swf" => $swf, 
                        "id" => $urlid
                    );
    }
    public function getreview($url = null)
    {
        $this->getid($url);
        $review = str_ireplace("{time}", time(), $this->review);
        $review = str_ireplace("{id}", $this->id, $review);
        $reviewpage = str_ireplace("{page}", 1, $review);
        $reviewpage = Util_Video_ApiConnect::get($reviewpage);
        $page = array();
        if(preg_match_all("/class=\"\s*pages\s*\"\s*>(.*)<\s*\/\s*ul\s*>/siU", $reviewpage, $out, 2))
        {
            if(preg_match_all("/<\s*li.*>(.*)<\s*\/li\s*>/siU", $out[0][1], $out1, 2))
            {
                foreach($out1 as $v)
                {
                    $page[] = strip_tags($v[1]);
                }
                $reviewarr = array(
                                    $reviewpage
                                   );
                array_shift($page);
                if(is_array($page))
                {
                    foreach($page as $v)
                    {
                        $url = str_ireplace("{page}", $v, $review);
                        $reviewarr[] = Util_Video_ApiConnect::get($url);
                    }
                }
            }
        }
        return array();
    }
}

?>