diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index f2fc2feb..5bcb8207 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -40,8 +40,14 @@ + + \ No newline at end of file diff --git a/melos_onyx_workspace.iml b/melos_onyx_workspace.iml index 5980f67d..e638f097 100644 --- a/melos_onyx_workspace.iml +++ b/melos_onyx_workspace.iml @@ -238,6 +238,9 @@ + + + diff --git a/packages/polytechcolloscopeclient/lib/polytechcolloscopeclient.dart b/packages/polytechcolloscopeclient/lib/polytechcolloscopeclient.dart index 0a1eedc9..f93c06b6 100644 --- a/packages/polytechcolloscopeclient/lib/polytechcolloscopeclient.dart +++ b/packages/polytechcolloscopeclient/lib/polytechcolloscopeclient.dart @@ -1 +1,2 @@ +export 'src/consts.dart'; export 'src/polytechcolloscopeclient_base.dart'; diff --git a/packages/polytechcolloscopeclient/lib/src/colloscope_datastructs.dart b/packages/polytechcolloscopeclient/lib/src/colloscope_datastructs.dart new file mode 100644 index 00000000..afa4e4c8 --- /dev/null +++ b/packages/polytechcolloscopeclient/lib/src/colloscope_datastructs.dart @@ -0,0 +1,43 @@ +import 'package:equatable/equatable.dart'; + +class Student extends Equatable { + final String name; + final String id; + + Student(this.name, this.id); + + @override + List get props => [name, id]; + + @override + bool? get stringify => true; +} + +class StudentColloscope extends Equatable { + final int studentId; + final int trinomeId; + final List kholles; + + StudentColloscope(this.studentId, this.trinomeId, this.kholles); + + @override + List get props => [studentId, trinomeId, kholles]; + + @override + bool? get stringify => true; +} + +class Kholle extends Equatable { + final DateTime date; + final String subject; + final String? message; + final String? kholleur; + + Kholle(this.date, this.subject, this.message, this.kholleur); + + @override + List get props => [date, subject, message, kholleur]; + + @override + bool? get stringify => true; +} diff --git a/packages/polytechcolloscopeclient/lib/src/consts.dart b/packages/polytechcolloscopeclient/lib/src/consts.dart new file mode 100644 index 00000000..6af117f4 --- /dev/null +++ b/packages/polytechcolloscopeclient/lib/src/consts.dart @@ -0,0 +1,37 @@ +enum Year { + first, + second, +} + +class Consts { + static const String userAgent = + "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:96.0) Gecko/20100101 Firefox/96.0"; + + // HTTPS parce que le https est pas géré + static const String _base = "http://math.univ-lyon1.fr/colles/"; + + static const kholleURL = { + Year.first: "$_base?page=colles_1A", + Year.second: "$_base?page=colles_2A" + }; + + static const khollesStudentURL = { + Year.first: "$_base?page=colles_1A&id_etudiant=:id", + Year.second: "$_base?page=colles_2A&id_etudiant=:id" + }; + + static const Map monthsTranslation = { + "janvier": DateTime.january, + "février": DateTime.february, + "mars": DateTime.march, + "avril": DateTime.april, + "mai": DateTime.may, + "juin": DateTime.june, + "juillet": DateTime.july, + "août": DateTime.august, + "septembre": DateTime.september, + "octobre": DateTime.october, + "novembre": DateTime.november, + "décembre": DateTime.december + }; +} diff --git a/packages/polytechcolloscopeclient/lib/src/polytechcolloscopeclient_base.dart b/packages/polytechcolloscopeclient/lib/src/polytechcolloscopeclient_base.dart index a2cb66f4..31a91530 100644 --- a/packages/polytechcolloscopeclient/lib/src/polytechcolloscopeclient_base.dart +++ b/packages/polytechcolloscopeclient/lib/src/polytechcolloscopeclient_base.dart @@ -1,51 +1,17 @@ import 'package:beautiful_soup_dart/beautiful_soup.dart'; -import 'package:equatable/equatable.dart'; -import 'package:requests_plus/requests_plus.dart'; - import 'package:html/dom.dart'; +import 'package:requests_plus/requests_plus.dart'; -enum Year { - first, - second, -} +import 'colloscope_datastructs.dart'; +import 'consts.dart'; class PolytechColloscopeClient { - static const String userAgent = - "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:96.0) Gecko/20100101 Firefox/96.0"; - - // HTTPS parce que le https est pas géré - static const String _base = "http://math.univ-lyon1.fr/colles/"; - - static const kholleURL = { - Year.first: "$_base?page=colles_1A", - Year.second: "$_base?page=colles_2A" - }; - - static const khollesStudentURL = { - Year.first: "$_base?page=colles_1A&id_etudiant=:id", - Year.second: "$_base?page=colles_2A&id_etudiant=:id" - }; - - static const Map monthsTranslation = { - "janvier": DateTime.january, - "février": DateTime.february, - "mars": DateTime.march, - "avril": DateTime.april, - "mai": DateTime.may, - "juin": DateTime.june, - "juillet": DateTime.july, - "août": DateTime.august, - "septembre": DateTime.september, - "octobre": DateTime.october, - "novembre": DateTime.november, - "décembre": DateTime.december - }; - final String username, password; + PolytechColloscopeClient(this.username, this.password); Future> fetchStudents(Year year) async { - var page = await RequestsPlus.get(kholleURL[year]!, + var page = await RequestsPlus.get(Consts.kholleURL[year]!, userName: username, password: password); BeautifulSoup bs = BeautifulSoup(page.body); @@ -71,7 +37,8 @@ class PolytechColloscopeClient { // TODO : Add a check to verify the studentid (look 5 lines below) var page = await RequestsPlus.get( - khollesStudentURL[year]!.replaceFirst(":id", studentId.toString()), + Consts.khollesStudentURL[year]! + .replaceFirst(":id", studentId.toString()), userName: username, password: password); @@ -102,31 +69,33 @@ class PolytechColloscopeClient { var secondTd = e.children.elementAtOrNull(1); var hourAndMinute = - secondTd?.children.first.innerHtml.replaceFirst(" ", "").trim(); + secondTd?.children.first.innerHtml.replaceFirst(" ", "").trim(); var secondDiv = secondTd?.children.elementAtOrNull(1); var kholleur = secondDiv?.find("a")?.text.trim(); - // This hold the subject of the kholle (and maybe the room because this website html is probably the worst I've ever seen) + // This holds the subject of the kholle (and maybe the room because this website html is probably the worst I've ever seen) var text = ""; - var divText = secondDiv?.nodes.where((element) => element.runtimeType == Text); + var divText = + secondDiv?.nodes.where((element) => element.runtimeType == Text); var subject = divText?.first.text?.replaceAll(RegExp(r'[()]'), "").trim(); String message; if (divText?.length == 2) { - message = divText?.last.text?.replaceAll(RegExp(r'[()]'), "").trim() ?? ""; + message = + divText?.last.text?.replaceAll(RegExp(r'[()]'), "").trim() ?? ""; } else { message = ""; } var dateParsed = RegExp(r"(\d{1,2}) (.{3,9})").firstMatch(date); var day = dateParsed?.group(1); - var month = monthsTranslation[dateParsed?.group(2)]; + var month = Consts.monthsTranslation[dateParsed?.group(2)]; var hourAndMinutesParsed = - RegExp(r"(\d{1,2}) h (\d{2})").firstMatch(hourAndMinute!); + RegExp(r"(\d{1,2}) h (\d{2})").firstMatch(hourAndMinute!); var hour = hourAndMinutesParsed?.group(1); var minutes = hourAndMinutesParsed?.group(2); @@ -136,55 +105,3 @@ class PolytechColloscopeClient { return Kholle(dateTime, subject!, message, kholleur); } } - -class Student extends Equatable { - final String name; - final String id; - - Student(this.name, this.id); - - @override - List get props => [name, id]; - - @override - bool? get stringify => true; -} - -class StudentColloscope extends Equatable { - final int studentId; - final int trinomeId; - final List kholles; - - StudentColloscope(this.studentId, this.trinomeId, this.kholles); - - @override - List get props => [studentId, trinomeId, kholles]; - - @override - bool? get stringify => true; -} - -class Kholle extends Equatable { - final DateTime date; - final String subject; - final String? message; - final String? kholleur; - - Kholle(this.date, this.subject, this.message, this.kholleur); - - @override - List get props => [date, subject, message, kholleur]; - - @override - bool? get stringify => true; -} - -Future main() async { - PolytechColloscopeClient client = PolytechColloscopeClient("", ""); - - var students = await client.fetchStudents(Year.first); - print(students); - - var colloscope = await client.getColloscope(Year.first, 828); - print(colloscope); -} diff --git a/packages/polytechcolloscopeclient/pubspec.yaml b/packages/polytechcolloscopeclient/pubspec.yaml index f517c905..b3369dc5 100644 --- a/packages/polytechcolloscopeclient/pubspec.yaml +++ b/packages/polytechcolloscopeclient/pubspec.yaml @@ -9,6 +9,7 @@ environment: # Add regular dependencies here. dependencies: beautiful_soup_dart: ^0.3.0 + dotenv: ^4.2.0 equatable: ^2.0.5 requests_plus: ^4.8.4 # path: ^1.8.0 diff --git a/packages/polytechcolloscopeclient/test/polytechcolloscopeclient_test.dart b/packages/polytechcolloscopeclient/test/polytechcolloscopeclient_test.dart index e05e1728..edd56a3f 100644 --- a/packages/polytechcolloscopeclient/test/polytechcolloscopeclient_test.dart +++ b/packages/polytechcolloscopeclient/test/polytechcolloscopeclient_test.dart @@ -1,6 +1,23 @@ +import 'package:dotenv/dotenv.dart'; import 'package:polytechcolloscopeclient/polytechcolloscopeclient.dart'; import 'package:test/test.dart'; -void main() { - +Future main() async { + DotEnv env = DotEnv(includePlatformEnvironment: true); + + env.load(); + final String username = env['USERNAME'] ?? ""; + final String password = env['PASSWORD'] ?? ""; + + if (username.isEmpty || password.isEmpty) { + fail("username or password were empty. check your envt variables"); + } + + PolytechColloscopeClient client = PolytechColloscopeClient("", ""); + + var students = await client.fetchStudents(Year.first); + print(students); + + var colloscope = await client.getColloscope(Year.first, 828); + print(colloscope); }