<?php
header("Content-type: text/html; charset=gb2312"); 
include "../../admin_loginstate.php";
include "../../../include/conn.php";
include "./collect.function.php";
include "../../../include/class/http.class.php";
include "../../admin_version.php";

//1、采集内容
//2、入库
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$cacheid = isset($_GET['cacheid']) ? strip_tags($_GET['cacheid']) : NULL;
$pagenow = isset($_GET['pagenow']) ? intval($_GET['pagenow']) : 0;
$pageend = isset($_GET['pageend']) ? intval($_GET['pageend']) : 0;

if (empty($id) || empty($cacheid) || empty($pagenow) || empty($pageend)) {
	exit('传参错误：rulecollect_2.php');
}

$cacheFile = '../../../cache/rule_'.$cacheid.'.txt';
if (!is_file($cacheFile)) {
	exit('对不起，没有找到该采集规则的缓存信息，无法继续采集');
}

$ruleConfig = unserialize(file_get_contents($cacheFile));
//读取配置文件
$playmode = $ruleConfig['playmode'];
if ($playmode==2) {//如果需要采集播放页，每次就只采集一条数据
	$pagesize = 1;
}else{
	$pagesize = 3;
}
//读取采集功能选项
$collectTYPE = str_split(strrev(str_pad(decbin($ruleConfig['type']),3,0,STR_PAD_LEFT)));

$cacheFile = '../../../cache/list_'.$cacheid.'.txt';
if (!is_file($cacheFile)) {
	exit('没有找到上一步操作设置的缓存文件'.$cacheFile);
}

$listhref = explode(',', file_get_contents($cacheFile));
if (!empty($listhref)) {
	for ($i = 0; $i < $pagesize; $i++) {
		$url = ($collectTYPE[0]==0) ? trim(array_shift($listhref)) : trim(array_pop($listhref));
		if($html = http::remotehttp($url)){
			$data['S_Name'] 	= strip_tags(preg_collect($ruleConfig['title'], $html, true));
			$data['S_ClassID'] 	= strip_tags(preg_collect($ruleConfig['class'], $html, true));
			if (empty($data['S_Name']) || empty($data['S_ClassID'])) {
				echo '标题或分类采集失败，继续采集下一条...<br />'; continue;
			}
			if (!is_numeric($data['S_ClassID']) && isset($ruleConfig['collectbind'][$data['S_ClassID']])) {
				$data['S_ClassID'] = $ruleConfig['collectbind'][$data['S_ClassID']];
			} else {
				echo '未找到数据绑定的分类信息，继续采集下一条...<br />'; continue;
			}
			$data['S_Remark'] 	= strip_tags(preg_collect($ruleConfig['state'], $html, true));
			$data['S_Direct'] 	= strip_tags(preg_collect($ruleConfig['direct'], $html, true));
			$data['S_Author'] 	= strip_tags(preg_collect($ruleConfig['author'], $html, true));
			$data['S_Area'] 	= strip_tags(preg_collect($ruleConfig['area'], $html, true));
			$data['S_Language'] = strip_tags(preg_collect($ruleConfig['language'], $html, true));
			$data['S_Year'] 	= strip_tags(preg_collect($ruleConfig['year'], $html, true));
			$data['S_Pic'] 		= strip_tags(preg_collect($ruleConfig['pic'], $html, true));
			$data['S_Content']	= strip_tags(preg_collect($ruleConfig['content'], $html, true),'<p>');
			$playrange = strip_tags(preg_collect($ruleConfig['playrange'], $html, true));
			$data['S_VData'] 	= array();
			$data['S_AddTime'] 	= date('Y-m-d H:i:s',time());
			if ($playrange) {
				if ($ruleConfig['playmode']==1) {
					$data['S_VData'] = preg_collect($ruleConfig['playurl'], $html);
				} else if($webplayurl = preg_collect($ruleConfig['webplayurl'], $html)){
					foreach ($webplayurl as $playurl) {
						$playurl = ((!empty($ruleConfig['urlreplace'])) && str_replace('{$变量}', $playurl, $ruleConfig['urlreplace'])) or $playurl;
						$playurl = formaturl($url, $playurl);
						if($playhtml = http::remotehttp($playurl)){
							$data['S_VData'][] = strip_tags(preg_collect($ruleConfig['playurl'], $playhtml, true));
						}
					}
				}
			}
			if (empty($data['S_VData'])) {
				echo '没有采集到视频播放资源，继续采集下一条...<br />'; continue;
			}
			$playurlcount = count($data['S_VData']);
			$data['S_VData'] = implode('#', $data['S_VData']);
			$data['S_Source'] = $ruleConfig['player'];
			
			if ($collectTYPE[2]==1) {
				$imgpic = uniqid().strrchr($data['S_Pic'],'.');
				http::curlDownload($data['S_Pic'], '../../../images/vodpic/'.$imgpic);
				$data['S_Pic'] = $imgpic;
			}
			//分两种情况
			//第一种：存储到临时表
			//	如果数据相同，则删除原来的数据，否则添加
			if ($collectTYPE[1]==0) {
				$sql = 'select count(1) as count from tempvideo where S_Name = \''.$data['S_Name'].'\'';
				$count = $db->getOne($sql);
				if ($count!=0) {
					$db->query('delete from tempvideo where S_Name = \''.$data['S_Name'].'\'');
				}

				$data['S_Url'] = $url;
				$db->query('insert into `tempvideo` '.array_to_sql($data,'add'));
			}
			//第二种：直接入库
			//	如果数据相同，覆盖相同来源数据，否则添加
			else {
				$sql = 'select S_ID,S_Name,S_Source,S_VData from s_video where S_Name = \''.$data['S_Name'].'\' ORDER BY S_AddTime DESC LIMIT 1';
				$res = $db->getrow($sql);
				if ($res) {
					$player		=array();
					$playdata	=array();
					$player		=explode("$$",$res['S_Source']);
					$playdata	=explode("$$$",$res['S_VData']);
					
					if(in_array($ruleConfig['player'],$player)){
						$play_key=array_search($ruleConfig['player'],$player);
						$detect_old=explode("#",$playdata[$play_key]);
						if(count($detect_old)>=$playurlcount){
							echo '资源未更新，跳过。。。。<br />';
							continue;
						}
						$playdata[$play_key]=$playurl;
						$data['S_Source'] 	= implode("$$",$player);
						$data['S_VData'] 	= implode("$$$",$playdata);
					}else{
						array_push($player,$ruleConfig['player']);
						array_push($playdata,$playurl);
						$data['S_Source'] 	= implode("$$",$player);
						$data['S_VData'] 	= implode("$$$",$playdata);
					}
					
					$sql = 'update `s_video` set '.array_to_sql($data,'update').' where id='.$res[0][0];
					unset($res);
				} else {
					$sql = 'insert into `s_video` '.array_to_sql($data,'add');
				}
				$db->query($sql);
			}
			echo '添加资源《'.$data['S_Name'].'》成功，继续添加下一条<br />';
			unset($html,$data);
		} else {
			echo $url.'无法采集，继续采集下一条...<br />'; continue;
		}
	}
	if (!empty($listhref)) {//如果本次操作尚未完成，则继续缓存刷新处理
		file_put_contents($cacheFile, implode(',', $listhref));
		$location = $_SERVER['REQUEST_URI'];
	}else{
		$pagenow = ($collectTYPE[0]==0) ? ($pagenow+1) : ($pagenow-1);
		$location = 'rulecollect.php?id='.$id.'&cacheid='.$cacheid.'&pagenow='.$pagenow.'&pageend='.$pageend;
	}
}
?><!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html>
<head>
<title>自定义采集平台</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="refresh" content="1; url=<?php echo $location;?>" />
</head>
<body>
继续下一步采集。。。
</body>
</html>