Skip to content

Commit

Permalink
Refactoring and cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
FlashOnFire committed Oct 20, 2023
1 parent 3c06b81 commit 04319c6
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 100 deletions.
6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions melos_onyx_workspace.iml
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@
<excludeFolder url="file://$MODULE_DIR$/apps/.flutter/packages/integration_test/integration_test_macos/.dart_tool" />
<excludeFolder url="file://$MODULE_DIR$/apps/.flutter/packages/integration_test/integration_test_macos/.pub" />
<excludeFolder url="file://$MODULE_DIR$/apps/.flutter/packages/integration_test/integration_test_macos/build" />
<excludeFolder url="file://$MODULE_DIR$/packages/polytechcolloscopeclient/.dart_tool" />
<excludeFolder url="file://$MODULE_DIR$/packages/polytechcolloscopeclient/.pub" />
<excludeFolder url="file://$MODULE_DIR$/packages/polytechcolloscopeclient/build" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Dart SDK" level="project" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export 'src/consts.dart';
export 'src/polytechcolloscopeclient_base.dart';
Original file line number Diff line number Diff line change
@@ -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<Object?> 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<Object?> 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<Object?> get props => [date, subject, message, kholleur];

@override
bool? get stringify => true;
}
37 changes: 37 additions & 0 deletions packages/polytechcolloscopeclient/lib/src/consts.dart
Original file line number Diff line number Diff line change
@@ -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
};
}
Original file line number Diff line number Diff line change
@@ -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<List<Student>> 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);
Expand All @@ -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);

Expand Down Expand Up @@ -102,31 +69,33 @@ class PolytechColloscopeClient {
var secondTd = e.children.elementAtOrNull(1);

var hourAndMinute =
secondTd?.children.first.innerHtml.replaceFirst("&nbsp;", "").trim();
secondTd?.children.first.innerHtml.replaceFirst("&nbsp;", "").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);

Expand All @@ -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<Object?> 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<Object?> 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<Object?> get props => [date, subject, message, kholleur];

@override
bool? get stringify => true;
}

Future<void> main() async {
PolytechColloscopeClient client = PolytechColloscopeClient("", "");

var students = await client.fetchStudents(Year.first);
print(students);

var colloscope = await client.getColloscope(Year.first, 828);
print(colloscope);
}
1 change: 1 addition & 0 deletions packages/polytechcolloscopeclient/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import 'package:dotenv/dotenv.dart';
import 'package:polytechcolloscopeclient/polytechcolloscopeclient.dart';
import 'package:test/test.dart';

void main() {

Future<void> 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);
}

0 comments on commit 04319c6

Please sign in to comment.