更新太多了,主要是代码结构的一些调整,一方面是service包下只保留请求部分,解析由调用方自行调用(因为存在部分只需要请求而不需要解析的情况),将本科教学系统的功能api拆分成不同的类,调用者可按需生成实例,api接口异常改用通用的业务异常
parent
3bc1df9836
commit
f3aa0d4be0
@ -1,38 +1,105 @@ |
||||
package cn.linghang.mywust.core.exception; |
||||
|
||||
public class ApiException extends BasicException { |
||||
private final int code; |
||||
private final Code code; |
||||
|
||||
public ApiException(int code) { |
||||
public ApiException(Code code) { |
||||
this.code = code; |
||||
} |
||||
|
||||
public ApiException(int code, String message) { |
||||
public ApiException(Code code, String message) { |
||||
super(message); |
||||
this.code = code; |
||||
} |
||||
|
||||
public int getCode() { |
||||
public Code getCode() { |
||||
return code; |
||||
} |
||||
|
||||
public int getCodeValue() { |
||||
return code.value; |
||||
} |
||||
|
||||
public String getCodeDescribe() { |
||||
return code.describe; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "接口调用异常: " + code; |
||||
} |
||||
|
||||
public static class ApiExceptionCode { |
||||
// 统一认证的异常
|
||||
public static final int UNI_LOGIN_PASSWORD_WRONG = 100101; |
||||
public static final int UNI_LOGIN_USER_NOT_EXISTS = 100102; |
||||
public static final int UNI_LOGIN_USER_BANNED = 100103; |
||||
public enum Code { |
||||
/** |
||||
* 未知的API异常 |
||||
*/ |
||||
UNKNOWN_EXCEPTION(-1, "未知错误(开发又有活干啦)"), |
||||
|
||||
// --------------------------------
|
||||
// 统一认证的异常(本科生、图书馆)
|
||||
|
||||
/** |
||||
* 密码错误 |
||||
*/ |
||||
UNI_LOGIN_PASSWORD_WRONG(100100, "统一认证登录: 密码错误"), |
||||
|
||||
/** |
||||
* 用户不存在 |
||||
*/ |
||||
UNI_LOGIN_USER_NOT_EXISTS(100101, "统一认证登录: 用户不存在"), |
||||
|
||||
/** |
||||
* 用户登录被封禁 |
||||
*/ |
||||
UNI_LOGIN_USER_BANNED(100102, "统一认证登录: 用户被封禁"), |
||||
|
||||
// --------------------------------
|
||||
// 共有异常码:cookie无效
|
||||
// 放一起了
|
||||
public static final int BKJX_COOKIE_INVALID = 110101; |
||||
public static final int LIBRARY_COOKIE_INVALID = 12101; |
||||
public static final int PHYSICS_COOKIE_INVALID = 130101; |
||||
public static final int GRADE_COOKIE_INVALID = 140101; |
||||
} |
||||
|
||||
/** |
||||
* Cookie无效 |
||||
*/ |
||||
COOKIE_INVALID(100101, "Cookie无效"), |
||||
|
||||
// --------------------------------
|
||||
// 本科生API异常代码
|
||||
|
||||
/** |
||||
* 需要评教 |
||||
*/ |
||||
BKJX_COURSE_NEED_EVALUATE(110102, "需要评教"), |
||||
|
||||
// --------------------------------
|
||||
// 物理实验系统API异常代码
|
||||
|
||||
/** |
||||
* 物理实验系统密码错误 |
||||
*/ |
||||
PHYSICS_PASSWORD_WRONG(130100, "物理实验系统登录: 密码错误"), |
||||
|
||||
// --------------------------------
|
||||
// 研究生API异常代码
|
||||
|
||||
/** |
||||
* 研究生密码错误 |
||||
*/ |
||||
GRADUATE_PASSWORD_WRONG(140100, "研究生登录: 密码错误"), |
||||
; |
||||
|
||||
// --------------------------------
|
||||
// 图书馆API异常代码
|
||||
|
||||
private final int value; |
||||
private final String describe; |
||||
|
||||
Code(int value, String describe) { |
||||
this.value = value; |
||||
this.describe = describe; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return String.format("%s(%d)", describe, value); |
||||
} |
||||
} |
||||
} |
@ -1,14 +0,0 @@ |
||||
package cn.linghang.mywust.core.exception; |
||||
|
||||
public class CookieInvalidException extends BasicException { |
||||
public CookieInvalidException() { |
||||
} |
||||
|
||||
public CookieInvalidException(String message) { |
||||
super(message); |
||||
} |
||||
|
||||
public CookieInvalidException(String message, Throwable cause) { |
||||
super(message, cause); |
||||
} |
||||
} |
@ -1,14 +0,0 @@ |
||||
package cn.linghang.mywust.core.exception; |
||||
|
||||
public class PasswordWornException extends BasicException { |
||||
public PasswordWornException() { |
||||
} |
||||
|
||||
public PasswordWornException(String message) { |
||||
super(message); |
||||
} |
||||
|
||||
public PasswordWornException(String message, Throwable cause) { |
||||
super(message, cause); |
||||
} |
||||
} |
@ -1,4 +1,4 @@ |
||||
package cn.linghang.mywust.core.parser.postgraduate; |
||||
package cn.linghang.mywust.core.parser.graduate; |
||||
|
||||
import cn.linghang.mywust.core.exception.ParseException; |
||||
import cn.linghang.mywust.core.parser.Parser; |
@ -1,4 +1,4 @@ |
||||
package cn.linghang.mywust.core.parser.postgraduate; |
||||
package cn.linghang.mywust.core.parser.graduate; |
||||
|
||||
import cn.linghang.mywust.core.exception.ParseException; |
||||
import cn.linghang.mywust.core.parser.Parser; |
@ -1,4 +1,4 @@ |
||||
package cn.linghang.mywust.core.parser.postgraduate; |
||||
package cn.linghang.mywust.core.parser.graduate; |
||||
|
||||
import cn.linghang.mywust.core.exception.ParseException; |
||||
import cn.linghang.mywust.core.parser.Parser; |
@ -1,4 +1,4 @@ |
||||
package cn.linghang.mywust.core.parser.postgraduate; |
||||
package cn.linghang.mywust.core.parser.graduate; |
||||
|
||||
import cn.linghang.mywust.core.exception.ParseException; |
||||
import cn.linghang.mywust.core.parser.Parser; |
@ -1,13 +0,0 @@ |
||||
package cn.linghang.mywust.core.parser.physics.xpath; |
||||
|
||||
public class PhysicsCourseXpath { |
||||
/** |
||||
* 用于获取表格中所有行的xpath |
||||
* */ |
||||
public static final String COURSE_ROWS_XPATH = "//*[@id=\"ID_PEE110301_gvpee120101\"]/tbody/tr"; |
||||
|
||||
public static final String COURSE_ROW_NAME_XPATH = ""; |
||||
public static final String COURSE_ROW_TEACHER_XPATH = ""; |
||||
public static final String COURSE_ROW_TIME_XPATH = ""; |
||||
public static final String COURSE_ROW_CLASSROOM_XPATH = ""; |
||||
} |
@ -1,5 +0,0 @@ |
||||
package cn.linghang.mywust.core.parser.physics.xpath; |
||||
|
||||
public class PhysicsIndexXpath { |
||||
public static final String PHYSICS_LINK_XPATH = "//*[@id=\"rolemenu\"]/tbody/tr[1]/td[1]/a[2]"; |
||||
} |
@ -1,5 +0,0 @@ |
||||
package cn.linghang.mywust.core.parser.undergraduate.xpath; |
||||
|
||||
public class ExamInfoXpath { |
||||
public static final String EXAM_INFO_ROWS_XPATH = "//*[@id=\"dataList\"]/tbody/tr"; |
||||
} |
@ -1,31 +0,0 @@ |
||||
package cn.linghang.mywust.core.parser.undergraduate.xpath; |
||||
|
||||
/** |
||||
* <p>本科生学生信息接口用到的xpath</p> |
||||
* <p>看着挺唬人的,其实直接浏览器选择元素复制xpath就行了</p> |
||||
* <p>这里的xpath只要网站UI不整什么花活就不会出问题</p> |
||||
* |
||||
* @author lensfrex |
||||
* @create 2022-10-22 22:16 |
||||
*/ |
||||
public class StudentInfoXpath { |
||||
public static final String STUDENT_NUMBER = "//*[@id=\"xjkpTable\"]/tbody/tr[3]/td[5]"; |
||||
|
||||
public static final String COLLEGE = "//*[@id=\"xjkpTable\"]/tbody/tr[3]/td[1]"; |
||||
|
||||
public static final String MAJOR = "//*[@id=\"xjkpTable\"]/tbody/tr[3]/td[2]"; |
||||
|
||||
public static final String CLASS = "//*[@id=\"xjkpTable\"]/tbody/tr[3]/td[4]"; |
||||
|
||||
public static final String NAME = "//*[@id=\"xjkpTable\"]/tbody/tr[4]/td[2]"; |
||||
|
||||
public static final String SEX = "//*[@id=\"xjkpTable\"]/tbody/tr[4]/td[4]"; |
||||
|
||||
public static final String BIRTHDAY = "//*[@id=\"xjkpTable\"]/tbody/tr[5]/td[2]"; |
||||
|
||||
public static final String HOMETOWN = "//*[@id=\"xjkpTable\"]/tbody/tr[7]/td[2]"; |
||||
|
||||
public static final String NATIONALITY = "//*[@id=\"xjkpTable\"]/tbody/tr[8]/td[4]"; |
||||
|
||||
public static final String ID_NUMBER = "//*[@id=\"xjkpTable\"]/tbody/tr[50]/td[4]"; |
||||
} |
@ -0,0 +1,30 @@ |
||||
package cn.linghang.mywust.core.request.library.response; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
import lombok.Data; |
||||
|
||||
@Data |
||||
public class BaseInfo { |
||||
|
||||
@JsonProperty("map") |
||||
private Map map; |
||||
|
||||
@JsonProperty("empty") |
||||
private boolean empty; |
||||
|
||||
public void setMap(Map map) { |
||||
this.map = map; |
||||
} |
||||
|
||||
public Map getMap() { |
||||
return map; |
||||
} |
||||
|
||||
public void setEmpty(boolean empty) { |
||||
this.empty = empty; |
||||
} |
||||
|
||||
public boolean isEmpty() { |
||||
return empty; |
||||
} |
||||
} |
@ -0,0 +1,30 @@ |
||||
package cn.linghang.mywust.core.request.library.response; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
import lombok.Data; |
||||
|
||||
@Data |
||||
public class DetailInfo { |
||||
|
||||
@JsonProperty("map") |
||||
private Map map; |
||||
|
||||
@JsonProperty("empty") |
||||
private boolean empty; |
||||
|
||||
public void setMap(Map map) { |
||||
this.map = map; |
||||
} |
||||
|
||||
public Map getMap() { |
||||
return map; |
||||
} |
||||
|
||||
public void setEmpty(boolean empty) { |
||||
this.empty = empty; |
||||
} |
||||
|
||||
public boolean isEmpty() { |
||||
return empty; |
||||
} |
||||
} |
@ -0,0 +1,30 @@ |
||||
package cn.linghang.mywust.core.request.library.response; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
import lombok.Data; |
||||
|
||||
@Data |
||||
public class ExtraInfo { |
||||
|
||||
@JsonProperty("map") |
||||
private Map map; |
||||
|
||||
@JsonProperty("empty") |
||||
private boolean empty; |
||||
|
||||
public void setMap(Map map) { |
||||
this.map = map; |
||||
} |
||||
|
||||
public Map getMap() { |
||||
return map; |
||||
} |
||||
|
||||
public void setEmpty(boolean empty) { |
||||
this.empty = empty; |
||||
} |
||||
|
||||
public boolean isEmpty() { |
||||
return empty; |
||||
} |
||||
} |
@ -0,0 +1,49 @@ |
||||
package cn.linghang.mywust.core.request.library.response; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
import lombok.Data; |
||||
|
||||
@Data |
||||
public class LoanResultItem { |
||||
@JsonProperty("bibId") |
||||
private String bookId; |
||||
|
||||
@JsonProperty("bibAttrs") |
||||
private BookAttribute bookAttribute; |
||||
|
||||
@JsonProperty("returnDate") |
||||
private String returnDate; |
||||
|
||||
@JsonProperty("loanDate") |
||||
private String loanDate; |
||||
|
||||
@JsonProperty("location") |
||||
private String location; |
||||
|
||||
@JsonProperty("barCode") |
||||
private String barCode; |
||||
|
||||
@Data |
||||
public static class BookAttribute { |
||||
@JsonProperty("pub_year") |
||||
private String publishYear; |
||||
|
||||
@JsonProperty("author") |
||||
private String author; |
||||
|
||||
@JsonProperty("callno") |
||||
private String callNumber; |
||||
|
||||
@JsonProperty("isbn") |
||||
private String isbn; |
||||
|
||||
@JsonProperty("classno") |
||||
private String classNumber; |
||||
|
||||
@JsonProperty("publisher") |
||||
private String publisher; |
||||
|
||||
@JsonProperty("title") |
||||
private String title; |
||||
} |
||||
} |
@ -0,0 +1,64 @@ |
||||
package cn.linghang.mywust.core.request.library.response; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
import lombok.Data; |
||||
|
||||
@Data |
||||
public class Map { |
||||
@JsonProperty("baseInfo") |
||||
private BaseInfo baseInfo; |
||||
|
||||
@JsonProperty("detailInfo") |
||||
private DetailInfo detailInfo; |
||||
|
||||
@JsonProperty("extraInfo") |
||||
private ExtraInfo extraInfo; |
||||
|
||||
@JsonProperty("score") |
||||
private int score; |
||||
|
||||
@JsonProperty("_clickCount") |
||||
private int clickCount; |
||||
|
||||
@JsonProperty("collected") |
||||
private boolean collected; |
||||
|
||||
@JsonProperty("出版发行项") |
||||
private String publisher; |
||||
|
||||
@JsonProperty("个人责任者") |
||||
private String author; |
||||
|
||||
@JsonProperty("提要文摘附注") |
||||
private String describe; |
||||
|
||||
@JsonProperty("ISBN及定价") |
||||
private String isbn; |
||||
|
||||
@JsonProperty("载体形态项") |
||||
private String size; |
||||
|
||||
@JsonProperty("学科主题") |
||||
private String category; |
||||
|
||||
@JsonProperty("使用对象附注") |
||||
private String reader; |
||||
|
||||
@JsonProperty("责任者附注") |
||||
private String authorDescribe; |
||||
|
||||
@JsonProperty("中图法分类号") |
||||
private String clcNumber; |
||||
|
||||
@JsonProperty("著录信息附注") |
||||
private String publishNotes; |
||||
|
||||
@JsonProperty("丛编项") |
||||
private String series; |
||||
|
||||
@JsonProperty("题名/责任者") |
||||
private String fullTitle; |
||||
|
||||
@JsonProperty("title") |
||||
private String title; |
||||
} |
@ -0,0 +1,17 @@ |
||||
package cn.linghang.mywust.core.request.library.response; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
|
||||
public class Response{ |
||||
|
||||
@JsonProperty("map") |
||||
private Map map; |
||||
|
||||
public void setMap(Map map){ |
||||
this.map = map; |
||||
} |
||||
|
||||
public Map getMap(){ |
||||
return map; |
||||
} |
||||
} |
@ -0,0 +1,150 @@ |
||||
package cn.linghang.mywust.core.request.library.response; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
import lombok.Data; |
||||
|
||||
import java.util.List; |
||||
|
||||
@Data |
||||
public class SearchResultItem { |
||||
/** |
||||
* 图书id |
||||
*/ |
||||
@JsonProperty("bibId") |
||||
private String bookId; |
||||
|
||||
/** |
||||
* 作者 |
||||
*/ |
||||
@JsonProperty("author") |
||||
private String author; |
||||
|
||||
/** |
||||
* 索书号 |
||||
*/ |
||||
@JsonProperty("callno") |
||||
private List<String> callNumber; |
||||
|
||||
/** |
||||
* doc类型 |
||||
*/ |
||||
@JsonProperty("docType") |
||||
private String docType; |
||||
|
||||
/** |
||||
* 分组id |
||||
*/ |
||||
@JsonProperty("groupId") |
||||
private String groupId; |
||||
|
||||
/** |
||||
* ISBN号 |
||||
*/ |
||||
@JsonProperty("isbn") |
||||
private String isbn; |
||||
|
||||
@JsonProperty("bibNo") |
||||
private String bibNo; |
||||
|
||||
/** |
||||
* 标题 |
||||
*/ |
||||
@JsonProperty("title") |
||||
private String title; |
||||
|
||||
/** |
||||
* 图书总数 |
||||
*/ |
||||
@JsonProperty("itemCount") |
||||
private int itemCount; |
||||
|
||||
/** |
||||
* 可借数 |
||||
*/ |
||||
@JsonProperty("circCount") |
||||
private int circCount; |
||||
|
||||
/** |
||||
* 出版年 |
||||
*/ |
||||
@JsonProperty("pub_year") |
||||
private String publishYear; |
||||
|
||||
/** |
||||
* 分类号 |
||||
*/ |
||||
@JsonProperty("classno") |
||||
private String classNumber; |
||||
|
||||
/** |
||||
* 出版商 |
||||
*/ |
||||
@JsonProperty("publisher") |
||||
private String publisher; |
||||
|
||||
/** |
||||
* 馆藏信息,原始数据为json字符串 |
||||
*/ |
||||
@JsonProperty("holdings") |
||||
private String collectionInfo; |
||||
|
||||
/** |
||||
* 馆藏信息 |
||||
*/ |
||||
@Data |
||||
public static class CollectionInfo { |
||||
/** |
||||
* 索书号 |
||||
*/ |
||||
@JsonProperty("callNo") |
||||
private String callNumber; |
||||
|
||||
/** |
||||
* 馆藏总数 |
||||
*/ |
||||
@JsonProperty("itemsCount") |
||||
private int itemsCount; |
||||
|
||||
/** |
||||
* 图书条码 |
||||
*/ |
||||
@JsonProperty("barCode") |
||||
private String barCode; |
||||
|
||||
/** |
||||
* 可借状态 |
||||
*/ |
||||
@JsonProperty("circStatus") |
||||
private int circStatus; |
||||
|
||||
/** |
||||
* 书籍分卷信息 |
||||
*/ |
||||
@JsonProperty("vol") |
||||
private String vol; |
||||
|
||||
/** |
||||
* 图书可借状态 |
||||
*/ |
||||
@JsonProperty("itemsAvailable") |
||||
private int itemsAvailable; |
||||
|
||||
/** |
||||
* 所属馆藏地 |
||||
*/ |
||||
@JsonProperty("location") |
||||
private String location; |
||||
|
||||
/** |
||||
* 捐赠者id |
||||
*/ |
||||
@JsonProperty("donatorId") |
||||
private Object donatorId; |
||||
|
||||
/** |
||||
* 状态信息 |
||||
*/ |
||||
@JsonProperty("status") |
||||
private String status; |
||||
} |
||||
} |
@ -0,0 +1,36 @@ |
||||
package cn.linghang.mywust.core.service.graduate; |
||||
|
||||
import cn.linghang.mywust.core.api.Graduate; |
||||
import cn.linghang.mywust.core.exception.ApiException; |
||||
import cn.linghang.mywust.core.request.RequestFactory; |
||||
import cn.linghang.mywust.network.RequestClientOption; |
||||
import cn.linghang.mywust.network.Requester; |
||||
import cn.linghang.mywust.network.entitys.HttpRequest; |
||||
import cn.linghang.mywust.network.entitys.HttpResponse; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
public class GraduateApiService { |
||||
protected final Requester requester; |
||||
|
||||
public GraduateApiService(Requester requester) { |
||||
this.requester = requester; |
||||
} |
||||
|
||||
public void checkResponse(HttpResponse response) throws ApiException { |
||||
// 检查响应是否正确
|
||||
if (response.getBody() == null || |
||||
response.getStatusCode() != HttpResponse.HTTP_OK || |
||||
new String(response.getBody()).contains("name=\"_ctl0:txtpassword\"")) { |
||||
|
||||
throw new ApiException(ApiException.Code.COOKIE_INVALID); |
||||
} |
||||
} |
||||
|
||||
public void checkCookies(String cookie, RequestClientOption option) throws ApiException, IOException { |
||||
HttpRequest request = RequestFactory.makeHttpRequest(Graduate.GRADUATE_INDEX_TEST_API, null, cookie); |
||||
HttpResponse response = requester.get(request, option); |
||||
|
||||
this.checkResponse(response); |
||||
} |
||||
} |
@ -0,0 +1,25 @@ |
||||
package cn.linghang.mywust.core.service.graduate; |
||||
|
||||
import cn.linghang.mywust.core.exception.ApiException; |
||||
import cn.linghang.mywust.core.request.graduate.GraduateRequestFactory; |
||||
import cn.linghang.mywust.network.RequestClientOption; |
||||
import cn.linghang.mywust.network.Requester; |
||||
import cn.linghang.mywust.network.entitys.HttpRequest; |
||||
import cn.linghang.mywust.network.entitys.HttpResponse; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
public class GraduateCourseTableApiService extends GraduateApiService{ |
||||
|
||||
public GraduateCourseTableApiService(Requester requester) { |
||||
super(requester); |
||||
} |
||||
|
||||
public String getCourseTablePage(String cookie, RequestClientOption option) throws IOException, ApiException { |
||||
HttpRequest request = GraduateRequestFactory.courseTableRequest(cookie); |
||||
HttpResponse response = requester.get(request, option); |
||||
super.checkResponse(response); |
||||
|
||||
return new String(response.getBody()); |
||||
} |
||||
} |
@ -0,0 +1,25 @@ |
||||
package cn.linghang.mywust.core.service.graduate; |
||||
|
||||
import cn.linghang.mywust.core.exception.ApiException; |
||||
import cn.linghang.mywust.core.request.graduate.GraduateRequestFactory; |
||||
import cn.linghang.mywust.network.RequestClientOption; |
||||
import cn.linghang.mywust.network.Requester; |
||||
import cn.linghang.mywust.network.entitys.HttpRequest; |
||||
import cn.linghang.mywust.network.entitys.HttpResponse; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
public class GraduateScoreApiService extends GraduateApiService{ |
||||
|
||||
public GraduateScoreApiService(Requester requester) { |
||||
super(requester); |
||||
} |
||||
|
||||
public String getCourseTablePage(String cookie, RequestClientOption option) throws IOException, ApiException { |
||||
HttpRequest request = GraduateRequestFactory.examScoreInfoRequest(cookie); |
||||
HttpResponse response = requester.get(request, option); |
||||
super.checkResponse(response); |
||||
|
||||
return new String(response.getBody()); |
||||
} |
||||
} |
@ -0,0 +1,25 @@ |
||||
package cn.linghang.mywust.core.service.graduate; |
||||
|
||||
import cn.linghang.mywust.core.exception.ApiException; |
||||
import cn.linghang.mywust.core.request.graduate.GraduateRequestFactory; |
||||
import cn.linghang.mywust.network.RequestClientOption; |
||||
import cn.linghang.mywust.network.Requester; |
||||
import cn.linghang.mywust.network.entitys.HttpRequest; |
||||
import cn.linghang.mywust.network.entitys.HttpResponse; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
public class GraduateStudentInfoApiService extends GraduateApiService { |
||||
|
||||
public GraduateStudentInfoApiService(Requester requester) { |
||||
super(requester); |
||||
} |
||||
|
||||
public String getStudentInfoPage(String cookie, RequestClientOption option) throws ApiException, IOException { |
||||
HttpRequest request = GraduateRequestFactory.studentInfoRequest(cookie); |
||||
HttpResponse response = requester.get(request, option); |
||||
super.checkResponse(response); |
||||
|
||||
return new String(response.getBody()); |
||||
} |
||||
} |
@ -0,0 +1,25 @@ |
||||
package cn.linghang.mywust.core.service.graduate; |
||||
|
||||
import cn.linghang.mywust.core.exception.ApiException; |
||||
import cn.linghang.mywust.core.request.graduate.GraduateRequestFactory; |
||||
import cn.linghang.mywust.network.RequestClientOption; |
||||
import cn.linghang.mywust.network.Requester; |
||||
import cn.linghang.mywust.network.entitys.HttpRequest; |
||||
import cn.linghang.mywust.network.entitys.HttpResponse; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
public class GraduateTrainingPlanApiService extends GraduateApiService{ |
||||
|
||||
public GraduateTrainingPlanApiService(Requester requester) { |
||||
super(requester); |
||||
} |
||||
|
||||
public String getCourseTablePage(String cookie, RequestClientOption option) throws IOException, ApiException { |
||||
HttpRequest request = GraduateRequestFactory.trainingPlanPageRequest(cookie); |
||||
HttpResponse response = requester.get(request, option); |
||||
super.checkResponse(response); |
||||
|
||||
return new String(response.getBody()); |
||||
} |
||||
} |
@ -1,4 +1,4 @@ |
||||
package cn.linghang.mywust.core.service.library; |
||||
|
||||
public class LibraryService { |
||||
public class LibraryApiService { |
||||
} |
@ -0,0 +1,31 @@ |
||||
package cn.linghang.mywust.core.service.physics; |
||||
|
||||
import cn.linghang.mywust.core.exception.ApiException; |
||||
import cn.linghang.mywust.core.request.physics.PhysicsSystemRequestFactory; |
||||
import cn.linghang.mywust.network.RequestClientOption; |
||||
import cn.linghang.mywust.network.Requester; |
||||
import cn.linghang.mywust.network.entitys.HttpRequest; |
||||
import cn.linghang.mywust.network.entitys.HttpResponse; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
public class PhysicsApiService { |
||||
private final Requester requester; |
||||
|
||||
public PhysicsApiService(Requester requester) { |
||||
this.requester = requester; |
||||
} |
||||
|
||||
public String getCoursePage(String cookie, RequestClientOption requestClientOption) throws IOException, ApiException { |
||||
requestClientOption.setFallowUrlRedirect(false); |
||||
|
||||
// 请求真正的课表页
|
||||
HttpRequest coursePageRequest = PhysicsSystemRequestFactory.physicsCourseRequest(cookie); |
||||
HttpResponse courseResponse = requester.get(coursePageRequest, requestClientOption); |
||||
if (courseResponse.getStatusCode() != HttpResponse.HTTP_OK) { |
||||
throw new ApiException(ApiException.Code.COOKIE_INVALID); |
||||
} |
||||
|
||||
return new String(courseResponse.getBody()); |
||||
} |
||||
} |
@ -1,38 +0,0 @@ |
||||
package cn.linghang.mywust.core.service.undergraduate; |
||||
|
||||
import cn.linghang.mywust.core.exception.CookieInvalidException; |
||||
import cn.linghang.mywust.core.exception.ParseException; |
||||
import cn.linghang.mywust.core.parser.undergraduate.StudentInfoPageParser; |
||||
import cn.linghang.mywust.core.request.undergrade.BkjxRequestFactory; |
||||
import cn.linghang.mywust.model.global.StudentInfo; |
||||
import cn.linghang.mywust.network.RequestClientOption; |
||||
import cn.linghang.mywust.network.Requester; |
||||
import cn.linghang.mywust.network.entitys.HttpRequest; |
||||
import cn.linghang.mywust.network.entitys.HttpResponse; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
public class StudentInfoApi extends UndergraduateApi { |
||||
private final Requester requester; |
||||
|
||||
private static final StudentInfoPageParser parser = new StudentInfoPageParser(); |
||||
|
||||
public StudentInfoApi(Requester requester) { |
||||
this.requester = requester; |
||||
} |
||||
|
||||
public String getStudentInfoPage(String cookies, RequestClientOption requestOption) throws IOException, CookieInvalidException { |
||||
HttpRequest request = BkjxRequestFactory.studentInfoRequest(cookies); |
||||
HttpResponse response = requester.get(request, requestOption); |
||||
|
||||
checkResponse(response, cookies); |
||||
|
||||
return new String(response.getBody()); |
||||
} |
||||
|
||||
public StudentInfo getStudentInfo(String cookies, RequestClientOption requestOption) throws IOException, CookieInvalidException, ParseException { |
||||
String studentInfoPage = this.getStudentInfoPage(cookies, requestOption); |
||||
|
||||
return parser.parse(studentInfoPage); |
||||
} |
||||
} |
@ -0,0 +1,26 @@ |
||||
package cn.linghang.mywust.core.service.undergraduate; |
||||
|
||||
import cn.linghang.mywust.core.exception.ApiException; |
||||
import cn.linghang.mywust.core.request.undergrade.BkjxRequestFactory; |
||||
import cn.linghang.mywust.network.RequestClientOption; |
||||
import cn.linghang.mywust.network.Requester; |
||||
import cn.linghang.mywust.network.entitys.HttpRequest; |
||||
import cn.linghang.mywust.network.entitys.HttpResponse; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
public class StudentInfoApiService extends UndergraduateApiService { |
||||
|
||||
public StudentInfoApiService(Requester requester) { |
||||
super(requester); |
||||
} |
||||
|
||||
public String getStudentInfoPage(String cookies, RequestClientOption requestOption) throws IOException, ApiException { |
||||
HttpRequest request = BkjxRequestFactory.studentInfoRequest(cookies); |
||||
HttpResponse response = requester.get(request, requestOption); |
||||
|
||||
super.checkResponse(response); |
||||
|
||||
return new String(response.getBody()); |
||||
} |
||||
} |
@ -1,36 +0,0 @@ |
||||
package cn.linghang.mywust.core.service.undergraduate; |
||||
|
||||
import cn.linghang.mywust.core.exception.CookieInvalidException; |
||||
import cn.linghang.mywust.core.exception.ParseException; |
||||
import cn.linghang.mywust.core.parser.undergraduate.TrainingPlanPageParser; |
||||
import cn.linghang.mywust.core.request.undergrade.BkjxRequestFactory; |
||||
import cn.linghang.mywust.network.RequestClientOption; |
||||
import cn.linghang.mywust.network.Requester; |
||||
import cn.linghang.mywust.network.entitys.HttpRequest; |
||||
import cn.linghang.mywust.network.entitys.HttpResponse; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
public class TrainingPlanApi extends UndergraduateApi { |
||||
private final Requester requester; |
||||
|
||||
private static final TrainingPlanPageParser parser = new TrainingPlanPageParser(); |
||||
|
||||
public TrainingPlanApi(Requester requester) { |
||||
this.requester = requester; |
||||
} |
||||
|
||||
public String getTrainingPlanPage(String cookies, RequestClientOption requestClientOption) throws CookieInvalidException, IOException { |
||||
HttpRequest request = BkjxRequestFactory.trainingPlanPageRequest(cookies); |
||||
HttpResponse response = requester.get(request, requestClientOption); |
||||
|
||||
checkResponse(response, cookies); |
||||
|
||||
return new String(response.getBody()); |
||||
} |
||||
|
||||
public String getPrueSchemePage(String cookies, RequestClientOption requestClientOption) throws IOException, CookieInvalidException, ParseException { |
||||
String fullPage = this.getTrainingPlanPage(cookies, requestClientOption); |
||||
return parser.parse(fullPage); |
||||
} |
||||
} |
@ -0,0 +1,26 @@ |
||||
package cn.linghang.mywust.core.service.undergraduate; |
||||
|
||||
import cn.linghang.mywust.core.exception.ApiException; |
||||
import cn.linghang.mywust.core.request.undergrade.BkjxRequestFactory; |
||||
import cn.linghang.mywust.network.RequestClientOption; |
||||
import cn.linghang.mywust.network.Requester; |
||||
import cn.linghang.mywust.network.entitys.HttpRequest; |
||||
import cn.linghang.mywust.network.entitys.HttpResponse; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
public class TrainingPlanApiService extends UndergraduateApiService { |
||||
|
||||
public TrainingPlanApiService(Requester requester) { |
||||
super(requester); |
||||
} |
||||
|
||||
public String getTrainingPlanPage(String cookies, RequestClientOption requestClientOption) throws IOException, ApiException { |
||||
HttpRequest request = BkjxRequestFactory.trainingPlanPageRequest(cookies); |
||||
HttpResponse response = requester.get(request, requestClientOption); |
||||
|
||||
super.checkResponse(response); |
||||
|
||||
return new String(response.getBody()); |
||||
} |
||||
} |
@ -1,17 +0,0 @@ |
||||
package cn.linghang.mywust.core.service.undergraduate; |
||||
|
||||
import cn.linghang.mywust.core.exception.CookieInvalidException; |
||||
import cn.linghang.mywust.core.util.BkjxUtil; |
||||
import cn.linghang.mywust.network.entitys.HttpResponse; |
||||
|
||||
public class UndergraduateApi { |
||||
public void checkResponse(HttpResponse response, String cookies) throws CookieInvalidException { |
||||
// 检查响应是否正确
|
||||
if (response.getBody() == null || |
||||
response.getStatusCode() != HttpResponse.HTTP_OK || |
||||
BkjxUtil.checkLoginFinger(response.getBody())) { |
||||
|
||||
throw new CookieInvalidException("响应无效,cookie可能过期:" + cookies); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,38 @@ |
||||
package cn.linghang.mywust.core.service.undergraduate; |
||||
|
||||
import cn.linghang.mywust.core.api.Bkjx; |
||||
import cn.linghang.mywust.core.exception.ApiException; |
||||
import cn.linghang.mywust.core.request.RequestFactory; |
||||
import cn.linghang.mywust.core.request.undergrade.BkjxRequestFactory; |
||||
import cn.linghang.mywust.core.util.BkjxUtil; |
||||
import cn.linghang.mywust.network.RequestClientOption; |
||||
import cn.linghang.mywust.network.Requester; |
||||
import cn.linghang.mywust.network.entitys.HttpRequest; |
||||
import cn.linghang.mywust.network.entitys.HttpResponse; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
public class UndergraduateApiService { |
||||
protected final Requester requester; |
||||
|
||||
public UndergraduateApiService(Requester requester) { |
||||
this.requester = requester; |
||||
} |
||||
|
||||
public void checkResponse(HttpResponse response) throws ApiException { |
||||
// 检查响应是否正确
|
||||
if (response.getBody() == null || |
||||
response.getStatusCode() != HttpResponse.HTTP_OK || |
||||
BkjxUtil.checkLoginFinger(response.getBody())) { |
||||
|
||||
throw new ApiException(ApiException.Code.COOKIE_INVALID); |
||||
} |
||||
} |
||||
|
||||
public void checkCookies(String cookie, RequestClientOption option) throws ApiException, IOException { |
||||
HttpRequest request = RequestFactory.makeHttpRequest(Bkjx.BKJX_TEST_API, null, cookie); |
||||
HttpResponse response = requester.get(request, option); |
||||
|
||||
this.checkResponse(response); |
||||
} |
||||
} |
@ -0,0 +1,27 @@ |
||||
package cn.linghang.mywust.core.util; |
||||
|
||||
import org.jsoup.Jsoup; |
||||
import org.jsoup.nodes.Document; |
||||
import org.jsoup.nodes.Element; |
||||
import org.jsoup.select.Elements; |
||||
|
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* html页面表单参数提取,用于简化部分繁琐的表单数据生成,模拟网页表单数据的请求参数 |
||||
*/ |
||||
public class PageFormExtractor { |
||||
public static void extractToMap(String html, String formId, Map<String, String> targetMap) { |
||||
Document page = Jsoup.parse(html); |
||||
Element form = page.getElementById(formId); |
||||
if (form == null) { |
||||
throw new NullPointerException("The Form Doesn't exist."); |
||||
} |
||||
|
||||
Elements formElements = form.getElementsByAttribute("name"); |
||||
|
||||
for (Element element : formElements) { |
||||
targetMap.put(element.attr("name"), element.attr("value")); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,2 @@ |
||||
public class UnderGraduateTest { |
||||
} |
Loading…
Reference in new issue