parent
bffcca9455
commit
56c0f3c19e
@ -0,0 +1,39 @@ |
||||
package cn.linghang.mywust.core.parser.physics; |
||||
|
||||
import cn.linghang.mywust.core.exception.ParseException; |
||||
import cn.linghang.mywust.core.parser.Parser; |
||||
import cn.linghang.mywust.core.util.JsoupUtil; |
||||
import lombok.Data; |
||||
import org.jsoup.Jsoup; |
||||
import org.jsoup.nodes.Document; |
||||
import org.jsoup.nodes.Element; |
||||
import org.jsoup.select.Elements; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
public class PhysicsScoreListPageParser implements Parser<PhysicsScoreListPageParser.PhysicsScoreListPageParseResult> { |
||||
private static final String scorePageLinkXpath = "//*[@id=\"ID_PEE63_gvpeeCourseScore\"]/tbody/tr/td/a"; |
||||
|
||||
@Override |
||||
public PhysicsScoreListPageParseResult parse(String html) throws ParseException { |
||||
List<String> scoreIds = new ArrayList<>(2); |
||||
Document document = Jsoup.parse(html); |
||||
Elements links = document.selectXpath(scorePageLinkXpath); |
||||
for (Element link : links) { |
||||
String href = link.attr("href"); |
||||
scoreIds.add(href.replaceAll("javascript:__doPostBack\\('(.*?)',''\\)", "$1")); |
||||
} |
||||
|
||||
Element termSelect = document.getElementById("ID_PEE63_ddlxq"); |
||||
String term = JsoupUtil.getSelectValue(termSelect); |
||||
|
||||
return new PhysicsScoreListPageParseResult(scoreIds, term); |
||||
} |
||||
|
||||
@Data |
||||
public static class PhysicsScoreListPageParseResult { |
||||
private final List<String> courseIds; |
||||
private final String term; |
||||
} |
||||
} |
@ -0,0 +1,42 @@ |
||||
package cn.linghang.mywust.core.parser.physics; |
||||
|
||||
import cn.linghang.mywust.core.exception.ParseException; |
||||
import cn.linghang.mywust.core.parser.Parser; |
||||
import cn.linghang.mywust.core.util.JsoupUtil; |
||||
import cn.linghang.mywust.data.global.Score; |
||||
import org.jsoup.Jsoup; |
||||
import org.jsoup.nodes.Element; |
||||
import org.jsoup.select.Elements; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
public class PhysicsScorePageParser implements Parser<List<Score>> { |
||||
private static final String scoreRowXpath = "//*[@id=\"ID_PEE63_gvpeeLabScore\"]/tbody/tr[@style=\"background-color:White;height:25px;\"]"; |
||||
|
||||
@Override |
||||
public List<Score> parse(String html) throws ParseException { |
||||
Elements scoreRows = Jsoup.parse(html).selectXpath(scoreRowXpath); |
||||
|
||||
List<Score> scores = new ArrayList<>(scoreRows.size()); |
||||
for (Element scoreRow : scoreRows) { |
||||
Elements girds = scoreRow.getElementsByTag("td"); |
||||
// 某行格子数少于8个,即到了成绩那部分就没了,就跳过
|
||||
if (girds.size() < 8) { |
||||
continue; |
||||
} |
||||
|
||||
// 有用的信息就这么多
|
||||
Score score = new Score(); |
||||
score.setId(JsoupUtil.getElementText(girds, 0)); |
||||
score.setCourseName(JsoupUtil.getElementText(girds, 1)); |
||||
score.setGroupName(JsoupUtil.getElementText(girds, 2)); |
||||
score.setFlag(JsoupUtil.getElementText(girds, 6)); |
||||
score.setScore(JsoupUtil.getElementText(girds, 7)); |
||||
|
||||
scores.add(score); |
||||
} |
||||
|
||||
return scores; |
||||
} |
||||
} |
@ -0,0 +1,19 @@ |
||||
package cn.linghang.mywust.core.service.physics; |
||||
|
||||
import cn.linghang.mywust.core.exception.ApiException; |
||||
import cn.linghang.mywust.network.Requester; |
||||
import cn.linghang.mywust.network.entitys.HttpResponse; |
||||
|
||||
public abstract class PhysicsApiServiceBase { |
||||
protected final Requester requester; |
||||
|
||||
public PhysicsApiServiceBase(Requester requester) { |
||||
this.requester = requester; |
||||
} |
||||
|
||||
protected void checkResponse(HttpResponse response) throws ApiException { |
||||
if (response.getStatusCode() != HttpResponse.HTTP_OK) { |
||||
throw new ApiException(ApiException.Code.COOKIE_INVALID); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,52 @@ |
||||
package cn.linghang.mywust.core.service.physics; |
||||
|
||||
import cn.linghang.mywust.core.exception.ApiException; |
||||
import cn.linghang.mywust.core.exception.ParseException; |
||||
import cn.linghang.mywust.core.parser.physics.PhysicsScoreListPageParser; |
||||
import cn.linghang.mywust.core.request.physics.PhysicsSystemRequestFactory; |
||||
import cn.linghang.mywust.core.util.PageFormExtractor; |
||||
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; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
public class PhysicsScoreApiServiceBase extends PhysicsApiServiceBase { |
||||
|
||||
private static final PhysicsScoreListPageParser scoreListPageParser = new PhysicsScoreListPageParser(); |
||||
|
||||
public PhysicsScoreApiServiceBase(Requester requester) { |
||||
super(requester); |
||||
} |
||||
|
||||
public String getPage(String cookie, String courseId, Map<String, String> pageParams, RequestClientOption requestClientOption) throws IOException, ApiException, ParseException { |
||||
HttpRequest scorePageRequest = PhysicsSystemRequestFactory.physicsScoreRequest(cookie, courseId, pageParams); |
||||
HttpResponse scorePageResponse = requester.post(scorePageRequest, requestClientOption); |
||||
checkResponse(scorePageResponse); |
||||
|
||||
return scorePageResponse.getStringBody(); |
||||
} |
||||
|
||||
public List<String> getAllPages(String cookie, RequestClientOption requestClientOption) throws IOException, ParseException, ApiException { |
||||
HttpRequest scoreListPageRequest = PhysicsSystemRequestFactory.physicsScoreListRequest(cookie); |
||||
HttpResponse scoreListPageResponse = requester.get(scoreListPageRequest, requestClientOption); |
||||
String scoreListPage = scoreListPageResponse.getStringBody(); |
||||
|
||||
PhysicsScoreListPageParser.PhysicsScoreListPageParseResult pageParseResult = scoreListPageParser.parse(scoreListPage); |
||||
|
||||
Map<String, String> pageParams = PageFormExtractor.extractAllParams(scoreListPage); |
||||
pageParams.put("ID_PEE63$ddlxq", pageParseResult.getTerm()); |
||||
|
||||
List<String> courseIds = pageParseResult.getCourseIds(); |
||||
List<String> scorePages = new ArrayList<>(courseIds.size()); |
||||
for (String courseId : courseIds) { |
||||
scorePages.add(this.getPage(cookie, courseId, pageParams, requestClientOption)); |
||||
} |
||||
|
||||
return scorePages; |
||||
} |
||||
} |
Loading…
Reference in new issue