<?php
function unicode_decode($name) {
        $pattern = '/([\w]+)|(\\\u([\w]{4}))/i';
        preg_match_all($pattern, $name, $matches);
        if (!empty ($matches)) {
                $name = '';
                for ($j = 0; $j < count($matches[0]); $j++) {
                        $str = $matches[0][$j];
                        if (strpos($str, '\\u') === 0) {
                                $code = base_convert(substr($str, 2, 2), 16, 10);
                                $code2 = base_convert(substr($str, 4), 16, 10);
                                $c = chr($code) . chr($code2);
                                $c = iconv('UCS-2', 'UTF-8', $c);
                                $name .= $c;
                        } else {
                                $name .= $str;
                        }
                }
        }
        return $name;
}
function g_s($url) {
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        $referer = 'http://cntv.cn/';
        $ch = curl_init();
        $timeout = 30;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_REFERER, $referer);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
        @ $c = curl_exec($ch);
        curl_close($ch);
        return $c;

        /*
        $c=file_get_contents($url);
        return $c;
        */
}
function c_l($p, $s) {
        preg_match_all($p, $s, $a);
        return $a[1];

}
function c_m($p, $s) {
        preg_match($p, $s, $a);
        return $a[1];
}
$fname = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER["SCRIPT_NAME"];
$xml = '<list>';
$xml .= "\n";
if (isset ($_GET['id'])) {
        /*单页内容采集程序开始
         * 动漫单页展示： http://donghua.cntv.cn/sanmaoqiyuji/classpage/video/20101203/100424.shtml
         * 动漫单页调用： ?id=http://donghua.cntv.cn/sanmaoqiyuji/classpage/video/20101203/100424.shtml
         * 电影单页展示：http://dianying.cntv.cn/C26898/classpage/video/20110506/100811.shtml
         * 电影单页调用： ?id=http://dianying.cntv.cn/C26898/classpage/video/20110506/100811.shtml
         * 电视剧单页调用 ：
         */
        $str = g_s($_GET['id']);
        if (preg_match('|videoCenterId","([0-9a-zA-Z]+)"|', $str, $ar1)) {
                $u = 'http://vdd.player.cntv.cn/index.php?pid=' . $ar1[1] . '&tz=-';
                $s = g_s($u);
                $s = str_replace('\/', '/', $s);
                //preg_match('|"title":"([^"]+)"|U', $s,$c);
                $c = c_m('|"title":"([^"]+)"|U', $s);
                @ $name = unicode_decode($c);
                @ $name = iconv('UTF-8', 'GB2312', $name);
                if (preg_match('|chapters2":(.*)\]|U', $s, $a)) {
                        $b = $a[1];
                        $b = c_l('|"url":"([^"]+)"|', $b);
                        foreach ($b as $k => $v) {
                                $z = $k +1;
                                $xml .= '<m  type="2" src="' . $v . '" label="' . $name . '第-' . $z . '段" /> ';
                                $xml .= "\n";
                        }
                }
                elseif (preg_match('|chapters":(.*)\]|U', $s, $a)) {
                        $b = $a[1];
                        $b = c_l('|"url":"([^"]+)"|', $b);
                        foreach ($b as $k => $v) {
                                $z = $k +1;
                                $xml .= '<m  type="2" src="' . $v . '" label="' . $name . '第-' . $z . '段" /> ';
                                $xml .= "\n";
                        }
                }
                elseif (preg_match('|streams([^]]+)\]|', $s, $a)) {
                        $r1 = c_m('|rtmp://([^"]+)"|', $a[1]);
                        $r2 = c_l('|streamName":"([^"]+)","|', $a[1]);
                        foreach ($r2 as $k => $v) {
                                $z = $k +1;
                                $xml .= '<m type="2" src="' . $v . '" rtmp="rtmp://' . $r1 . '" label="' . $name . '第' . $z . '段" />';
                                $xml .= "\n";
                        }

                }
        }
}
elseif (isset ($_GET['pid'])) {
        /*
        * 二级目录采集  主要防止一个mid参数不够使用
        */
        $str = g_s($_GET['pid']);
        $ar = c_l("|_array\('(.*)'\);|", $str);
        foreach ($ar as $k => $v) {
                $v = str_replace("'", "", $v);
                $v = explode(',', $v);
                $xml .= '<m list_src="' . $fname . '?mid=' . $v[3] . '" label="' . $v[0] . '" /> ';
                $xml .= "\n";
        }

}
elseif (isset ($_GET['mid'])) {
        /*
        * CNTV电影和电视剧分段介绍页采集程序开始
        * 电影列表典型页面：http://dianying.cntv.cn/other/yewen/videopage/index.shtml
        * 使用方法 ?mid=http://dianying.cntv.cn/C24824/videopage/index.shtml
        * 电视列表典型页面：http://dianshiju.cntv.cn/aizidejiejieminzi/videopage/index.shtml
        * 使用方法：?mid=http://dianshiju.cntv.cn/aizidejiejieminzi/videopage/index.shtml
        * 动漫列表典型页面：http://donghua.cntv.cn/sanmaoqiyuji/videopage/index.shtml
        * 使用方法：?mid=http://donghua.cntv.cn/sanmaoqiyuji/videopage/index.shtml
        * 以上只做展示  所有类似以上的网页均可采集
        */
        $str = g_s($_GET['mid']);
        $ar = c_l("|_array\('(.*)'\);|", $str);
        foreach ($ar as $k => $v) {
                $v = str_replace("'", "", $v);
                $v = explode(',', $v);
                if (preg_match('|^https?:\/\/.+\.[a-z]{2,4}\/.*$|', $v[3])) {
                        //判断$v[3]是网址 则生成列表 否则失败
                        $xml .= '<m list_src="' . $fname . '?id=' . $v[3] . '" label="' . $v[0] . '" /> ';
                        $xml .= "\n";
                }
        }
}
elseif (isset ($_GET['bid'])) {
        /*
         * CNTV视频博客区 采集程序开始
         * 单页地址 ： http://xiyou.cntv.cn/v-29211090-4648-11df-be82-001e0bbb2454.html
         * 使用方法： ?bid=http://xiyou.cntv.cn/v-29211090-4648-11df-be82-001e0bbb2454.html
        */
        $bid = $_GET['bid'];
        $bid = str_replace('http://xiyou.cntv.cn/v-', 'http://xiyou.cntv.cn/interface/index?videoId=', $bid);
        $bid = str_replace('.html', '', $bid);
        $str = g_s($bid);
        $name = c_m('|title":"([^"]+)"|', $str);
        $ar = c_m('|videoFilePath":"(.*)#|', $str);
        $time = c_m('|"timeSpan":"([0-9]+)"|', $str);
        //取得视频时间，300秒每段取得视频分段
        $name = unicode_decode($name);
        @ $name = iconv('UTF-8', 'GB2312', $name);
        $times = ceil($time / 300);
        for ($i = 1; $i <= $times; $i++) {
                if ($i < 10) {
                        $flv = str_replace('\/', '/', $ar) . '_00' . $i . '.mp4';
                } else {
                        $flv = str_replace('\/', '/', $ar) . '_0' . $i . '.mp4';
                }
                /*上面这几行代码是我很纠结的地方
                 * 有的地方是_001 还有的地方是-1
                 * 关键这坑爹的分段参数居然在配置里没有
                 * 只有靠视频时间长度来给他加分段参数
                 * 希望有心人 能完善下这里 一般用不到
                 */
                $xml .= '<m type="2" src="' . $flv . '" label="' . $name . '" /> ';
                $xml .= "\n";
        }

}
elseif (isset ($_GET['move'])) {
        /*
         * 如果光想调用全部电影可以设置参数
         * ?move=1 来调用全部电影
         */
        $str = g_s('http://dianying.cntv.cn/index/index.shtml');
        $ar = c_l('|<li><a href="(.*)</a></li>|', $str);
        foreach ($ar as $k => $v) {
                $arr = explode('" target="_blank">', $v);
                $xml .= '<m list_src="' . $fname . '?mid=' . $arr[0] . '" label="' . $arr[1] . '" /> ';
                $xml .= "\n";
        }
}
elseif (isset ($_GET['tv'])) {
        /*
         * 如果光想调用全部电视剧可以设置参数
         * ?tv=1 来调用全部电视剧
         */
        $str = g_s('http://dianshiju.cntv.cn/zimupaixu/index.shtml');
        $ar = c_m('|<div id="page\_body">(.*)<div class="mw\_custom">|is', $str);
        $ar2 = c_l('|<td><a href="([^<]+)</a></td>|', $ar);
        foreach ($ar2 as $k => $v) {
                $arr = explode('">', $v);
                $xml .= '<m list_src="' . $fname . '?mid=' . $arr[0] . '" label="' . $arr[1] . '" /> ';
                $xml .= "\n";
        }
}
elseif (isset ($_GET['js'])) {
        /*
         * 记实栏目开始 调用方法 ?js=1
         */
        $str = g_s('http://jishi.cntv.cn/letters/leixingjiansuo/index.shtml');
        $ar = c_l('|<td valign="middle"><a href="([^<]+)</a></td>|', $str);
        foreach ($ar as $k => $v) {
                $arr = explode('" target="_blank">', $v);
                $xml .= '<m list_src="' . $fname . '?mid=' . $arr[0] . '" label="' . $arr[1] . '" /> ';
                $xml .= "\n";
        }
}
elseif (isset ($_GET['zy'])) {
        /*
         * 卫视栏目综艺节目开始
         * 调用方法?zy=1
         */
        $str = g_s('http://ent.cntv.cn/weishilanmushpin/index.shtml');
        $ar = c_l("|_array\('(.*)'\);|", $str);
        foreach ($ar as $k => $v) {
                $v = str_replace("'", "", $v);
                $v = explode(',', $v);
                $xml .= '<m list_src="' . $fname . '?id=' . $v[2] . '" label="' . $v[0] . '" /> ';
                $xml .= "\n";
        }
}
elseif (isset ($_GET['kj'])) {
        /*
         *科技频道调用开始
         * 调用方法?kj=1
         */
        $str = g_s('http://kejiao.cntv.cn/map/index.shtml');
        $ar = c_m('|search_content_1">(.*)search_content_2|is', $str);
        $ar1 = c_l('|<li><a href="([^<]+)</a></li>|', $ar);
        //var_dump($ar1);
        foreach ($ar1 as $k => $v) {
                $arr = explode('" >', $v);
                $xml .= '<m list_src="' . $fname . '?mid=' . $arr[0] . '" label="' . $arr[1] . '" /> ';
                $xml .= "\n";
        }
} else {
        $xml .= '<m list_src="' . $fname . '?kj=1"  label="科技频道"/> ';
        $xml .= '<m list_src="' . $fname . '?tv=1"  label="电视频道"/> ';
        $xml .= '<m list_src="' . $fname . '?move=1"  label="电影频道"/> ';
        $xml .= '<m list_src="' . $fname . '?zy=1"  label="综艺频道"/> ';
        $xml .= '<m list_src="' . $fname . '?js=1"  label="纪实频道"/> ';
}
$xml .= '</list>';
echo $xml;
?>