<?php
@set_time_limit(0);
@ob_implicit_flush();
require_once(dirname(__FILE__)."/../../inc/common.php");
$ISDEBUG = false;
$gatherPicSet=0;//是否采集图片到本地,0为不采集,1为采集
$pg=1;$resSite=getForm("ressite","both");$sitePic = sitePic."/".date('Y')."-".date('m');
$res = array(
	'1' => "http://www.qvodzy.com.cn/api/max.asp",
	'2' => "http://www.hacow.me/xml/maxresxml.asp", 
	'3' => "http://www.kuaizy.com/xml/max.asp",
	'4' => "http://www.439p.com/api/maxresxml.asp",
	'5' => "http://www.gvodfk.com/max/maxresxml.asp", 
	'6' => "http://www.369ys.org/ls/maxresxml.asp", 
	'7' => "http://www.baduzy.com/xml/caiji.asp",
);
if ($resSite>-1 && $resSite<=count($res)){
	$var_url=$res[$resSite];gatherDay();
}else{
	die("参数不正确");
}

function getReferedId($str) {
	if (isExistStr($str,"土豆高清")) return "hd_tudou";
	if (isExistStr($str,"新浪高清")) return "hd_iask";
	if (isExistStr($str,"搜狐高清")) return "hd_sohu";
	if (isExistStr($str,"天线高清")) return "hd_openv";
	if (isExistStr($str,"56高清")) return "hd_56";
	if (isExistStr($str,"56")) return "56";
	if (isExistStr($str,"优酷")) return "youku";
	if (isExistStr($str,"土豆")) return "tudou";
	if (isExistStr($str,"搜狐")) return "sohu";
	if (isExistStr($str,"新浪")) return "iask";
	if (isExistStr($str,"六间房")) return "6rooms";
	if (isExistStr($str,"qq")) return "qq";
	if (isExistStr($str,"youtube")) return "youtube";
	if (isExistStr($str,"17173")) return "17173";
	if (isExistStr($str,"ku6视频")) return "ku6";
	if (isExistStr($str,"FLV")) return "flv";
	if (isExistStr($str,"SWF")) return "swf";
	if (isExistStr($str,"real")) return "real";
	if (isExistStr($str,"media")) return "media";
	if (isExistStr($str,"qvod")) return "qvod";
	if (isExistStr($str,"ppstream")) return "pps";
	if (isExistStr($str,"迅播高清")) return "gvod";
	if (isExistStr($str,"远古高清")) return "wp2008";
	if (isExistStr($str,"ppvod高清")) return "ppvod";
	if (isExistStr($str,"闪播Pvod")) return "pvod";
	if (isExistStr($str,"播客CC")) return "cc";
	if (isExistStr($str,"皮皮影音")) return "pipi";
	if (isExistStr($str,"激动")) return "joy";
	if (isExistStr($str,"奇艺")) return "qiyi";
}

function intoDatabase($url,$gtype){
	global $ac,$res,$resSite,$t,$h,$pg,$wd,$backurl;
	$pagecount=0;$page=$pg;$rid=$resSite;
	echo "<div style='font-size:13px'><font color=red>视频采集开始：</font>".$url."<br>";
	$xml=utf82gb(file_get_contents($url));
	if ($gtype == "type" || $gtype == "day"){
		$pagecount = StrSlice($xml,'pagecount="','"');
	}
	if (!is_numeric($pagecount)) $pagecount=1; else $pagecount=intval($pagecount);
	$nodes=getrulevaluearr($xml,'video');
	foreach ($nodes as $v) {
	    $m_where="";$vid="";
		$vidas=getrulevaluearr($v,'dl');
		foreach ($vidas as $y) {
		    $flag = StrSlice($y,'<dd flag="','">');
		    $vods = StrSlice($y,'<dd flag="'.$flag.'"><![CDATA[',']]></dd>');
		    if ($vid=="") {
				$vid = $flag."$$".$vods;
			} else {
			    $vid .= "$$$".$flag."$$".$vods;
		    }
		}
		$vtype = StrSlice($v,'<tid>','</tid>');
		$libid = $rid."_".$vtype;
		$title = StrSlice($v,'<name><![CDATA[',']]></name>');
		if (computeStrLen($title)>115) $title=getStrByLen($title,115);
		$pic = StrSlice($v,'<pic>','</pic>');
		$des = StrSlice($v,'<des><![CDATA[',']]></des>');
		$m_lang = StrSlice($v,'<lang>','</lang>');
		if (computeStrLen($m_lang)>30) $m_lang=getStrByLen($m_lang,30);
		$publisharea = StrSlice($v,'<area>','</area>');
		if (computeStrLen($publisharea)>30) $publisharea=getStrByLen($publisharea,30);
		$publishyear = StrSlice($v,'<year>','</year>');
		$states = StrSlice($v,'<state>','</state>');
		$m_note = StrSlice($v,'<note><![CDATA[',']]></note>');
		$actor = StrSlice($v,'<actor><![CDATA[',']]></actor>');
		if (computeStrLen($actor)>250) $actor=getStrByLen($actor,250);
		$director = StrSlice($v,'<director><![CDATA[',']]></director>');
		if (computeStrLen($director)>40) $director=getStrByLen($director,40);
		$publishyear = empty($publishyear) ? date('Y') : intval($publishyear);
		$states = empty($states) ? 0 : intval($states);
		$localId = getBindedLocalId($libid);$m_enname=MoviePinYin($title);
		if (!empty($title)) {
			$title = str_replace("]","】",str_replace("[","【",$title));
			if (is_numeric($localId)) {
				$titleArray = explode("/", $title);
				foreach ($titleArray as $v_title) {
					if (!empty($v_title)) $m_where .= " or concat('/',m_name,'/') like '%/".$v_title."/%' ";
				}
				$m_where = ltrim($m_where," or");
				$rs = DB::fetch_first("SELECT m_id,m_playdata FROM ".DB::table('data')." WHERE ".$m_where." order by m_id desc");
				if (!is_array($rs)) {
					$feedarr = array(
					    'm_name' => $title,
						'm_type' => $localId,
						'm_state' => $state,
						'm_pic' => $pic,
						'm_actor' => $actor,
						'm_des' => $des,
						'm_playdata' => $vid,
						'm_isunion' => 1,
						'm_publishyear' => $publishyear,
						'm_publisharea' => $publisharea,
						'm_note' => $m_note,
						'm_letter' => msubstr($m_enname,1),
						'm_enname' => $m_enname,
						'm_director' => $director,
						'm_lang' => $m_lang,
						'm_addtime' => time(),
					);
					DB::insert('data', $feedarr);
				}else{
					switch (gatherSet) {
						case 0 :case 1 :case 2 :case 3 :
							$m_playdata = gatherIntoLibTransfer($rs['m_playdata'],$vid,gatherSet);
							$feedarr = array(
								'm_state' => $state,
								'm_lang' => $m_lang,
								'm_playdata' => $m_playdata,
								'm_isunion' => 1,
								'm_addtime' => time(),
							);
							ifthen(isSpecial($vtype),$feedarr['m_note'] = $m_note,"");
							DB::update('data', $feedarr, array('m_id' => $rs['m_id']));
						break;
						case 4:
						    $feedarr = array(
							    'm_name' => $title,
								'm_type' => $localId,
								'm_state' => $state,
								'm_pic' => $pic,
								'm_actor' => $actor,
								'm_des' => $des,
								'm_playdata' => $vid,
								'm_isunion' => 1,
								'm_publishyear' => $publishyear,
								'm_publisharea' => $publisharea,
								'm_note' => $m_note,
								'm_letter' => msubstr($m_enname,1),
								'm_enname' => $m_enname,
								'm_director' => $director,
								'm_lang' => $m_lang,
								'm_addtime' => time(),
							);
							DB::insert('data', $feedarr);
						break;
						case 5:
						    $feedarr = array(
								'm_actor' => $actor,
								'm_director' => $director,
								'm_lang' => $m_lang,
								'm_pic' => $pic,
								'm_des' => $des,
								'm_publishyear' => $publishyear,
								'm_publisharea' => $publisharea,
								'm_addtime' => time(),
							);
							ifthen(isSpecial($vtype),$feedarr['m_note'] = $m_note,"");
							DB::update('data', $feedarr, array('m_id' => $rs['m_id']));
						break;
					}
				}
				//echo "OK<br>";
			} else {
				$libid=000000;
				$titleArray = explode("/", $title);
				foreach ($titleArray as $v_title) {
					if (!empty($v_title)) $m_where .= " or concat('/',m_name,'/') like '%/".$v_title."/%' ";
				}
				$m_where = ltrim($m_where," or");
				$rs = DB::fetch_first("SELECT m_id,m_playdata FROM ".DB::table('temp')." WHERE ".$m_where." order by m_id desc");
				if (!is_array($rs)) {
					$feedarr = array(
					    'm_name' => $title,
						'm_type' => $localId,
						'm_state' => $state,
						'm_pic' => $pic,
						'm_actor' => $actor,
						'm_des' => $m_des,
						'm_playdata' => $vid,
						'm_publishyear' => $publishyear,
						'm_publisharea' => $publisharea,
						'm_director' => $director,
						'm_lang' => $m_lang,
						'm_addtime' => time(),
					);
					DB::insert('temp', $feedarr);
				} else {
					switch (gatherSet) {
						case 0 :case 1 :case 2 :case 3 :
						    $m_playdata = gatherIntoLibTransfer($rs['m_playdata'],$vid,gatherSet);
							$feedarr = array(
							    'm_name' => $title,
								'm_state' => $state,
								'm_lang' => $m_lang,
								'm_playdata' => $m_playdata,
								'm_addtime' => time(),
							);
							DB::update('temp', $darr, array('m_id' => $rs['m_id']));
						break;
						case 4:
							$feedarr = array(
							    'm_name' => $title,
								'm_type' => $localId,
								'm_state' => $state,
								'm_pic' => $pic,
								'm_actor' => $actor,
								'm_des' => $des,
								'm_playdata' => $vid,
								'm_publishyear' => $publishyear,
								'm_publisharea' => $publisharea,
								'm_director' => $director,
								'm_lang' => $m_lang,
								'm_addtime' => time(),
							);
							DB::insert('temp', $feedarr);
						break;
						case 5:
							$feedarr = array(
								'm_actor' => $actor,
								'm_director' => $director,
								'm_lang' => $m_lang,
								'm_pic' => $pic,
								'm_des' => $des,
								'm_publishyear' => $publishyear,
								'm_publisharea' => $publisharea,
								'm_addtime' => time(),
							);
							DB::update('temp', $feedarr, array('m_id' => $rs['m_id']));
						break;
					}
				}
			}
		}
		echo "数据<font color=red>".$title."</font>已经采集成功<br>";
	}
	if ($pg>=$pagecount){
		echo "<font color=red>恭喜，全部搞定</font><br>";
	}else{
		$pg=$pg+1;gatherDay();
	}
}

function getBindedLibIds() {
	$libIds = "";
	$query = DB::query("select m_unionid from ".DB::table('type')." where m_type=0 AND m_unionid<>''");
	while($row = DB::fetch($query)) {
		$libIds .= $row['m_unionid'].',';
	}
	$libIds = rtrim($libIds, ',');
	return $libIds;
}

function getBindedLocalId($libId) {
	$row = DB::fetch_first("SELECT count(*) as dd FROM ".DB::table('type')." WHERE m_unionid<>''");
	if (!is_array($row)) return '';
	$query = DB::query("select m_id,m_unionid from ".DB::table('type')." where m_type=0 AND m_unionid<>''");
	while($row = DB::fetch($query)) {
		$unionArray = explode(",", $row['m_unionid']); $arrayLen2 = count($unionArray);
		for ($i = 0;$i < $arrayLen2;$i++) {
			if (trim($unionArray[$i]) == trim($libId)) return $row['m_id'];
		}
	}
	return '';
}

function gatherDay(){
	global $var_url,$pg,$resSite;
	$weburl=$var_url."?ac=videolist&h=24&pg=".$pg;
	intoDatabase($weburl,"day");
}


function gatherIntoLibTransfer($data, $str, $typeset) {
	$str1 = $data;$str2 = $str;
	$transtr = '';
	if ($str1 != '' && $str2 != ''){
		switch ($typeset) {
			case 0: //智能覆盖
				$str1Array = explode("$$$",$str1);$str2Array = explode("$$$",$str2);$m = count($str1Array);$n = count($str2Array);
				for ($k = 0;$k < $n;$k++) {
					$str2fromarray = explode("$$", $str2Array[$k]);
					$x = findIsExistFrom($str1Array, $str2fromarray[0]);
					if (is_numeric($x)) $str1Array[$x] = $str2Array[$k]; else $transtr .= $str2Array[$k]."$$$";
				}
				for ($j = 0;$j < $m;$j++) {
					$transtr .= $str1Array[$j]."$$$";
				}
				$transtr = rtrim($transtr,"$$$");
			break;
			case 1: //追加为第一个来源
				$transtr = $str."$$$".$data;
			break;
			case 2: //追加为最后一个来源
				$transtr = $data."$$$".$str;
			break;
			case 3: //完全覆盖
				$transtr = $str;
			break;
		}
	} else if ($str1!='' && empty($str2)){
		$transtr=$str1;
	} else if (empty($str1) && $str2!=''){
		$transtr = $str2;
	}
	return $transtr;
}

function findIsExistFrom($array1, $from) {
	$str1 = $array1;$str2 = $from;
	$m = count($str1);
	for ($i = 0;$i < $m;$i++) {
		$array1fromarray = explode("$$",$str1[$i]);
		if (trim($array1fromarray[0]) == trim($str2)) return $i;
	}
	return "";
}

function isSpecial($sType){
	$isSpecial=false;
	if (strpos($sType,"剧")>2){
		$isSpecial=true;
	}elseif (strpos($sType,"动漫")>0){
		$isSpecial=true;
	}elseif (strpos($sType,"动画")>0){
		$isSpecial=true;
	}elseif (strpos($sType,"综艺")>0){
		$isSpecial=true;
	}
	return $isSpecial;
}
?>