
local json=require "json"
local com=require "res.util.comm"
spiderCode="js_jsszbtbggfwpt_zhbjggs";
spiderName="江苏省招标投标公共服务平台";
spiderChannel="中标结果公示";
spiderDownDetailPage=true;
spiderStartPage=1;
spiderMaxPage=5;
spiderRunRate=30;
spider2Collection="bidding";
spiderPageEncoding="utf8";
spiderStoreMode=2;
spiderStoreToMsgEvent=4002;
spiderTargetChannelUrl="http://www.jszbtb.com/#/announcedetail/0";
spiderLastDownloadTime="2018-10-12 13:34:24";
spiderIsHistoricalMend=false;
spiderIsMustDownload=false;
spiderUserName="lyf";
spiderUserEmail="liuyifan@topnet.net.cn";
spiderUploadTime="2021-03-11 17:19:52";
spiderCoverAttr="title";
spiderSleepBase=1000;
spiderSleepRand=5000;
spiderTimeout=150;
spiderIsFlow=1; 
			
function getLastPublishTime()
	local timeType="yyyyMMdd"
	if timeType=="yyyyMMdd" or timeType=="MMdd" then
		return com.nowDate()
	end
	local content = download("",{})
	local tmp = findOneText("",content)
	local lastpushtime=com.parseDate(tmp,timeType)
	return lastpushtime
end
	
			function downloadAndParseListPage(pageno)
	for i=1,5 do 	--5次下载任务不成功，退出
		local update=""
		local page={}		
		local href="http://api.jszbtb.com/DataSyncApi/HomeWinBidBulletin?PageSize=20&CurrentPage="..pageno  --列表页通用地址
		local hrefs={''}--固定列表配置
		 
		local content = download(href,{})
		content =json.decode(content).data.data
		
		if content~=nil then
			for k, v in pairs(content) do
				local item={}
				item["href"]=v.id  --信息地址
				item["title"]=v.bulletinName  --信息标题
				item["publishtime"]=v.create_time  --信息时间
				if item["title"]~=nil and item["title"]~="" then
					item["title"]=com.trim(item["title"])
					local timeType="yyyyMMddHHmmss"
					item["publishtime"] = regexp(item["publishtime"],"((\\d+)-(\\d+)-(\\d+)T(\\d+):(\\d+):(\\d+))",2)[1]
					item["publishtime"] = replace(item["publishtime"],"T"," ")
					item["publishtime"]=com.parseDate(item["publishtime"],timeType)--时间格式
					item["href"]="http://www.jszbtb.com/#/bulletindetail/WinBidBulletin/"..item["href"]
					item["spidercode"]="js_jsszbtbggfwpt_zhbjggs";item["site"]="江苏省招标投标公共服务平台";item["channel"]="中标结果公示"; --通用数据补充
					item["publishdept"]="";item["type"]="";item["area"]="江苏";item["city"]="";  --模型数据补充
			        table.insert(page,item) 
				end	
			end
		end
		if table.getn(page)>0 then
			return page
		end
	end
end
			function downloadDetailPage(data)
	local update=""
	for i=1,5 do 	--5次下载任务不成功，退出
		local id = regexp(data["href"],"WinBidBulletin/(.+)",2)[1]
		local link = "http://api.jszbtb.com/DataSyncApi/WinBidBulletin/id/"..id
		local content = download(link,{})
		content = json.decode(content).data.data[1]
		content = content.bulletincontent
		data["detail"]=findContentText("body",content)
		data["contenthtml"]=findOneHtml("body",content)
		
		data["jsondata"] = {
			["toptype"] = "结果",
			["subtype"] = "中标"
		}
		
		data["l_np_publishtime"]=com.strToTimestamp(data["publishtime"])
		data["_d"]="comeintime"
		local checkAttr={"title","href","publishtime","detail","contenthtml"}
		local b,err=com.checkData(checkAttr,data)
		if b then
			return data
		else
			timeSleep(60)--延时60秒再次请求
			if i==5 then
				--print("下载失败")
				saveErrLog(spiderCode,spiderName,data["href"],err)
			end
		end
	end
end