Skip to content

Commit

Permalink
Merge pull request #4 from dutwrapper/draft - Bumped to v1.8.3
Browse files Browse the repository at this point in the history
Bumped to v1.8.3
  • Loading branch information
ZoeMeow1027 committed Dec 6, 2023
2 parents 0565e7e + a973176 commit 89841ae
Show file tree
Hide file tree
Showing 11 changed files with 348 additions and 145 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

This file will list all version log for modified, add or remove function of dutwrapper.

## 1.8.3
- Fixed a issue cause news sorted by descending in get news group by date.
- Fixed a issue cause subject code item throw a exception about string format.

## 1.8.2
- Add a option for get news to group by date. You can manually access them by two function in News class:
- getNewsGlobalGroupByDate
- getNewsSubjectGroupByDate
- Merge two news test into once.

## 1.8.1
- Solved a mistake in SubjectResult constructor.
- Updated dependencies to latest version.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'io.dutwrapper.dutwrapper'
version '1.8.1'
version '1.8.3'

repositories {
mavenCentral()
Expand Down
159 changes: 103 additions & 56 deletions src/main/java/io/dutwrapper/dutwrapper/News.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import io.dutwrapper.dutwrapper.model.enums.NewsSearchType;
import io.dutwrapper.dutwrapper.model.enums.NewsType;
import io.dutwrapper.dutwrapper.model.news.LinkItem;
import io.dutwrapper.dutwrapper.model.news.NewsGlobalGroupByDate;
import io.dutwrapper.dutwrapper.model.news.NewsGlobalItem;
import io.dutwrapper.dutwrapper.model.news.NewsSubjectAffectedItem;
import io.dutwrapper.dutwrapper.model.news.NewsSubjectGroupByDate;
import io.dutwrapper.dutwrapper.model.news.NewsSubjectItem;

import java.net.URLEncoder;
Expand All @@ -33,18 +35,16 @@
@SuppressWarnings("SpellCheckingInspection")
public class News {
public static ArrayList<NewsGlobalItem> getNews(
@Nullable NewsType newsType,
@Nullable Integer page,
@Nullable NewsSearchType searchType,
@Nullable String searchQuery
) throws Exception {
@Nullable NewsType newsType,
@Nullable Integer page,
@Nullable NewsSearchType searchType,
@Nullable String searchQuery) throws Exception {
String url = String.format(
Variables.URL_NEWS,
newsType == null ? NewsType.Global.toString() : newsType.toString(),
page == null ? 1 : page,
searchType == null ? NewsSearchType.ByTitle.toString() : searchType.toString(),
URLEncoder.encode(searchQuery == null ? "" : searchQuery, StandardCharsets.UTF_8.toString())
);
Variables.URL_NEWS,
newsType == null ? NewsType.Global.toString() : newsType.toString(),
page == null ? 1 : page,
searchType == null ? NewsSearchType.ByTitle.toString() : searchType.toString(),
URLEncoder.encode(searchQuery == null ? "" : searchQuery, StandardCharsets.UTF_8.toString()));

CustomResponse response = CustomRequest.get(null, url, 60);
if (response.getReturnCode() < 200 || response.getReturnCode() >= 300) {
Expand Down Expand Up @@ -76,7 +76,8 @@ public static ArrayList<NewsGlobalItem> getNews(
LocalDateTime dateTime = date.atTime(time);
newsItem.setDate(dateTime.atZone(ZoneOffset.UTC).toInstant().toEpochMilli());
newsItem.setTitle(titleTemp[1].trim());
} else newsItem.setTitle(title.text().trim());
} else
newsItem.setTitle(title.text().trim());

newsItem.setContent(content.html());
newsItem.setContentString(content.wholeText());
Expand All @@ -92,8 +93,7 @@ public static ArrayList<NewsGlobalItem> getNews(
LinkItem item1 = new LinkItem(
item.wholeText(),
item.attr("abs:href"),
position
);
position);
links.add(item1);
position += item.wholeText().length();

Expand All @@ -112,30 +112,26 @@ public static ArrayList<NewsGlobalItem> getNews(
}

public static ArrayList<NewsGlobalItem> getNewsGlobal(
@Nullable Integer page,
@Nullable NewsSearchType searchType,
@Nullable String searchQuery
) throws Exception {
@Nullable Integer page,
@Nullable NewsSearchType searchType,
@Nullable String searchQuery) throws Exception {
return getNews(
NewsType.Global,
page == null ? 1 : page,
searchType,
searchQuery
);
NewsType.Global,
page == null ? 1 : page,
searchType,
searchQuery);
}

public static ArrayList<NewsSubjectItem> getNewsSubject(
@Nullable Integer page,
@Nullable NewsSearchType searchType,
@Nullable String searchQuery
) throws Exception {
@Nullable Integer page,
@Nullable NewsSearchType searchType,
@Nullable String searchQuery) throws Exception {
ArrayList<NewsSubjectItem> result = new ArrayList<>();
ArrayList<NewsGlobalItem> listTemp = getNews(
NewsType.Subject,
page == null ? 1 : page,
searchType,
searchQuery
);
NewsType.Subject,
page == null ? 1 : page,
searchType,
searchQuery);

for (NewsGlobalItem item : listTemp) {
NewsSubjectItem subjectItem = new NewsSubjectItem();
Expand Down Expand Up @@ -163,24 +159,30 @@ public static ArrayList<NewsSubjectItem> getNewsSubject(
if (subjectItem.getContent().contains("HỌC BÙ")) {
subjectItem.setLessonStatus(LessonStatus.MakeUp);
subjectItem.setAffectedDate(Utils.date2UnixTimestamp(
Utils.findFirstString(subjectItem.getContentString(), "\\d{2}[-|/]\\d{2}[-|/]\\d{4}")
));
Utils.findFirstString(subjectItem.getContentString(), "\\d{2}[-|/]\\d{2}[-|/]\\d{4}")));
try {
subjectItem.setAffectedLesson(getLessonItem(
Objects.requireNonNull(Utils.findFirstString(subjectItem.getContentString().toLowerCase(), "tiết: .*[0-9],")).replace("tiết:", "").replace(",", "").trim()
));
} catch (Exception ignored) { }
Objects.requireNonNull(Utils.findFirstString(subjectItem.getContentString().toLowerCase(),
"tiết: .*[0-9],")).replace("tiết:", "").replace(",", "").trim()));
} catch (Exception ignored) {
}
try {
subjectItem.setAffectedRoom(Objects.requireNonNull(Utils.findFirstString(subjectItem.getContentString().toLowerCase(), "phòng:.*")).replace("phòng:", "").replace(",", "").trim().toUpperCase());
} catch (Exception ignored) { }
subjectItem.setAffectedRoom(Objects
.requireNonNull(
Utils.findFirstString(subjectItem.getContentString().toLowerCase(), "phòng:.*"))
.replace("phòng:", "").replace(",", "").trim().toUpperCase());
} catch (Exception ignored) {
}
} else if (subjectItem.getContent().contains("NGHỈ HỌC")) {
subjectItem.setLessonStatus(LessonStatus.Leaving);
subjectItem.setAffectedDate(Utils.date2UnixTimestamp(Utils.findFirstString(subjectItem.getContentString(), "\\d{2}[-|/]\\d{2}[-|/]\\d{4}")));
subjectItem.setAffectedDate(Utils.date2UnixTimestamp(
Utils.findFirstString(subjectItem.getContentString(), "\\d{2}[-|/]\\d{2}[-|/]\\d{4}")));
try {
subjectItem.setAffectedLesson(getLessonItem(
Objects.requireNonNull(Utils.findFirstString(subjectItem.getContentString().toLowerCase(), "\\(tiết:.*[0-9]\\)")).replace("(tiết:", "").replace(")", "").trim()
));
} catch (Exception ignored) { }
Objects.requireNonNull(Utils.findFirstString(subjectItem.getContentString().toLowerCase(),
"\\(tiết:.*[0-9]\\)")).replace("(tiết:", "").replace(")", "").trim()));
} catch (Exception ignored) {
}
} else {
subjectItem.setLessonStatus(LessonStatus.Unknown);
}
Expand All @@ -198,41 +200,40 @@ private static LessonItem getLessonItem(String input) {
item.setStart(Integer.parseInt(input.split("-")[0]));
item.setEnd(Integer.parseInt(input.split("-")[1]));
return item;
}
else return null;
} else
return null;
}

private static ArrayList<NewsSubjectAffectedItem> getAffectedClass(String input) {
String subjectProcessing = input.split(" thông báo đến lớp:")[1].trim();
ArrayList<String> data1 = Arrays.stream(subjectProcessing.split(" , ")).collect(Collectors.toCollection(ArrayList::new));
ArrayList<String> data1 = Arrays.stream(subjectProcessing.split(" , "))
.collect(Collectors.toCollection(ArrayList::new));
ArrayList<NewsSubjectAffectedItem> data2 = new ArrayList<>();

for (String item: data1) {
for (String item : data1) {
String itemSubjectName = item.substring(0, item.indexOf("[")).trim();
String itemClass = item.substring(item.indexOf("[") + 1, item.indexOf("]")).toLowerCase();
SubjectCodeItem codeItem;
if (itemClass.contains(".nh")) {
String[] data = itemClass.split(".nh");
codeItem = new SubjectCodeItem(
data[0],
data[1]
);
data[0],
data[1]);
} else {
String[] data = itemClass.split("nh");
codeItem = new SubjectCodeItem(
data[0],
data[1]
);
data[0],
data[1]);
}

if (data2.stream().noneMatch(p -> Objects.equals(p.getSubjectName(), itemSubjectName))) {
NewsSubjectAffectedItem item2 = new NewsSubjectAffectedItem();
item2.setSubjectName(itemSubjectName);
item2.getCodeList().add(codeItem);
data2.add(item2);
}
else {
Optional<NewsSubjectAffectedItem> tempdata = data2.stream().filter(p -> Objects.equals(p.getSubjectName(), itemSubjectName)).findFirst();
} else {
Optional<NewsSubjectAffectedItem> tempdata = data2.stream()
.filter(p -> Objects.equals(p.getSubjectName(), itemSubjectName)).findFirst();
if (tempdata.isPresent()) {
NewsSubjectAffectedItem temp = tempdata.get();
temp.getCodeList().add(codeItem);
Expand All @@ -242,4 +243,50 @@ private static ArrayList<NewsSubjectAffectedItem> getAffectedClass(String input)

return data2;
}

public static ArrayList<NewsGlobalGroupByDate> getNewsGlobalGroupByDate(
@Nullable Integer page,
@Nullable NewsSearchType searchType,
@Nullable String searchQuery) throws Exception {
ArrayList<NewsGlobalGroupByDate> _result = new ArrayList<>();
ArrayList<NewsGlobalItem> _temp = getNewsGlobal(page, searchType, searchQuery);

for (NewsGlobalItem item : _temp) {
if (_result.stream().anyMatch(p -> p.getDateInUnixTimeMilliseconds() == item.getDate())) {
// if date group exist
_result.stream().filter(p -> p.getDateInUnixTimeMilliseconds() == item.getDate()).findFirst().get()
.addData(item);
} else {
// if not
NewsGlobalGroupByDate group = new NewsGlobalGroupByDate(item.getDate());
group.addData(item);
_result.add(group);
}
}

return _result;
}

public static ArrayList<NewsSubjectGroupByDate> getNewsSubjectGroupByDate(
@Nullable Integer page,
@Nullable NewsSearchType searchType,
@Nullable String searchQuery) throws Exception {
ArrayList<NewsSubjectGroupByDate> _result = new ArrayList<>();
ArrayList<NewsSubjectItem> _temp = getNewsSubject(page, searchType, searchQuery);

for (NewsSubjectItem item : _temp) {
if (_result.stream().anyMatch(p -> p.getDateInUnixTimeMilliseconds() == item.getDate())) {
// if date group exist
_result.stream().filter(p -> p.getDateInUnixTimeMilliseconds() == item.getDate()).findFirst().get()
.addData(item);
} else {
// if not
NewsSubjectGroupByDate group = new NewsSubjectGroupByDate(item.getDate());
group.addData(item);
_result.add(group);
}
}

return _result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public String toString() {

public String toString(Boolean twoLastDigit) {
if (twoLastDigit)
return String.format("%02d.%s", studentYearId, classId);
return String.format("%s.%s", studentYearId, classId);
else
return String.format("%02d.%02d.%s.%s", subjectId, schoolYearId, studentYearId, classId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.dutwrapper.dutwrapper.model.news;

import java.util.ArrayList;

public class NewsGlobalGroupByDate {
ArrayList<NewsGlobalItem> data;
long date;

public NewsGlobalGroupByDate(long date) {
this.date = date;
this.data = new ArrayList<NewsGlobalItem>();
}

public ArrayList<NewsGlobalItem> getData() {
return data;
}

public long getDateInUnixTimeMilliseconds() {
return date;
}

public void addData(NewsGlobalItem item) {
if (item.getDate() != date) {
return;
}

if (!(data.stream().anyMatch(p -> p.getDate() == item.getDate() &&
p.getTitle() == item.getTitle() &&
p.getContent() == item.getContent()))) {
data.add(item);
}
}

public void addData(ArrayList<NewsGlobalItem> itemList) {
for (NewsGlobalItem item : itemList) {
addData(item);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.dutwrapper.dutwrapper.model.news;

import java.util.ArrayList;

public class NewsSubjectGroupByDate {
ArrayList<NewsSubjectItem> data;
long date;

public NewsSubjectGroupByDate(long date) {
this.date = date;
this.data = new ArrayList<NewsSubjectItem>();
}

public ArrayList<NewsSubjectItem> getData() {
return data;
}

public long getDateInUnixTimeMilliseconds() {
return date;
}

public void addData(NewsSubjectItem item) {
if (item.getDate() != date) {
return;
}

if (!(data.stream().anyMatch(p -> p.getDate() == item.getDate() &&
p.getTitle() == item.getTitle() &&
p.getContent() == item.getContent()))) {
data.add(item);
}
}

public void addData(ArrayList<NewsSubjectItem> itemList) {
for (NewsSubjectItem item : itemList) {
addData(item);
}
}
}
16 changes: 16 additions & 0 deletions src/test/java/io/dutwrapper/dutwrapper/AccountTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,26 @@ static void login(String sessionId, String user, String pass) throws Exception {

static void getSubjectSchedule(String sessionId, Integer year, Integer semester) throws Exception {
ArrayList<SubjectScheduleItem> subjectScheduleList = Account.getSubjectSchedule(sessionId, year, semester);
for (SubjectScheduleItem subjectScheduleItem: subjectScheduleList) {
System.out.println(String.format(
"Subject #%d\nName: %s\nID: %s\n========================",
subjectScheduleList.indexOf(subjectScheduleItem),
subjectScheduleItem.getName(),
subjectScheduleItem.getId()
));
}
}

static void getSubjectFee(String sessionId, Integer year, Integer semester) throws Exception {
ArrayList<SubjectFeeItem> subjectFeeList = Account.getSubjectFee(sessionId, year, semester);
for (SubjectFeeItem subjectFeeItem: subjectFeeList) {
System.out.println(String.format(
"Subject #%d\nName: %s\nID: %s\n========================",
subjectFeeList.indexOf(subjectFeeItem),
subjectFeeItem.getName(),
subjectFeeItem.getId()
));
}
}

static void getAccountInformation(String sessionId) throws Exception {
Expand Down
Loading

0 comments on commit 89841ae

Please sign in to comment.