From 90b971847197372df851a433ed200fafbacefa57 Mon Sep 17 00:00:00 2001 From: Yutousama <583819556@qq.com> Date: Thu, 9 Dec 2021 12:39:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=A4=A9=E4=BD=BF=E5=8A=A8?= =?UTF-8?q?=E6=BC=AB=E4=B8=8B=E8=BD=BD=E4=BB=A5=E5=8F=8A=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E7=AD=BE=E5=88=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/yutou/qqbot/QQBotManager.java | 2 + .../com/yutou/qqbot/models/Commands/Tsdm.java | 44 ++++++++ .../java/com/yutou/qqbot/models/Model.java | 2 + .../java/com/yutou/qqbot/utlis/WebClient.java | 105 +++++++++++++++--- 4 files changed, 140 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/yutou/qqbot/models/Commands/Tsdm.java diff --git a/src/main/java/com/yutou/qqbot/QQBotManager.java b/src/main/java/com/yutou/qqbot/QQBotManager.java index 2d6fb45..d2927d8 100644 --- a/src/main/java/com/yutou/qqbot/QQBotManager.java +++ b/src/main/java/com/yutou/qqbot/QQBotManager.java @@ -4,6 +4,7 @@ import com.yutou.qqbot.Listeners.QQMessageListener; import com.yutou.qqbot.models.Animal.TurnipProphet; import com.yutou.qqbot.models.Commands.Bangumi; import com.yutou.qqbot.models.Commands.System.*; +import com.yutou.qqbot.models.Commands.Tsdm; import com.yutou.qqbot.models.Model; import com.yutou.qqbot.models.setu.QQSetu; import com.yutou.qqbot.utlis.*; @@ -38,6 +39,7 @@ public class QQBotManager { Model.classList.add(Bangumi.class); Model.classList.add(TurnipProphet.class); + Model.classList.add(Tsdm.class); } private static QQBotManager botManager = null; private Bot bot; diff --git a/src/main/java/com/yutou/qqbot/models/Commands/Tsdm.java b/src/main/java/com/yutou/qqbot/models/Commands/Tsdm.java new file mode 100644 index 0000000..a528be7 --- /dev/null +++ b/src/main/java/com/yutou/qqbot/models/Commands/Tsdm.java @@ -0,0 +1,44 @@ +package com.yutou.qqbot.models.Commands; + +import com.yutou.qqbot.QQBotManager; +import com.yutou.qqbot.models.Model; +import com.yutou.qqbot.utlis.StringUtils; +import com.yutou.qqbot.utlis.WebClient; +import net.mamoe.mirai.event.events.MessageEvent; + +public class Tsdm extends Model { + @Override + public boolean isUserPublic() { + return false; + } + + @Override + public String[] getUsePowers() { + return new String[]{ + QQFromCommands.TSDM_PAY + }; + } + + @Override + public void onMessage(Long qq, MessageEvent event, boolean isGroup) { + super.onMessage(qq, event, isGroup); + if(msg.startsWith(QQFromCommands.TSDM_PAY)){ + String url=msg.replace(QQFromCommands.TSDM_PAY,"").trim(); + if(StringUtils.isEmpty(url)){ + QQBotManager.getInstance().sendMessage(qq,"使用说明:!tsdm [url]"); + return; + } + QQBotManager.getInstance().sendMessage(qq,"正在获取,请稍等..."); + QQBotManager.getInstance().sendMessage(qq,WebClient.getInstance().tsdm(url)); + } + } + + @Override + public void onTime(String time) { + super.onTime(time); + if("08:00".equals(time)){ + WebClient.getInstance().tsdmSign(); + QQBotManager.getInstance().sendMessage(QQBotManager.defGroup,"已完成天使动漫的签到"); + } + } +} diff --git a/src/main/java/com/yutou/qqbot/models/Model.java b/src/main/java/com/yutou/qqbot/models/Model.java index 0556390..5370033 100644 --- a/src/main/java/com/yutou/qqbot/models/Model.java +++ b/src/main/java/com/yutou/qqbot/models/Model.java @@ -36,6 +36,8 @@ public abstract class Model implements ModelInterface { public static class QQFromCommands { public static final String TURNIP_PROPHET = "大头菜"; + public static final String TSDM_PAY = "!tsdm"; + public static final String TSDM_SIGN = "!tsdm签到"; } diff --git a/src/main/java/com/yutou/qqbot/utlis/WebClient.java b/src/main/java/com/yutou/qqbot/utlis/WebClient.java index 795dc78..b22e65a 100644 --- a/src/main/java/com/yutou/qqbot/utlis/WebClient.java +++ b/src/main/java/com/yutou/qqbot/utlis/WebClient.java @@ -3,17 +3,16 @@ 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.By; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.WebElement; +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; -import java.util.logging.Level; public class WebClient { private static WebClient client; @@ -28,12 +27,12 @@ public class WebClient { 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); + ConfigTools.load(ConfigTools.CONFIG, "chrome", String.class)); + // java.util.logging.Logger.getLogger("org.openqa.selenium").setLevel(Level.OFF); } public Map openTurnip(String prices, String pattern) throws Exception { - String url=String.format("https://turnipprophet.io?prices=%s%s", + String url = String.format("https://turnipprophet.io?prices=%s%s", prices, pattern == null ? "" : "&pattern=" + pattern ); @@ -48,7 +47,7 @@ public class WebClient { List list = element.findElements(By.tagName("tr")); JSONObject pr = new JSONObject(); JSONArray array = new JSONArray(); - if(list.size()==0){ + if (list.size() == 0) { return null; } @@ -86,7 +85,7 @@ public class WebClient { } } for (String key : map.keySet()) { - System.err.println("记录最高日:"+map.get(key)+" > "+key); + 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); @@ -96,20 +95,100 @@ public class WebClient { 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("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(); } }