<?php
/*
秀影迅雷采集器2.0
208-10-6
*/
class xunlei extends DBModel {
	public $server = null;
	public $xml;
	public $IO;
	public function init(){
		$this->IO = new Easy_Filesystem();
	}
	public function import(){
		$i=0;
		$stime = strtotime($this->_Response->getParams('stime'));
		$etime = strtotime($this->_Response->getParams('etime'));
		$save = intval($this->_Response->getParams('save'));
		$this->num = intval($this->_Response->getParams('num'));
		while($stime < $etime){	//循环导入影片资源
			$stime = $stime+86400;
			$this->IO->wfile(ROOT.'xunlei.log', date('Y-m-d', $stime));
			$file = 'http://u.xunlei.com/xml/'.date('Ymd', $stime).'.xml';
			echo $file;
			$filename = ROOT.'Cache/xunlei/'.basename($file);
			if (file_exists(ROOT.'Cache/xunlei') === false){
				mkdir(ROOT.'Cache/xunlei', 0755);
			}
			if (file_exists($filename) === false){
				$contents = file_get_contents($file);
				if (file_exists($filename)===false && $contents){
					$this->IO->wfile($filename, $contents);
				}
			}else{
				$contents = file_get_contents($filename);
			}
			$this->parseMovie($contents);
			$uri = preg_replace('/xunlei\/(.*)/is', 'xunlei/', $_SERVER['REQUEST_URI']).'stime/'.date('Y-m-d', $stime).'/etime/'.date('Y-m-d', $etime).'/save/'.$save.'/num/'.$this->num.'/';
			header("Location:".INSTALLDIR.'plugins/xunlei/uri/'.base64_encode($uri));
			exit;
		}
		return true;
	}
	/*解析影片资料*/
	public function parseMovie(&$content){
		$mov = new MovieModel();
		$filter = array();
		$filter['name'] = 'title';
		$filter['big_pic'] = 'pic';
		$filter['small_pic'] = 'otherpic';
		$filter['actor'] = 'actor';
		$filter['director'] = 'director';
		$filter['description'] = 'content';
		preg_match_all('/\<movie+\s+(.+?)\>(.+?)\<\/movie\>/is', $content, $temp);
		if (is_array($temp[1])){
			$i=0;
			foreach ($temp[1] as $str){
				$str = parse_str(str_replace(' ', '&', str_replace('"', '', $str)), $output);
				//echo $str.'<br>';
				foreach ($filter as $key=>$name){
					$array[$name] =$this->parseName($temp[2][$i], $key);
				}//end for
				$category = null;
				$category[] = $output['main_type'];
				$category[] = $output['sub_type'];
				$array['category'] = $this->changeCategory($category);
				$array['player'] = 'xunlei';
				if (preg_match('/([a-zA-Z]+)/is', $array['actor'])){
					$array['area'] = '欧美';
				}else{
					$array['area'] = '港台';
				}
				$urls = $this->parseUrl($temp[2][$i]);	//从地址中获取路径与标题
				$array['url'] = $urls['url'];
				$array['alt'] = $urls['name'];
				$array['publish'] = '未知';
				$array['sid'] = 1;
				$array['look'] = 1;
				$array['mkey'] = substr(md5($array['title']. $array['actor'].$array['director']),8,16);
				$i++;
				if ($output['change'] == 'update'){
					$mov->delete($array['mkey']);	//删除已经存在的影片
					$mov->add($array);	//修改已经存在的
				}else{
					$movid = $mov->exists(array('mkey'=> $array['mkey']));
					if ( $movid ){
						$array['movid'] = $movid;
						$mov->add($array, 'modify');	//修改已经存在的
					}else{
						$mov->add($array);
					}
				}
				$this->num = $this->num+1;
				unset($array);
			}//end for
		}
		
	}
	public function parseName($content, $name){
		
		$re = "/\<".$name."\>\<\!\[CDATA\[(.+?)\]\]\>\<\/".$name."\>/is";
		preg_match($re, $content, $result);
		return $result[1];
	}
	public function parseUrl($content){
		preg_match('/\<file_list+\s(.+?)>(.+?)\<\/file_list\>/is', $content, $tmp);
		$re = "/\<name\>\<\!\[CDATA\[(.+?)\]\]\>\<\/name\>/is";
		preg_match_all($re, $tmp[2], $val);
		$data['name'] = $val[1];
		$re = "/\<params\>\<\!\[CDATA\[(.+?)\]\]\>\<\/params\>/is";
		preg_match_all($re, $tmp[2], $val);
		$data['url'] = $val[1];
		return $data;
	}
	public function changeCategory($category){
		$cate = new CategoryModel();
		$main_type = array_shift($category);
		$row = $cate->getRow("category like '{$main_type}%'");
		if ($main_type){
			if ($row['category']){
				$mainid = $row['id'];
			}else{
				$mainid = $cate->Add(array('category' => $main_type, 'parentid'=>0));
			}
			$this->IO->wfile(ROOT.'Cache/xunlei/category.log', $mainid."\n", 'a');	//写入日志
		}
		foreach($category as $sub_type){
			if ($sub_type){
				$row = $cate->getRow("category like '{$sub_type}%'");
				if ($row['category']){
					$newType = $row['id'];
				}else{
					$newType = $cate->Add(array('category' => $sub_type, 'parentid'=>(int)$mainid));
				}
				$this->IO->wfile(ROOT.'Cache/xunlei/category.log', $newType."\n", 'a');	//写入日志
			}
			$cateid[] = $newType;
		}
		if (!$newType){
			$cateid = $mainid;
		}
		return $cateid;
	}
}?>