|
@@ -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;
|
|
|
+ }
|
|
|
+}
|