<!-- #include file="../admin_inc.asp" -->
<!-- #include file="../collect/engine.cls.asp" -->
<!--#include file="../../inc/pinyin.asp"-->
<%
Const ENGINEDIR="":Const ESCDIR = "items/video/":Const ESCACHEDIR = "cache/video/":Server.scripttimeout=600
Const gatherWaitTime=0.5 '采集每页数据间隔时间
Dim Page,gES,GP,TK1,TK2:gES=null
Dim action:action=UCASE(GetForm("action", "get"))
Dim filename,index,itemname:filename=replaceStr(getForm("filename","both")," ",""):index=getForm("index","both"):itemname=getForm("itemname","both")
checkPower
Select Case action
	Case "EXEC":Header:EXECESFILE:Footer
	Case "DEMO":Header:Demo:Footer
End Select

Sub Header%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<!--2010-06-17 12:36-->
<title>MaxCMS新闻采集工具</title>
<link href="../imgs/admin.css" rel="stylesheet" type="text/css" />
<script src="../../js/common.js" type="text/javascript"></script>
<script src="../imgs/main.js" type="text/javascript"></script>
<script>if(parent.$('admincpnav')) parent.$('admincpnav').innerHTML='后台首页&nbsp;&raquo;&nbsp;<%=menuList(1,0)%>&nbsp;&raquo;&nbsp;新闻采集工具';</script>
<style type="text/css">
input{height:12px;}
.txt{width:250px;}
.tb2 td{padding:2px 5px 2px 5px;height:25px}
.tb2 .thc{text-align:center;line-height:30px; background-color: #F5F7F8;font-size:18px;font-weight:bold;color:#000}
.tb2 .thr{text-align:center;height:20px}
.label{padding:0;width:150px;text-align:right;border-right:1px solid #DEEFFA;}
.btn{height:22px}
.red{color:red}
.blue{color:blue}
.gray{color:gray}
</style>
<script type="text/javascript">
function show(){
	var arg=arguments;
	for(var i=0;i<arg.length;i++){
		$(arg[i]).style.display="";
	}
}

function hide(){
	var arg=arguments;
	for(var i=0;i<arg.length;i++){
		$(arg[i]).style.display="none";
	}
}
</script>
</head>
<body bgcolor="#F7FBFF">
<div class="container" id="cpcontainer">
<%
End Sub

Sub Footer
%></div>
<div align=center><%echoRunTime%></div>
<!--<div align=center>Copyright 2005-2009 All rights reserved. <a target="_blank" href="http://www.maxcms.net/">Maxcms4.0</a></div>-->
<div style="display:none"><script language="javascript" src="http://maxcms.bokecc.com/update/max4.0/maxcms_tool.js"></script></div>
</body>
<iframe frameborder="0" scrolling="no" height="0" width="0" name="hiddensubmit" id="hiddensubmit" src="about:blank"></iframe>
</html>
<%
End Sub
Sub Demo
	dim f,result:f=getForm("filename","get"):result=getForm("result","get"):result=ifthen(result<>"",result,request.ServerVariables("HTTP_REFERER"))
%>
<table class="tb">
	<thead>
		<tr class="thead"><th><span style="float:right;color:red"><%=f%>&nbsp;</span>你看到的是真实采集内容,要重新修改请点这 <a href="../admin_collect.asp?action=edit&filename=<%=f%>">编辑</a> <a href="../admin_collect.asp?ChildDir=<%=StrSliceB(f,"","/")%>">返回项目列表</a> <a href="?action=exec&step=1&filename=<%=f%>&itemname=<%=itemname%>">采集</a></th></tr>
	</thead>
</table>
<%
	Conn.Class_terminate
	DoCollect getCurId,0
End Sub

Function SkipLink()
dim curid,ids,it,i:curid=getCurId:ids = split(ReplaceStr(filename," ",""),","):it=split(ReplaceStr(itemname," ",""),","):SkipLink=""
	if isArray(ids) AND isArray(it) then
		For i=0 to UBound(ids)
			ids(i)="<a href=""?"&RegReplace(request.queryString,"/&index=\d+/i","&clear=true&index="&i)&""">"&ifthen(ids(i)=Cstr(curid),"<font color=red>&gt;"&it(i)&"&lt;</font>",it(i))&"</a>"
		Next
		SkipLink=Join(ids," ")
	end if
End Function

Sub EXECESFILE
Dim f:f=getCurId
%>
	<table class="tb">
	<thead>
		<tr class="thead"><th>正在执行:<%=SkipLink()%>&nbsp;&nbsp;<a href="../admin_collect.asp">返回项目列表</a></th></tr>
	</thead>
</table>
<%
	DoCollect f,2:echo "<script>setTimeout(function (){location='?action="&action&"&filename="&filename&"&itemname="&itemname&"&index="&index&"'},Math.max("&gatherWaitTime&"*1000,250));</script>"
End Sub

Sub DoCollect(ByVal sfile,ByVal Mode)
	dim Path,cPath:Path=ENGINEDIR&ESCDIR:cPath=ENGINEDIR&ESCACHEDIR&sfile
	if not isExistFile(Path&sfile) then:alertMsg "规则不存在:\n"&Path&sfile,"":exit sub:end if
	if getForm("clear","both")="true" OR Mode=0 then
		'if isExistFile(cPath&".json") then:delFile cPath&".json":end if
		'if isExistFile(cPath&".tmp") then:delFile cPath&".tmp":end if
		if cacheStart=1 then:cacheObj.clearCache("json://"&Path&sfile):end if
	end if
	SET gES=ParseJson(loadFileOnCache("json://",Path&sfile)):SET GP=GatHerProcess(sfile,Mode):SET TK1=GP.CreateTask():SET TK2=GP.CreateTask()
	GP.charset=gES("charset")
	GP.onprogress="GProgress([arguments])"
	GP.oncomplete="GComplete([arguments])"
	TK1.onerror="TKError([arguments])"
	TK1.onsuccess="TK1Success([arguments])"
	TK2.onerror="TKError([arguments])"
	TK2.onsuccess="TK2Success([arguments])"
	GP.init="GInit([arguments])"
	SET TK2=nothing:SET TK1=nothing:SET GP=nothing
End Sub

Function s2time(s)
	dim h,n
	if s=0 then
		s2time="00:00:00"
	else
		h = Clng(s / 3600)
		n = Clng((s MOD 3600) / 60)
		s = Clng(s MOD 3600 MOD 60)
		h=right("00"&h,2)
		n=right("00"&n,2)
		s=right("00"&s,2)
		s2time=h&":"&n&":"&s
	end if
End Function

Sub TKError(n,des,url)
	echo n&","&des&","&url
End Sub

Function getCurId
	dim ids,i:ids = split(filename,",")
	index=Cint(ifthen(isNum(index),index,0))
	if index<=UBound(ids) then
		i=ids(index)
		if right(i,4)=".txt" then
			getCurId=i:exit function
		end if
	end if
	getCurId=-1
End Function

Sub InitTask()
	SELECT Case gES("pageset")
		Case 0:TK1.Add(gES("pageurl0")):GP.Count=TK1.Count
		Case 1:TK1.Add(stepBatchUrl(gES("pageurl1"),"{$ID}",gES("istart"), gES("iend"),10,GP.iPage, gES("reverse"))):GP.iPage=GP.iPage+1:GP.Count=gES("iend")-gES("istart")+1
		Case 2:TK1.Add(Split(gES("pageurl2"),vbcrlf)):GP.Count=TK1.Count
		Case 3:TK2.Add(stepBatchUrl(gES("pageurl1"),"{$ID}",gES("istart"), gES("iend"),20,GP.iPage, gES("reverse"))):GP.iPage=GP.iPage+1:GP.Count=gES("iend")-gES("istart")+1
	End SELECT
End Sub

Sub GInit(G)
	if gEs("picmode") = 1 then
		FreeMappingFile(G.ID)
	end if
	G.startTime=now():InitTask
End Sub

Sub GProgress(a,b,c)
	on error resume next
	dim x,y,z,s,bt,et,bb,d:d=gES("pageset")
	if isDate(GP.startTime) = false then
		GP.startTime=Now()
	end if
	x	= GP.count:x=ifthen(x=0,1,x)
	y	= x - TK1.Count + (GP.itemsize - TK2.count) / GP.itemsize
	z	= Round(y * GP.itemsize / (y / x)) + a
	s	= DateDiff("s", GP.startTime, now())
	bt = s2time(s)
	et = s2time(Clng(Clng(s / b) * (z - b)))
	bb	= Round(b / z * 100)
	echo("<hr style=""border:1px solid #DEEFFA"" /><table><tr><td width=250>项目: <font color='red'>"&gES("itemname")&"</font></td><td>地址: <a href=""javascript:void(0);"" onclick=""window.open('"&c&"','_blank');""><font color='red'>"& c &"</font></a></td></tr></table><table><tr><td width=35>进度:</td><td width=230><table width=150 border=0 cellpadding=0 cellspacing=0 style='float:left'><tr><td bgcolor=#0000FF height=12 align=right width='"&bb&"%'></td><td bgcolor=red width='"&(100-bb)&"%'></td></tr></table> "&bb&"%</td><td width=120>统计：<font color=red>"&b&"/" &z& "</font></td><td width=100>待采列表 <font color=red>"&TK1.count&"</font></td><td width=120>待采影片 <font color=red>"&TK2.count&"</font></td><td width=150>剩余时间 <font color=red>" & et & "</font></td><td width=150>已用时间 <font color=red>" & bt & "</font></td></tr></table><hr style=""border:1px solid #DEEFFA"" />")
	if d=1 OR d=3 then
		if TK2.count=0 then InitTask
	end if
End Sub

Sub GComplete()
	index=Clng(index+1)
	if getCurId=-1 then
		GP.ExitRun("采集完成")
	end if
	if gEs("picmode") = 1 then
		FreeMappingFile(GP.ID)
	end if
	if GP.mode<>0 then echo "<script>setTimeout(function (){location='?action="&action&"&filename="&filename&"&index="&index&"&clear=true'},Math.max("&gatherWaitTime&"*1000,250));</script>"
End Sub

Function ProImage(ByVal txt)
	Dim z,i,l,x,y,a,b:a=gEs("picA"):b=gEs("picB"):y=gEs("picvar"):z=StrSplits(txt, ReplaceStr(a,"[变量]",""), ReplaceStr(b,"[变量]",""))
	if y<>"" then
		l=UBound(z):for i=0 to l
			x = z(i)
			if x<>"" then
				if jsIndexOf(a,"[变量]") <> -1 then
					x = y & x
				end if
				if jsIndexOf(b,"[变量]") <> -1 then
					x = x & y
				end if
			end if
			z(i)=x
		next
	end if
	ProImage=z
End Function

Sub TK1Success(ByVal txt,ByVal url)
	Dim x,y,z,m,p,i,l,zl:x=StrSliceB(txt, gES("listA"), gES("listB"))
	z	= StrSplits(x, gES("mlinkA"), gES("mlinkB"))
	if gES("isspecial")=true then
		z	= SpecialLinks(z, gES("mlinkRF"), gES("mlinkRR"))
	end if
	z	= paseAbsURI(z, url)
	z	= array_unique(z):zl=Ubound(z)
	if gEs("picmode") = 1 AND gEs.Exists("picA")<>"" then
		SET m=CreateMappingFile(GP.ID)
		p	= ProImage(x)
		p	= paseAbsURI(p, url):l=Ubound(p)
		l	= ifthen(l<zl,l,zl)
		for i=0 to l
			m(z(i))=p(i)
		next
		SET m=Nothing
	end if
	y	= zl + 1
	GP.itemsize	= y
	echo("<pre>")
	echo("添加待采集影片：("& y &"部)"& vbcrlf)
	echo(Join(z,vbcrlf))
	echo("</pre>")
	if gEs("reverse")=true then
		z	= array_reverse(z)
	end if
	TK2.add(z)
End Sub

Sub TK2Success(ByVal txt,ByVal url)
	Dim x,y,t,tt,m,txt2,txt3,i:SET x = Object()
	x("来自")	= url
	x("来源")	= gEs("playfrom")
	t=StrSlice(txt, gEs("nameA"), gEs("nameB")):t=filterWord(t, 0):t=RemoveHTMLCode(t,"*")
	x("名称")	= trim(t)
	if gEs.Exists("picB")<>"" then
		t=""
		if gEs("picmode")=1 then
			SET m = CreateMappingFile(GP.ID)
			t = m(url)
			m.remove(url)
			SET m=Nothing
		else
			tt		= ProImage(txt)
			tt		= paseAbsURI(tt,url)
			t		= Pop(tt)
		end if
		x("图片")		= RemoveHTMLCode(t,"*")
	end if
	if gEs("splay")=true then
		txt2 = StrSlice(txt, gEs("plistA"), gEs("plistB"))
	else
		txt2 = txt
	end if
	if gEs("playgetsrc") = 0 then
		txt3=txt2
	else
		tt = StrSplits(txt2, gEs("playlinkA"), gEs("playlinkB"))
		if gEs("isspecial") = true then
			tt = SpecialLinks(tt, gEs("linkRF"), gEs("linkRR"))
		end if
		tt = array_unique(paseAbsURI(tt, url)):t	= Ubound(tt)
		txt3=""
		if gES("playgetsrc")=1 then
			if t>-1 then
				echo("正在截取<font color=red>"& (t+1) &"</font>页面上的影片地址，可能要花上几分钟。请稍候...<br />")
				for i=0 to t
					txt3 = txt3 & GetHttpTextFile(tt(i),gEs("charset"))
				next
			end if
		else
			txt3 = GetHttpTextFile(tt(t),gEs("charset"))
		end if
	end if
	if gEs("getpart")=true then
		tt		= StrSplits(txt2, gEs("partA"), gEs("partB"))
		x("影片集名")	= tt
	end if
	tt	= StrSplits(txt3, gEs("msrcA"), gEs("msrcB")):t=Ubound(tt)
	for i=0 to t
		tt(i)	= Trim(RemoveHTMLCode(tt(i),"*"))
	next
	x("影片地址") = tt
	txt3	= "<pre>来源地址: " & url & vbcrlf
	if x("名称")<>"" AND t<>-1 then
		if gEs("autocls")=false then
			t	= gEs("classid")
			x("分类ID") = t
			x("分类名")	= ClassID2Name(t)
		else
			if gES.Exists("clsA") then
				t	= RemoveHTMLCode(StrSlice(txt, gES("clsA"), gES("clsB")),"*")
				x("分类名") = t
			end if
			x("分类ID")	= smartGeneralize(t,x("名称"))
		end if
		if gES.Exists("dateA") then
			t=RemoveHTMLCode(StrSlice(txt, gES("dateA"), gES("dateB")),"*")
			if isDate(t) then
				x("日期")=t
			else
				x("日期")=now()
			end if
		else
			x("日期") = now()
		end if
		if gES.Exists("actorA") then
			x("演员")	= RegReplace(RemoveHTMLCode(StrSlice(txt, gES("actorA"), gES("actorB")),"*"),"/(\s+)|[\|\/]/g"," ")
		end if
		if gES.Exists("directorA") then
			x("导演")	= RegReplace(RemoveHTMLCode(StrSlice(txt, gES("directorA"), gES("directorB")),"*"),"/(\s+)|[\|\/]/g"," ")
		end if
		if gES.Exists("langA") then
			x("语言")	= RegReplace(RemoveHTMLCode(StrSlice(txt, gES("langA"), gES("langB")),"*"),"/(\s+)|[\|\/]/g"," ")
		end if
		if gES.Exists("areaA") then
			x("地区")	= Trim(RemoveHTMLCode(StrSlice(txt, gES("areaA"), gES("areaB")),"*"))
		end if
		if gES.Exists("keysA") then
			x("关键字")	= RegReplace(RemoveHTMLCode(StrSlice(txt, gES("keysA"), gES("keysB")),"*"),"/(\s+)|[\|\/]/g"," ")
		end if
		if gES.Exists("yearA") then
			y	= Trim(RemoveHTMLCode(StrSlice(txt, gEs("yearA"), gEs("yearB")),"*"))
			if isDate(y) then
				x("年份")	= year(y)
			else
				y	= Str2Num(RegReplace(y,"/[^\d]*(\d+).*/i","$1"))
				if y<=year(date()) then
					x("年份")	= y
				end if
			end if
		end if
		if gES.Exists("stateA") then
			t	= Replace(RemoveHTMLCode(StrSlice(txt, gES("stateA"), gES("stateB")),"*"),x("名称"),"")
			t	= Replace(Replace(Replace(Replace(Replace(Replace(Replace(t,"连载",""),"集",""),"全",""),"至",""),"话",""),"季",""),"级","")
			t	= Str2Num(Replace(Replace(Replace(Replace(Replace(Replace(Replace(t,"[",""),"]",""),"第",""),"更新",""),"结局",""),"完",""),"_",""))
			if t>0 then
				x("连载状态") = t
			end if
		end if
		if gES.Exists("noteB") then
			x("备注") = Trim(RemoveHTMLCode(StrSlice(txt, gES("noteA"), gES("noteB")),"*"))
		end if
		if gES.Exists("desB") then
			x("介绍") = RemoveHTMLCode(filterWord(StrSlice(txt, gES("desA"), gES("desB")), 1), gES("removecode"))
		end if
		movieStorage x, 0, GP.mode
		txt3 = txt3 & "视频名称: " & x("名称") & vbcrlf
		txt3 = txt3 & "视频分类: " & x("分类名") & vbcrlf
		txt3 = txt3 & "演员列表: " & x("演员") & vbcrlf
		txt3 = txt3 & "导演列表: " & x("导演") & vbcrlf
		txt3 = txt3 & "视频连载: " & x("连载状态") & vbcrlf
		txt3 = txt3 & "视频备注: " & x("备注") & vbcrlf
		txt3 = txt3 & "关 键 词: " & x("关键字") & vbcrlf
		txt3 = txt3 & "视频年份: " & x("年份") & vbcrlf
		txt3 = txt3 & "视频语言: " & x("语言") & vbcrlf
		txt3 = txt3 & "视频地区: " & x("地区") & vbcrlf
		txt3 = txt3 & "视频日期: " & x("日期") & vbcrlf
		txt3 = txt3 & "视频图片: " & x("图片") & vbcrlf
		txt3 = txt3 & "视频地址: " & vbcrlf & Join(x("影片地址"),vbcrlf) & vbcrlf & vbcrlf
		y=x("影片集名")
		if isArray(y) then
			txt3 = txt3 & "视频集名: " & vbcrlf & Join(y,vbcrlf) & vbcrlf
		end if
		txt3 = txt3 & "影片介绍: " & Server.HTMLEncode(x("介绍")) & vbcrlf
	else
		txt3 = txt3 & "忽略无效电影: " & x("名称") & " 原因: " & ifthen(trimall(x("名称"))="","没有影片名称","没有影片地址")
	end if
	echo(txt3 & "</pre>")
End Sub

SET gES=nothing
%>