<%
Response.Buffer = True 
Server.ScriptTimeOut=999999
Response.Expires = -1
Response.ExpiresAbsolute = Now() - 1 
Response.Expires = 0 
Response.CacheControl = "no-cache"
response.Charset="gb2312"
%>
<!--#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/checkrole.asp" -->
<!-- #include file="inc/function.asp" -->
<%
'''''''''''''''''''''''''''''''''''''''''''''''
call checkPrevilege(Session("Name"),"p09")
'''''''''''''''''''''''''''''''''''''''''''''''
dim taskid,id,linkurl
current_time=now()
'获得任务和链接地址ID
if not isempty(request("taskid")) then 
	taskid=Cint(request("taskid"))
else
	response.Write("<script>alert(""错误的任务ID号"");</script>")
	response.End()
end if
if not isempty(request("id")) then 
	id=Clng(request("id"))
else
	response.Write("<script>alert(""错误的采集地址ID"");</script>")
	response.End()
end if
'取出对应的链接URL
dim rs,getHtml,getTitle,getContent,newsContent,getContentLink,FirstLinkUrl,ALinkArr,Nurl
set rs=server.CreateObject("adodb.recordset") 
rs.open "select linkurl from collect_his where iscollected=0 and taskid="&taskid&" and id="&id,conn,3,1		
'获得参数
if not rs.eof then
	linkurl=rs("linkurl")
	rs.close
	set rs=nothing
else
	response.Write("没有记录")
	rs.close
	set rs=nothing
	call connclose()
	response.End()
end if
if(instr(linkurl,"http")=0) then
	response.Write("错误的地址格式:"&linkurl)
	response.End()
end if
FirstLinkUrl=linkurl
FirstLinkUrl=left(FirstLinkUrl,instr(FirstLinkUrl,"/"))
if(application("ParseType")=0) then
	getHtml=ReadHtml(linkurl,application("Encode"))
else
	getHtml=RemoveBlank(RemoveHTML(ReadHtml(linkurl,application("Encode"))))
end if		
getTitle=GetBody(getHtml,application("TitleStartTag"),application("TitleEndTag"),false,false)		
getTitle=replace(getTitle,"购房专用名词详解－","")
getTitle=replace(getTitle,"购房专用名词详解——","")
getContent=RemoveAdsCode(GetBody(getHtml,application("ContentStartTag"),application("ContentEndTag"),false,false))

getContentLink=GetBody(getHtml,application("ContentLinkStart"),application("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
				if(application("ParseType")=0) then
					getHtml=ReadHtml(Nurl,application("Encode"))
				else
					getHtml=RemoveBlank(RemoveHTML(ReadHtml(Nurl,application("Encode"))))
				end if		
				getContent=getContent&"<br>$Page_Split$<br>"&RemoveAdsCode(GetBody(getHtml,application("ContentStartTag"),application("ContentEndTag"),false,false))
			next
			
		end if
newsContent=getContent
newsContent=replace(newsContent,"0518home资讯：","")
newsContent=replace(newsContent,"src=""/","src="""&application("CollectUrl")&"/")
newsContent=replace(newsContent,"src='/","src='"&application("CollectUrl")&"/")
if(newsContent<>"") then
	if(GatherInfo(2)<>"") then
		newsContent=replace(newsContent,GatherInfo(2),GatherInfo(3))
	end if
end if

if(getTitle="$False$" or getContent="$False$") then
	response.Write("没有采集到数据")
	response.End()
end if
'response.Write("linkUrl:"&linkurl&"<br>"&Chr(10))
'response.Write("Title:"&getTitle&"<br>"&Chr(10))
'response.Write("Content:"&getContent&"<br>"&Chr(10))
'call SaveGetNews(application(catalogid),"","","",1,getTitle,getContent)
'=================================================================================
'采集新闻入库并生成静态文件
'=================================================================================
'function SaveGetNews(news_class,author,editor,newsfrom,templet,title,content)
dim catalogid,parentid,position,i,file_name,TempletContent,current_time,lastid
'获得参数
catalogid=Cint(application("catalogid"))
parentid=getparentid(catalogid)
position="<a href="""&SiteUrl&""">"&SiteTitle&"</a>"'&getPosition(catalogid,"-&gt;")

templet=GatherInfo(0)
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_M=chan_data(current_time)
	current_D=chan_day(current_time)
	folderStr=catalogSaveDirUrl&"\"&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&"/"&current_M&current_D&"/"&tempid&"."&FileExt	
	newsLinkUrl=catalogDomainUrl&"/"&current_M&current_D&"/"&tempid&"."&FileExt
		
	'保存远程图片
	if(GatherInfo(1)=true) then
		response.write("save pic")
		'创建保存远程图片的文件夹
		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
		newsContent=saveAndReplaceRemoteImg(newsContent,server.MapPath("../"&Imgsavedir)&"\"&current_M&"\"&current_D&"\",current_M&"/"&current_D,tempid)
	end if
	'替换模板内容	
	'response.Write(TempletContent)
	TempletContent=replace(TempletContent,"$title$",getTitle)
	TempletContent=replace(TempletContent,"$keywords$",getTitle)
	TempletContent=replace(TempletContent,"$moditime$",now)
	TempletContent=replace(TempletContent,"$subhead$","")
	TempletContent=replace(TempletContent,"$newsfrom$","")
	TempletContent=replace(TempletContent,"$author$","")
	TempletContent=replace(TempletContent,"$editor$","")
	TempletContent=replace(TempletContent,"$content$",newsContent)
	TempletContent=replace(TempletContent,"$id$",lastid)
	TempletContent=replace(TempletContent,"$catalogid$",catalogid)
	TempletContent=replace(TempletContent,"$PageUrl$",file_path)
	if(Instr(TempletContent,"$GetRelateNews$")<>0) then
		TempletContent=replace(TempletContent,"$GetRelateNews$",GetRelateNews("","100%",22,10,"news","id","",20,"",0))
	end if
	TempletContent=replace(TempletContent,"$PrePage_NextPage$","")
	TempletContent=replace(TempletContent,"$PageSplitLists$","1")
	TempletContent=ReplaceAllLabel(TempletContent,catalogid)
	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
	response.Write("Success!<br>文件已保存到："&files&"<br>")
'===================================================================
'                          添加到数据库
'===================================================================
	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")=getTitle
	rs("keywords")=keywords
	rs("photo")=""
	rs("content")=newsContent
	rs("parentid")=parentid
	rs("classid")=catalogid
	rs("templetid")=templet
	rs("top1")=0
	rs("totop")=0
	rs("tohot")=0
	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
	conn.execute("update collect_his set iscollected=1 where taskid="&taskid&" and id="&id)
'end function
call connclose()
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
%>
