<!--#include file="connart.asp" -->
<%
Response.Buffer = true
Server.ScriptTimeOut=9999999
headAdminCollect "文章自定义采集"

dim action,p_id,p_ids,num,arrid,arrcount,viewnum,viewcount,listnum,txt,sb,cg

p_ids = be("all","p_id")
action  = be("get","action")
num = be("get","num")
listnum = Request.QueryString("listnum")
viewnum = be("all","viewnum")
sb = Request.QueryString("sb")
cg = Request.QueryString("cg")

if isn(listnum) then listnum=0 else listnum = clng(listnum)
if isn(viewnum) then viewnum=0 else viewnum = clng(viewnum)
if isn(num) then num =0 else num = clng(num)

if (isN(action) and instr(p_ids,",")> 0) or action="pl" Then
	action = "pl"
	arrid = split(p_ids,",")
	arrcount = ubound(arrid) + 1
	if num >= arrcount then
		dBreakpoint "../../upload/artbreakpoint"
		showmsg "<font color='red'><b>批量采集完成</b></font>","collect_art_manage.asp"
	end if
	p_id = arrid(num)
else
	p_id = p_ids
end if

if isN(p_id) then errmsg "采集提示","采集项目ID不能为空!"

dim rs,sql,cjlms
dim p_script,p_name,p_coding,p_pagetype,p_url,p_pagebatchurl,p_manualurl,p_pagebatchid1,p_pagebatchid2,p_authortype,p_authorstart,p_authorend,p_titletype,p_titlestart,p_titleend,p_collecorder,p_savefiles,p_intolib,p_ontime,p_listcodestart,p_listcodeend,p_listlinkstart,p_listlinkend,p_collect_type,p_time,p_timestart,p_timeend,P_AreaStart,P_AreaEnd,p_classtype,p_typestart,p_typeend,p_contentstart,p_contentend,p_hitsstart,p_hitsend,p_showtype,p_cpagecodestart,p_cpagecodeend,p_cpagetype,p_cpagestart,p_cpageend
dim linkarr,linkarrcode,authorarr,authorarrcode,titlearr,titlearrcode

if sb="" and cg="" then
	cjdb.db "update [Tbl_Art_Project] set [p_time]='"&now()&"' where [p_id]="&p_id,"exe"
	sb=0 :	cg=0
end if
%>
<DIV class=bodytitle>
<DIV class=bodytitleleft></DIV>
<DIV class=bodytitletxt>采集中...  第<%=listnum+1%>页， 成功<%=cg%>条，失败<%=sb%>条</DIV>
<DIV class=bodytitletxt2></DIV>
</DIV>
<%

sql = "select * from Tbl_Art_Project where p_id="&p_id
set rs = cjdb.db(sql,"rs1")
If Not rs.Eof Then
	p_id = rs("p_id")
	p_name = rs("p_name")
	p_coding = rs("p_coding")
	p_pagetype = rs("p_pagetype")
	p_url = rs("p_url")
	p_pagebatchurl = rs("p_pagebatchurl")
	p_manualurl = rs("p_manualurl")
	p_pagebatchid1 = rs("p_pagebatchid1") : if not isNum(p_pagebatchid1) then p_pagebatchid1=0 else p_pagebatchid1 = clng(p_pagebatchid1)
	p_pagebatchid2 = rs("p_pagebatchid2") : if not isNum(p_pagebatchid2) then p_pagebatchid2=0 else p_pagebatchid2 = clng(p_pagebatchid2)
	p_script = rs("p_script")
	p_collecorder = rs("p_collecorder")
	p_savefiles = rs("p_savefiles")
	p_intolib = rs("p_intolib")
	p_ontime = rs("p_ontime")
	p_listcodestart = rs("p_listcodestart")
	p_listcodeend =rs("p_listcodeend")
	p_classtype = rs("p_classtype")
	p_collect_type = rs("p_collect_type")
	p_time = rs("p_time")
	p_listlinkstart = rs("p_listlinkstart")
	p_listlinkend = rs("p_listlinkend")
	p_authortype = rs("p_authortype")
	p_authorstart = rs("p_authorstart")
	p_authorend = rs("p_authorend")
	p_titletype = rs("p_titletype")
	p_titlestart = rs("p_titlestart")
	p_titleend = rs("p_titleend")
	p_timestart = rs("p_timestart")
	p_timeend = rs("p_timeend")
	p_typestart = rs("p_typestart")
	p_typeend = rs("p_typeend")
	p_contentstart = rs("p_contentstart")
	p_contentend = rs("p_contentend")
	p_hitsstart = rs("p_hitsstart")
	p_hitsend = rs("p_hitsend")
	p_showtype = rs("p_showtype")
	p_cpagecodestart = rs("p_cpagecodestart")
	p_cpagecodeend = rs("p_cpagecodeend")
	p_cpagetype = rs("p_cpagetype")
	p_cpagestart = rs("p_cpagestart")
	p_cpageend = rs("p_cpageend")
end if
cjdb.C(rs)


dim strListUrl,ListEnd,ListArray

Select Case p_pagetype
	Case 0 
			If listnum < 1 Then
            	strListUrl = p_url
			Else
				ListEnd = 1
			End if
	Case 1,3
          If p_collecorder = 1 then
			 If (p_pagebatchid2-(listnum+1))<p_pagebatchid1 or (p_pagebatchid2-(listnum+1))<0 Then
				ListEnd=1
			 Else
				strListUrl=Replace(p_pagebatchurl,"{ID}",(p_pagebatchid2-listnum))
			 End if
		  Else
			 If (p_pagebatchid1+listnum)> p_pagebatchid2  Then
				ListEnd=1
			 Else
				strListUrl=Replace(p_pagebatchurl,"{ID}",(p_pagebatchid1+listnum))
			 End If
		  End If
	Case 2
         ListArray=Split(p_manualurl,"|")
		  If (listnum)>CInt(Ubound(ListArray)) Then
			 ListEnd=1
		  Else
			 strListUrl = ListArray(listnum)
		  End If  
End Select

Select Case ListEnd
	Case 1
		if action <> "pl" then
			dBreakpoint "../../upload/artbreakpoint"
			showmsg "采集完成","collect_art_manage.asp"
		else
			if num >= arrcount then
				dBreakpoint "../../upload/artbreakpoint"
				showmsg "批量采集完成","collect_art_manage.asp"
			else
			writeE "<br> 此项目数据采集完毕 ---   暂停2秒后继续采集<script language=""javascript"">setTimeout(""makeNextPage();"",2000);function makeNextPage(){location.href='collect_art_cj.asp?p_id="&p_ids&"&ListNum=0&num="&(num+1)&"&action="&action&"';}</script>"
			end if
		end if
	case else
	call cjList()
End Select

sub cjList()
	dim strListCode,UrlTest,Url,i,strViewCode,startnum,endnum,j,tempStep,tempPoor,strListCodeCut,strdstate
	
	if isN(session("strListCodeart")) then
		strListCode = getPage(strListUrl,p_coding)
		session("strListCodeart") = strListCode
	else
		strListCode = session("strListCodeart")
	end if
	
	if strListCode = False then
		writeF "<tr><TD vAlign=center  colspan=""2"">在获取:"&strListUrl&"网页源码时发生错误！</TD></tr>"
		exit sub
	end if
	listnum = listnum+1 :  tempStep = 1
	
	select case p_pagetype
		case 3
			strViewCode = strListCode
			j = 1
			if p_collecorder = 1 then 
				startnum = p_pagebatchid2 : endnum = p_pagebatchid1
			else
				startnum = p_pagebatchid1 : endnum = p_pagebatchid2
			end if
			call wtablehead
			for i=startnum to endnum
				UrlTest = replaceStr(p_pagebatchurl,"{$ID}",i)
				writeF "<tr><TD vAlign=center  colspan=""2""></TD>正在采集列表："&UrlTest&"的数据 </tr>"
				call cjView(UrlTest,j)
				writeF ""
				j = j + 1
			next
			call wtablefoot
			clearSessionart
			writeF "<br> 此项目数据采集完毕<script language=""javascript"">setTimeout(""makeNextPage();"",2000);function makeNextPage(){location.href='collect_art_manage.asp';}</script>"
		case else
			if isN(session("strListCodeCutart")) then
				strListCodeCut = getBody(strListCode,p_listcodestart,p_listcodeend)
				session("strListCodeCutart") = strListCodeCut
			else
				strListCodeCut = session("strListCodeCutart")
			end if
			
			if isN(session("linkarrcodeart")) then
				linkarrcode = getArray(strListCodeCut,p_listlinkstart,p_listlinkend)
				session("linkarrcodeart") = linkarrcode
			else
				linkarrcode = session("linkarrcodeart")
			end if
			
			if p_authortype = 1 then
				authorarr = getArray(strListCodeCut,p_authorstart,p_authorend)
			end if
			if p_titletype = 1 then
				titlearrcode = getArray(strListCodeCut,p_titlestart,p_titleend)
			end if
			
			
			if linkarrcode = False then
				writeA "<tr><TD  colspan=""2""></td>在获取链接列表时出错！</tr>"
				sb = sb+1
				exit sub
			End If
			
			wBreakpoint "../../upload/artbreakpoint",geturl()
			
			linkarr = Split(linkarrcode,"$Array$")
			if p_authortype = 1 then authorarr = Split(authorarrcode,"$Array$")
			if p_titletype = 1 then titlearr = Split(titlearrcode,"$Array$")
			viewcount = ubound(linkarr)
			
			if p_showtype=1 then
				if viewnum >= viewcount then
					clearSessionart
					writeE "分页数据采集完毕 ---   暂停2秒后继续采集<script language=""javascript"">setTimeout(""makeNextPage();"",2000);function makeNextPage(){location.href='collect_art_cj.asp?p_id="&p_ids&"&ListNum="&listnum&"&sb="&sb&"&cg="&cg&"&num="&num&"&action="&action&"';}</script>"
				else
					strdstate = "true"
					call wtablehead
					call cjView(linkarr(viewnum),viewnum)
					writeF ""
					call wtablefoot
					writeE "数据采集完毕 --- 稍后继续采集<script language=""javascript"">var dstate="&strdstate&";setInterval(""makeNextPage();"",500);function makeNextPage(){if(dstate){dstate=false;location.href='collect_art_cj.asp?p_id="&p_ids&"&ListNum="&listnum-1&"&sb="&sb&"&cg="&cg&"&num="&num&"&viewnum="&viewnum+1&"&action="&action&"';}}</script>"
				end if
			else
				for i=0 to ubound(linkarr)
					'if i > 2 then exit for
					call wtablehead
					if i=0 then
					writeF "<tr><TD vAlign=center  colspan=""2""></TD>正在采集列表："&strListUrl&"的数据 </tr>"
					end if
					call cjView(linkarr(i),i)
					writeF ""
					call wtablefoot
				next
				clearSessionart
				writeE "<br> 此分页数据采集完毕 ---   暂停2秒后继续采集 <script language=""javascript"">setTimeout(""makeNextPage();"",2000);function makeNextPage(){location.href='collect_art_cj.asp?p_id="&p_ids&"&ListNum="&listnum&"&sb="&sb&"&cg="&cg&"&num="&num&"&action="&action&"';}</script>"
			end if
	end select
end sub

sub cjView(Byval strlink,Byval num)
	dim strViewCode
	dim titlecode,authorcode,timecode,typecode,contentcode,cpagecode,cpagelinkarrcode,cpagelinkarr,cpagelink,cpagelinkcode
    dim weburl,movieid,webArray,i,m_typeid,m_hits,typecodearr
	
	strlink = DefiniteUrl(strlink,strListUrl)
	strViewCode = getPage(strlink,p_coding)
	
	If strViewCode = False then
		writeA "<tr><TD vAlign=center  colspan=""2"">在获取内容页时出错："&strlink&" </TD></tr>"
		sb=sb+1
		exit sub
	Else
		
		if p_titletype = 1 then
			titlecode = titlearr(num)
		else
			titlecode = getBody(strViewCode,p_titlestart,p_titleend)
		end if
		
		titlecode = FilterScript(titlecode,p_script)
		titlecode = replaceFilters(titlecode,p_id,1)
		titlecode = replaceStr(titlecode,"'","")
		
		sql="select m_id,m_title from [tbl_art] where m_title='"&titlecode&"'"
		set rs=cjdb.db(sql,"rs1")
		
		if not (rs.eof and rs.bof) then
			writeA "<tr><TD vAlign=center  colspan=""2"">遇到重复电影名称跳过采集!</TD></tr>"
			exit sub
		end if
		cjdb.c(rs)
		
		if not isNum(p_hitsstart) then p_hitsstart = 0 
		if not isNum(p_hitsend) then p_hitsend = 0 
		if p_hitsstart =0 and p_hitsend = 0 then m_hits = 0 else m_hits = rndNum(p_hitsend,p_hitsstart)
		
		if p_authortype = 1 then
			authorcode = authorarr(num)
		else
			authorcode = getBody(strViewCode,p_authorstart,p_authorend)
		end if
		authorcode = replaceStr(authorcode,"False","未知")
		authorcode = FilterScript(authorcode,p_script)
		
		
		if p_classtype = 1 then
			typecode = getBody(strViewCode,p_typestart,p_typeend)
			typecode = FilterScript(typecode,p_script)
			m_typeid = ClassTypeId(typecode,p_id)
		else
			typecode = p_collect_type
			m_typeid = p_collect_type
			typecodearr = getValueByArray(cachearttype, typecode)
			typecode = typecodearr(1)
		end if
		
		timecode = getBody(strViewCode,p_timestart,p_timeend)
		timecode = replaceStr(timecode,"False",now)
		timecode = FilterScript(timecode,p_script)
		
		
		contentcode = getBody(strViewCode,p_contentstart,p_contentend)
		cpagecode = getBody(strViewCode,p_cpagecodestart,p_cpagecodeend)
		contentcode = replaceStr(contentcode,cpagecode,"")
		
		if p_cpagetype=1 then
			cpagelinkarrcode= getArray(cpagecode,p_cpagestart,p_cpageend)
			cpagelinkarr = Split(cpagelinkarrcode,"$Array$")
			for i=0 to ubound(cpagelinkarr)
				cpagelink = cpagelinkarr(i)
				if cpagelink <> "" and cpagelink<>"#" then
					cpagelink = DefiniteUrl(cpagelink,strListUrl)
					cpagelinkcode =  getPage(cpagelink,p_coding)
					if cpagelinkcode <> False then
						cpagecode = getBody(cpagelinkcode,p_cpagecodestart,p_cpagecodeend)
						cpagelinkcode = getBody(cpagelinkcode,p_contentstart,p_contentend)
						cpagelinkcode = replace(cpagelinkcode,cpagecode,"")
						contentcode = contentcode & cpagelinkcode
					end if
				end if
			next
		end if
		contentcode = replaceFilters( contentcode ,p_id,2)
		
		
		if p_showtype=1 then
			writeA "<tr><td colspan=""2"" align=""center"">此列表中第"&(num+1)&"条数据采集结果</TD></tr><tr><TD >来源地址：</TD><td >"&strlink&"</td></tr><TD >文章标题：</TD><td >"&titlecode&"</td></tr><td >文章作者：</TD><td >"&authorcode&"</td></tr><TD >发布时间：</TD><td >"&timecode&"</td></tr><TD >分类：</TD><td >"&typecode&"</td></tr> <td >内容：</td><td>"&left( htmlFilter(contentcode) ,50)&"......</td></tr>"
		else
			writeA "<tr><TD colspan=""2"" align=""center"">此列表中第"&(num+1)&"条数据采集结果</TD></tr><tr><TD >来源地址：</TD><td >"&strlink&"</td></tr><TD >文章标题：</TD><td >"&titlecode&"</td></tr>"
		end if
		
		sql="select * from [tbl_art] where m_urltest='"&strlink&"' "
		set rs=cjdb.DB(sql,"rs3")
	    if not rs.eof then
			cg=cg+1
			rs("m_type") = typecode
			rs("m_urltest") = strlink
			rs("m_title") = titlecode
			rs("m_author") = authorcode
			rs("m_content") = contentcode
			rs("m_addtime") = now()
			rs("m_zt") = 0
			rs("m_pid") = p_id
			rs("m_typeid") = m_typeid
            rs.update
			movieid=rs("m_id")
		else
			cg=cg+1
			rs.addnew
			rs("m_type") = typecode	
			rs("m_urltest") = strlink
			rs("m_title") = titlecode
			rs("m_author") = authorcode
			rs("m_content") = contentcode
			rs("m_addtime") = now()
			rs("m_zt") = 0
			rs("m_pid") = p_id
			rs("m_typeid") = m_typeid
			rs("m_hits")= m_hits
			rs.update
			movieid=rs("m_id")
		end if
		cjdb.c(rs)
	end if
end sub

sub wtablehead
%>
<TABLE width="96%" border=0 align=center cellpadding="4" cellSpacing=1 class=tb>
<TBODY>
<%
end sub

sub wtablefoot
%>
</TBODY>
</TABLE>
<%
end sub
%>