Skip to content

Commit

Permalink
Merge pull request #5 from dutwrapper/draft - v1.8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
ZoeMeow1027 committed Jan 5, 2024
2 parents 89841ae + 8d9c21e commit e64086f
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 43 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

## 1.8.4
- Update dependencies to latest.
- `NewsGroupByDate` is now interface. Use `NewsGlobalGroupByDate` and `NewsSubjectGroupByDate` for initializing group.

## 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.
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ This library provides wrapper (for this repository - crawl data from a page) for
## FAQ

### Branch in dutwrapper?
- "main": Default branch and main release.
- "draft": Alpha branch. This code isn't tested and use it at your own risk.
- `stable`/`main`: Default branch and main release.
- `draft`: Alpha branch. This branch is used for update my progress and it's very unstable. Use it at your own risk.

### I received error about login while running AccountTest?
- Did you mean this error: `dut_account environment variable not found. Please, add or modify this environment in format "username|password"`?
- If so, you will need to add environment variable named `dut_account` with syntax `studentid|password`.

### Wiki, or manual for how-to-use?
- In a plan, please be patient.

### Latest change log?

- To view log for all versions, [click here](CHANGELOG.md).

## Copyright?

- This project - dutwrapper - is not affiliated with [Da Nang University of Technology](http://sv.dut.udn.vn).
- DUT, Da Nang University of Technology, web materials and web contents are trademarks and copyrights of [Da Nang University of Technology](http://sv.dut.udn.vn) school.
10 changes: 6 additions & 4 deletions 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.3'
version '1.8.4'

repositories {
mavenCentral()
Expand All @@ -17,16 +17,18 @@ dependencies {
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'

// This dependency is exported to consumers, that is to say found on their compile classpath.
// commons-math3 - https://mvnrepository.com/artifact/org.apache.commons/commons-math3
api 'org.apache.commons:commons-math3:3.6.1'

// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:32.1.3-jre'
// Guava - https://mvnrepository.com/artifact/com.google.guava/guava
implementation 'com.google.guava:guava:33.0.0-jre'

// JSoup - https://mvnrepository.com/artifact/org.jsoup/jsoup
implementation 'org.jsoup:jsoup:1.16.2'
implementation 'org.jsoup:jsoup:1.17.2'

// OkHttp3 - https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.11'
implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.12'

// Google Gson - https://mvnrepository.com/artifact/com.google.code.gson/gson
implementation 'com.google.code.gson:gson:2.10.1'
Expand Down
23 changes: 13 additions & 10 deletions src/main/java/io/dutwrapper/dutwrapper/News.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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.NewsGroupByDate;
import io.dutwrapper.dutwrapper.model.news.NewsSubjectAffectedItem;
import io.dutwrapper.dutwrapper.model.news.NewsSubjectGroupByDate;
import io.dutwrapper.dutwrapper.model.news.NewsSubjectItem;
Expand Down Expand Up @@ -46,6 +47,8 @@ public static ArrayList<NewsGlobalItem> getNews(
searchType == null ? NewsSearchType.ByTitle.toString() : searchType.toString(),
URLEncoder.encode(searchQuery == null ? "" : searchQuery, StandardCharsets.UTF_8.toString()));

// System.out.println(url);

CustomResponse response = CustomRequest.get(null, url, 60);
if (response.getReturnCode() < 200 || response.getReturnCode() >= 300) {
throw new Exception("Server was returned with code " + response.getReturnCode() + ".");
Expand Down Expand Up @@ -244,21 +247,21 @@ private static ArrayList<NewsSubjectAffectedItem> getAffectedClass(String input)
return data2;
}

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

for (NewsGlobalItem item : _temp) {
if (_result.stream().anyMatch(p -> p.getDateInUnixTimeMilliseconds() == item.getDate())) {
if (_result.stream().anyMatch(p -> p.getDateInUnixMilliseconds() == item.getDate())) {
// if date group exist
_result.stream().filter(p -> p.getDateInUnixTimeMilliseconds() == item.getDate()).findFirst().get()
_result.stream().filter(p -> p.getDateInUnixMilliseconds() == item.getDate()).findFirst().get()
.addData(item);
} else {
// if not
NewsGlobalGroupByDate group = new NewsGlobalGroupByDate(item.getDate());
NewsGroupByDate<NewsGlobalItem> group = new NewsGlobalGroupByDate(item.getDate());
group.addData(item);
_result.add(group);
}
Expand All @@ -267,21 +270,21 @@ public static ArrayList<NewsGlobalGroupByDate> getNewsGlobalGroupByDate(
return _result;
}

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

for (NewsSubjectItem item : _temp) {
if (_result.stream().anyMatch(p -> p.getDateInUnixTimeMilliseconds() == item.getDate())) {
if (_result.stream().anyMatch(p -> p.getDateInUnixMilliseconds() == item.getDate())) {
// if date group exist
_result.stream().filter(p -> p.getDateInUnixTimeMilliseconds() == item.getDate()).findFirst().get()
_result.stream().filter(p -> p.getDateInUnixMilliseconds() == item.getDate()).findFirst().get()
.addData(item);
} else {
// if not
NewsSubjectGroupByDate group = new NewsSubjectGroupByDate(item.getDate());
NewsGroupByDate<NewsSubjectItem> group = new NewsSubjectGroupByDate(item.getDate());
group.addData(item);
_result.add(group);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.ArrayList;

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

Expand All @@ -11,29 +11,56 @@ public NewsGlobalGroupByDate(long date) {
this.data = new ArrayList<NewsGlobalItem>();
}

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

public long getDateInUnixTimeMilliseconds() {
@Override
public long getDateInUnixMilliseconds() {
return date;
}

public void addData(NewsGlobalItem item) {
@Override
public void addData(NewsGlobalItem item, Boolean addToTop) {
if (item.getDate() != date) {
return;
}

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

if (addToTop) {
data.add(0, item);
} else {
data.add(item);
}
}

public void addData(ArrayList<NewsGlobalItem> itemList) {
for (NewsGlobalItem item : itemList) {
addData(item);
@Override
public void addData(NewsGlobalItem item) {
addData(item, false);
}

@Override
public void addData(ArrayList<NewsGlobalItem> list, Boolean addToTop) {
if (addToTop) {
for (int i = list.size() - 1; i >= 0; i--) {
addData(list.get(i), true);
}
} else {
for (NewsGlobalItem item : list) {
addData(item);
}
}
}

@Override
public void addData(ArrayList<NewsGlobalItem> list) {
addData(list, false);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.dutwrapper.dutwrapper.model.news;

import java.util.ArrayList;

public interface NewsGroupByDate<T> {
public ArrayList<T> getData();

public long getDateInUnixMilliseconds();

public void addData(T item);

public void addData(T item, Boolean addToTop);

public void addData(ArrayList<T> list);

public void addData(ArrayList<T> list, Boolean addToTop);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.ArrayList;

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

Expand All @@ -11,29 +11,55 @@ public NewsSubjectGroupByDate(long date) {
this.data = new ArrayList<NewsSubjectItem>();
}

public ArrayList<NewsSubjectItem> getData() {
return data;
@Override
public void addData(NewsSubjectItem item) {
addData(item, false);
}

public long getDateInUnixTimeMilliseconds() {
@Override
public void addData(ArrayList<NewsSubjectItem> list) {
addData(list, false);
}

@Override
public long getDateInUnixMilliseconds() {
return date;
}

public void addData(NewsSubjectItem item) {
@Override
public void addData(NewsSubjectItem item, Boolean addToTop) {
if (item.getDate() != date) {
return;
}

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

if (addToTop) {
data.add(0, item);
} else {
data.add(item);
}
}

public void addData(ArrayList<NewsSubjectItem> itemList) {
for (NewsSubjectItem item : itemList) {
addData(item);
@Override
public void addData(ArrayList<NewsSubjectItem> list, Boolean addToTop) {
if (addToTop) {
for (int i = list.size() - 1; i >= 0; i--) {
addData(list.get(i), true);
}
} else {
for (NewsSubjectItem item : list) {
addData(item);
}
}
}

@Override
public ArrayList<NewsSubjectItem> getData() {
return data;
}
}
4 changes: 4 additions & 0 deletions src/test/java/io/dutwrapper/dutwrapper/AccountTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.junit.jupiter.api.Test;

import com.google.gson.Gson;

import io.dutwrapper.dutwrapper.customrequest.CustomResponse;
import io.dutwrapper.dutwrapper.model.accounts.AccountInformation;
import io.dutwrapper.dutwrapper.model.accounts.SubjectFeeItem;
Expand Down Expand Up @@ -55,10 +57,12 @@ static void getSubjectFee(String sessionId, Integer year, Integer semester) thro

static void getAccountInformation(String sessionId) throws Exception {
AccountInformation accInfo = Account.getAccountInformation(sessionId);
System.out.println(new Gson().toJson(accInfo));
}

static void getAccountTrainingStatus(String sessionId) throws Exception {
AccountTrainingStatus accountTrainingStatus = Account.getAccountTrainingStatus(sessionId);
System.out.println(new Gson().toJson(accountTrainingStatus));
}

static void logout(String sessionId) throws Exception {
Expand Down
15 changes: 7 additions & 8 deletions src/test/java/io/dutwrapper/dutwrapper/NewsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import io.dutwrapper.dutwrapper.model.enums.LessonStatus;
import io.dutwrapper.dutwrapper.model.enums.NewsSearchType;
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.NewsSubjectGroupByDate;
import io.dutwrapper.dutwrapper.model.news.NewsGroupByDate;
import io.dutwrapper.dutwrapper.model.news.NewsSubjectItem;

import java.util.ArrayList;
Expand Down Expand Up @@ -49,14 +48,14 @@ void getNewsGlobalGroupByDate() throws Exception {

while (page <= pageMax) {
System.out.println("===========================================");
ArrayList<NewsGlobalGroupByDate> newsList = News.getNewsGlobalGroupByDate(page, null, null);
ArrayList<NewsGroupByDate<NewsGlobalItem>> newsList = News.getNewsGlobalGroupByDate(page, null, null);
System.out.println(String.format("Page %d (%d item(s))", page, newsList.size()));

for (NewsGlobalGroupByDate newsGroup : newsList) {
for (NewsGroupByDate<NewsGlobalItem> newsGroup : newsList) {
System.out.println(String.format(
"Index %d - date: %d - count: %d",
newsList.indexOf(newsGroup) + 1,
newsGroup.getDateInUnixTimeMilliseconds(),
newsGroup.getDateInUnixMilliseconds(),
newsGroup.getData().size()
));
for (NewsGlobalItem newsItem: newsGroup.getData()) {
Expand Down Expand Up @@ -114,14 +113,14 @@ void getNewsSubjectGroupByDate() throws Exception {

while (page <= pageMax) {
System.out.println("===========================================");
ArrayList<NewsSubjectGroupByDate> newsList = News.getNewsSubjectGroupByDate(page, null, null);
ArrayList<NewsGroupByDate<NewsSubjectItem>> newsList = News.getNewsSubjectGroupByDate(page, null, null);
System.out.println(String.format("Page %d (%d item(s))", page, newsList.size()));

for (NewsSubjectGroupByDate newsGroup : newsList) {
for (NewsGroupByDate<NewsSubjectItem> newsGroup : newsList) {
System.out.println(String.format(
"Index %d - date: %d - count: %d",
newsList.indexOf(newsGroup) + 1,
newsGroup.getDateInUnixTimeMilliseconds(),
newsGroup.getDateInUnixMilliseconds(),
newsGroup.getData().size()
));
for (NewsSubjectItem newsItem: newsGroup.getData()) {
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/io/dutwrapper/dutwrapper/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

import org.junit.jupiter.api.Test;

import com.google.gson.Gson;

import io.dutwrapper.dutwrapper.model.utils.DutSchoolYearItem;

public class UtilsTest {
@Test
void finalTest() throws Exception {
Long currentUnix = Utils.getCurrentTimeInUnix();
System.out.println(currentUnix);

DutSchoolYearItem test = Utils.getCurrentSchoolWeek();

System.out.println(new Gson().toJson(test));
System.out.println(Utils.getCurrentSchoolWeek().toString());
}
}

0 comments on commit e64086f

Please sign in to comment.