package com.tidemedia.cutv; import java.io.*; import java.sql.ResultSet; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.ArrayList; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import tidemedia.cms.base.MessageException; import tidemedia.cms.base.TableUtil; import tidemedia.cms.system.Channel; import tidemedia.cms.system.CmsCache; import tidemedia.cms.system.Document; import tidemedia.cms.system.Site; import tidemedia.cms.util.FileUtil; import tidemedia.cms.util.Util; public class ExportSiteMapXMLDate { public ExportSiteMapXMLDate() { } public void emportXml() throws MessageException, SQLException, JSONException { // 3:主站内容中心 6617:精品栏目 8800:新媒资 16350:新闻的内容中心 String content = "[{\"channelid\":\"3,6617,8800\"},{\"channelid\":\"16350\"}]"; JSONArray ja = new JSONArray(content); for(int i=0 ;i<ja.length();i++){ JSONObject jb =ja.getJSONObject(i); String channelid = jb.getString("channelid"); emportXml_(channelid); } } public void emportXml_(String channelids) throws MessageException, SQLException { String channelid[] = channelids.split(","); //获取channelid[0] 来安排存放sitemap文件的位置 Channel channel = CmsCache.getChannel(Util.parseInt(channelid[0])); long t1 = System.currentTimeMillis(); String xmlfilepath = channel.getSite().getSiteFolder(); if (!xmlfilepath.endsWith("/")) { xmlfilepath = xmlfilepath + "/"; } if (!xmlfilepath.startsWith("/")) { xmlfilepath = "/" + xmlfilepath; } String xmlfile = xmlfilepath + "cutvsitemap.xml"; String strxml = ""; try { strxml = (new StringBuilder(String.valueOf(strxml))).append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n").toString(); strxml = (new StringBuilder(String.valueOf(strxml))).append( "<urlset>").toString(); strxml = (new StringBuilder(String.valueOf(strxml))).append( "<url>").toString(); long now = System.currentTimeMillis(); // 现在经过的毫秒数 long time = now / 1000;// 现在经过的秒数 long time2 = time - 2 * 24 * 60 * 60;// 少两天的天数 for (int i = 0; i < channelid.length; i++) { TableUtil tu = new TableUtil(); Channel ch = CmsCache.getChannel(Util.parseInt(channelid[i])); SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd"); String datetime = tempDate.format(new java.util.Date()); //循环内容页面--------精品栏目中没有内容页 所以 !6617 if(ch.getId()!=6617){ String sql = "select GlobalID from " + ch.getTableName() + " where Status=1 and ChannelCode like '"+ch.getChannelCode()+"%' and CreateDate>" + time2+" order by PublishDate desc"; ResultSet rs = tu.executeQuery(sql); Document doc = null; while (rs.next()) { int globalid = rs.getInt("GlobalID"); doc = new Document(globalid); strxml = (new StringBuilder(String.valueOf(strxml))) .append("<loc>").append(doc.getHttpHref()).append( "</loc>").append("<lastmod>").append(doc.getPublishDate().substring(0, 10)).append("</lastmod>").append("<changefreq>").append("daily").append("</changefreq>").append("<priority>").append("0.8").append("</priority>").toString(); } tu.closeRs(rs); } //循环列表页------------循环所有频道的列表页面 ArrayList<Integer> list =new ArrayList<Integer>(); if(ch.getId()!=8800){ String sql = "select id,ChannelCode from channel where ChannelCode like '"+ch.getChannelCode()+"%'"; ResultSet rs = tu.executeQuery(sql); while (rs.next()) { int id = rs.getInt("id"); String code =rs.getString("ChannelCode"); if(id!=16847){ if(code.indexOf("3625_178_6617_6618_8331_")<0){//去掉视频组播单16847 ,去掉年代秀 list.add(id); } } } tu.closeRs(rs); } /** * 获取精品栏目内容页中TVNumber放到list中 */ if(ch.getId()==6617){ TableUtil tu2 = new TableUtil(); String sql2 = "select TVNumber from "+ch.getTableName()+" where Status=1";//精品栏目中年代秀的channelcode ResultSet rs = tu2.executeQuery(sql2); while(rs.next()){ String tvn = rs.getString("TVNumber"); System.out.println("tvn---------->"+tvn); list.add(Util.parseInt(tvn)); } } for(int k:list){ Channel chk = CmsCache.getChannel(k); String fullpath = chk.getFullPath(); String url = chk.getSite().getExternalUrl(); /** * fullpath中有“/”结尾的 也有没有"/"结尾的 做一下判断 */ fullpath = fullpath.replace("/jplm/index", "/jplm/"); if(!fullpath.endsWith("/")){ fullpath+="/"; } if(fullpath.indexOf("/v/")>=0){ continue; } strxml = (new StringBuilder(String.valueOf(strxml))) .append("<loc>").append(url).append(fullpath).append("index.shtml").append("</loc>").append("<lastmod>").append(datetime).append("</lastmod>").append("<changefreq>").append("daily").append("</changefreq>").append("<priority>").append("0.9").append("</priority>").toString(); } } strxml = (new StringBuilder(String.valueOf(strxml))).append( "</url>").toString(); strxml = (new StringBuilder(String.valueOf(strxml))).append( "</urlset>").toString(); PrintWriter out1 = new PrintWriter(new BufferedWriter( new FileWriter(xmlfile))); out1.write(strxml); out1.close(); int SiteId = channel.getSite().getId(); Site site = CmsCache.getSite(SiteId); String SiteFolder = site.getSiteFolder(); FileUtil fileutil = new FileUtil(); fileutil .PublishFiles("cutvsitemap.xml", "/", SiteFolder, 13, site); } catch (IOException e) { e.printStackTrace(); System.out.println("error------>"+e.getMessage()); } File xml_file = new File(xmlfile); int filesize = (int) (xml_file.length() / 1024L); long t2 = System.currentTimeMillis(); long lasttime = t2 - t1; System.out.println("end--------------end"); } }
sitemap 循环 获取最近两天内新发布的页面链接
最新推荐文章于 2022-05-17 16:19:33 发布