<%
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/SaveRemoteFile.asp" -->
<!-- #include file="inc/function.asp" -->
<!-- #include file="inc/checkrole.asp" -->
<%
BeginTime=Timer
saveremoteimg=1
'''''''''''''''''''''''''''''''''''''''''''''''
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
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
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.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.update	
		rs.close
		set rs=nothing
	end if
end if
%>
<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="../admin/cs.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;
}
-->
</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>
<!--以下是采集地址的内容-->
<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&"_CollectNum"))'Cint(application(taskname&"-NUM"))
		response.Write("·采集到的第"&jj&"条数据"&"<br>")
		response.Write("·标题："&application(taskname&"-title-"&jj)&"<br>")
		call SaveGetNews(catalogid,"","","",GatherTempletId,application(taskname&"-title-"&jj),application(taskname&"-content-"&jj))'采集新闻入库并生成静态文件	
	next	
	application.Contents.RemoveAll()
	response.Write("<br><p></p><p>新闻成功采集，您可以选择:</p><p>")
	response.Write("<input type=""button"" value=""批量审核文章->"" onClick=""javascript:location.href='news_manage.asp'"">，")
	response.Write("<input type=""button"" value=""发布刚刚采集的新闻文章->"" onClick=""javascript:location.href='send_news.asp'""> (将采集的文章生成静态)</p>")
else
	linkarr=application(taskname)
	j=0
	for i=0 to ubound(linkarr)
		if(j>=cint(application(taskname&"_CollectNum"))) then
			exit for
		end if
		if(linkarr(i)<>"") then
		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))
		'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>")
		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
%>
</fieldset>
<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(conn.execute("select count(*) from news")(0)<1) then
   		lastid=1
	else
		lastid=conn.execute("select top 1 id from news order by id desc")(0)+1
   end if

'创建文件夹及文件
current_time=now
Set fs=Server.CreateObject("Scripting.FileSystemObject")
	'创建当天的文件夹
	current_M=chan_data(current_time)
	current_D=chan_day(current_time)
	folder_mon=server.mappath("../html")&"\"&current_M  '今月的目录
	folder_day=server.mappath("../html")&"\"&current_M&"\"&current_D  '今天的目录
	'创建本月文件夹
	If (fs.FolderExists(folder_mon)) Then		'今月的目录是否存在

	Else
		fs.CreateFolder(folder_mon)		'创建今月的目录
	End If
	'创建今天文件夹
	If (fs.FolderExists(folder_day)) Then		'今天的目录是否存在

	Else
		fs.CreateFolder(folder_day)		'创建今天的目录
	End If
	Randomize timer
	zNum = cint(8999*Rnd+1000)
	tempid=chan_data(current_time)&day(current_time)&zNum
	
	files=folder_day&"\"&tempid&"."&FileExt	
	file_path="html"&"/"&current_M&"/"&current_D&"/"&tempid&"."&FileExt		
	'保存远程图片
	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
	'替换模板内容	
	set fs=nothing
'===================================================================
'                          添加到数据库
'===================================================================
	set rs=server.CreateObject("adodb.recordset") 
	rs.open "select top 1 * from news where 1=0 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")=current_time	
	rs.update
	rs.close
	set rs=nothing
end function
function initStr(strs)
	Set objRegExp = New Regexp'设置配置对象
	objRegExp.IgnoreCase = True'忽略大小写
	objRegExp.Global = True'设置为全文搜索
	objRegExp.Pattern = "<img.+?>"'为了确保能准确地取出图片地址所以分为两层配置：首先找到里面的<IMG>标签，然后再取出里面的图片地址后面的getimgs函数就是实现后一个功能的。
	'objRegExp.Pattern = "<img.+?\.(gif|jpg|bmp|jpeg|psd|png|svg|dxf|wmf|tiff)>"
	Set Matches =objRegExp.Execute(strs)'开始执行配置
	For Each Match in Matches	
 		initStr = initStr &getimgs( Match.Value )'执行第二轮的匹配
	Next 
end function
function getimgs(str)
	getimgs=""
	Set objRegExp1 = New Regexp
	objRegExp1.IgnoreCase = True
	objRegExp1.Global = True
	objRegExp1.Pattern = "http://.+?\.(gif|jpg|bmp|jpeg|psd|png|svg|dxf|wmf|tiff)"'取出里面的地址
	set mm=objRegExp1.Execute(str)
	For Each Match1 in mm		
		getimgs=getimgs&"||"&Match1.Value'把里面的地址串起来备用
	next
end function
call connclose()
%>
