123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- package com.example.demo.service;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.example.demo.base.Constants;
- import com.example.demo.domain.*;
- import com.example.demo.mapper.QrtzTaskMapper;
- import com.example.demo.model.dto.QuartzTaskDTO;
- import com.example.demo.model.dto.TaskWebSocketDTO;
- import com.example.demo.model.dto.WebQueryDTO;
- import com.example.demo.util.FileUtils;
- import com.example.demo.util.Response;
- import com.example.demo.util.ResponseEnum;
- import com.example.demo.util.WebSocketServer;
- import io.swagger.v3.oas.models.security.SecurityScheme;
- import lombok.RequiredArgsConstructor;
- import org.apache.commons.lang3.StringUtils;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.chrome.ChromeDriver;
- import org.openqa.selenium.chrome.ChromeOptions;
- import org.openqa.selenium.htmlunit.HtmlUnitWebElement;
- import org.openqa.selenium.interactions.Actions;
- import org.openqa.selenium.support.ui.ExpectedConditions;
- import org.openqa.selenium.support.ui.WebDriverWait;
- import org.springframework.context.annotation.Lazy;
- import org.springframework.scheduling.annotation.Async;
- import org.springframework.stereotype.Service;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.IOException;
- import java.util.*;
- import java.util.concurrent.TimeUnit;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * @author admin
- * @description 针对表【qrtz_task(定时任务表)】的数据库操作Service实现
- * @createDate 2023-03-15 13:40:32
- */
- @Service
- @RequiredArgsConstructor(onConstructor_ = {@Lazy})
- public class UploadFromWebService {
- private final FileUtils fileUtils;
- private final OutInterfaceService outInterfaceService;
- private final WebConfigService webConfigService;
- private final QrTaskService qrTaskService;
- // 查询并上传(专利之星)
- public List<PatentCell> getPatentStar(WebQueryDTO webQueryDTO) throws Exception {
- List<PatentCell> patentCells = new ArrayList<>();
- try {
- List<String> cells = webQueryDTO.getConCells();
- String conditions = webQueryDTO.getConditions();
- Integer id = webQueryDTO.getWebConfigId();
- conditions = this.formatConditions(conditions);
- //根据id 获得网站配置
- WebConfig webConfig = webConfigService.getConfigById(id);
- //1.获得驱动
- System.setProperty("webdriver.chrome.driver", "D:\\driver\\chromedriver.exe");
- // 2.页面最大化
- ChromeOptions chromeOptions = new ChromeOptions();
- chromeOptions.addArguments("--start-maximized");
- // 3.创建驱动
- WebDriver driver = new ChromeDriver(chromeOptions);
- // 4.创建动作
- Actions action = new Actions(driver);
- //设置等待时间
- long formSecond1 = 10;
- WebDriverWait wait1 = new WebDriverWait(driver, formSecond1);
- // 打开专利之星首页
- driver.get(webConfig.getWebAddress());
- wait1.until(ExpectedConditions.presenceOfElementLocated(By.id("loginname")));
- WebElement loginName = driver.findElement(By.id("loginname"));
- WebElement password = driver.findElement(By.id("password"));
- loginName.sendKeys(webConfig.getWebAccount());
- password.sendKeys(webConfig.getWebPassword());
- WebElement loginButton = driver.findElement(By.id("login"));
- loginButton.click();
- TimeUnit.MILLISECONDS.sleep(5000);//毫秒
- //获得表格搜索按钮并点击
- wait1.until(ExpectedConditions.presenceOfElementLocated(By.id("tablepage")));
- WebElement countEle = driver.findElement(By.className("allcountlab"));
- String countText =countEle.getText();
- Integer count = Integer.parseInt(countText);
- WebElement tablePage = driver.findElement(By.id("tablepage"));
- WebElement tablePagea = tablePage.findElement(By.tagName("a"));
- tablePagea.click();
- TimeUnit.MILLISECONDS.sleep(5000);//毫秒
- //获得搜索框并点击
- wait1.until(ExpectedConditions.presenceOfElementLocated(By.id("TxtSearch")));
- WebElement TxtSearch = driver.findElement(By.id("TxtSearch"));
- TxtSearch.sendKeys(conditions);
- WebElement searchBtn = driver.findElement(By.id("searchbtn2"));
- searchBtn.click();
- wait1.until(ExpectedConditions.presenceOfElementLocated(By.className("tcdNumber")));
- List<WebElement> tcdNumbers = driver.findElements(By.className("tcdNumber"));
- String pagesText = tcdNumbers.get(tcdNumbers.size() - 1).getText();
- int page = Integer.parseInt(pagesText);
- String Handle = driver.getWindowHandle();
- //创建一个map集合存放浏览器句柄
- HashMap<String, String> handleMap = new HashMap<>();
- //将智慧芽句柄放到map中
- handleMap.put("mainPage", Handle);
- WebElement pagNum = driver.findElement(By.className("page_num"));
- pagNum.clear();
- pagNum.sendKeys("4");
- WebElement pagBtn = driver.findElement(By.className("page_btn"));
- pagBtn.click();
- TimeUnit.MILLISECONDS.sleep(10000);//毫秒
- for (int p = 4; p < page; p++) {
- //等待数据加载
- wait1.until(ExpectedConditions.presenceOfElementLocated(By.className("patent")));
- TimeUnit.MILLISECONDS.sleep(1000);//毫秒
- //获得列表
- List<WebElement> patentContents = driver.findElements(By.className("patent"));
- // 页面元素标签
- for (int i = 4; i < patentContents.size(); i++) {
- PatentCell patentCell = new PatentCell();
- patentCell.setProjectId(webQueryDTO.getProjectId());
- patentCell.setReportId(webQueryDTO.getReportId());
- wait1.until(ExpectedConditions.presenceOfElementLocated(By.className("title-color")));
- WebElement titleA = patentContents.get(i).findElement(By.className("title-color"));
- titleA.click();
- TimeUnit.MILLISECONDS.sleep(10000);//毫秒
- //9、获取到所有的句柄
- Set<String> set = driver.getWindowHandles();
- //10、循环找到详情窗口句柄
- for (String s : set) {
- //10.1、将详情窗口的句柄放到map中
- if (!s.equals(Handle)) {
- handleMap.put("detail", s);
- }
- }
- driver.switchTo().window(handleMap.get("detail"));
- wait1.until(ExpectedConditions.presenceOfElementLocated(By.className("item-content")));
- WebElement itemContent = driver.findElement(By.className("item-content"));
- List<WebElement> divItems = itemContent.findElements(By.tagName("div"));
- for (int t = 0; t < divItems.size(); t++) {
- WebElement temEle = divItems.get(t);
- String text = temEle.getText();
- String value = "";
- String[] strings = text.split(":");
- if (strings.length > 1) {
- value = strings[1].trim();
- String key = strings[0].trim();
- switch (key) {
- case "申请号":
- patentCell.setApplicationNo(value);
- break;
- case "申请日":
- patentCell.setApplicationDate(value);
- break;
- case "国家/省市":
- patentCell.setCountry(value);
- break;
- case "公开号":
- patentCell.setPublicNo(value);
- break;
- case "公开日":
- patentCell.setPubilcDate(value);
- break;
- case "授权公告号":
- patentCell.setPublicAccreditNo(value);
- break;
- case "授权公告日":
- patentCell.setPublicAccreditDate(value);
- break;
- case "主分类号":
- patentCell.setMainIpc(value);
- break;
- case "分类号":
- patentCell.setIpc(this.StringToList(value, ";"));
- break;
- case "申请人":
- patentCell.setApplicationPersons(this.StringToList(value, " "));
- break;
- case "申请人地址":
- patentCell.setApplicationAddress(this.StringToList(value, " "));
- break;
- case "发明人":
- patentCell.setInventors(this.StringToList(value, " "));
- break;
- case "当前权利人":
- patentCell.setApplicationCurrents(this.StringToList(value, " "));
- break;
- case "代理人":
- patentCell.setAgencyPersons(this.StringToList(value, " "));
- break;
- case "代理机构":
- patentCell.setAgency(value);
- break;
- case "范畴分类":
- patentCell.setClassical(value);
- break;
- case "当前状态":
- patentCell.setStatue(value);
- break;
- }
- }
- }
- //获得摘要
- WebElement abstrElement = driver.findElement(By.className("item-summary"));
- List<WebElement> spans = abstrElement.findElements(By.tagName("span"));
- if (cells.contains(1)) {
- String abstrText = spans.get(0).getText();
- patentCell.setAbstrText(abstrText);
- }
- //获得图片路径
- wait1.until(ExpectedConditions.presenceOfElementLocated(By.className("item-img")));
- WebElement picElement = driver.findElement(By.className("item-img"));
- WebElement imag = picElement.findElement(By.tagName("img"));
- String url = imag.getAttribute("src");
- url = fileUtils.uploadToLocal(url, ".jpg").getPath();
- patentCell.setPicUrl(url);
- if (cells.contains(2)) {
- //获得主权要
- String mainRight = spans.get(1).getText();
- patentCell.setMainRignt(mainRight);
- //获得权要
- WebElement CLElement = driver.findElement(By.id("itemCL"));
- CLElement.click();
- TimeUnit.MILLISECONDS.sleep(10000);//毫秒
- wait1.until(ExpectedConditions.presenceOfElementLocated(By.tagName("claim-text")));
- List<WebElement> rightElements = driver.findElements(By.tagName("claim-text"));
- List<String> rights = new ArrayList<>();
- rightElements.forEach(item -> {
- rights.add(item.getText());
- });
- patentCell.setRights(rights);
- }
- if (cells.contains(3)) {
- //获得说明书
- WebElement DSElement = driver.findElement(By.id("itemDS"));
- DSElement.click();
- TimeUnit.MILLISECONDS.sleep(10000);//毫秒
- wait1.until(ExpectedConditions.presenceOfElementLocated(By.tagName("invention-title")));
- WebElement titleElement = driver.findElement(By.tagName("invention-title"));
- patentCell.setTitle(titleElement.getText());
- StringBuilder stringBuilder = new StringBuilder();
- int flag = 1;
- //技术领域
- WebElement textElement1 = driver.findElement(By.tagName("description"));
- List<WebElement> textP1 = textElement1.findElements(By.tagName("p"));
- stringBuilder.append(textP1.get(0).getText());
- stringBuilder.append("\r\n");
- for (int t = 1; t < textP1.size(); t++) {
- String tem = String.format("%04d", flag);
- stringBuilder.append("[" + tem + "]" + textP1.get(t).getText());
- stringBuilder.append("\r\n");
- flag += 1;
- }
- patentCell.setPatentInstructionText(stringBuilder.toString());
- }
- //获得pdf文档
- if (cells.contains(4)) {
- WebElement PDFElement = driver.findElement(By.id("itemPdf"));
- PDFElement.click();
- TimeUnit.MILLISECONDS.sleep(10000);//毫秒
- driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
- List<WebElement> PDFFile = driver.findElements(By.tagName("embed"));
- String pdfUrl = "";
- if (PDFFile.size() == 0) {
- List<WebElement> PDFFiles = driver.findElements(By.className("pdfbtn"));
- if (PDFFiles.size() != 0) {
- pdfUrl = PDFFiles.get(0).getAttribute("data-url");
- } else {
- WebElement suBtn = driver.findElement(By.className("layui-layer-btn0"));
- suBtn.click();
- TimeUnit.MILLISECONDS.sleep(10000);//毫秒
- }
- } else {
- pdfUrl = PDFFile.get(0).getAttribute("src");
- }
- if (pdfUrl != "") {
- UploadFileDTO fileDTO = fileUtils.uploadToLocal(pdfUrl, ".PDF");
- patentCell.setPDFName(fileDTO.getFileName());
- patentCell.setPDFSize(fileDTO.getFileSize());
- patentCell.setPDFUrl(fileDTO.getPath());
- patentCell.setPdf(fileDTO);
- }
- }
- if (cells.contains(5)) {
- //获得法律状态
- WebElement flztElement = driver.findElement(By.id("flztbtn"));
- flztElement.click();
- TimeUnit.MILLISECONDS.sleep(10000);//毫秒
- wait1.until(ExpectedConditions.presenceOfElementLocated(By.id("legalContainer")));
- WebElement tBody = driver.findElement(By.id("legalContainer"));
- TimeUnit.MILLISECONDS.sleep(1000);//毫秒
- List<WebElement> trs = tBody.findElements(By.tagName("tr"));
- List<PatentCell.PatentAffair> affairs = new ArrayList<>();
- trs.forEach(item -> {
- List<WebElement> tds = item.findElements(By.tagName("td"));
- PatentCell.PatentAffair affair = new PatentCell.PatentAffair();
- affair.setDateTime(tds.get(0).getText());
- affair.setSimpleStatus(tds.get(1).getText());
- affair.setStatus(tds.get(2).getText());
- affairs.add(affair);
- });
- patentCell.setPatentAffairs(affairs);
- if (patentCell.getPublicNo() != null) {
- patentCell.setPatentNo(patentCell.getPublicNo());
- } else {
- patentCell.setPatentNo(patentCell.getPublicAccreditNo());
- patentCell.setPublicNo(patentCell.getPublicAccreditNo());
- }
- }
- if (patentCell.getPatentNo() != null) {
- String res = outInterfaceService.importPatents(patentCell);
- JSONObject jsonObject = JSONObject.parseObject(res);
- if (jsonObject.get("code").toString().equals("500")) {
- return null;
- }
- }
- WebSocketServer.sendInfo(Response.websocket(new TaskWebSocketDTO()
- .setTaskId(webQueryDTO.getTaskId())
- .setProjectId(webQueryDTO.getProjectId())
- .setComplete(false)
- .setIndex(i)
- .setTaskType(Constants.TASK_IMPORT_PATENT)
- .setPercentage(count == 0 ? 0 : Math.round((count.equals(i) ? (i * 1D) : (i + 1D)) / count * 100D))
- .setFileName("")
- .setUrl("")
- .setTotal(count), ResponseEnum.PATENT_IMPORT_TASK_SUCCESS),"");
- patentCells.add(patentCell);
- driver.close();
- driver.switchTo().window(handleMap.get("mainPage"));
- }
- wait1.until(ExpectedConditions.presenceOfElementLocated(By.className("tcdPageCode")));
- List<WebElement> nexts = driver.findElements(By.className("nextPage"));
- if (nexts.size() != 0) {
- nexts.get(0).click();
- TimeUnit.MILLISECONDS.sleep(10000);//毫秒
- }
- }
- WebSocketServer.sendInfo(Response.websocket(new TaskWebSocketDTO()
- .setTaskId(webQueryDTO.getTaskId())
- .setProjectId(webQueryDTO.getProjectId())
- .setComplete(true)
- .setIndex(count)
- .setTaskType(Constants.TASK_IMPORT_PATENT)
- .setPercentage(100L)
- .setFileName("")
- .setUrl("")
- .setTotal(count), ResponseEnum.PATENT_IMPORT_TASK_SUCCESS), "");
- QrtzTask qrtzTask = qrTaskService.getById(webQueryDTO.getTaskId());
- qrtzTask.setTaskState(2);
- qrtzTask.updateById();
- driver.quit();
- }
- catch (Exception e){
- QrtzTask qrtzTask = qrTaskService.getById(webQueryDTO.getTaskId());
- qrtzTask.setTaskState(3);
- qrtzTask.updateById();
- }
- return patentCells;
- }
- public List<PatentCell> getPatentya(String patentVO) throws IOException, InterruptedException {
- //1.获得驱动
- System.setProperty("webdriver.chrome.driver", "D:\\driver\\chromedriver.exe");
- // 2.页面最大化
- ChromeOptions chromeOptions = new ChromeOptions();
- chromeOptions.addArguments("--start-maximized");
- // 3.创建驱动
- WebDriver driver = new ChromeDriver(chromeOptions);
- // 4.创建动作
- Actions action = new Actions(driver);
- // 打开智慧芽首页
- driver.get("https://account.zhihuiya.com");
- //设置等待时间
- long formSecond1 = 50;
- WebDriverWait wait1 = new WebDriverWait(driver, formSecond1);
- //等待密码登录界面加载
- wait1.until(ExpectedConditions.presenceOfElementLocated(By.id("tab-password")));
- // 点击密码登录
- driver.findElement(By.id("tab-password")).click();
- // 获取输入框,输入账号密码
- List<WebElement> ret = driver.findElements(By.className("el-input__inner"));
- ret.get(0).sendKeys("liting@china-wispro.com");
- ret.get(1).sendKeys("Lt199299");
- // 点击登录
- driver.findElement(By.className("el-button")).click();
- //等待加载
- TimeUnit.MILLISECONDS.sleep(1000);//毫秒
- List<WebElement> alerts = driver.findElements(By.className("patsnap-el-confirm"));
- if (alerts.size() != 0) {
- alerts.get(0).click();
- }
- wait1.until(ExpectedConditions.presenceOfElementLocated(By.className("master_nav-item__2nd_lv")));
- WebElement searchTag = driver.findElement(By.className("master_nav-item__2nd_lv"));
- searchTag.click();
- //等待搜索框加载
- wait1.until(ExpectedConditions.presenceOfElementLocated(By.className("div-textarea")));
- List<WebElement> ret1 = driver.findElements(By.className("div-textarea"));
- //搜索框赋值
- ret1.get(0).sendKeys(patentVO);
- TimeUnit.MILLISECONDS.sleep(2000);//毫秒
- wait1.until(ExpectedConditions.presenceOfElementLocated(By.className("search-form__btn")));
- WebElement rr = driver.findElement(By.className("search-form__btn"));
- rr.click();
- TimeUnit.MILLISECONDS.sleep(1000);//毫秒
- List<PatentCell> patentCells = new ArrayList<>();
- long formSecond = 50;
- WebDriverWait wait = new WebDriverWait(driver, formSecond);
- wait.until(ExpectedConditions.presenceOfElementLocated(By.className("search-result-standard-view__patent")));
- // String js ="document.getElementsByClassName('sidebar-table__right')[0].scrollBy(0, 8000)";
- // ((JavascriptExecutor) driver).executeScript(js);
- List<WebElement> ret2 = driver.findElements(By.className("search-result-standard-view__patent"));
- ret2.remove(ret2.size() - 1);
- List<WebElement> ret3 = driver.findElements(By.className("pn-cell-popover"));
- for (int i = 0; i < ret2.size(); i++) {
- action.moveToElement(ret3.get(i)).perform();
- wait.until(ExpectedConditions.attributeToBeNotEmpty(ret3.get(i), "href"));
- String patenNo = ret3.get(i).getAttribute("data-link-data");
- System.out.println(patenNo);
- }
- for (WebElement item : ret2) {
- List<WebElement> aLinks = item.findElements(By.tagName("a"));
- PatentCell patentCell = new PatentCell();
- aLinks.forEach(tem -> {
- String type = tem.getAttribute("data-link-type");
- if (type != null && type.equals("TITLE")) {
- String title = tem.getAttribute("data-link-data");
- patentCell.setTitle(title);
- } else if (type != null && type.equals("PN")) {
- String url = tem.getAttribute("href");
- patentCell.setUrl(url);
- String patenNo = tem.getAttribute("data-link-data");
- patentCell.setPatentNo(patenNo);
- } else if (type != null && type.equals("ANC")) {
- String applications = tem.getAttribute("data-link-data");
- patentCell.setApplications(applications);
- }
- });
- String legal = driver.findElement(By.className("legal-tag__LEGAL_STATUS")).getText();
- patentCell.setLegal(legal);
- patentCells.add(patentCell);
- }
- return patentCells;
- }
- //格式化参数
- public String formatConditions(String condition) throws IOException {
- String jsons = FileUtils.readerMethod();
- JSONObject jsonObject = new JSONObject();
- List<JSONObject> sources = JSONObject.parseArray(jsons, JSONObject.class);
- for (JSONObject jsonObject1 : sources) {
- jsonObject = jsonObject1;
- }
- condition = condition.replace(" ", "");
- condition = condition.replace("&&", "*");
- condition = condition.replace("||", "+");
- StringBuilder stringBuilder = new StringBuilder("F XX ");
- String pattern = "\\([^)]*\\)";
- Pattern p = Pattern.compile(pattern);
- Matcher m = p.matcher(condition);
- while (m.find()) { // 当字符串中有匹配到 {} 时
- String param = m.group(0);
- String cell = formatParam(param, jsonObject);
- condition = condition.replace(param, cell);
- }
- return condition;
- }
- public String formatParam(String param, JSONObject jsonObject) {
- param = param.replace("(", "");
- param = param.replace(")", "");
- String[] params = param.split("=");// {} 和里面的内容
- StringBuilder stringBuilder = new StringBuilder("(" + params[1]);
- stringBuilder.append("/");
- String cell = jsonObject.get(params[0]).toString();
- stringBuilder.append(cell + ")");
- return stringBuilder.toString();
- }
- //将值转换为list
- public List<String> StringToList(String value, String split) {
- List<String> list = new ArrayList<>();
- if (value != null && value != "") {
- String[] temValue = value.split(split);
- list = new ArrayList<>(Arrays.asList(temValue));
- }
- return list;
- }
- }
|