Skip to content

Commit

Permalink
feat: add new schedule api (#1281)
Browse files Browse the repository at this point in the history
  • Loading branch information
DGoiana authored Sep 10, 2024
2 parents f2cb059 + 8e43723 commit 8a518bc
Show file tree
Hide file tree
Showing 25 changed files with 365 additions and 1,008 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ abstract class ScheduleFetcher extends SessionDependantFetcher {
return thisWeek == nextWeek ? [thisWeek] : [thisWeek, nextWeek];
}

int getLectiveYear(DateTime date) {
return date.month < 8 ? date.year - 1 : date.year;
}

/// Returns [Dates].
List<Dates> getDates() {
final date = DateTime.now();

final weeks = getWeeks(date);
final lectiveYear = date.month < 8 ? date.year - 1 : date.year;
final lectiveYear = getLectiveYear(date);

return weeks.map((week) => Dates(week, lectiveYear)).toList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:http/http.dart' as http;
import 'package:uni/controller/fetchers/schedule_fetcher/schedule_fetcher.dart';
import 'package:uni/controller/networking/network_router.dart';
import 'package:uni/controller/parsers/parser_schedule.dart';
import 'package:uni/controller/parsers/schedule/api/parser.dart';
import 'package:uni/model/entities/lecture.dart';
import 'package:uni/model/entities/profile.dart';
import 'package:uni/model/entities/session.dart';
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import 'package:uni/controller/fetchers/schedule_fetcher/schedule_fetcher.dart';
import 'package:uni/controller/networking/network_router.dart';
import 'package:uni/controller/parsers/schedule/new_api/parser.dart';
import 'package:uni/model/entities/lecture.dart';
import 'package:uni/model/entities/profile.dart';
import 'package:uni/model/entities/session.dart';

/// Class for fetching the user's lectures from the schedule's HTML page.
class ScheduleFetcherNewApi extends ScheduleFetcher {
@override
List<String> getEndpoints(Session session) {
final urls = NetworkRouter.getBaseUrlsFromSession(session)
.map((url) => '${url}hor_geral.estudantes_view')
.toList();
return urls;
}

/// Fetches the user's lectures from the schedule's HTML page.
@override
Future<List<Lecture>> getLectures(Session session, Profile profile) async {
final endpoints = getEndpoints(session);
final lectiveYear = getLectiveYear(DateTime.now());

final futures = endpoints.map((baseUrl) async {
final scheduleResponse = await NetworkRouter.getWithCookies(
baseUrl,
{
'pv_num_unico': session.username,
'pv_ano_lectivo': lectiveYear.toString(),
'pv_periodos': '1',
},
session,
);

final scheduleApiUrl = getScheduleApiUrlFromHtml(scheduleResponse);

final scheduleApiResponse = await NetworkRouter.getWithCookies(
scheduleApiUrl,
{},
session,
);

return getLecturesFromApiResponse(scheduleApiResponse);
});

final results = await Future.wait(futures);

// TODO(limwa,#1281): Check if handling of lectures in both faculties is correct.
final lectures = results.expand((element) => element).toList()
..sort((l1, l2) => l1.compare(l2));

return lectures;
}
}
183 changes: 0 additions & 183 deletions packages/uni_app/lib/controller/parsers/parser_schedule_html.dart

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:uni/controller/parsers/schedule/new_api/models/response_lecture_class.dart';
import 'package:uni/controller/parsers/schedule/new_api/models/response_lecture_person.dart';
import 'package:uni/controller/parsers/schedule/new_api/models/response_lecture_room.dart';
import 'package:uni/controller/parsers/schedule/new_api/models/response_lecture_typology.dart';
import 'package:uni/controller/parsers/schedule/new_api/models/response_lecture_unit.dart';

part '../../../../../generated/controller/parsers/schedule/new_api/models/response_lecture.g.dart';

@JsonSerializable(explicitToJson: true)
class ResponseLecture {
ResponseLecture(
this.start,
this.end,
this.units,
this.classes,
this.persons,
this.rooms,
this.typology,
);

factory ResponseLecture.fromJson(Map<String, dynamic> json) =>
_$ResponseLectureFromJson(json);

DateTime start;
DateTime end;
@JsonKey(name: 'ucs')
List<ResponseLectureUnit> units;
List<ResponseLectureClass> classes;
List<ResponseLecturePerson> persons;
List<ResponseLectureRoom> rooms;
ResponseLectureTypology typology;

Map<String, dynamic> toJson() => _$ResponseLectureToJson(this);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:json_annotation/json_annotation.dart';

part '../../../../../generated/controller/parsers/schedule/new_api/models/response_lecture_class.g.dart';

@JsonSerializable(explicitToJson: true)
class ResponseLectureClass {
ResponseLectureClass(this.acronym);
factory ResponseLectureClass.fromJson(Map<String, dynamic> json) =>
_$ResponseLectureClassFromJson(json);

String acronym;

Map<String, dynamic> toJson() => _$ResponseLectureClassToJson(this);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:json_annotation/json_annotation.dart';

part '../../../../../generated/controller/parsers/schedule/new_api/models/response_lecture_person.g.dart';

@JsonSerializable(explicitToJson: true)
class ResponseLecturePerson {
ResponseLecturePerson(this.acronym);
factory ResponseLecturePerson.fromJson(Map<String, dynamic> json) =>
_$ResponseLecturePersonFromJson(json);

String acronym;

Map<String, dynamic> toJson() => _$ResponseLecturePersonToJson(this);
}
Loading

0 comments on commit 8a518bc

Please sign in to comment.