From b7c61453c566c9879314e881648cdb34bf90aaa9 Mon Sep 17 00:00:00 2001 From: lensferno Date: Mon, 10 Jul 2023 11:26:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=A0=94=E7=A9=B6=E7=94=9F=EF=BC=9A?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=AA=8C=E8=AF=81=E7=A0=81=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E9=87=8D=E8=AF=95;=20ref:=20=E7=A0=94?= =?UTF-8?q?=E7=A9=B6=E7=94=9F=EF=BC=9A=E4=BF=AE=E6=94=B9=E5=9F=B9=E5=85=BB?= =?UTF-8?q?=E6=96=B9=E6=A1=88=E8=A7=A3=E6=9E=90=E6=95=B0=E6=8D=AE=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F;=20=E7=89=A9=E7=90=86=E5=AE=9E=E9=AA=8C=EF=BC=9A?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=99=BB=E5=BD=95=E7=BB=93=E6=9E=9C=E2=80=9C?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=B8=8D=E5=9C=A8=E5=BD=93=E5=89=8D=E5=AD=A6?= =?UTF-8?q?=E6=9C=9F=E2=80=9D=E4=BD=9C=E4=B8=BA=E7=8B=AC=E7=AB=8B=E7=9A=84?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E6=83=85=E5=86=B5=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mywust/exception/ApiException.java | 9 ++++++ .../GraduateTrainingPlanPageParser.java | 5 +--- .../core/request/factory/RequestFactory.java | 6 +++- .../request/service/auth/GraduateLogin.java | 29 ++++++++++++++++++- .../request/service/auth/PhysicsLogin.java | 13 +++++++-- 5 files changed, 53 insertions(+), 9 deletions(-) diff --git a/mywust-common/src/main/java/cn/wustlinghang/mywust/exception/ApiException.java b/mywust-common/src/main/java/cn/wustlinghang/mywust/exception/ApiException.java index 18edd12..a00d7ee 100644 --- a/mywust-common/src/main/java/cn/wustlinghang/mywust/exception/ApiException.java +++ b/mywust-common/src/main/java/cn/wustlinghang/mywust/exception/ApiException.java @@ -128,6 +128,11 @@ public class ApiException extends BasicException { */ PHYSICS_PASSWORD_WRONG(130100, "物理实验系统登录: 密码错误"), + /** + * 物理实验系统用户不存在于当前学期 + */ + PHYSICS_NOT_CURRENT_TERM(130101, "物理实验系统登录: 当前用户不存在于当前学期"), + // -------------------------------- // 研究生API异常代码 @@ -136,6 +141,10 @@ public class ApiException extends BasicException { */ GRADUATE_PASSWORD_WRONG(140100, "研究生登录: 密码错误"), + /** + * 研究生验证码错误 + */ + GRADUATE_CAPTCHA_WRONG(140101, "研究生登录: 验证码错误"), // -------------------------------- // 图书馆API异常代码 diff --git a/mywust-core/src/main/java/cn/wustlinghang/mywust/core/parser/graduate/GraduateTrainingPlanPageParser.java b/mywust-core/src/main/java/cn/wustlinghang/mywust/core/parser/graduate/GraduateTrainingPlanPageParser.java index 1c6d703..431b7d7 100644 --- a/mywust-core/src/main/java/cn/wustlinghang/mywust/core/parser/graduate/GraduateTrainingPlanPageParser.java +++ b/mywust-core/src/main/java/cn/wustlinghang/mywust/core/parser/graduate/GraduateTrainingPlanPageParser.java @@ -9,9 +9,6 @@ import org.jsoup.nodes.Element; public class GraduateTrainingPlanPageParser implements Parser { - private static final String HTML_PREFIX = "培养计划"; - private static final String HTML_SUFFIX = ""; - @Override public String parse(String html) throws ParseException { try { @@ -25,7 +22,7 @@ public class GraduateTrainingPlanPageParser implements Parser { String table = JsoupUtil.getOuterHtml(fullTable.getElementById("_ctl0_MainWork_dgData")); String foot = JsoupUtil.getOuterHtml(fullTable.getElementById("Table4")); - return HTML_PREFIX + head + table + foot + HTML_SUFFIX; + return head + table + foot; }catch (Exception e) { // 解析失败就直接返回原网页展示 return html; diff --git a/mywust-core/src/main/java/cn/wustlinghang/mywust/core/request/factory/RequestFactory.java b/mywust-core/src/main/java/cn/wustlinghang/mywust/core/request/factory/RequestFactory.java index d5547bd..41426e6 100644 --- a/mywust-core/src/main/java/cn/wustlinghang/mywust/core/request/factory/RequestFactory.java +++ b/mywust-core/src/main/java/cn/wustlinghang/mywust/core/request/factory/RequestFactory.java @@ -1,11 +1,14 @@ package cn.wustlinghang.mywust.core.request.factory; import cn.wustlinghang.mywust.network.entitys.HttpRequest; +import lombok.extern.slf4j.Slf4j; +import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.Map; +@Slf4j public class RequestFactory { protected static final HttpRequest DEFAULT_HTTP_REQUEST = new HttpRequest(); @@ -52,7 +55,8 @@ public class RequestFactory { public static URL makeUrl(String url) { try { return new URL(url); - } catch (Exception e) { + } catch (MalformedURLException e) { + log.error("[mywust]: Invalid url: {} , {}", url, e.getMessage()); return null; } } diff --git a/mywust-core/src/main/java/cn/wustlinghang/mywust/core/request/service/auth/GraduateLogin.java b/mywust-core/src/main/java/cn/wustlinghang/mywust/core/request/service/auth/GraduateLogin.java index 6ce19f1..707ed0a 100644 --- a/mywust-core/src/main/java/cn/wustlinghang/mywust/core/request/service/auth/GraduateLogin.java +++ b/mywust-core/src/main/java/cn/wustlinghang/mywust/core/request/service/auth/GraduateLogin.java @@ -11,6 +11,7 @@ import cn.wustlinghang.mywust.network.RequestClientOption; import cn.wustlinghang.mywust.network.Requester; import cn.wustlinghang.mywust.network.entitys.HttpRequest; import cn.wustlinghang.mywust.network.entitys.HttpResponse; +import lombok.extern.slf4j.Slf4j; import javax.imageio.ImageIO; import java.awt.*; @@ -18,7 +19,9 @@ import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.nio.charset.StandardCharsets; +@Slf4j public class GraduateLogin { private final Requester requester; @@ -55,13 +58,37 @@ public class GraduateLogin { // 登陆成功,应该会是302跳转,不是的话多半是认证错误 if (loginResponse.getStatusCode() != HttpResponse.HTTP_REDIRECT_302) { - throw new ApiException(ApiException.Code.GRADUATE_PASSWORD_WRONG); + String responseHtml = loginResponse.getStringBody(); + if (responseHtml.contains("验证码错误")) { + throw new ApiException(ApiException.Code.GRADUATE_CAPTCHA_WRONG); + } else if (responseHtml.contains("密码错误") || responseHtml.contains("用户名不存在")) { + throw new ApiException(ApiException.Code.GRADUATE_PASSWORD_WRONG); + } else { + throw new ApiException(ApiException.Code.UNKNOWN_EXCEPTION); + } } // 使用首页第一次访问得到的cookie来作为登录cookie return loginCookie; } + public String getLoginCookie(String username, String password, int maxRetryTimes, RequestClientOption option) + throws IOException, ApiException { + for (int i = 0; i < maxRetryTimes; i++) { + try { + return getLoginCookie(username, password, option); + } catch (ApiException e) { + if (e.getCode() != ApiException.Code.GRADUATE_CAPTCHA_WRONG) { + throw e; + } else { + log.info("[mywust]: Retrying login for {} time(s)", i); + } + } + } + + return ""; + } + public void checkCookies(String cookie, RequestClientOption option) throws ApiException, IOException { HttpRequest request = RequestFactory.makeHttpRequest(GraduateUrls.GRADUATE_INDEX_TEST_API, null, cookie); HttpResponse response = requester.get(request, option); diff --git a/mywust-core/src/main/java/cn/wustlinghang/mywust/core/request/service/auth/PhysicsLogin.java b/mywust-core/src/main/java/cn/wustlinghang/mywust/core/request/service/auth/PhysicsLogin.java index 09759c5..93adc9a 100644 --- a/mywust-core/src/main/java/cn/wustlinghang/mywust/core/request/service/auth/PhysicsLogin.java +++ b/mywust-core/src/main/java/cn/wustlinghang/mywust/core/request/service/auth/PhysicsLogin.java @@ -1,9 +1,9 @@ package cn.wustlinghang.mywust.core.request.service.auth; -import cn.wustlinghang.mywust.exception.ApiException; -import cn.wustlinghang.mywust.exception.ParseException; import cn.wustlinghang.mywust.core.parser.physics.PhysicsIndexPageParser; import cn.wustlinghang.mywust.core.request.factory.physics.PhysicsSystemRequestFactory; +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.exception.ParseException; import cn.wustlinghang.mywust.network.RequestClientOption; import cn.wustlinghang.mywust.network.Requester; import cn.wustlinghang.mywust.network.entitys.HttpRequest; @@ -34,7 +34,14 @@ public class PhysicsLogin { HttpRequest loginCookieRequest = PhysicsSystemRequestFactory.loginCookiesRequest(username, password, loginIndex); HttpResponse loginCookieResponse = requester.post(loginCookieRequest, requestClientOption); if (loginCookieResponse.getStatusCode() != HttpResponse.HTTP_REDIRECT_302) { - throw new ApiException(ApiException.Code.PHYSICS_PASSWORD_WRONG); + String responseHtml = loginCookieResponse.getStringBody(); + if (responseHtml.contains("该用户不存在于当前学期")){ + throw new ApiException(ApiException.Code.PHYSICS_NOT_CURRENT_TERM); + } else if (responseHtml.contains("用户名或者密码有误")) { + throw new ApiException(ApiException.Code.PHYSICS_PASSWORD_WRONG); + } else { + throw new ApiException(ApiException.Code.UNKNOWN_EXCEPTION); + } } String loginCookies = loginCookieResponse.getCookies();