一、背景
SEO 為了讓百度搜索引擎收錄更多的文章,需要將歷史數據的文章鏈接進行提交。SEO 那邊已經申請熊掌號,這邊需要調用熊掌號提供的接口進行操作(詳情可查看熊掌號搜索資源平台)。
二、代碼實現
通過執行定時任務的方式,每天定時提交前一天的數據至熊掌號
/** * 定時任務,定時向熊掌號推送文章數據 */ public class XiongzhangQuartz { private static Log LOG = LogFactory.getLog(XiongzhangQuartz.class); @Autowired private IXiongzhangTaskBiz xiongzhangTaskBiz; @Autowired private IArticleBiz articleBiz; // 定時任務執行時間 每天 1點 執行 public void postArticleToXiongzhang() { LOG.info("--------推送文章至熊掌號開始!--------"); // 文章主域名 String appHostUrl = MyPropertiesUtil.getFileDirPath("config/postToXiongzhang.properties", "appHostUrl"); // 百度熊掌歷史數據提交api請求地址 String postUrl = MyPropertiesUtil.getFileDirPath("config/postToXiongzhang.properties", "postUrl"); XiongzhangTaskEntity xiongzhangIndexTask = xiongzhangTaskBiz.queryTheLastOne(); // 查詢上一次執行的最后一次的文章id int articleId = 0; if (xiongzhangIndexTask != null) { articleId = xiongzhangIndexTask.getArticleId(); } // 通過id 查詢新增加的文章,做靜態化用 List<ArticleEntity> articleIds = articleBiz.queryLuceneIndexByArticleId(articleId, null); // 每1000條數據提交一次熊掌號 int times = articleIds.size() % 1000 == 0 ? (articleIds.size() / 1000) : (articleIds.size() / 1000 + 1); for (int i = 0; i < times; i++) { int end = (i + 1) * 1000; if (end >= articleIds.size()) { end = articleIds.size(); } List<ArticleEntity> subList = articleIds.subList(i * 1000, end); if (subList != null && subList.size() > 0) { int lastArticleId = subList.get(0).getArticleID(); StringBuilder articleLinks = new StringBuilder(); for (ArticleEntity article : subList) { // 文章url 拼接 String columnPath = StringUtil.null2String(article.getColumn().getColumnPathUrl()); String link = appHostUrl + columnPath + File.separator + article.getBasicId() + IParserRegexConstant.HTML_SUFFIX; articleLinks.append(link).append("\n"); } String result = postToXiongzhang(postUrl, articleLinks.toString()); XiongzhangTaskEntity xiongzhangResult = new XiongzhangTaskEntity(); xiongzhangResult.setArticleId(lastArticleId); xiongzhangResult.setPostSum(subList.size()); xiongzhangResult.setPostMessage(result); xiongzhangResult.setCreateDate(new Date()); xiongzhangResult.setTotalArticle(articleLinks.toString()); xiongzhangTaskBiz.insert(xiongzhangResult); } } LOG.info("--------推送文章至熊掌號結束!--------成功推送:" + articleIds.size()); } // --發送POST請求必須設置允許輸出 // --不要使用緩存,容易出現問題. // --在開始用HttpURLConnection對象的setRequestProperty()設置,就是生成HTML文件頭. /** * * @param postUrl * @param articleLinks * @return */ public static String postToXiongzhang(String postUrl, String articleLinks) { if (null == postUrl || null == articleLinks) { return null; } String result = ""; PrintWriter out = null; BufferedReader in = null; try { // URL統一資源定位符,使用此類可找到互聯網上的資源(簡單網頁),讀取網頁的內容顯示為HTML代碼(類似於文件讀寫時的File,都是先建立一個“數據源”) URL url = new URL(postUrl); // 建立URL之間的連接,openConnection 打開一個URL連接,並運行客戶端訪問資源。 URLConnection conn = url.openConnection(); // 設置通用的請求屬性 conn.setRequestProperty("User-Agent", "curl/7.12.1"); conn.setRequestProperty("Host", "data.zz.baidu.com"); conn.setRequestProperty("Content-Type", "text/plain"); conn.setRequestProperty("Content-Length", "83"); // 不使用Cache // conn.setUseCaches(false); // 發送POST請求必須設置如下兩行,表示設置允許輸出 // URL 連接可用於輸入和/或輸出。如果打算使用 URL 連接進行輸入,則將 DoInput 標志設置為 // true;如果不打算使用,則設置為 false。默認值為 true。 conn.setDoInput(true); // URL 連接可用於輸入和/或輸出。如果打算使用 URL 連接進行輸出,則將 DoOutput 標志設置為 // true;如果不打算使用,則設置為 false。默認值為 false。 conn.setDoOutput(true); // 返回URL的輸出流, 用於寫入資源 // 向網絡發送數據,獲取conn對應的輸出流,根據現有的 OutputStream 創建不帶自動行刷新的新 // PrintWriter。 out = new PrintWriter(conn.getOutputStream()); // 發送請求參數,打印字符串 out.print(articleLinks.trim()); // 進行輸出流的緩沖 out.flush(); // 通過BufferedReader輸入流來讀取Url的響應,conn.getInputStream()是網絡返回的數據,寫入本地 in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { result = "post推送出現異常!" + e; e.printStackTrace(); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; } }
postToXiongzhang.properties 配置文件
#熊掌號相關 appHostUrl=http://www.xxxx.com postUrl=http://data.zz.baidu.com/urls?appid=熊掌號ip&token=xxx&type=batch
三、結果: