<%
Response.Buffer = True 
Server.ScriptTimeOut=999999
Response.Expires = -1
Response.ExpiresAbsolute = Now() - 1 
Response.Expires = 0 
Response.CacheControl = "no-cache"
%>
<!--#include file="../inc/config.asp"-->
<!--#include file="../inc/conn.asp"-->
<!-- #include file="inc/Session.asp" -->
<!--#include file="inc/collectfunc.asp"-->
<!-- #include file="inc/function.asp" -->
<!-- #include file="inc/checkrole.asp" -->
<%
BeginTime=Timer
'''''''''''''''''''''''''''''''''''''''''''''''
call checkPrevilege(Session("Name"),"p09")
'''''''''''''''''''''''''''''''''''''''''''''''
dim taskid
if not isempty(request("taskid")) then 
	taskid=Cint(request("taskid"))
else
	taskid=0
end if
dim ParseType,TitleStartTag,TitleEndTag,ContentStartTag,ContentEndTag,encode,taskname,act,catalogid,ContentLinkStart,ContentLinkEnd
ParseType=Cint(request.Form("ParseType"))
TitleStartTag=request.Form("TitleStartTag")
if(TitleStartTag="" or len(TitleStartTag)<1) then
	response.Write("错误!标题开始标记不能为空!")
	response.Write("&nbsp;&nbsp;<a href=""#"" onClick=""javascript:history.back();""><==返回</a>")
	response.End()
end if
TitleEndTag=request.Form("TitleEndTag")
if(TitleEndTag="" or len(TitleEndTag)<1) then
	response.Write("错误!标题结束标记不能为空!")
	response.Write("&nbsp;&nbsp;<a href=""#"" onClick=""javascript:history.back();""><==返回</a>")
	response.End()
end if
ContentStartTag=request.Form("ContentStartTag")
if(ContentStartTag="" or len(ContentStartTag)<1) then
	response.Write("错误!内容开始标记不能为空!")
	response.Write("&nbsp;&nbsp;<a href=""#"" onClick=""javascript:history.back();""><==返回</a>")
	response.End()
end if
ContentEndTag=request.Form("ContentEndTag")
if(ContentEndTag="" or len(ContentEndTag)<1) then
	response.Write("错误!内容结束标记不能为空!")
	response.Write("&nbsp;&nbsp;<a href=""#"" onClick=""javascript:history.back();""><==返回</a>")
	response.End()
end if
ContentLinkStart=request.Form("ContentLinkStart")
ContentLinkEnd=request.Form("ContentLinkEnd")
encode=trim(request.Form("encode"))
if(encode="" or len(encode)<1) then
	response.Write("错误!编码不能为空!")
	response.Write("&nbsp;&nbsp;<a href=""#"" onClick=""javascript:history.back();""><==返回</a>")
	response.End()
end if
taskname=trim(request.Form("taskname"))
if(taskname="" or len(taskname)<1) then
	response.Write("错误!任务名称不能为空!")
	response.Write("&nbsp;&nbsp;<a href=""#"" onClick=""javascript:history.back();""><==返回</a>")
	response.End()
end if
act=trim(request.QueryString("act"))
catalogid=Cint(request.Form("catalogid"))
if(taskid>=1) then
'======================================
'修改任务到库
'======================================
	set rs=server.CreateObject("adodb.recordset") 
	rs.open "select top 1 * from collect where id="&taskid,conn,3,3
	if not rs.eof then
		rs("ParseType")=ParseType
		rs("TitleStartTag")=TitleStartTag
		rs("TitleEndTag")=TitleEndTag
		rs("ContentStartTag")=ContentStartTag
		rs("ContentEndTag")=ContentEndTag	
		rs("ContentLinkStart")=request.Form("ContentLinkStart")
		rs("ContentLinkEnd")=request.Form("ContentLinkEnd")
		rs.update	
		rs.close
		set rs=nothing
	else
		response.Write("没有找到ID为"&taskid&"的任务")
		response.End()
	end if
else
'======================================
'添加数据提取规则到库
'======================================
	set rs=server.CreateObject("adodb.recordset") 
	rs.open "select top 1 * from collect where TaskName='"&taskname&"'",conn,3,3
	if not rs.eof then
		rs("ParseType")=ParseType
		rs("TitleStartTag")=TitleStartTag
		rs("TitleEndTag")=TitleEndTag
		rs("ContentStartTag")=ContentStartTag
		rs("ContentEndTag")=ContentEndTag	
		rs("ContentLinkStart")=request.Form("ContentLinkStart")
		rs("ContentLinkEnd")=request.Form("ContentLinkEnd")
		rs.update	
		rs.close
		set rs=nothing
	end if
end if
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title>信息采集</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	background-color: #D4D0C8;
}
input{ font-size:12px;}
form{margin:0;}
-->
</style>
<link href="style/global.css" rel="stylesheet" type="text/css">
<style type="text/css">
body{font-size:12px;}
<!--
.style1 {color: #FF0000}
.style2 {color: #CCCCCC}
.style3 {color: #FFFFFF;
	font-weight: bold;
}
#Layer2 {
	margin:5px auto;	
	width:314px;
	z-index:2;
}
-->
</style>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//-->
</script>
</head>
<body>
<table width="100%"  border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><iframe src="right_top.htm" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" height="30" width="100%"></iframe></td>
  </tr>
</table>
<table width="100%"  border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#999999">
  <tr bgcolor="#FF9900">
    <td width="100%" height="22"><div align="center"><span class="style3">◆ 信息采集 ◆</span></div></td>
  </tr>
</table>
<!--开始采集规则-->
<!--开如数据设置规则-->
<fieldset style="margin:5px 50px 5px 50px; border-top:1px solid #ffffff; border-right:1px solid #000000;border-bottom:1px solid #000000;border-left:1px solid #ffffff;">
	<form name="form1" method="post" action="collect4.asp?act=add">
	  <legend>提取数据</legend>
	<table width="100%"  border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="35%">&nbsp;</td>
    <td width="65%" height="50">
	<input type="hidden" name="taskid" value="<%=taskid%>">
	<input type="hidden" name="encode" value="<%=encode%>">
	<input type="hidden" name="taskname" value="<%=taskname%>">
	<textarea name="TitleStartTag" cols="50" rows="6" id="TitleStartTag" style="display:none; "><%=TitleStartTag%></textarea>
	<textarea name="TitleEndTag" cols="50" rows="6" id="TitleEndTag" style="display:none; "><%=TitleEndTag%></textarea>
	<textarea name="ContentStartTag" cols="50" rows="6" id="ContentStartTag" style="display:none; "><%=ContentStartTag%></textarea>
	<textarea name="ContentEndTag" cols="50" rows="6" id="ContentEndTag" style="display:none; "><%=ContentEndTag%></textarea>
	<input type="hidden" name="catalogid" value="<%=catalogid%>">
	<%if(act="add") then%>
	<input type="button" name="button2" value="完  成!==>" onClick="location.href='collect.asp'">
	<%else%>
	<input type="submit" name="Submit32" value="最后一步!添加到库==>" onClick="Layer1.style.display=''">
	<%end if%>
      <input type="button" name="btn" value="返  回" onClick="window.location.href='collect.asp?taskid=<%=taskid%>'"></td>
  </tr>
</table>
</form>
</fieldset>
<div id="Layer2">
  <table width="100%" border="0" bgcolor="#A3B2CC">
    <tr>
      <td bgcolor="#D7F7FF"><marquee direction="left" width="100%">信息采集中...</marquee></td>
    </tr>
  </table>
</div>
<!--以下是采集地址的内容-->
<fieldset style="margin:5px 50px 5px 50px; padding:10px; border-top:1px solid #ffffff; border-right:1px solid #000000;border-bottom:1px solid #000000;border-left:1px solid #ffffff;">
<legend>以下是采集到的新闻数据</legend>
<%
dim linkarr,getTitle,getContent,getHtml,j
if(act="add") then
	for jj=1 to Cint(application(taskname&"-NUM"))
		response.Write("·采集到的第"&jj&"条数据"&"<br>")
		response.Write("·标题："&application(taskname&"-title-"&jj)&"<br>")
		call SaveGetNews(catalogid,"","","",GatherInfo(0),application(taskname&"-title-"&jj),application(taskname&"-content-"&jj))'采集新闻入库并生成静态文件	
	next	
	application.Contents.RemoveAll()
else
	linkarr=application(taskname)
	j=0
	for i=0 to ubound(linkarr)
		if(i>5) then
			exit for
		end if
		if(linkarr(i)<>"") then
			FirstLinkUrl=linkarr(i)
			FirstLinkUrl=left(FirstLinkUrl,InStrRev(FirstLinkUrl,"/"))
			j=j+1		
			response.Write("·采集到的第"&j&"条数据"&"<br>")
			response.Write("·地址："&linkarr(i)&"<br>")
			if(ParseType=0) then
				getHtml=ReadHtml(linkarr(i),encode)
			else
				getHtml=RemoveBlank(RemoveHTML(ReadHtml(linkarr(i),encode)))
			end if		
			getTitle=GetBody(getHtml,TitleStartTag,TitleEndTag,false,false)		
			getContent=RemoveAdsCode(GetBody(getHtml,ContentStartTag,ContentEndTag,false,false))
			getContentLink=GetBody(getHtml,ContentLinkStart,ContentLinkEnd,false,false)		
			getContentLink=GetLink(getContentLink)
			CLinkArr=split(getContentLink,"|")
			if isarray(CLinkArr) then
				for k=0 to ubound(CLinkArr)-2
					if(instr(CLinkArr(k),"http")>0) then
						Nurl=CLinkArr(k)
					else
						Nurl=FirstLinkUrl&CLinkArr(k)
					end if
					response.Write(Nurl&"<br>")
					if(ParseType=0) then
						getHtml=ReadHtml(Nurl,encode)
					else
						getHtml=RemoveBlank(RemoveHTML(ReadHtml(Nurl,encode)))
					end if		
					getContent=getContent&"<br>$Page_Split$<br>"&RemoveAdsCode(GetBody(getHtml,ContentStartTag,ContentEndTag,false,false))
				next
				
			end if
			'getContent=GetBody(getHtml,ContentStartTag,ContentEndTag,false,false)
			application.Lock()
			application(taskname&"-title-"&j)=getTitle
			application(taskname&"-content-"&j)=getContent
			application(taskname&"-NUM")=j
			application.UnLock()
			response.Write("·标题："&getTitle&"<br>")
			response.Write("·链接："&FirstLinkUrl&getContentLink&"<br>")
			if(j>10) then
				response.Write("内容：...<br>")
			else
				response.Write("·内容：<br><textarea  cols=""70"" rows=""10"" >"&getContent&"</textarea><br>")
				response.Flush()
			end if		
		end if
	next
end if
%><br>
注：此处仅采集前10条记录，以便测试用途，采集更多数据请从“执行任务”中点击“采集”链接。
</fieldset>
<script language="javascript">
document.getElementById('Layer2').style.display='none';
</script>
<p align="center"><%=CStr(FormatNumber((Timer-BeginTime)*1000,2))& "毫秒"%></p>
<div id="Layer1" style="position:absolute; display:none; width:285px; height:159px; z-index:1; left: 353px; top: 110px; background-color: #003399; layer-background-color: #99FFFF; border: 1px none #000000;">
  <div align="center">
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p><font color="#FFFFFF">正在添加到库...</font></p>
  </div>
</div>
</body>
</html>
<%
'=================================================================================
'采集新闻入库并生成静态文件
'=================================================================================
function SaveGetNews(news_class,author,editor,newsfrom,templet,title,content)
'获得参数
classid=news_class
parentid=getparentid(news_class)
position="<a href="""&SiteUrl&""">"&SiteTitle&"</a>"&getPosition(classid,"-&gt;")

Dim i,file_name,TempletContent,current_time,lastid
if(application("TemplateCont")="") then
'获取模板内容
	set rs=server.CreateObject("adodb.recordset")  
    rs.open "select content from templet where id="&templet,conn,3,1
	if not rs.eof then
		TempletContent=rs("content")
		application("TemplateCont")=TempletContent
	else
		response.Write("请选择正确的模板！")
		response.End()
	end if
   rs.close
   set rs=nothing
else
	TempletContent=application("TemplateCont")
end if

  '读取文章ID
   set rs=server.CreateObject("adodb.recordset")
   rs.open "select top 1 id from news order by id desc",conn,3,1
   if not rs.eof then
   		lastid=Clng(rs(0))+1
   else
   		lastid=1
   end if 
   rs.close
   set rs=nothing  
	'读取栏目保存路径
	set rs=server.CreateObject("adodb.recordset")
   rs.open "select savedir,domainurl from catalogs where id="&catalogid,conn,3,1
   if not rs.eof then
   		catalogSaveDir=rs(0)
		catalogDomainUrl=rs(1)
   end if 
   rs.close
   set rs=nothing
   if(catalogSaveDir<>"") then
   		catalogSaveDirUrl=replace(catalogSaveDir,"/","\")
	else
		response.write("栏目保存路径设置错误,栏目ID:"&classid)
		response.end()
   end if
   
	'创建文件夹及文件	
	Set fs=Server.CreateObject("Scripting.FileSystemObject")
	'栏目文件夹
	folderCategory=server.MapPath("../")	
	current_time=now()
	'创建当天的文件夹
	current_M=chan_data(current_time)
	current_D=chan_day(current_time)
	folderStr=catalogSaveDirUrl&"\html\"&current_M&current_D
	folder_day=folderCategory&"\"&folderStr
	folderArr=split(folderStr,"\")
	for fi=0 to ubound(folderArr)
		tempFolder=tempFolder&"\"&folderArr(fi)
		If (fs.FolderExists(folderCategory&tempFolder)) Then	'目录是否存在

		Else
			fs.CreateFolder(folderCategory&tempFolder)		'创建目录
		End If
	next
	Randomize timer
	zNum = cint(8999*Rnd+1000)
	tempid=chan_data(current_time)&day(current_time)&zNum
	files=folderCategory&folderStr&"\"&tempid&"."&FileExt	
	file_path=catalogSaveDir&"/html/"&current_M&current_D&"/"&tempid&"."&FileExt	
	newsLinkUrl=catalogDomainUrl&"/html/"&current_M&current_D&"/"&tempid&"."&FileExt
	response.Write("Success!<br>文件已保存到："&files&"<br>")	
	'保存远程图片
	if(saveremoteimg=1) then
		'创建保存远程图片的文件夹
		If (fs.FolderExists(server.MapPath("../"&Imgsavedir)&"\"&current_M)) Then		'今月的目录是否存在

		Else
			fs.CreateFolder(server.MapPath("../"&Imgsavedir)&"\"&current_M)		'创建今月的目录
		End If
		'创建今天文件夹
		If (fs.FolderExists(server.MapPath("../"&Imgsavedir)&"\"&current_M&"\"&current_D)) Then		'今天的目录是否存在

		Else
			fs.CreateFolder(server.MapPath("../"&Imgsavedir)&"\"&current_M&"\"&current_D)		'创建今天的目录
		End If
		content=saveAndReplaceRemoteImg(content,server.MapPath("../"&Imgsavedir)&"\"&current_M&"\"&current_D&"\",current_M&"/"&current_D,tempid)
	end if
	'替换模板内容
	TempletContent=replace(TempletContent,"$position$",position)
	TempletContent=replace(TempletContent,"$title$",title)
	TempletContent=replace(TempletContent,"$keywords$",keywords)
	TempletContent=replace(TempletContent,"$moditime$",current_time)
	TempletContent=replace(TempletContent,"$newsfrom$",newsfrom)
	TempletContent=replace(TempletContent,"$author$",author)
	TempletContent=replace(TempletContent,"$editor$",editor)
	TempletContent=replace(TempletContent,"$content$",content)
	TempletContent=replace(TempletContent,"$id$",lastid)
	TempletContent=replace(TempletContent,"$classid$",classid)
	TempletContent=replace(TempletContent,"$PageUrl$",file_path)
	if(Instr(TempletContent,"$GetRelateNews$")<>0) then
		TempletContent=replace(TempletContent,"$GetRelateNews$",GetRelateNews(relate_key,"100%",22,10,"news","id","",20,"",0))
	end if
	TempletContent=replace(TempletContent,"$PrePage_NextPage$","")
	TempletContent=replace(TempletContent,"$PageSplitLists$","1")
	TempletContent=ReplaceAllLabel(TempletContent,classid)
	TempletContent=replace(TempletContent,"$SiteUrl$",SiteUrl)		
	Set file_w = fs.CreateTextFile(files)
	file_w.WriteLine(TempletContent)
	file_w.close
	set file_w=nothing
	set fs=nothing
'===================================================================
'                          添加到数据库
'===================================================================
	set rs=server.CreateObject("adodb.recordset") 
	rs.open "select top 1 * from news order by id desc",conn,3,3
	rs.addnew	
	rs("pagepath")=file_path
	rs("title")=title
	rs("keywords")=keywords
	rs("photo")=photo
	rs("content")=content
	rs("parentid")=parentid
	rs("classid")=classid
	rs("templetid")=templet
	rs("top1")=top1
	rs("totop")=top
	rs("tohot")=hot
	rs("author")=author
	rs("news_edit")=editor
	rs("news_from")=newsfrom
	rs("imgnews")=0
	rs("checked")=0
	rs("recommend")=0
	rs("moditime")=now()	
	rs("linkurl")=newsLinkUrl
	rs.update
	rs.close
	set rs=nothing
end function
call connclose()
%>
