feat: 研究生:新增验证码错误自动重试;

ref: 研究生:修改培养方案解析数据格式; 物理实验:修改登录结果“用户不在当前学期”作为独立的异常情况处理
main
lensfrex 1 year ago
parent bf828e3418
commit b7c61453c5
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 9
      mywust-common/src/main/java/cn/wustlinghang/mywust/exception/ApiException.java
  2. 5
      mywust-core/src/main/java/cn/wustlinghang/mywust/core/parser/graduate/GraduateTrainingPlanPageParser.java
  3. 6
      mywust-core/src/main/java/cn/wustlinghang/mywust/core/request/factory/RequestFactory.java
  4. 29
      mywust-core/src/main/java/cn/wustlinghang/mywust/core/request/service/auth/GraduateLogin.java
  5. 13
      mywust-core/src/main/java/cn/wustlinghang/mywust/core/request/service/auth/PhysicsLogin.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异常代码

@ -9,9 +9,6 @@ import org.jsoup.nodes.Element;
public class GraduateTrainingPlanPageParser implements Parser<String> {
private static final String HTML_PREFIX = "<!DOCTYPE html><html lang=\"zh\"><head><meta charset=\"UTF-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><title>培养计划</title></head><body>";
private static final String HTML_SUFFIX = "</body></html>";
@Override
public String parse(String html) throws ParseException {
try {
@ -25,7 +22,7 @@ public class GraduateTrainingPlanPageParser implements Parser<String> {
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;

@ -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;
}
}

@ -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);

@ -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();

Loading…
Cancel
Save