|
@@ -1,23 +1,33 @@
|
|
|
package cn.com.ctop.crawler.modules.appium.service.impl;
|
|
|
|
|
|
+import cn.com.ctop.common.module.utils.HttpUtils2;
|
|
|
import cn.com.ctop.common.module.utils.ResultMapUtils;
|
|
|
import cn.com.ctop.common.module.utils.StatusCode;
|
|
|
-import cn.com.ctop.crawler.modules.appium.entity.AppiumJob;
|
|
|
-import cn.com.ctop.crawler.modules.appium.entity.AppiumTask;
|
|
|
-import cn.com.ctop.crawler.modules.appium.entity.AppiumTaskItem;
|
|
|
+import cn.com.ctop.crawler.modules.appium.entity.*;
|
|
|
import cn.com.ctop.crawler.modules.appium.mapper.AppiumJobMapper;
|
|
|
import cn.com.ctop.crawler.modules.appium.mapper.AppiumTaskItemMapper;
|
|
|
+import cn.com.ctop.crawler.modules.appium.mapper.AppiumTaskLogMapper;
|
|
|
+import cn.com.ctop.crawler.modules.appium.service.IAppiumDeviceService;
|
|
|
import cn.com.ctop.crawler.modules.appium.service.IAppiumJobService;
|
|
|
+import cn.com.ctop.crawler.modules.appium.service.IAppiumTaskService;
|
|
|
import cn.com.ctop.crawler.modules.core.util.AppiumUtil;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import io.appium.java_client.android.AndroidDriver;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.impl.client.BasicCookieStore;
|
|
|
+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.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Stack;
|
|
|
|
|
|
/**
|
|
|
* 爬虫调度任务
|
|
@@ -33,6 +43,12 @@ public class AppiumJobServiceImpl extends ServiceImpl<AppiumJobMapper, AppiumJob
|
|
|
private AppiumJobMapper appiumJobMapper;
|
|
|
@Autowired
|
|
|
private AppiumTaskItemMapper taskItemMapper;
|
|
|
+ @Value("${jeecg.path.chrome-driver}")
|
|
|
+ private String chromeDriver;
|
|
|
+ @Autowired
|
|
|
+ private IAppiumDeviceService appiumDeviceService;
|
|
|
+ @Autowired
|
|
|
+ private AppiumTaskLogMapper appiumTaskLogMapper;
|
|
|
|
|
|
@Override
|
|
|
public void runTask(Long jobId, Long num) {
|
|
@@ -76,4 +92,155 @@ public class AppiumJobServiceImpl extends ServiceImpl<AppiumJobMapper, AppiumJob
|
|
|
System.out.println(kuaishouId);
|
|
|
return kuaishouId;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean bindCreative(String account, String password, String creativeName, String ksId) {
|
|
|
+ boolean isBind = false;
|
|
|
+ String url = "https://ad.e.kuaishou.com/#/welcome?redirectUrl=https%3A%2F%2Fad.e.kuaishou.com%2F%23%2Findex";
|
|
|
+ System.getProperties().setProperty("webdriver.chrome.driver", chromeDriver);
|
|
|
+ ChromeOptions chromeOptions = new ChromeOptions();
|
|
|
+ chromeOptions.addArguments("--headless");
|
|
|
+ chromeOptions.addArguments("--incognito");
|
|
|
+ chromeOptions.addArguments("--disable-gpu");
|
|
|
+// chromeOptions.addArguments("--no-sandbox");
|
|
|
+ chromeOptions.addArguments("--window-size=1920,1080");
|
|
|
+ chromeOptions.addArguments("--user-agent=" + HttpUtils2.USER_AGENT);
|
|
|
+ chromeOptions.setAcceptInsecureCerts(true);
|
|
|
+ WebDriver webDriver = new ChromeDriver(chromeOptions);
|
|
|
+ try {
|
|
|
+ Thread.sleep(3000L);
|
|
|
+ HttpUtils2.cookieStore = new BasicCookieStore();
|
|
|
+ webDriver.manage().deleteAllCookies();
|
|
|
+ //获取登录页面
|
|
|
+ webDriver.get(url);
|
|
|
+ Thread.sleep(3000L);
|
|
|
+ WebElement accountElement = webDriver.findElement(By.xpath("//div[@class='phone ']/input[@type='text']"));
|
|
|
+ accountElement.sendKeys(account);
|
|
|
+ Thread.sleep(3000L);
|
|
|
+ WebElement passwordElement = webDriver.findElement(By.xpath("//div[@class='password ']/input[@type='password']"));
|
|
|
+ passwordElement.sendKeys(password);
|
|
|
+ WebElement loginElement = webDriver.findElement(By.xpath("//div[@class='foot']"));
|
|
|
+ Thread.sleep(3000L);
|
|
|
+ //点击登录
|
|
|
+ loginElement.click();
|
|
|
+ Thread.sleep(3000L);
|
|
|
+ //获取推广按钮
|
|
|
+ WebElement spreadElement = webDriver.findElement(By.linkText("推广"));
|
|
|
+ spreadElement.click();
|
|
|
+ Thread.sleep(3000L);
|
|
|
+ //选择广告创意
|
|
|
+ WebElement creativeElement = webDriver.findElement(By.xpath("//div[text()='广告创意']"));
|
|
|
+ creativeElement.click();
|
|
|
+ Thread.sleep(3000L);
|
|
|
+ //输入创意名称,点击搜索
|
|
|
+ WebElement searchElement = webDriver.findElement(By.xpath("//input[@type='text']"));
|
|
|
+ searchElement.sendKeys(creativeName);
|
|
|
+ Thread.sleep(3000L);
|
|
|
+ WebElement searchButton = webDriver.findElement(By.xpath("//button[@class='ant-btn ant-input-search-button ant-btn-primary']"));
|
|
|
+ searchButton.click();
|
|
|
+ Thread.sleep(3000L);
|
|
|
+ WebElement tiyanElement = webDriver.findElement(By.linkText("体验"));
|
|
|
+ tiyanElement.click();
|
|
|
+ Thread.sleep(2000L);
|
|
|
+ WebElement inputKsIdElement = webDriver.findElement(By.xpath("//textarea[@placeholder='请输入快手账号…']"));
|
|
|
+ inputKsIdElement.sendKeys(ksId);
|
|
|
+ Thread.sleep(2000L);
|
|
|
+ WebElement chufaElement = webDriver.findElement(By.xpath("//button[@class='creative-experience-btn-enable']"));
|
|
|
+ chufaElement.click();
|
|
|
+ isBind = true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("绑定体验快手账号失败");
|
|
|
+ } finally {
|
|
|
+ webDriver.manage().deleteAllCookies();
|
|
|
+ webDriver.close();
|
|
|
+ return isBind;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IAppiumTaskService appiumTaskService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> startTask(String account, String password, String creativeName, Integer num) {
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ int i = 0;
|
|
|
+ while (i < num) {
|
|
|
+ if (i > num) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ //获取手机设备信息
|
|
|
+ AppiumDevice appiumDevice = appiumDeviceService.getById(2L);
|
|
|
+ appiumDevice.setStatus(2);
|
|
|
+ appiumDeviceService.updateById(appiumDevice);
|
|
|
+ AppiumTaskLog log = new AppiumTaskLog();
|
|
|
+ log.setDeviceId(2);
|
|
|
+ log.setDeviceIp(appiumDevice.getIp());
|
|
|
+ log.setDevicePort(appiumDevice.getPort());
|
|
|
+ log.setStatus(1);
|
|
|
+ log.setTaskId(1);
|
|
|
+ log.setTaskName("快手刷量任务");
|
|
|
+ appiumTaskLogMapper.insert(log);
|
|
|
+ log.setId(log.getId());
|
|
|
+ try {
|
|
|
+ String ksId = loginTask(2L);
|
|
|
+ if (null == ksId) {
|
|
|
+ updateStatus(appiumDevice, log, -1, 1);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ boolean isBind = bindCreative(account, password, creativeName, ksId);
|
|
|
+ if (!isBind) {
|
|
|
+ updateStatus(appiumDevice, log, -2, 1);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ appiumTaskService.runTask(1, 1);
|
|
|
+ i++;
|
|
|
+ } catch (Exception e) {
|
|
|
+ updateStatus(appiumDevice, log, -3, 1);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateStatus(AppiumDevice appiumDevice, AppiumTaskLog log, Integer logStatus, Integer deviceStatus) {
|
|
|
+ log.setStatus(3);
|
|
|
+ appiumTaskLogMapper.updateById(log);
|
|
|
+ appiumDevice.setStatus(1);
|
|
|
+ appiumDeviceService.updateById(appiumDevice);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isValid(String s) {
|
|
|
+ if (null == s || s.length() <= 0) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ if (s.length() % 2 == 1) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Stack<Character> stack = new Stack<>();
|
|
|
+ for (int i = 0; i < s.length(); i++) {
|
|
|
+ char getchar = s.charAt(i);
|
|
|
+ if (getchar == '(' || getchar == '{' || getchar == '[') {
|
|
|
+ stack.push(getchar);
|
|
|
+ }
|
|
|
+ if (getchar == ')' || getchar == '}' || getchar == ']') {
|
|
|
+ if (stack.empty()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ char sufix = stack.pop();
|
|
|
+ if ((getchar == ')' && sufix != '(') || (getchar == '}' && sufix != '{') || (getchar == ']' && sufix != '[')) {
|
|
|
+ stack.push(sufix);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (stack.empty()) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ System.out.println(isValid("[]()[]"));
|
|
|
+ }
|
|
|
}
|