正文

芒果解析源码

芒果解析源码

  1. <?php   
  2. $ids = Mgtv::parse($url);  
  3. $data = Mgtv::get_PC_B_video($ids);  
  4. echo json_encode($data);  
  5.   
  6. /** 
  7.  
  8. */  
  9. class Mgtv  
  10. {  
  11.     //============================================================================================================================  
  12.     /** 
  13.      * [parse 解析获取视频 ID 和视频专辑 ID] 
  14.      * @param  [type] $url [播放网址] 
  15.      * @return [type]      [description] 
  16.      */  
  17.     public static function parse($url)  
  18.     {  
  19.         $name = “MGTV-“.md5($url);  
  20.   
  21.         if (file_exists(NAME_PATH.$name) && time() – filemtime(NAME_PATH.$name) < 604800) { //文件存在并且文件创建时间小于7天  
  22.             $data = json_decode(file_get_contents(NAME_PATH.$name),true);  
  23.             $cid = $data['cid’];  
  24.             $vid = $data['vid’];  
  25.             $isIntact = $data['isIntact’];  
  26.         }else{  
  27.             $html = self::curl($url);  
  28.             preg_match('#cid: ([\d]+),#iU’,$html,$cids);  
  29.             preg_match('#vid: ([\d]+),#iU’,$html,$vids);  
  30.             preg_match('#isIntact: ([\d]+),#iU’,$html,$isIntacts);  
  31.             file_put_contents(NAME_PATH.$name, json_encode(array(“cid"=>$cids[1],“vid"=>$vids[1],“isIntact"=>$isIntacts[1])));  
  32.             $cid = $cids[1];  
  33.             $vid = $vids[1];  
  34.             $isIntact = $isIntacts[1];  
  35.   
  36.         }  
  37.   
  38.         return array(“cid"=>$cid,“vid"=>$vid,“url"=>$url,“isIntact"=>$isIntact);  
  39.     }  
  40.   
  41.     /** 
  42.      * [get_video_files 获取视频信息] 
  43.      * @param  [type]  $vid [视频ID] 
  44.      * @param  integer $def [视频清晰度] 
  45.      * @return [type]       [description] 
  46.      */  
  47.     public static function get_PC_B_video($ids//芒果浏览器pc端  
  48.     {  
  49.         $ref = $ids['url’];  
  50.         $ids['re’] = $ids['url’];  
  51.         $ids['ua’] = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36’;  
  52.         $vid = $ids['vid’];  
  53.         $pno = “1000";  
  54.         $wpno = “2010";  
  55.         $ver =  “0.3.0001";  
  56.         $suuid = self::createuuid(8) . “-“ . self::createuuid(4) . “-“ . self::createuuid(4) . “-“ . self::createuuid(12);  
  57.         $collection_id = “315515";  
  58.         $_support = “10000000";  
  59.         $files = [];  
  60.   
  61.         $did = self::createuuid(8) . “-“ . self::createuuid(4) . “-“ . self::createuuid(4) . “-“ . self::createuuid(’12’);  
  62.         $clit = time();  
  63.         $api = “https://pcweb.api.mgtv.com/player/video?video_id={$vid}&suuid={$suuid}&cid={$ids['cid’]}&tk2=".self::getTK2(array(“did"  => “{$did}",“ver"  => “{$ver}",“pno"  => “{$pno}",“clit" => time())).“&_support={$_support}";  
  64.         if (isset($_COOKIE['PM_CHKID’])) {  
  65.             $ids['cookie’] = COOKIE_MGTV.“PM_CHKID=".$_COOKIE['PM_CHKID’].“;";  
  66.         }   
  67.         $content = self::curl($api,$ids); /* COOKIE_MGTV */  
  68.         //print_r($content);exit;  
  69.         $json = json_decode($content,true);  
  70.         if ($json['code’] == 200 && isset($json['data’]['info’]) && $json['data’]['info’]['paymark’] == '0’) {  
  71.             if (!isset($_COOKIE['PM_CHKID’]) && is_file(COOKIE_PATH.’mgtv.txt’)) {  
  72.                 $cookie = file_get_contents(COOKIE_PATH.’mgtv.txt’);  
  73.                 $cc = explode(“PM_CHKID",$cookie);  
  74.                 $c1 = “PM_CHKID=".trim($cc['1’]).“;";  
  75.                 setcookie(“PM_CHKID",trim($cc['1’]));  
  76.                 $ids['cookie’] = COOKIE_MGTV.$c1;  
  77.             }  
  78.             $pm2 = $json['data’]['atc’]['pm2’];  
  79.             $g = “https://pstream.api.mgtv.com/player/getSource?tk2=" .self::getTK2(array(“did"  => “{$did}",“ver"  => “{$ver}",“pno"  => “{$pno}",“clit" => time())) . “&pm2=" . $json['data’]['atc’]['pm2’] . “&video_id=" . $vid . “&_support={$_support}&did={$did}&suuid={suuid}&collection_id={$collection_id}"/* &type=pch5 */;  
  80.   
  81.             $g1 = self::curl($g,$ids);  
  82.   
  83.             $k1 = json_decode($g1,true);  
  84.   
  85.             if (isset($k1['data’])) {  
  86.                 $data = $k1['data’];  
  87.                 $info = $data['info’];//视频信息  
  88.                 $points = $json['data’]['points’];  
  89.                 $preview = $json['data’]['frame’];  
  90.   
  91.                 $domain = $data['stream_domain’];//视频域名数组  
  92.                 $stream = $data['stream’];  
  93.                 $count = 0;  
  94.                 if ($points['content’] != null || $points['content’] != ") {  
  95.                     $_a = explode('|’,$points['start’]);  
  96.                     $_b = explode('|’,$points['end']);  
  97.                     foreach ($points['content’] as $k => $v) {  
  98.                         $_a = explode('|’,$v);  
  99.                         $_tmp['words’] .= self::filterGBK_SpecialChars($_a[1]).’,’;  
  100.                         $_tmp['time’] .= (int)$_a[0].’,’;  
  101.                     }  
  102.                     $_tmp['words’] = substr($_tmp['words’],0,strlen($_tmp['words’])-1);  
  103.                     $_tmp['time’] = substr($_tmp['time’],0,strlen($_tmp['time’])-1);  
  104.                     //$videoinfo[“prompt"] = $_tmp;  
  105.                 }  
  106.   
  107.                 $videoinfo[“data"][“poster"] = $json['data’]['info’]['thumb’];  
  108.   
  109.                 foreach ($stream as $key => $value) {  
  110.                     $def = $value['def’];  
  111.                     if ($def==1) {  
  112.                         continue;  
  113.                     }  
  114.                     if ($domain[$key] == null) {  
  115.                         $domain[$key] = $domain[0];  
  116.                     }  
  117.                     //print_r($domain[$key].$value['url’]);exit;  
  118.                     if(!emptyempty($value['url’])){  
  119.                         $vurl = self::get_video_url($domain[$key].$value['url’],$ids);  
  120.                         if ($vurl == 'failed’) {  
  121.                             $vurl = self::get_video_url($domain[$key].$value['url’],$ids);  
  122.                             if ($vurl == 'failed’) {  
  123.                                 $vurl = self::get_video_url($domain[$key].$value['url’],$ids);  
  124.                                 if ($vurl == 'failed’) {  
  125.                                     $vurl = self::get_video_url($domain[$key].$value['url’],$ids);  
  126.                                     if ($vurl == 'failed’) {  
  127.                                         break;    
  128.                                     }  
  129.                                 }  
  130.                             }  
  131.   
  132.                         }  
  133.                         if (GlobalBase::is_ipad()) {  
  134.                             if($def==3 || $def == 2){  
  135.                                 $true_url = $vurl;  
  136.                                 $videoinfo['code’] = 200;  
  137.                                 $videoinfo['play’] = 'hls’;  
  138.                                 $videoinfo['type’] = 'mgtv’;  
  139.                                 $videoinfo[“data"][“url"] = str_replace(“http://",“https://",$true_url);  
  140.                                 exit(json_encode($videoinfo));  
  141.                             } else {  
  142.                                 $videoinfo['play’] = 'url’;  
  143.                                 $videoinfo[“url"] = '../yun/?url=’.urlencode($ref);  
  144.                                 exit(json_encode($videoinfo));  
  145.                             }  
  146.                         }else{  
  147.                             $video[0] = $vurl;  
  148.                             $video[1] = “video/m3u8";  
  149.                             $video[2] = $value['name’];  
  150.                             $video[3] = $def == 3 ? 10: 0;  
  151.                             $videoinfo1[“video"][$count] = str_replace('http://’,’//’,$video);;  
  152.                             $count++;  
  153.                         }  
  154.                     }  
  155.                 }  
  156.                 //print_r(json_encode($videoinfo1));exit;  
  157.                 if (!emptyempty($videoinfo1[“video"]) && $videoinfo1[“video"][count($videoinfo1[“video"])-1][0] != 'busy’) {  
  158.                     $videoinfo['code’] = 200;  
  159.                     $videoinfo['msg’] = '解析成功’;  
  160.                     $mp4url = !emptyempty($videoinfo1[“video"][count($videoinfo1[“video"])-1][0]) ? $videoinfo1[“video"][count($videoinfo1[“video"])-1][0] : $videoinfo1[“video"][0][0];  
  161.                     $videoinfo['data’]['url’] = $mp4url;  
  162.                     if (emptyempty($mp4url)) {  
  163.                         $videoinfo['code’] = 404;  
  164.                         $videoinfo['msg’] = '获取视频地址失败’;  
  165.                     }  
  166.                     $videoinfo['play’] = 'hls’;  
  167.   
  168.                     $imp4url = $videoinfo1[“video"][0][0];  
  169.                 } else {  
  170.                     $videoinfo['code’] = 302;  
  171.                     $videoinfo['msg’] = $json['data’]['info’]['title’].’视频正在加载···请稍后···’;  
  172.                     $videoinfo[“play"] = 'url’;  
  173.                     $videoinfo[“url"] = '../yun/?url=’.urlencode($ref);  
  174.                 }  
  175.   
  176.                 $videoinfo[“data"][“name"] = $json['data’]['info’]['title’];  
  177.                 $videoinfo[“data"][“series"] = $json['data’]['info’]['series’];  
  178.                 $videoinfo[“data"][“desc"] = $json['data’]['info’]['desc’];  
  179.   
  180.                 return $videoinfo;  
  181.             }  
  182.         } else {  
  183.             if (isset($json['data’]['info’]) && $json['data’]['info’]['paymark’] == '1’) {  
  184.                 $_loc1['code’] = 302;  
  185.                 $_loc1['msg’] = '芒果VIP视频’;  
  186.                 $_loc1['url’] = isset($json['url’]) ? $json['url’] : '../yun/?url=’.urlencode($ref);  
  187.             } else if (isset($json['code’]) && $json['code’] == 40001) {  
  188.                 $_loc1['code’] = 302;  
  189.                 $_loc1['msg’] = isset($json['msg’]) ? $json['msg’] : '解析失败!’;  
  190.                 $_loc1['url’] = isset($json['url’]) ? $json['url’] : '../yun/?url=’.urlencode($ref);  
  191.             } else {  
  192.                 $_loc1 = array(  
  193.                     “code" => 302,  
  194.                     “msg" => '解析失败!’,  
  195.                     “url" => '../yun/?url=’.urlencode($ref)  
  196.                 );  
  197.             }  
  198.             return $_loc1;  
  199.         }  
  200.     }  
  201.   
  202.     /* 
  203.       *  
  204.      */  
  205.     public static function get_Phone_C_video($ids//芒果浏览器pc端  
  206.     {  
  207.         $ref = $ids['url’];  
  208.         $ids['re’] = $ids['url’];  
  209.         $ids['ua’] = 'okhttp/imgotv’;  
  210.         $ids['cookie’] = COOKIE_MGTV;  
  211.         $vid = $ids['vid’];  
  212.         $ver =  “0.2.24011";  
  213.         $suuid = self::createuuid(8) . “-“ . self::createuuid(4) . “-“ . self::createuuid(4) . “-“ . self::createuuid(12);  
  214.         $_support = “10100001";  
  215.   
  216.         $api = “https://mobile.api.mgtv.com/v8/video/getSource?_support=10100001&device=oppo%20R11&osVersion=4.4.2&appVersion=5.8.6_1&ticket=&userId=0&mac=i352419010176358&osType=android&channel=360dev&uuid=&endType=mgtvapp&androidid=b0359fa2c8301858&imei=352419010176358&macaddress=B2%3A35%3A9F%3AA2%3AC8%3A30&seqId=3cdb503a7f75ab0d54139bf4a92bf380&version=5.2&type=10&abroad=0&src=mgtv&uid=&phonetype=oppo%20R11&videoId={$vid}&isowner=0&clipId=323323&playType=1&dataType=1&keepPlay=0&source=40&localPlayVideoId=4458399&localVideoWatchTime=121&did=i352419010176358&suuid={$suuid}&hdts=h264%2Ch265";  
  217.   
  218.         $content = self::curl($api,$ids); /* COOKIE_MGTV */  
  219.   
  220.         $json = json_decode($content,true);  
  221.         $_tmp = array();  
  222.   
  223.         if ($json['code’] == 200) {  
  224.             $domains = $json['data’]['videoDomains’];  
  225.             $data = $json['data’]['videoSources’];  
  226.             foreach ($data as $key => $value) {  
  227.                 $def = $value['definition’];  
  228.                 if ($value['url’] == ") {  
  229.                     continue;  
  230.                 }  
  231.   
  232.                 $url = $domains[1].$value['url’].“&ver={$ver}&chk=074a2db93003e523b945509df080ac00&_support={$_support}&did=i352419010176358&suuid={$suuid}";  
  233.   
  234.                 //print_r($url);exit;  
  235.                 $url = self::get_video_url($url,$ids);  
  236.   
  237.                 if (GlobalBase::is_ipad()) {  
  238.                     if($def==3 || $def == 2 && $url != "){  
  239.                         $true_url = self::get_video_mp4_url($url);  
  240.                         $videoinfo[“video"][“file"] = $true_url;  
  241.                         $videoinfo[“video"][“type"] = “video/mp4";  
  242.                         exit(json_encode($videoinfo));  
  243.                     } else {  
  244.                         $videoinfo['play’] = 'url’;  
  245.                         $videoinfo[“url"] = '../yun/?url=’.urlencode($ids['url’]);  
  246.                         exit(json_encode($videoinfo));  
  247.                     }  
  248.                 } else {  
  249.                     if ($def == 4 && $url!=") {  
  250.                         $_tmp[4]['def’] = '蓝光’;  
  251.                         $_tmp[4]['url’] = $url;  
  252.                     }  
  253.                     if ($def == 3 && $url!=") {  
  254.   
  255.                         $_tmp[3]['def’] = '超清’;  
  256.                         $_tmp[3]['url’] = $url;  
  257.                     }  
  258.                     if ($def == 2 && $url!=") {  
  259.                         $_tmp[2]['def’] = '高清’;  
  260.                         $_tmp[2]['url’] = $url;  
  261.                     }  
  262.                     if ($def == 1 && $url!=") {  
  263.                         $_tmp[1]['def’] = '标清’;  
  264.                         $_tmp[1]['url’] = self::get_video_mp4_url($url);  
  265.   
  266.                     }  
  267.   
  268.                 }  
  269.   
  270.             }  
  271.             //print(json_encode($_tmp[3]));exit;  
  272.             for ($i=1; $i <= 5 ; $i++) {   
  273.                 if ($_tmp[$i] == ") {  
  274.                     continue;  
  275.                 }  
  276.                 $key_arrays[]=$_tmp[$i];  
  277.             }  
  278.             $videoinfo[“video"][“file"] = $key_arrays[count($key_arrays)-1]['url’];  
  279.             $videoinfo[“video"][“type"] = “video/mp4";  
  280.             $videoinfo['msg’] = '网站资源’;  
  281.   
  282.         }  
  283.         return $videoinfo;  
  284.   
  285.     }  
  286.   
  287.     /* 
  288.     public getMgtv($ids) 
  289.     { 
  290.         $api = “https://mobile.api.hunantv.com/v6/video/getSource?_support=10100001&device=N5207&src=mgtv&appVersion=5.7.0_1&osVersion=4.2.2&osType=android&version=5.2&type=10&ticket=&userId=0&channel=mgtv3&videoid={$ids['vid’]}"; 
  291.         $data = self::curl($api,$ids); 
  292.         print_r($data); 
  293.     } */  
  294.     /** 
  295.      * [get_video_url 获取视频最终地址] 
  296.      * @param  [type] $url [视频请求链接] 
  297.      * @return [type]      [description] 
  298.      */  
  299.     public static function get_video_url($url,$ids){  
  300.   
  301.         $content = self::curl($url,$ids);  
  302.         //print_r($content);exit;  
  303.         $data = json_decode($content,true);  
  304.         $vurl = $data[“info"];  
  305.         return $vurl;  
  306.     }  
  307.   
  308.     /** 
  309.      * [get_video_mp4_url 获取视频MP4视频] 
  310.      * @param [type] $url [视频请求连接] 
  311.      * @return [type] url [mp4最终地址]   
  312.      */  
  313.     public static function get_video_mp4_url($url){  
  314.         $u1 = explode('?’,$url);  
  315.         $u1 = explode('//’,$u1['0’]);  
  316.         $u1 = explode('/’,$u1['1’]);  
  317.         $f1 = explode('_’,$u1['5’]);  
  318.         $fid = $f1['0’];  
  319.         $file2 = “/".$u1[1].“/".$u1[2].“/".$u1[3].“/".$u1[4].“/".$u1[5];  
  320.         if(strstr($file2,’_mp4′)){  
  321.             $filestr_replace('_mp4′,’.mp4′,$file2);  
  322.         }else{  
  323.             $file=$file;  
  324.         }  
  325.         $true_url = “https://disp.titan.mgtv.com/vod.do?fmt=4&pno=1042000&fid=$fid&file=$file";  
  326.         return $true_url;  
  327.     }  
  328.   
  329.     public static function curl($url,$ids)  
  330.     {  
  331.         $params[“ua"] = !emptyempty($ids['ua’]) ? $ids['ua’] : “Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36";  
  332.         $params[“cookie"] = $ids['cookie’];  
  333.         //$params[“proxy"] = PROXY;  
  334.         $params[“ip"] = “14.21.96.129";//伪装为固定IP,否则会报异地登录,会封号,不要更改  
  335.         return GlobalBase::mgtv_curl($url,$params);  
  336.     }  
  337.   
  338.     /* 
  339.      * 生成0到1随机数 
  340.      */  
  341.     public static function random($min = 0, $max = 1){       
  342.         return $min + mt_rand()/mt_getrandmax()*($max$min);   
  343.     }  
  344.   
  345.     /* 
  346.     * did 生成 
  347.     *  
  348.      */  
  349.     public static function createuuid($b$a=null) {  
  350.         switch ($a) {  
  351.             case 'h’:  
  352.                 $a = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";  
  353.                 break;  
  354.             case 's’:  
  355.                 $a = “0123456789";  
  356.                 break;  
  357.             case 'd’:  
  358.                 $a = “ABCDEFGHIJKLMNOPQRSTUVWXYZ";  
  359.                 break;  
  360.             case 'x’:  
  361.                 $a = “abcdefghijklmnopqrstuvwxyz";  
  362.                 break;  
  363.             default:  
  364.                 $a = “0123456789abcdef";  
  365.                 break;  
  366.         }  
  367.         $z = str_split($a);  
  368.         for ($c=",$g=0; $g < $b$g++) {   
  369.             $c .= $z[ceil(100000000 * (self::random())) % count($z)];  
  370.         }  
  371.         return $c;  
  372.     }  
  373.   
  374.     public static function filterGBK_SpecialChars($str)  
  375.     {  
  376.         $str = str_replace('“’,",$str);  
  377.         $str = str_replace('”’,",$str);  
  378.         return $str;  
  379.     }  
  380.   
  381.     public static function charAt($str$index = 0){  
  382.         return substr($str$index, 1);  
  383.     }  
  384.   
  385.     public static function getTK2($param1){  
  386.         $_loc1_ = 0;  
  387.         $_loc2_ = 0;  
  388.         foreach$param1 as $_loc3_ => $_loc4_ ){  
  389.             $_loc5_[] = $_loc3_ . “=" . $_loc4_;  
  390.         }  
  391.         $_loc6_ = join(“|",$_loc5_);  
  392.         $_loc7_ = base64_encode($_loc6_);  
  393.         $_loc8_ = str_replace(array(“+",“/",“="),array(“_",“~",“-“),$_loc7_);  
  394.         $_loc11 = “";  
  395.         foreachstr_split($_loc8_as $_loc9_ => $_loc10_ ){  
  396.             $_loc11 .= self::charAt($_loc8_,strlen($_loc8_)-$_loc9_-1);  
  397.         }  
  398.         return $_loc11;  
  399.     }  
  400. }