QQBot/src/main/java/com/yutou/qqbot/utlis/WebClient.java

195 lines
7.1 KiB
Java
Raw Normal View History

package com.yutou.qqbot.utlis;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yutou.qqbot.models.Animal.TurnipProphet;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.io.File;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class WebClient {
private static WebClient client;
public static WebClient getInstance() {
if (client == null) {
client = new WebClient();
}
return client;
}
private WebClient() {
System.setProperty("webdriver.chrome.driver",
ConfigTools.load(ConfigTools.CONFIG, "chrome", String.class));
// java.util.logging.Logger.getLogger("org.openqa.selenium").setLevel(Level.OFF);
}
public Map<String, String> openTurnip(String prices, String pattern) throws Exception {
String url = String.format("https://turnipprophet.io?prices=%s%s",
prices,
pattern == null ? "" : "&pattern=" + pattern
);
System.out.println("url = " + url);
LinkedHashMap<String, String> map = new LinkedHashMap<>();
WebDriver webDriver = new ChromeDriver(getOptions());
webDriver.get(url);
webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(500);
WebElement element = webDriver.findElement(By.id("output"));
List<WebElement> list = element.findElements(By.tagName("tr"));
JSONObject pr = new JSONObject();
JSONArray array = new JSONArray();
if (list.size() == 0) {
return null;
}
for (WebElement webElement : list) {
String[] tmp;
try {
tmp = webElement.getText().replace(" ~ ", "~").split(" ");
} catch (Exception e) {
//e.printStackTrace();
continue;
}
if ("所有趋势".equals(tmp[0])) {
map.put(TurnipProphet.TurnipData.MONDAY_UP, tmp[3]);
map.put(TurnipProphet.TurnipData.MONDAY_DOWN, tmp[4]);
map.put(TurnipProphet.TurnipData.TUESDAY_UP, tmp[5]);
map.put(TurnipProphet.TurnipData.TUESDAY_DOWN, tmp[6]);
map.put(TurnipProphet.TurnipData.WEDNESDAY_UP, tmp[7]);
map.put(TurnipProphet.TurnipData.WEDNESDAY_DOWN, tmp[8]);
map.put(TurnipProphet.TurnipData.THURSDAY_UP, tmp[9]);
map.put(TurnipProphet.TurnipData.THURSDAY_DOWN, tmp[10]);
map.put(TurnipProphet.TurnipData.FRIDAY_UP, tmp[11]);
map.put(TurnipProphet.TurnipData.FRIDAY_DOWN, tmp[12]);
map.put(TurnipProphet.TurnipData.SATURDAY_UP, tmp[13]);
map.put(TurnipProphet.TurnipData.SATURDAY_DOWN, tmp[14]);
map.put(TurnipProphet.TurnipData.MIX, tmp[15]);
map.put(TurnipProphet.TurnipData.MAX, tmp[16]);
} else {
if (!array.toJSONString().contains(tmp[0])) {
JSONObject json = new JSONObject();
json.put(TurnipProphet.TurnipData.MODEL, tmp[0]);
json.put(TurnipProphet.TurnipData.PR, tmp[1]);
array.add(json);
}
}
}
for (String key : map.keySet()) {
System.err.println("记录最高日:" + map.get(key) + " > " + key);
if (!key.equals(TurnipProphet.TurnipData.MAX) &&
map.get(key).contains(map.get(TurnipProphet.TurnipData.MAX))) {
map.put(TurnipProphet.TurnipData.DAY, key);
break;
}
}
pr.put(TurnipProphet.TurnipData.MODEL, array);
map.put(TurnipProphet.TurnipData.MODEL, pr.toJSONString());
webDriver.close();
webDriver.quit();
return map;
}
public String tsdm(String url) {
JSONArray array = JSONArray.parseArray(ConfigTools.readFile(new File("cookie.json")));
if (array == null) {
System.err.println("array is null");
return null;
}
WebDriver driver = new ChromeDriver(getOptions());
driver.get(url);
getTsdm(array, driver);
try {
((JavascriptExecutor) driver).executeScript("document.getElementsByClassName('y viewpay')[0].click()");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.findElement(By.id("payform")).submit();
} catch (Exception ignored) {
}
String body = driver.findElement(By.name("tsdmlz")).getText();
driver.close();
driver.quit();
return body;
}
public void tsdmSign(){
JSONArray array = JSONArray.parseArray(ConfigTools.readFile(new File("cookie.json")));
if (array == null) {
System.err.println("array is null");
return;
}
WebDriver driver = new ChromeDriver(getOptions());
driver.get("https://www.tsdm39.net/forum.php");
getTsdm(array, driver);
try {
((JavascriptExecutor) driver).executeScript("showWindow('dsu_paulsign', 'plugin.php?id=dsu_paulsign:sign&9b5e8da2')");
}catch (Exception ignored){
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.findElement(By.id("wl")).click();
driver.findElement(By.xpath("//input[@value='3']")).click();
driver.findElement(By.id("qiandao")).submit();
driver.close();
driver.quit();
}
private void getTsdm(JSONArray array, WebDriver driver) {
driver.manage().timeouts().implicitlyWait(10000, TimeUnit.SECONDS);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (Object o : array) {
JSONObject json = (JSONObject) o;
long t = Long.parseLong(json.getString("expirationDate").replace(".", "")) / 1000;
Cookie cookie = new Cookie(json.getString("name"),
json.getString("value"),
json.getString("domain"),
json.getString("path"),
new Date(t),
json.getBoolean("secure"),
json.getBoolean("httpOnly")
);
driver.manage().addCookie(cookie);
}
driver.navigate().refresh();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private ChromeOptions getOptions() {
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
options.addArguments("blink-settings=imagesEnabled=false");
options.addArguments("--headless");
options.addArguments("--no-sandbox");
return options;
}
public static void main(String[] args) {
WebClient.getInstance().tsdmSign();
}
}