From b4484f1424d9cc9560cb252914b59094e851d9de Mon Sep 17 00:00:00 2001 From: Yutousama <583819556@qq.com> Date: Tue, 8 Feb 2022 12:27:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9ENicePT=E7=AD=BE=E5=88=B0=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9Ehttps=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../qqbot/MessageEvent/AdminMessage.java | 8 +++ .../yutou/qqbot/models/WebSign/NicePT.java | 66 +++++++++++++++++++ .../java/com/yutou/qqbot/utlis/HttpTools.java | 53 ++++++++++----- .../java/com/yutou/qqbot/utlis/WebClient.java | 4 +- 4 files changed, 111 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/yutou/qqbot/models/WebSign/NicePT.java diff --git a/src/main/java/com/yutou/qqbot/MessageEvent/AdminMessage.java b/src/main/java/com/yutou/qqbot/MessageEvent/AdminMessage.java index 550915c..d08cb15 100644 --- a/src/main/java/com/yutou/qqbot/MessageEvent/AdminMessage.java +++ b/src/main/java/com/yutou/qqbot/MessageEvent/AdminMessage.java @@ -4,6 +4,7 @@ import com.yutou.qqbot.QQBotManager; import com.yutou.qqbot.QQNumberManager; import com.yutou.qqbot.models.Model; import com.yutou.qqbot.models.WebSign.BaiHeHui; +import com.yutou.qqbot.models.WebSign.NicePT; import com.yutou.qqbot.models.WebSign.Tsdm; import com.yutou.qqbot.utlis.AppTools; import com.yutou.qqbot.utlis.Log; @@ -159,6 +160,13 @@ public class AdminMessage extends Message { QQBotManager.getInstance().sendMessage(qq,"百合会签到失败:"+AppTools.getExceptionString(e)); e.printStackTrace(); } + try { + new NicePT().sign(); + }catch (Exception e){ + sign=false; + QQBotManager.getInstance().sendMessage(qq,"NicePT签到失败:"+AppTools.getExceptionString(e)); + e.printStackTrace(); + } if(sign){ QQBotManager.getInstance().sendMessage(qq,"签到任务完成"); } diff --git a/src/main/java/com/yutou/qqbot/models/WebSign/NicePT.java b/src/main/java/com/yutou/qqbot/models/WebSign/NicePT.java new file mode 100644 index 0000000..d00c3b5 --- /dev/null +++ b/src/main/java/com/yutou/qqbot/models/WebSign/NicePT.java @@ -0,0 +1,66 @@ +package com.yutou.qqbot.models.WebSign; + +import com.alibaba.fastjson.JSONArray; +import com.yutou.qqbot.QQBotManager; +import com.yutou.qqbot.models.Model; +import com.yutou.qqbot.utlis.*; +import org.openqa.selenium.Cookie; +import org.openqa.selenium.WebDriver; + +import java.io.File; +import java.util.concurrent.TimeUnit; + +public class NicePT extends Model { + @Override + public boolean isUserPublic() { + return false; + } + + @Override + public String[] getUsePowers() { + return new String[0]; + } + + @Override + public void onTime(String time) { + super.onTime(time); + if ("08:01:00".equals(time)) { + Log.i("开始天使动漫签到"); + try { + sign(); + QQBotManager.getInstance().sendMessage(QQBotManager.defGroup, "已完成NicePT的签到"); + } catch (Exception e) { + QQBotManager.getInstance().sendMessage(QQBotManager.defGroup, "NicePT签到失败:" + AppTools.getExceptionString(e)); + e.printStackTrace(); + } + } + } + + public void sign(){ + String url="https://www.nicept.net/attendance.php"; + JSONArray array = JSONArray.parseArray(ConfigTools.readFile(new File("nicept.json"))); + if (array == null) { + System.err.println("array is null"); + return; + } + WebDriver driver = WebClient.getInstance().getWebDriver(); + driver.manage().timeouts().implicitlyWait(10000, TimeUnit.SECONDS); + driver.get(url); + for (Cookie _cookie : WebClient.loadCookie(array)) { + driver.manage().addCookie(_cookie); + } + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + driver.get(url); + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + driver.close(); + driver.quit(); + } +} diff --git a/src/main/java/com/yutou/qqbot/utlis/HttpTools.java b/src/main/java/com/yutou/qqbot/utlis/HttpTools.java index 5810336..a2aa07e 100644 --- a/src/main/java/com/yutou/qqbot/utlis/HttpTools.java +++ b/src/main/java/com/yutou/qqbot/utlis/HttpTools.java @@ -2,6 +2,7 @@ package com.yutou.qqbot.utlis; import com.alibaba.fastjson.JSONObject; import com.yutou.qqbot.interfaces.DownloadInterface; +import org.jetbrains.annotations.NotNull; import javax.net.ssl.HttpsURLConnection; import java.io.*; @@ -14,7 +15,7 @@ public class HttpTools { private static final int HttpRequestIndex = 3; public static String get(String url) { - return https_get(url, null); + return http_get(url,null); } public static String post(final String url, final byte[] body) { @@ -27,29 +28,45 @@ public class HttpTools { public static String https_get(String url, Map header) { try { - URLConnection connection; - connection = new URL(url).openConnection(); - connection.setRequestProperty("User-Agent", getExtUa()); - if (header != null) { - for (String key : header.keySet()) { - connection.addRequestProperty(key, header.get(key)); - } - } - connection.connect(); - BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); - StringBuilder str = new StringBuilder(); - String tmp; - while ((tmp = reader.readLine()) != null) { - str.append(tmp); - } - reader.close(); - return str.toString(); + HttpsURLConnection connection; + connection = (HttpsURLConnection) new URL(url).openConnection(); + return urlConnection(header, connection); } catch (Exception e) { System.err.println("error url = " + url); e.printStackTrace(); } return null; } + public static String http_get(String url,Map header){ + try { + HttpURLConnection connection; + connection = (HttpURLConnection) new URL(url).openConnection(); + return urlConnection(header, connection); + } catch (Exception e) { + System.err.println("error url = " + url); + e.printStackTrace(); + } + return null; + } + + @NotNull + private static String urlConnection(Map header, HttpURLConnection connection) throws IOException { + connection.setRequestProperty("User-Agent", getExtUa()); + if (header != null) { + for (String key : header.keySet()) { + connection.addRequestProperty(key, header.get(key)); + } + } + connection.connect(); + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + StringBuilder str = new StringBuilder(); + String tmp; + while ((tmp = reader.readLine()) != null) { + str.append(tmp); + } + reader.close(); + return str.toString(); + } public static String http_post(String url, byte[] body, int index, Map headers) { String tmp; diff --git a/src/main/java/com/yutou/qqbot/utlis/WebClient.java b/src/main/java/com/yutou/qqbot/utlis/WebClient.java index 7f679be..9d1e336 100644 --- a/src/main/java/com/yutou/qqbot/utlis/WebClient.java +++ b/src/main/java/com/yutou/qqbot/utlis/WebClient.java @@ -62,8 +62,8 @@ public class WebClient { ChromeOptions options = new ChromeOptions(); // options.addArguments("--disable-gpu"); // options.addArguments("blink-settings=imagesEnabled=false"); - options.addArguments("--headless"); - options.addArguments("--no-sandbox"); + // options.addArguments("--headless"); + // options.addArguments("--no-sandbox"); // options.addArguments("--incognito"); options.addArguments("--disable-plugins"); options.addArguments("--lang=zh-CN");