<!--#include file="admin_inc.asp"-->
<!--#include file="../inc/pinyin.asp"-->
<!--#include file="KuaiwanConfig.asp"-->
<%

'''''''''''''''''''''超时时间'''''''''''''''''''''
Server.ScriptTimeOut = 999999
'''''''''''''''''''''超时时间'''''''''''''''''''''

Dim objRootsite,objXML

'''''''''''''''''''''''''''文件加载方法文件'''''''''''''''''''''''''''

Init()

Function Init()
	If int(Request("t")) = 0 Then '首次采集
		FirstCaiJi()
	End If
	If int(Request("t")) = 1 Then '更新
		UpdateCaiJi()
	End If
	If int(Request("t")) = 2 Then '检索分类
		CheckClass()
	End If
End Function

'''''''''''''''''''''''''''加载xml'''''''''''''''''''''''''''

Function OpenXml(filepath)
	Set objXML = Server.CreateObject("Microsoft.XMLDOM")
	objXML.async = "false"
	objXML.resolveExternals = "false"
	objXML.setProperty "ServerHTTPRequest", true
	objXML.load(filepath)
	set objRootsite = objXML.documentElement
End Function

'''''''''''''''''''''''''''写配置文件'''''''''''''''''''''''''''
Function SetDocuments(path,last_update_id,game_abs_max_id)
	dim myfileobject,mytextfile
	set myfileobject = server.CreateObject("Scripting.FileSystemObject")
	set mytextfile = myfileobject.OpenTextFile(server.MapPath(path),2,True)
	mytextfile.WriteLine("<"&Chr(37))
	mytextfile.WriteLine("	'''''''''''''''''''''''''''''''''''''''''''快玩接口配置'''''''''''''''''''''''''''''''''''''''''''")
	mytextfile.WriteLine("  Dim game_last_update_id,game_classes,game_bulk,game_update,game_img_post,game_abs_max_id")
	mytextfile.WriteLine("	game_last_update_id = "&last_update_id)
	mytextfile.WriteLine("	game_abs_max_id = "&game_abs_max_id)
	mytextfile.WriteLine("	game_classes = ""http://113.12.80.166:8000/game/classes/""")
	mytextfile.WriteLine("	game_bulk = ""http://113.12.80.166:8000/game/info/bulk/?min_id={min_id}&max_id={max_id}""")
	mytextfile.WriteLine("	game_update = ""http://113.12.80.166:8000/game/info/update/?last_update_id={game_last_update_id}&limit=30""")
	mytextfile.WriteLine("	game_img_post = ""http://yximg.hacow.cn/""")
	mytextfile.WriteLine("	")
	mytextfile.WriteLine("	'''''''''''''''''''''''''''''''''''''''''''快玩接口配置'''''''''''''''''''''''''''''''''''''''''''")
	mytextfile.WriteLine(Chr(37)&">")
	mytextfile.close
	set myfileobject = Nothing
End Function

'''''''''''''''''''''''''''检索分类'''''''''''''''''''''''''''
Function CheckClass()
	dim i,j,tempclassvalue,insertsql
	OpenXml(game_classes)
	If objRootsite.childNodes.length > 0 Then
		For i = 0 To objRootsite.childNodes.length - 1
			If objRootsite.childNodes.item(i).childNodes.item(0).text = "flash" Then
				For j = 0 To objRootsite.childNodes.item(i).childNodes.item(1).childNodes.length - 1
					tempclassvalue = GetClassId(objRootsite.childNodes.item(i).childNodes.item(1).childNodes.item(j).text)
					If tempclassvalue = "" Then
						insertsql = "Insert into {pre}type(m_name,m_enname,m_sort,m_upid,m_hide,m_template,m_keyword,m_description,m_type,m_subtemplate)"
						insertsql = insertsql & ("values('"&ClassName&"','',0,0,0,'channel.html','','',0,'content.html')")
						conn.db insertsql,"execute"
					End If
				Next
				Response.Write("检索成功.")
				Response.End()
			End If
		Next
	Else
		Response.Write("分类目录为空.")
		Response.End()
	End If
	Response.End()
End Function

'''''''''''''''''''''''''''更新数据'''''''''''''''''''''''''''
Function UpdateCaiJi()

	Dim page,i,j,temp_game_last_update_id,temp_this_update_id,m_name,m_typename,m_pic,m_des,m_playdata,m_keyword,m_lang,m_gamedes,m_recycle,m_shotimg,m_xml_id,tem_pic,temp_playdate,temp_shotimg,temp_game_abs_max_id,temp_game_this_update_id,m_commend,temp_game_update
	
	temp_game_last_update_id = game_last_update_id
	temp_game_update = game_update
	
	Response.Write("正在执行更新程序 <br /> <br />")
	Response.Flush()
	Response.Write("正在加载xml <br /> <br />")
	Response.Flush()
	
	game_update = replace(game_update,"{game_last_update_id}",temp_game_last_update_id)
	OpenXml(game_update)
	
	Response.Write("加载xml结束 <br /> <br /> <br />")
	If clng(temp_game_this_update_id) <> clng(objRootsite.childNodes.item(1).Text) then
		If Request.QueryString("update") <> "" Then
			game_last_update_id = objRootsite.childNodes.item(1).Text
			
			game_bulk = replace(game_bulk,"{min_id}",1)
			game_bulk = replace(game_bulk,"{max_id}",1)

			OpenXml(game_bulk)
			
			temp_game_abs_max_id = objRootsite.childNodes.item(0).Text
			
			Response.Write("正在修改配置文件 <br /> <br />")
			Response.Flush()
			SetDocuments "/admin/KuaiwanConfig.asp",game_last_update_id,temp_game_abs_max_id
			Response.Write("修改配置文件结束 <br /> <br />")
			Response.Flush()
		End If
		
		If Request.QueryString("page") <> "" then
			page = clng(Request.QueryString("page"))
			If page > temp_game_last_update_id Then
				Response.Write("更新完毕")
				Response.End()
			Else
				page = clng(Request.QueryString("page"))
			End If
		Else
			page = temp_game_last_update_id
		End If
		temp_game_update = replace(temp_game_update,"{game_last_update_id}",page)
		OpenXml(temp_game_update)
		
			For i = 0 To objRootsite.childNodes.item(2).childNodes.length -1
				'''''''''''''''''读取xml数据'''''''''''''''''
				m_xml_id = objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(0).text
				
				m_name = objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(1).text
				
				m_recycle = objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(16).text
				
				If objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(20).childNodes.length > 0 then
					
					If objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(20).childNodes.item(0).childNodes.length > 1 then
						tem_pic =  SaveRemoteFile(objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(20).childNodes.item(0).childNodes.item(1).childNodes.item(1).childNodes.item(0).text)
					Else
						tem_pic = "/Uploads/notimages.jpeg"
					End If
				Else
					tem_pic = "/Uploads/notimages.jpeg"
				End If
				
				m_pic = tem_pic
				
				If objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(9).text <> "" then
					temp_playdate = "qvod://" & objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(9).text & "|"
				Else
					temp_playdate = "qvod://10253620|"
				End If
				If objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(7).text <> "" then
					temp_playdate = temp_playdate & objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(7).text & "|"
				Else
					temp_playdate = temp_playdate & "|"
					m_recycle = "false"
				End If
				
				If m_recycle = "true" Then
					m_recycle = 0
					m_typename = GetClassId(objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(5).text)
					If m_typename = "" Then
						m_recycle = 1
						m_typename = 0
					End If
				Else
					m_recycle = 1
					m_typename = 0
				End If
				
				temp_playdate = temp_playdate & objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(0).text & ".rar|"
				m_playdata = temp_playdate
				
				m_keyword = objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(4).text
				
				m_lang = objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(9).text
				
				m_gamedes = objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(3).text
				
				m_shotimg = ""
				
				If objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(21).childNodes.length > 0 then
					For j = 0 To objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(21).childNodes.length - 1
						temp_shotimg = SaveRemoteFile(objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(21).childNodes.item(j).childNodes.item(0).text)
						
						m_shotimg = m_shotimg & temp_shotimg & ","
					Next
				Else
					m_shotimg = ""
				End If
				
				If objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(12).text <> "" Then
					m_commend = cint(objRootsite.childNodes.item(2).childNodes.item(i).childNodes.item(12).text / 2)
				Else
					m_commend = 0
				End If
				
				'''''''''''''''''读取xml数据'''''''''''''''''
				
				If IdIsExisit(m_xml_id) <> 0 Then
					'''''''''''''''''修改'''''''''''''''''
					UpdateGame m_name,m_typename,m_pic,m_des,m_playdata,m_keyword,m_lang,m_gamedes,m_recycle,m_shotimg,m_xml_id,m_commend
					Response.Write("<font style=""color:red;"">" & m_name & "-------已修改</font> <br /> <br />")
					Response.Flush()
				Else
					'''''''''''''''''添加'''''''''''''''''
					AddGame m_name,m_typename,m_pic,m_des,m_playdata,m_keyword,m_lang,m_gamedes,m_recycle,m_shotimg,m_xml_id,m_commend
					Response.Write("<font style=""color:red;"">" & m_name & "-------已入库</font> <br /> <br />")
					Response.Flush()
				End If
			Next

			Response.Write("<script language=""javascript"" type=""text/javascript"">location.href=""Kuaiwan.asp?t=1&page="&(page + 30)&""";</script>")
		
	Else
		Response.Write("无需更新")
	End If
	
	
	
End Function


'''''''''''''''''''''''''''首次采集'''''''''''''''''''''''''''

Function FirstCaiJi()
	If game_abs_max_id = 0 or request.QueryString("page") <> "" Then
		Dim page,i,j,m_name,m_typename,m_pic,m_des,m_playdata,m_keyword,m_lang,m_gamedes,m_recycle,m_shotimg,m_xml_id,tem_pic,m_commend,temp_playdate,temp_shotimg,last_update_id,temp_game_abs_max_id,temp_game_bulk
			Response.Write("正在执行首次采集 <br /> <br />")
			
			Response.Write("正在加载xml <br /> <br />")
			Response.Flush()
			game_update = replace(game_update,"{game_last_update_id}",game_last_update_id)
			
			OpenXml(game_update)	''加载xml
			
			last_update_id = objRootsite.childNodes.item(1).Text
			temp_game_bulk = game_bulk
			game_bulk = Replace(game_bulk,"{min_id}",1)
			game_bulk = Replace(game_bulk,"{max_id}",1)
			
			OpenXml(game_bulk)
			temp_game_abs_max_id = objRootsite.childNodes.item(0).Text
			
			Response.Write("加载xml结束 <br /> <br /> <br />")
			If Request.QueryString("update") <> "" Then
				Response.Write("正在修改配置文件 <br /> <br />")
				Response.Flush()
			
				SetDocuments "/admin/KuaiwanConfig.asp",last_update_id,temp_game_abs_max_id		''读取this_update_id,并写入配文件
				
				Response.Write("修改配置文件结束 <br /> <br />")
				Response.Flush()
			End If
	
			If Request("page") <> "" then
				page = clng(Request.QueryString("page"))
				temp_game_abs_max_id = clng(temp_game_abs_max_id)
				If temp_game_abs_max_id > page Then
					temp_game_bulk = Replace(temp_game_bulk,"{min_id}",page)	
					temp_game_bulk = Replace(temp_game_bulk,"{max_id}",(30 + page))	
				Else
					Response.Write("采集完成.")
					Response.End()
				End If
			Else
				page = 1
				temp_game_bulk = Replace(temp_game_bulk,"{min_id}",page)
				temp_game_bulk = Replace(temp_game_bulk,"{max_id}",(30 + page))			
			End If
			
			OpenXml(temp_game_bulk)
				
				For i = 1 To objRootsite.childNodes.length - 1
					If objRootsite.childNodes.item(i).childNodes.item(6).text = "flash" Then
						'''''''''''''''''读取xml数据'''''''''''''''''
						m_xml_id = objRootsite.childNodes.item(i).childNodes.item(0).text
						
						m_name = objRootsite.childNodes.item(i).childNodes.item(1).text
						
						m_recycle = objRootsite.childNodes.item(i).childNodes.item(16).text
						
						If objRootsite.childNodes.item(i).childNodes.item(20).childNodes.length > 0 then
							If objRootsite.childNodes.item(i).childNodes.item(20).childNodes.item(0).childNodes.length > 1 then
								tem_pic =  SaveRemoteFile(objRootsite.childNodes.item(i).childNodes.item(20).childNodes.item(0).childNodes.item(1).childNodes.item(1).childNodes.item(0).text)
							Else
								tem_pic = "/Uploads/notimages.jpeg"
							End If
						Else
							tem_pic = "/Uploads/notimages.jpeg"
						End If
						
						m_pic = tem_pic
						
						m_des = objRootsite.childNodes.item(i).childNodes.item(2).text
						
						If objRootsite.childNodes.item(i).childNodes.item(9).text <> "" then
							temp_playdate = "qvod://" & objRootsite.childNodes.item(i).childNodes.item(9).text & "|"
						Else
							temp_playdate = "qvod://10253620|"
						End If
						If objRootsite.childNodes.item(i).childNodes.item(7).text <> "" then
							temp_playdate = temp_playdate & objRootsite.childNodes.item(i).childNodes.item(7).text & "|"
						Else
							temp_playdate = temp_playdate & "|"
							m_recycle = "false"
						End If
						
						If m_recycle = "true" Then
							m_recycle = 0
							m_typename = GetClassId(objRootsite.childNodes.item(i).childNodes.item(5).text)
							If m_typename = "" Then
								m_recycle = 1
								m_typename = 0
							End If
						Else
							m_recycle = 1
							m_typename = 0
						End If
						
						temp_playdate = temp_playdate & objRootsite.childNodes.item(i).childNodes.item(0).text & ".rar|"
						
						m_playdata = temp_playdate
						
						m_keyword = objRootsite.childNodes.item(i).childNodes.item(4).text
						
						m_lang = objRootsite.childNodes.item(i).childNodes.item(9).text
						
						m_gamedes = objRootsite.childNodes.item(i).childNodes.item(3).text
						
						m_shotimg = ""
						
						If objRootsite.childNodes.item(i).childNodes.item(12).text <> "" Then
							m_commend = cint(objRootsite.childNodes.item(i).childNodes.item(12).text / 2)
						Else
							m_commend = 0
						End If
						
						If objRootsite.childNodes.item(i).childNodes.item(21).childNodes.length > 0 then
							For j = 0 To objRootsite.childNodes.item(i).childNodes.item(21).childNodes.length - 1
								temp_shotimg = SaveRemoteFile(objRootsite.childNodes.item(i).childNodes.item(21).childNodes.item(j).childNodes.item(0).text)
							
								m_shotimg = m_shotimg & temp_shotimg & ","
							Next
						Else
							m_shotimg = ""
						End If
						'''''''''''''''''读取xml数据'''''''''''''''''
						
						'''''''''''''''''入库'''''''''''''''''
						
						AddGame m_name,m_typename,m_pic,m_des,m_playdata,m_keyword,m_lang,m_gamedes,m_recycle,m_shotimg,m_xml_id,m_commend
						'''''''''''''''''入库'''''''''''''''''
						Response.Write("<font style=""color:red;"">" & m_name & "-------已入库</font> <br /> <br />")
						Response.Flush()
					End If
				Next
				Response.Write("<script language=""javascript"" type=""text/javascript"">location.href=""Kuaiwan.asp?t=0&page="&(page + 30)&""";</script>")
			Response.End()
	Else
		Response.Write("已执行过首次采集命令,请执行更新命令.")
		Response.End()
	End If
End Function

'''''''''''''''''''''''''''添加游戏'''''''''''''''''''''''''''
Function AddGame(m_name,m_typename,m_pic,m_des,m_playdata,m_keyword,m_lang,m_gamedes,m_recycle,m_shotimg,m_xml_id,m_commend)
	Dim insertSql
	
	m_name = replace(m_name,"'","''")
	m_pic = replace(m_pic,"'","''")
	m_des = replace(m_des,"'","''")
	m_keyword = replace(m_keyword,"'","''")
	m_gamedes = replace(m_gamedes,"'","''")
	m_shotimg = replace(m_shotimg,"'","''")
	
	insertSql = "insert into {pre}data(m_name,m_type,m_state,m_pic,m_hit,m_digg,m_actor,m_note,m_des,m_topic,m_color,m_commend,m_wrong,m_addtime,m_publishyear,m_publisharea,m_playdata,m_downdata,m_isunion,m_letter,m_keyword,m_tread,m_director,m_lang,m_dayhit,m_weekhit,m_monthhit,m_enname,m_datetime,m_recycle,m_score,m_gamedes,m_shotimg,m_xml_id) values('"&m_name&"',"&m_typename&",0,'"&m_pic&"',0,0,'','','<p>"&m_des&"</p>',0,'',"&m_commend&",0,'"&now()&"',0,'','快玩$$第1集$"&m_playdata&"$kuaiwan','',0,'','"&m_keyword&"',0,'',"&m_lang&",0,0,0,'','"&now()&"',"&m_recycle&",0,'<p>"&m_gamedes&"</p>','"&m_shotimg&"',"&m_xml_id&")"
	conn.db insertSql,"execute"
End Function

'''''''''''''''''''''''''''修改游戏'''''''''''''''''''''''''''

Function UpdateGame(m_name,m_typename,m_pic,m_des,m_playdata,m_keyword,m_lang,m_gamedes,m_recycle,m_shotimg,m_xml_id,m_commend)
	Dim updateSql
	
	m_name = replace(m_name,"'","''")
	m_pic = replace(m_pic,"'","''")
	m_des = replace(m_des,"'","''")
	m_keyword = replace(m_keyword,"'","''")
	m_gamedes = replace(m_gamedes,"'","''")
	m_shotimg = replace(m_shotimg,"'","''")
	
	updateSql = "update m_data set m_name = '"&m_name&"',m_datetime = '"&now()&"',m_type = "&m_typename&",m_pic = '"&m_pic&"',m_des = '"&m_des&"',m_playdata = '快玩$$第1集$"&m_playdata&"$kuaiwan',m_keyword = '"&m_keyword&"',m_lang = "&m_lang&",m_gamedes = '"&m_gamedes&"',m_recycle = "&m_recycle&",m_shotimg = '"&m_shotimg&"' where m_xml_id = "&m_xml_id
	
	conn.db updateSql,"execute"
End Function

'''''''''''''''''''''''''''查询此ID是否存在'''''''''''''''''''''''''''
Function IdIsExisit(xml_id)
	dim sql,rsObj
	sql = "Select count(*) as [xml_id_count] from {pre}data where m_xml_id = " & xml_id
	set rsObj = conn.db(sql,"records1")
	If Not rsObj.eof Then
		IdIsExisit = rsObj("xml_id_count")
	Else
		IdIsExisit = 0
	End If
End Function


'''''''''''''''''''''''''''返回分类ID 如不存则添加'''''''''''''''''''''''''''
Function GetClassId(ClassName)
	dim Sql,rsObj,InsertSql,SelectSql,rs
	If ClassName <> "" Then
		Sql = "Select m_id from m_type where m_name = '"&ClassName&"'"
		set rsObj = conn.db(Sql,"records1")
		If Not rsObj.eof Then
			GetClassId = rsObj("m_id")
		Else
			GetClassId = ""
		End If
	Else
		GetClassId = ""
	End If
End Function

'''''''''''''''''''''''''''保存本地图片'''''''''''''''''''''''''''

Function SaveRemoteFile(RemoteFileUrl) 
	dim Ads,Retrieval,GetRemoteData,LocalFileName,rannum,strDate,SuffixName,imgurl,e
	Getname()
	Call GetSuffixName(RemoteFileUrl,SuffixName)
	strDate = Date()
	strDate = split(strDate,"-")
	LocalFileName = strDate(0) & "/" & strDate(1) & "/" & strDate(2) & "/" & Getname & SuffixName
	e = POSTURLGB(game_img_post&"save_img.asp?saveimgpath="&LocalFileName&"&imgpath="&RemoteFileUrl&"",imgurl)
	if e = "error" then
		SaveRemoteFile = RemoteFileUrl
	else 
		SaveRemoteFile = e
	end if
End Function  

'''''''''''''''''''''''''''生成图片目录'''''''''''''''''''''''''''
Function Getname()
	Dim y, m, d, h, mm, S, r
	Randomize
	y = Year(Now)
	m = Month(Now): If m < 10 Then m = "0" & m
	d = Day(Now): If d < 10 Then d = "0" & d
	h = Hour(Now): If h < 10 Then h = "0" & h
	mm = Minute(Now): If mm < 10 Then mm = "0" & mm
	S = Second(Now): If S < 10 Then S = "0" & S
	r = 0
	r = CInt(Rnd() * 1000)
	If r < 10 Then r = "00" & r
	If r < 100 And r >= 10 Then r = "0" & r
	Getname = y & m & d & h & mm & S & r
End Function

'''''''''''''''''''''''''''生成图片名称'''''''''''''''''''''''''''
Function GetSuffixName(str,SuffixName)
	Dim lname,la,num,lb,num2
	lname=str
	la=split(lname,"/")
	num=ubound(la)
	lb=split(la(num),".")
	num2=ubound(lb)
	SuffixName = "." & lb(num2)
End Function

'''''''''''''''''''''''''''模拟post方法'''''''''''''''''''''''''''
Function POSTURLGB(urlstr,imgurl)
Dim url,txt,https,str
url=split(urlstr,"?")(0)
txt=split(urlstr,"?")(1)
Set https = Server.CreateObject("MSXML2.XMLHTTP") 
With https 
.Open "Post", url, False
.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
.Send txt
str = bytesToBSTR(.ResponseBody,"GB2312")
End With 
Set https = Nothing
POSTURLGB=str
End Function


Function BytesToBstr(body,Cset)
  dim objstream,str
  set objstream = Server.CreateObject("adodb.stream")
  objstream.Type = 1
  objstream.Mode =3
  objstream.Open
  objstream.Write body
  objstream.Position = 0
  objstream.Type = 2
  objstream.Charset = Cset
  str = objstream.ReadText 
  objstream.Close
  set objstream = nothing
  BytesToBstr=str
End Function

%>