Skip to content

Commit

Permalink
Hotfix: Rewrite score connecter (#200)
Browse files Browse the repository at this point in the history
* Fix score connector  data extraction error on unexpected data format

* remove unused dart library

* remove unused local variable tableNodes

* Add final on variable h3Node

* Eliminat unneeded array element

* Remove unused variable h3Nodes

* Add element null value handling logics

* Rewrite rank node filter

* Rewrite valid rank node methods

* Remove unused variable rankNodes
  • Loading branch information
James-Lu-none authored Sep 7, 2023
1 parent 4e0fbb8 commit e0f53c0
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions lib/src/connector/score_connector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,22 @@ class ScoreConnector {
ConnectorParameter parameter;
String result;
Document tagNode;
Element tableNode, h3Node, scoreNode;
List<Element> tableNodes, h3Nodes, scoreNodes, rankNodes;
Element scoreNode;
List<Element> scoreNodes;
List<SemesterCourseScoreJson> courseScoreList = [];
try {
Map<String, String> data = {"format": "-2"};
parameter = ConnectorParameter(_scoreAllScoreUrl);
parameter.data = data;
result = await Connector.getDataByGet(parameter);
tagNode = parse(result);
tableNodes = tagNode.getElementsByTagName("table");
h3Nodes = tagNode
.getElementsByTagName("h3")
.reversed
.toList()
.getRange(0, tableNodes.length)
.toList()
.reversed
.toList();
final h3Nodes = tagNode.getElementsByTagName("h3");

//依照學期取得課程資料
for (int i = 0; i < tableNodes.length; i++) {
tableNode = tableNodes[i];
h3Node = h3Nodes[i];
for (final h3Node in h3Nodes) {
final siblingOfH3 = h3Node.nextElementSibling;
if (siblingOfH3 == null || siblingOfH3.localName != "table") continue;
final tableNode = siblingOfH3;

SemesterCourseScoreJson courseScore = SemesterCourseScoreJson();

Expand Down Expand Up @@ -140,14 +134,18 @@ class ScoreConnector {
parameter.charsetName = "big5";
result = await Connector.getDataByGet(parameter);
tagNode = parse(result);
rankNodes = tagNode.getElementsByTagName("tbody")[0].getElementsByTagName("tr");
rankNodes = rankNodes.getRange(2, rankNodes.length).toList().reversed.toList();
final rankNodes = tagNode
.getElementsByTagName("tbody")
.first
.getElementsByTagName("tr")
.reversed
.where((row) => row.getElementsByTagName("td").length >= 7)
.toList(growable: false);
for (int i = 0; i < (rankNodes.length / 3).floor(); i++) {
SemesterJson semester = SemesterJson();
String semesterString = rankNodes[i * 3 + 2].getElementsByTagName("td")[0].innerHtml.split("<br>").first;
semester.year = semesterString.split(" ")[0];
semester.semester = semesterString.split(" ").reversed.toList()[0];

//取得學期成績排名
RankJson rankNow = RankJson();
RankItemJson rankItemCourse = RankItemJson();
Expand Down

0 comments on commit e0f53c0

Please sign in to comment.