Skip to content

Commit

Permalink
Improve rapla parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Bennik2000 committed Aug 29, 2020
1 parent b2804ef commit 77d37e9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
26 changes: 22 additions & 4 deletions lib/schedule/service/rapla/rapla_response_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:dhbwstudentapp/schedule/model/schedule.dart';
import 'package:dhbwstudentapp/schedule/model/schedule_entry.dart';
import 'package:html/dom.dart';
import 'package:html/parser.dart' show parse;
import 'package:http/http.dart';
import 'package:intl/intl.dart';

class RaplaResponseParser {
Expand Down Expand Up @@ -123,7 +122,10 @@ class RaplaResponseParser {
}

ScheduleEntry _extractScheduleEntry(Element value, DateTime date) {
// The tooltip tag contains the most relevant information
var tooltip = value.getElementsByClassName(TOOLTIP_CLASS);

// The only reliable way to extract the time
var timeAndClassName = value.getElementsByTagName("a");

if (tooltip.length != 1) return null;
Expand All @@ -145,6 +147,22 @@ class RaplaResponseParser {
title = properties[CLASS_NAME_LABEL];
professor = properties[PROFESSOR_NAME_LABEL];
details = properties[DETAILS_LABEL];

if (title == null) {
return null;
}

// Sometimes the entry type is not set correctly. When the title of a class
// begins with "Online - " it implies that it is online
// In this case remove the online prefix and set the type correctly
if (title.startsWith("Online - ") && type == ScheduleEntryType.Class) {
title = title.substring("Online - ".length);
type = ScheduleEntryType.Online;
}

if (professor?.endsWith(",") ?? false) {
professor = professor.substring(0, professor.length - 1);
}
}

var resource = _extractResources(value);
Expand All @@ -156,7 +174,7 @@ class RaplaResponseParser {
details: trimAndEscapeString(details),
professor: trimAndEscapeString(professor),
type: type,
room: trimAndEscapeString(concatStringList(resource, ", ")),
room: trimAndEscapeString(resource),
);
return scheduleEntry;
}
Expand Down Expand Up @@ -194,14 +212,14 @@ class RaplaResponseParser {
}
}

List<String> _extractResources(Element value) {
String _extractResources(Element value) {
var resources = value.getElementsByClassName(RESOURCE_CLASS);

var resourcesList = <String>[];
for (var resource in resources) {
resourcesList.add(resource.innerHtml);
}

return resourcesList;
return concatStringList(resourcesList, ", ");
}
}
26 changes: 18 additions & 8 deletions test/schedule/service/rapla/rapla_response_parser_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:io';

import 'package:dhbwstudentapp/schedule/model/schedule_entry.dart';
import 'package:dhbwstudentapp/schedule/service/rapla/rapla_response_parser.dart';
import 'package:test/test.dart';

Expand All @@ -15,36 +16,45 @@ Future<void> main() async {

expect(schedule.entries.length, 8);

expect(schedule.entries[0].title, "Online - Netztechnik I");
expect(schedule.entries[0].title, "Netztechnik I");
expect(schedule.entries[0].start, DateTime(2020, 09, 07, 09, 15));
expect(schedule.entries[0].end, DateTime(2020, 09, 07, 11, 45));
expect(schedule.entries[0].type, ScheduleEntryType.Online);
expect(schedule.entries[0].professor, "Müller, Georg");

expect(schedule.entries[1].title, "Online - Semestereinführung");
expect(schedule.entries[1].title, "Semestereinführung");
expect(schedule.entries[1].start, DateTime(2020, 09, 07, 12, 00));
expect(schedule.entries[1].end, DateTime(2020, 09, 07, 12, 30));
expect(schedule.entries[1].type, ScheduleEntryType.Online);

expect(schedule.entries[2].title, "Online - Messdatenerfassung");
expect(schedule.entries[2].title, "Messdatenerfassung");
expect(schedule.entries[2].start, DateTime(2020, 09, 07, 13, 00));
expect(schedule.entries[2].end, DateTime(2020, 09, 07, 14, 30));
expect(schedule.entries[2].type, ScheduleEntryType.Online);

expect(schedule.entries[3].title, "Online - Formale Sprachen & Automaten");
expect(schedule.entries[3].title, "Formale Sprachen & Automaten");
expect(schedule.entries[3].start, DateTime(2020, 09, 08, 08, 15));
expect(schedule.entries[3].end, DateTime(2020, 09, 08, 11, 45));
expect(schedule.entries[3].type, ScheduleEntryType.Online);

expect(schedule.entries[4].title, "Online - Signale & Systeme I");
expect(schedule.entries[4].title, "Signale & Systeme I");
expect(schedule.entries[4].start, DateTime(2020, 09, 08, 13, 00));
expect(schedule.entries[4].end, DateTime(2020, 09, 08, 15, 00));
expect(schedule.entries[4].type, ScheduleEntryType.Online);

expect(schedule.entries[5].title, "Online - Angewandte Mathematik");
expect(schedule.entries[5].title, "Angewandte Mathematik");
expect(schedule.entries[5].start, DateTime(2020, 09, 09, 09, 00));
expect(schedule.entries[5].end, DateTime(2020, 09, 09, 11, 45));
expect(schedule.entries[5].type, ScheduleEntryType.Online);

expect(schedule.entries[6].title, "Online - SWE");
expect(schedule.entries[6].title, "SWE");
expect(schedule.entries[6].start, DateTime(2020, 09, 10, 09, 15));
expect(schedule.entries[6].end, DateTime(2020, 09, 10, 12, 00));
expect(schedule.entries[6].type, ScheduleEntryType.Online);

expect(schedule.entries[7].title, "Online - Messdatenerfassung");
expect(schedule.entries[7].title, "Messdatenerfassung");
expect(schedule.entries[7].start, DateTime(2020, 09, 10, 13, 00));
expect(schedule.entries[7].end, DateTime(2020, 09, 10, 14, 30));
expect(schedule.entries[7].type, ScheduleEntryType.Online);
});
}

0 comments on commit 77d37e9

Please sign in to comment.