<?php
error_reporting(0);
header("Content-type:text/html; charset=utf-8");
$query=$_SERVER['QUERY_STRING'];
parse_str($query);
//$type 类型 ：电影m  电视剧 t   综艺 z
if($type&&$link){
        tudou($type,$link);
        exit;
}else{
        $s= "参数不正确\n<hr>";
        $s.="电影正确的形式是：?type=m&link=\n<hr>";
        $s.="电视剧的格式是：?type=t&link=\n<hr>";
        $s.="综艺的格式是：?type=z&link=\n<hr>";
        echo  $s;
}
function tudou($type,$tv){
        $types=array('m'=>'c22','t'=>'c30','z'=>'c31');
        $key=$types[$type];
        $app="http://outapp.api.tudou.com/weibo/baidu/app.html?key=$key";
        $len=strlen($tv);
        switch($len){
                case 6 :
                $url=$app.$tv;
                break;
                case 43 :
                preg_match('/playlist\/p\/(\w+)\.html/',$tv,$ts);
                $url=$app.$ts[1];
                break;
                case 48:
                preg_match('/playlist\/p\/(\w+)\.html/',file_get_contents($tv),$ts);
                $url=$app.$ts[1];
                break;
                case 53:
                preg_match('/playlist\/p\/([al0-9]+)i\d+\.html/',$tv,$ts);
                $url=$app.$ts[1];
                break;
                default :
                preg_match('/playlist\/p\/([al0-9]+)i?\d+?\.html/',$tv,$ts);
                $url=$app.$ts[1];
                break;
        }
        $appString=file_get_contents($url);
        preg_match_all('/rel="([0-9]+)"/imsU',$appString,$tids);
         if($type=='m'){
                  $src="http://v2.tudou.com/v2/cdn?id={$tids[1][0]}";
                    header("location:$src");
                  exit;
         }
        $d=0;
        $list="<list>\n";
        foreach($tids[1] as $tid){
                $d++;
                $list.="<m  type=\"tudou\"  src=\"http://v2.tudou.com/v2/cdn?id={$tid}\" label=\"第{$d}集\" />\n";                
        }
        $list.="</list>";
        header("Content-type:text/xml; charset=utf-8");
        echo $list;
}


?>