Skip to content

Commit

Permalink
Bumped to 0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ZoeMeow1027 committed Dec 8, 2023
1 parent a4c5cb0 commit 78de7a4
Show file tree
Hide file tree
Showing 21 changed files with 516 additions and 127 deletions.
126 changes: 98 additions & 28 deletions lib/account.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
library dutwrapper;

import 'dart:convert';
import 'dart:developer';

import 'package:html/parser.dart';
import 'package:http/http.dart' as http;

import 'model/account_obj.dart';
import 'model/account/subject_fee.dart';
import 'model/account/subject_schedule.dart';
import 'model/account/subject_schedule_study.dart';
import 'model/enums.dart';
import 'model/global_variables.dart';
import 'model/range_class.dart';
import 'model/request_result.dart';
import 'model/subject_code_item.dart';
import 'model/subject_code.dart';
import 'model/global_variables_url.dart';

class Account {
static Future<RequestResult> generateSessionID({int timeout = 60}) async {
RequestResult ars = const RequestResult();
RequestResult ars = RequestResult();

try {
final response = await http
.get(Uri.parse('http://sv.dut.udn.vn/'))
.get(Uri.parse(GlobalVariablesUrl.baseLink()))
.timeout(Duration(seconds: timeout));

// Get session id
Expand Down Expand Up @@ -67,7 +71,7 @@ class Account {
final response = await http
.get(
Uri.parse(
'http://sv.dut.udn.vn/WebAjax/evLopHP_Load.aspx?E=TTKBLoad&Code=2010'),
GlobalVariablesUrl.subjectScheduleLink(year: 22, semester: 1)),
headers: header,
)
.timeout(Duration(seconds: timeout));
Expand Down Expand Up @@ -107,7 +111,7 @@ class Account {
};
try {
await http
.post(Uri.parse('http://sv.dut.udn.vn/PageDangNhap.aspx'),
.post(Uri.parse(GlobalVariablesUrl.loginLink()),
headers: header,
encoding: Encoding.getByName('utf-8'),
body: postData)
Expand Down Expand Up @@ -135,8 +139,7 @@ class Account {

try {
await http
.get(Uri.parse('http://sv.dut.udn.vn/PageLogout.aspx'),
headers: header)
.get(Uri.parse(GlobalVariablesUrl.logoutLink()), headers: header)
.timeout(Duration(seconds: timeout));
RequestResult loginStatus = await isLoggedIn(sessionId: sessionId);
ars = ars.clone(
Expand All @@ -155,35 +158,25 @@ class Account {
return ars;
}

static Future<RequestResult<List<SubjectScheduleItem>>> getSubjectSchedule({
static Future<RequestResult<List<SubjectSchedule>>> getSubjectSchedule({
required String sessionId,
required int year,
required int semester,
int timeout = 60,
}) async {
RequestResult<List<SubjectScheduleItem>> ars =
RequestResult<List<SubjectScheduleItem>>(data: []);

// Create object if null
// ars.data ??= [];
RequestResult<List<SubjectSchedule>> ars =
RequestResult<List<SubjectSchedule>>(data: []);

try {
if (semester <= 0 || semester > 3) {
throw ArgumentError(
'Invalid value (previous value: year: $year, semester: $semester)');
}
String code =
'$year${semester < 3 ? semester : 2}${semester == 3 ? 1 : 0}';

// Header data
Map<String, String> header = <String, String>{
'cookie': 'ASP.NET_SessionId=$sessionId;',
};

final response = await http
.get(
Uri.parse(
'http://sv.dut.udn.vn/WebAjax/evLopHP_Load.aspx?E=TTKBLoad&Code=$code'),
Uri.parse(GlobalVariablesUrl.subjectScheduleLink(
year: year, semester: semester)),
headers: header)
.timeout(Duration(seconds: timeout));

Expand All @@ -198,9 +191,9 @@ class Account {
continue;
}

SubjectScheduleItem item = SubjectScheduleItem();
SubjectSchedule item = SubjectSchedule();
// Subject id
item.id = SubjectCodeItem.fromString(input: schCell[1].text);
item.id = SubjectCode.fromString(input: schCell[1].text);
// Subject name
item.name = schCell[2].text;
// Subject credit
Expand All @@ -213,7 +206,7 @@ class Account {
// Subject study
if (schCell[7].text.isNotEmpty) {
schCell[7].text.split('; ').forEach((element) {
SubjectStudyItem subjectStudyItem = SubjectStudyItem();
SubjectScheduleStudy subjectStudyItem = SubjectScheduleStudy();
// Day of week
if (element.toUpperCase().contains('CN')) {
subjectStudyItem.dayOfWeek = 0;
Expand Down Expand Up @@ -263,7 +256,7 @@ class Account {
}

try {
SubjectScheduleItem schItem = ars.data!.firstWhere(
SubjectSchedule schItem = ars.data!.firstWhere(
(element) => element.id.toString() == schCell[1].text);
// Set group
schItem.subjectExam.group = schCell[3].text;
Expand All @@ -287,7 +280,8 @@ class Account {
.toList()
.join('-'));
} catch (ex) {
print(ex);
// TODO Error log
log(ex.toString());
}
} else if (element.contains('Phòng')) {
schItem.subjectExam.room = itemSplitted[1];
Expand All @@ -313,6 +307,82 @@ class Account {
}
}

ars = ars.clone(
statusCode: response.statusCode,
requestCode: [200, 204].contains(response.statusCode)
? RequestCode.successful
: RequestCode.failed,
);
} on ArgumentError {
ars = ars.clone(requestCode: RequestCode.invalid);
} catch (ex) {
log(ex.toString());
ars = ars.clone(requestCode: RequestCode.exceptionThrown);
}

return ars;
}

static Future<RequestResult<List<SubjectFee>>> getSubjectFee({
required String sessionId,
required int year,
required int semester,
int timeout = 60,
}) async {
RequestResult<List<SubjectFee>> ars =
RequestResult<List<SubjectFee>>(data: []);

try {
// Header data
Map<String, String> header = <String, String>{
'cookie': 'ASP.NET_SessionId=$sessionId;',
};

final response = await http
.get(
Uri.parse(GlobalVariablesUrl.subjectFeeLink(
year: year, semester: semester)),
headers: header)
.timeout(Duration(seconds: timeout));

// Main processing
var docSchFee = parse(response.body).getElementById("THocPhi_GridInfo");
if (docSchFee != null) {
var schRow = docSchFee.getElementsByClassName("GridRow");
if (schRow.isNotEmpty) {
for (var row in schRow) {
var schCell = row.getElementsByClassName('GridCell');
if (schCell.length < 10) {
continue;
}

SubjectFee item = SubjectFee();
// Subject id
item.id = SubjectCode.fromString(input: schCell[1].text);
// Subject name
item.name = schCell[2].text;
// Subject credit
item.credit = int.tryParse(schCell[3].text) ?? 0;
// Subject is high quality
item.isHighQuality =
schCell[4].attributes['class']?.contains('GridCheck') ?? false;
// Subject price
item.price =
double.tryParse(schCell[5].text.replaceAll(",", "")) ?? 0;
// Is debt
item.isDebt =
schCell[6].attributes['class']?.contains('GridCheck') ?? false;
// Is restudy
item.isReStudy =
schCell[7].attributes['class']?.contains('GridCheck') ?? false;
// Payment at
item.confirmedPaymentAt = schCell[8].text;

ars.data!.add(item);
}
}
}

ars = ars.clone(
statusCode: response.statusCode,
requestCode: [200, 204].contains(response.statusCode)
Expand Down
12 changes: 12 additions & 0 deletions lib/model/account/subject_fee.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import '../subject_code.dart';

class SubjectFee {
SubjectCode id = SubjectCode();
String name = '';
int credit = 0;
bool isHighQuality = false;
double price = 0;
bool isDebt = false;
bool isReStudy = false;
String? confirmedPaymentAt;
}
14 changes: 14 additions & 0 deletions lib/model/account/subject_schedule.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import '../subject_code.dart';
import 'subject_schedule_exam.dart';
import 'subject_schedule_study.dart';

class SubjectSchedule {
SubjectCode id = SubjectCode();
String name = '';
int credit = 0;
bool isHighQuality = false;
String lecturerName = '';
SubjectScheduleStudyList subjectStudy = SubjectScheduleStudyList();
SubjectScheduleExam subjectExam = SubjectScheduleExam();
String pointFormula = '';
}
6 changes: 6 additions & 0 deletions lib/model/account/subject_schedule_exam.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class SubjectScheduleExam {
int date = 0;
String room = '';
bool isGlobal = false;
String group = '';
}
13 changes: 13 additions & 0 deletions lib/model/account/subject_schedule_study.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import '../range_class.dart';

class SubjectScheduleStudy {
// 0: Sunday, 1: Monday -> 6: Saturday
int dayOfWeek = 0;
RangeInt lesson = RangeInt(start: 0, end: 0);
String room = '';
}

class SubjectScheduleStudyList {
List<SubjectScheduleStudy> subjectStudyList = [];
List<RangeInt> weekList = [];
}
32 changes: 0 additions & 32 deletions lib/model/account_obj.dart

This file was deleted.

74 changes: 74 additions & 0 deletions lib/model/custom_clock.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class CustomClock {
final int hour, minute;

CustomClock({
required this.hour,
required this.minute,
});

CustomClock.current()
: hour = DateTime.now().hour,
minute = DateTime.now().minute;

@override
bool operator ==(other) {
return other is CustomClock && hour == other.hour && minute == other.minute;
}

bool operator >(other) {
return other is CustomClock &&
(hour > other.hour || (hour == other.hour && minute > other.minute));
}

bool operator <(other) {
return other is CustomClock &&
(hour < other.hour || (hour == other.hour && minute < other.minute));
}

@override
int get hashCode => hour * 60 + minute;

@override
String toString() {
return "${hour.toString().padLeft(2, '0')}:${minute.toString().padLeft(2, '0')}";
}

bool isInRange(CustomClock valLeft, CustomClock valRight) {
return (valLeft < valRight)
? (valLeft < this && this < valRight)
: (valLeft > valRight)
? ((valRight < this && valLeft < this) ||
(valRight > this && valLeft > this))
: (valLeft == this && this == valRight);
}

int toDUTLesson() {
var data = [
CustomClock(hour: 7, minute: 0),
CustomClock(hour: 8, minute: 0),
CustomClock(hour: 9, minute: 0),
CustomClock(hour: 10, minute: 0),
CustomClock(hour: 11, minute: 0),
CustomClock(hour: 12, minute: 0),
CustomClock(hour: 12, minute: 30),
CustomClock(hour: 13, minute: 30),
CustomClock(hour: 14, minute: 30),
CustomClock(hour: 15, minute: 30),
CustomClock(hour: 16, minute: 30),
CustomClock(hour: 17, minute: 30),
CustomClock(hour: 18, minute: 15),
CustomClock(hour: 19, minute: 10),
CustomClock(hour: 19, minute: 55),
CustomClock(hour: 20, minute: 30),
];
var data2 = [-2, 1, 2, 3, 4, 5, -1, 6, 7, 8, 9, 10, 11, 12, 13, 14];

for (var item in data) {
if (this < item) {
return data2[data.indexOf(item)];
}
}

return 0;
}
}
16 changes: 16 additions & 0 deletions lib/model/dut_school_year.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class DutSchoolYear {
int week;
String schoolYear;
int schoolYearVal;

DutSchoolYear({
required this.schoolYear,
required this.schoolYearVal,
required this.week,
});

@override
String toString() {
return "School year: $schoolYear\nSchool year value: $schoolYearVal\nWeek $week";
}
}
Loading

0 comments on commit 78de7a4

Please sign in to comment.