Ver Fonte

文章百度收录推送,保存已推送文章id

huangzhiguo há 2 anos atrás
pai
commit
e7b093dfd2

+ 19 - 0
src/main/java/com/caimei365/tools/mapper/InfoIncludedMapper.java

@@ -0,0 +1,19 @@
+package com.caimei365.tools.mapper;
+
+import com.caimei365.tools.model.po.InfoIncludedPo;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2022/9/19
+ */
+@Mapper
+public interface InfoIncludedMapper {
+    /**
+     * 保存已被百度收录的文章id
+     * @param infoIncluded
+     */
+    void inData(InfoIncludedPo infoIncluded);
+}

+ 17 - 0
src/main/java/com/caimei365/tools/mapper/InfoMapper.java

@@ -0,0 +1,17 @@
+package com.caimei365.tools.mapper;
+
+import com.caimei365.tools.model.po.InfoPo;
+import org.apache.ibatis.annotations.Mapper;
+
+import java.util.List;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2022/9/17
+ */
+@Mapper
+public interface InfoMapper {
+    List<InfoPo> selectListId();
+}

+ 16 - 0
src/main/java/com/caimei365/tools/model/po/InfoIncludedPo.java

@@ -0,0 +1,16 @@
+package com.caimei365.tools.model.po;
+
+import lombok.Data;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2022/9/19
+ */
+@Data
+public class InfoIncludedPo {
+    private int id;
+    private int infoId;         // 文章id
+
+}

+ 44 - 0
src/main/java/com/caimei365/tools/model/po/InfoPo.java

@@ -0,0 +1,44 @@
+package com.caimei365.tools.model.po;
+
+import lombok.Data;
+
+import java.util.Date;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2022/9/17
+ */
+@Data
+public class InfoPo {
+    private Integer Id;
+    private String title;		// 标题
+    private String label;		// 信息标签
+    private String publisher;		// 发布人
+    private Integer publishSource;		// 文章发布来源:1采美,2供应商
+    private String source;		// 发布人
+    private Integer shopId;		// 供应商id,publishSource为2时才有值
+    private String keyword;		// 关键词
+    private String recommendContent;		// 推荐语
+    private String infoContent;		// 信息内容
+    private String guidanceImage;		// 引导图
+    private String homePageImage;		// 商城首页图
+    private Date pubdate;		// 发布时间
+    private String recommendStatus;		// 推荐状态
+    private Long basePraise;		// 基础点赞
+    private Long realPraise;		// 实际点赞
+    private Long basePv;		// 基础阅读量
+    private Long pv;		// 阅读总量
+    private Long priorityIndex;//优先级
+    private Integer auditStatus;	//供应商文章审核状态:1待审核,2审核通过,3审核失败
+    private Integer onlineStatus;   //文章上线状态:1待上线,2已上线,3已下线
+    private String failReason;	//审核失败理由
+    private String topPosition;	//置顶位
+    private String topFlag;	//虚拟字段,是否置顶标识:0否,1是
+
+    private String shopName;	// 供应商名称
+    private String startPubDate;		// 发布时间
+    private String endPubDate;		// 发布时间
+    private Integer auditFlag;		// 是否审核保存,1是
+}

+ 14 - 0
src/main/java/com/caimei365/tools/service/InfoIncludedService.java

@@ -0,0 +1,14 @@
+package com.caimei365.tools.service;
+
+import com.caimei365.tools.model.po.InfoIncludedPo;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2022/9/19
+ */
+public interface InfoIncludedService {
+
+    void inData(InfoIncludedPo infoIncluded);
+}

+ 15 - 0
src/main/java/com/caimei365/tools/service/InfoService.java

@@ -0,0 +1,15 @@
+package com.caimei365.tools.service;
+
+import com.caimei365.tools.model.po.InfoPo;
+
+import java.util.List;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2022/9/17
+ */
+public interface InfoService {
+    List<InfoPo> selectListId();
+}

+ 25 - 0
src/main/java/com/caimei365/tools/service/impl/InfoIncludedServiceImpl.java

@@ -0,0 +1,25 @@
+package com.caimei365.tools.service.impl;
+
+import com.caimei365.tools.mapper.InfoIncludedMapper;
+import com.caimei365.tools.model.po.InfoIncludedPo;
+import com.caimei365.tools.service.InfoIncludedService;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2022/9/19
+ */
+@Service
+public class InfoIncludedServiceImpl implements InfoIncludedService {
+
+    @Resource private InfoIncludedMapper infoIncludedMapper;
+
+    @Override
+    public void inData(InfoIncludedPo infoIncluded) {
+        infoIncludedMapper.inData(infoIncluded);
+    }
+}

+ 29 - 0
src/main/java/com/caimei365/tools/service/impl/InfoServiceImpl.java

@@ -0,0 +1,29 @@
+package com.caimei365.tools.service.impl;
+
+import com.caimei365.tools.mapper.InfoMapper;
+import com.caimei365.tools.model.po.InfoPo;
+import com.caimei365.tools.service.InfoService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.List;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2022/9/17
+ */
+@Slf4j
+@Service
+public class InfoServiceImpl implements InfoService {
+
+    @Resource
+    private InfoMapper infoMapper;
+
+    @Override
+    public List<InfoPo> selectListId() {
+        return infoMapper.selectListId();
+    }
+}

+ 136 - 0
src/main/java/com/caimei365/tools/task/IncludedByBaiduTask.java

@@ -0,0 +1,136 @@
+package com.caimei365.tools.task;
+
+import com.caimei365.tools.model.po.InfoIncludedPo;
+import com.caimei365.tools.model.po.InfoPo;
+import com.caimei365.tools.service.InfoIncludedService;
+import com.caimei365.tools.service.InfoService;
+import com.caimei365.tools.utils.GenerateUtils;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.scheduling.annotation.Scheduled;
+
+
+import javax.annotation.Resource;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.List;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2022/9/17
+ */
+@Slf4j
+@Configuration
+@EnableScheduling
+@RequiredArgsConstructor
+public class IncludedByBaiduTask {
+
+    @Resource
+    private GenerateUtils generateUtils;
+
+    @Resource
+    private InfoService infoService;
+
+    @Resource
+    private InfoIncludedService infoIncludedService;
+
+    /**
+     * 推送链接到百度收录
+     *
+     * @throws IOException
+     */
+    @Scheduled(cron = "0 30 1 * * ?")
+    public void articlePush() throws IOException {
+        log.info("每天推送文章到百度收录");
+        // 查询未被百度收录的文章id
+        List<InfoPo> infoIdList = infoService.selectListId();
+        System.out.println(infoIdList.size());
+        for (InfoPo info : infoIdList) {
+            String paramUrl = "https://www.caimei365.com/info/detail-" + info.getId() + "-1.html";
+            // 判断该链接是否被搜录
+            Integer integer = searchEverything(paramUrl);
+            if (integer != null) {
+                if (integer == 0) {
+                    generateUtils.pushBaiduLink(paramUrl);
+                }
+            } else {
+                generateUtils.pushBaiduLink(paramUrl);
+            }
+
+        }
+
+    }
+
+    /**
+     * 保存被百度收录的文章信息
+     *
+     * @throws IOException
+     */
+    @Scheduled(cron = "0 30 1 1 * ?")
+    public void saveLink() throws IOException {
+        log.info("每月将已经被百度收录的文章信息保存");
+        // 查询未被百度收录的文章id
+        List<InfoPo> infoIdList = infoService.selectListId();
+        InfoIncludedPo infoIncludedPo = new InfoIncludedPo();
+        for (InfoPo info : infoIdList) {
+            String paramUrl = "https://www.caimei365.com/info/detail-" + info.getId() + "-1.html";
+            // 判断该链接是否被搜录
+            Integer integer = searchEverything(paramUrl);
+            if (integer != null) {
+                if (integer > 0) {
+                    // 将已保存的文章链接信息保存
+                    infoIncludedPo.setInfoId(info.getId());
+                    infoIncludedService.inData(infoIncludedPo);
+                }
+            }
+
+        }
+
+    }
+
+    /**
+     * 判断链接在百度搜索总是否有结果,对链接是否被收录进行判断
+     *
+     * @param keyWord
+     * @return
+     * @throws IOException
+     */
+
+    public Integer searchEverything(String keyWord) throws IOException {
+        URL url = new URL("http://www.baidu.com/s?wd=" + keyWord);    //搜索功能在二级域名中
+        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
+        httpURLConnection.setDoOutput(true);
+        httpURLConnection.setDoInput(true);
+        httpURLConnection.setRequestMethod("GET");
+        // httpURLConnection.setRequestProperty();
+
+        // 接收数据
+        InputStream inputStream = httpURLConnection.getInputStream();
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        byte[] b = new byte[1024];
+        int len = 0;
+        while (true) {
+            len = inputStream.read(b);
+            if (len == -1) {
+                break;
+            }
+            byteArrayOutputStream.write(b, 0, len);
+        }
+        String toString = byteArrayOutputStream.toString();
+        if(toString.lastIndexOf(">百度为您找到相关结果") != -1) {
+            String substring = toString.substring(toString.lastIndexOf(">百度为您找到相关结果") + 10, toString.lastIndexOf("个</span>"));
+            String subs = substring.substring(1, substring.length());
+            Integer number = Integer.parseInt(subs);
+            inputStream.close();
+            return number;
+        }
+        return null;
+    }
+}

+ 72 - 0
src/main/java/com/caimei365/tools/utils/GenerateUtils.java

@@ -0,0 +1,72 @@
+package com.caimei365.tools.utils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.net.URL;
+import java.net.URLConnection;
+
+/**
+ * www静态页面生成utils
+ *
+ * @author : Charles
+ * @date : 2021/5/10
+ */
+@Component
+public class GenerateUtils {
+
+	protected Logger logger = LoggerFactory.getLogger(getClass());
+
+    /**
+     * 百度链接实时推送
+     */
+    public void pushBaiduLink(String param) {
+        String result = "";
+        PrintWriter out = null;
+        BufferedReader in = null;
+        try {
+            //建立URL之间的连接
+            URLConnection conn = new URL("http://data.zz.baidu.com/urls?site=https://www.caimei365.com&token=hgKJrx3lqsPPCf73").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");
+            //发送POST请求必须设置如下两行
+            conn.setDoInput(true);
+            conn.setDoOutput(true);
+            //获取conn对应的输出流
+            out = new PrintWriter(conn.getOutputStream());
+            //发送请求参数
+            out.print(param.trim());
+            //进行输出流的缓冲
+            out.flush();
+            //通过BufferedReader输入流来读取Url的响应
+            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
+            String line;
+            while((line=in.readLine()) != null){
+                result += line;
+            }
+        } catch (Exception e) {
+            logger.info("百度链接实时推送出现异常!" + e);
+        } finally {
+            try{
+                if(out != null) {
+                    out.close();
+                }
+                if(in != null) {
+                    in.close();
+                }
+            } catch(IOException ex) {
+                ex.printStackTrace();
+                logger.info("百度链接实时推送关闭连接异常!" + ex);
+            }
+        }
+        logger.info("百度链接实时推送:"+result);
+    }
+}

+ 8 - 0
src/main/resources/mapper/InfoIncludedMapper.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.caimei365.tools.mapper.InfoIncludedMapper">
+
+    <insert id="inData">
+        insert into info_Included(infoId) values (#{infoId})
+    </insert>
+</mapper>

+ 7 - 0
src/main/resources/mapper/InfoMapper.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.caimei365.tools.mapper.InfoMapper">
+    <select id="selectListId" resultType="com.caimei365.tools.model.po.InfoPo">
+        SELECT id AS Id FROM info WHERE id NOT IN (SELECT infoId FROM info_included) and enabledStatus = 1
+    </select>
+</mapper>