|
@@ -0,0 +1,118 @@
|
|
|
+package cn.com.ctop.crawler.modules.pangolin.service.impl;
|
|
|
+
|
|
|
+import cn.com.ctop.common.module.entity.BindAccountLogin;
|
|
|
+import cn.com.ctop.common.module.service.IBindAccountLoginService;
|
|
|
+import cn.com.ctop.common.module.utils.ChaojiyingUtils;
|
|
|
+import cn.com.ctop.common.module.utils.HttpUtils2;
|
|
|
+import cn.com.ctop.crawler.modules.pangolin.service.PangolinLoginService;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import org.apache.http.impl.client.BasicCookieStore;
|
|
|
+import org.apache.http.impl.cookie.BasicClientCookie;
|
|
|
+import org.openqa.selenium.*;
|
|
|
+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.*;
|
|
|
+
|
|
|
+
|
|
|
+@Service
|
|
|
+public class PangolinLoginServiceImpl implements PangolinLoginService {
|
|
|
+ @Autowired
|
|
|
+ private IBindAccountLoginService bindAccountLoginService;
|
|
|
+ @Value("${jeecg.path.chrome-driver}")
|
|
|
+ private String chromeDriver;
|
|
|
+
|
|
|
+ public static void main(String[] args){
|
|
|
+ PangolinLoginServiceImpl loginService = new PangolinLoginServiceImpl();
|
|
|
+ loginService.pangolinLogin("douyin_novel60","BpGT7afFWU");
|
|
|
+ String result = HttpUtils2.httpGetRequest("https://pangolin.bytedance.com/api/search/external_default");
|
|
|
+ System.out.println(result);
|
|
|
+ }
|
|
|
+ public void pangolinLogin(String username,String password) {
|
|
|
+ System.getProperties().setProperty("webdriver.chrome.driver", chromeDriver);
|
|
|
+ String url = "https://pangolin.bytedance.com/auth/login";
|
|
|
+ ChromeOptions chromeOptions = new ChromeOptions();
|
|
|
+ chromeOptions.addArguments("--headless");
|
|
|
+ chromeOptions.addArguments("--incognito");
|
|
|
+ chromeOptions.addArguments("--disable-gpu");
|
|
|
+ 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);
|
|
|
+ List<WebElement> list = webDriver.findElements(By.xpath("//input[@id='captcha']"));
|
|
|
+ do {
|
|
|
+ Thread.sleep(3000L);
|
|
|
+ WebElement accountElement = webDriver.findElement(By.xpath("//input[@id='username']"));
|
|
|
+ accountElement.sendKeys(username);
|
|
|
+ Thread.sleep(3000L);
|
|
|
+ WebElement passwordElement = webDriver.findElement(By.xpath("//input[@id='password']"));
|
|
|
+ passwordElement.sendKeys(password);
|
|
|
+ WebElement captchaElement = webDriver.findElement(By.xpath("//div[@class='ant-form-item-control ']/img"));
|
|
|
+ String outputType = captchaElement.getScreenshotAs(OutputType.BASE64);
|
|
|
+ String res = ChaojiyingUtils.process(outputType,1902);
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ JsonNode n = mapper.readTree(res);
|
|
|
+ String captcha = n.get("pic_str").asText();
|
|
|
+ WebElement captchaInputElement = webDriver.findElement(By.xpath("//input[@id='captcha']"));
|
|
|
+ captchaInputElement.sendKeys(captcha);
|
|
|
+ WebElement loginElement = webDriver.findElement(By.xpath("//button[@type='submit']"));
|
|
|
+ Thread.sleep(3000L);
|
|
|
+ //点击登录
|
|
|
+ loginElement.click();
|
|
|
+ Thread.sleep(3000L);
|
|
|
+ webDriver.get("https://pangolin.bytedance.com");
|
|
|
+ list = webDriver.findElements(By.xpath("//input[@id='captcha']"));
|
|
|
+ }while (list != null && list.size() > 0);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Set<Cookie> cookies = webDriver.manage().getCookies();
|
|
|
+ List<String> cookieList = new ArrayList<>();
|
|
|
+ Iterator iterator = cookies.iterator();
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ String cookieStr = iterator.next().toString();
|
|
|
+ cookieList.add(cookieStr);
|
|
|
+ String[] cookieArray = cookieStr.split(";");
|
|
|
+ Map<String, String> cookieMap = new HashMap<String, String>();
|
|
|
+ for (String cookie : cookieArray) {
|
|
|
+ String[] kv = cookie.split("=");
|
|
|
+ if (kv.length > 1) {
|
|
|
+ cookieMap.put(kv[0].trim(), kv[1].trim());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ BasicClientCookie clientCookie = new BasicClientCookie(cookieArray[0].split("=")[0],
|
|
|
+ cookieArray[0].split("=")[1]);
|
|
|
+ clientCookie.setDomain("pangolin.bytedance.com");
|
|
|
+ clientCookie.setPath(cookieMap.get("path"));
|
|
|
+ HttpUtils2.cookieStore.addCookie(clientCookie);
|
|
|
+ }
|
|
|
+
|
|
|
+ QueryWrapper<BindAccountLogin> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("account_name",username);
|
|
|
+ queryWrapper.eq("login_type","pangolin");
|
|
|
+ BindAccountLogin bindAccountLogin = bindAccountLoginService.getOne(queryWrapper);
|
|
|
+ if (bindAccountLogin != null){
|
|
|
+ bindAccountLogin.setCookie(new Gson().toJson(cookieList));
|
|
|
+ bindAccountLoginService.updateById(bindAccountLogin);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ webDriver.manage().deleteAllCookies();
|
|
|
+// webDriver.close();
|
|
|
+ webDriver.quit();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|