zoukankan      html  css  js  c++  java
  • 百度熊掌号历史数据提交

    一、背景

    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("
    ");
                    }
                    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

    三、结果:

     

    声明:本文版权归作者和博客园共有,欢迎转载,但请在文章页面明显位置给出原文连接。 
  • 相关阅读:
    第三百三十二节,web爬虫讲解2—Scrapy框架爬虫—Scrapy使用
    trim思考
    国王验毒酒问题
    有人在群里问mysql如何选择性更新部分条件的问题
    有人在群里问 20180222055怎么转20180222-055 这样的问题
    如何下载腾讯视频的视频转为MP4常用格式视频
    天气预报的大雪真的下了
    群友面试的问题 我搞笑的帮忙回答一下
    电台大神打油诗
    ajax简单手写了一个猜拳游戏
  • 原文地址:https://www.cnblogs.com/hellovoyager1/p/9224160.html
Copyright © 2011-2022 走看看