From 7b7be1eef12c7ada1a4efebef24465201105c31f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sun, 10 Jun 2018 19:46:34 +0200 Subject: [PATCH] API fixes (#136) --- .../java/io/github/wulkanowy/api/Client.java | 7 +++++- .../io/github/wulkanowy/api/login/Login.java | 9 +++++++- .../wulkanowy/api/timetable/Timetable.java | 22 +++++++++++++++++-- .../api/timetable/TimetableTest.java | 1 + .../api/timetable/PlanLekcji-full.html | 15 ++++++++++++- 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/api/src/main/java/io/github/wulkanowy/api/Client.java b/api/src/main/java/io/github/wulkanowy/api/Client.java index 869ed65e..8bbae7ca 100644 --- a/api/src/main/java/io/github/wulkanowy/api/Client.java +++ b/api/src/main/java/io/github/wulkanowy/api/Client.java @@ -62,6 +62,7 @@ public class Client { return; } + clearCookies(); this.symbol = new Login(this).login(email, password, symbol); logger.info("Login successful on {} at {}", getHost(), new Date()); } @@ -87,6 +88,10 @@ public class Client { return cookies.getItems(); } + public void clearCookies() { + cookies = new Cookies(); + } + String getHost() { return host; } @@ -216,7 +221,7 @@ public class Client { } if ("Błąd strony".equals(title)) { - throw new NotLoggedInErrorException(title + " " + doc.selectFirst("body") + ", status: " + code); + throw new NotLoggedInErrorException(title + " " + doc.body() + ", status: " + code); } return doc; diff --git a/api/src/main/java/io/github/wulkanowy/api/login/Login.java b/api/src/main/java/io/github/wulkanowy/api/login/Login.java index 04e1efc1..f6d4cd11 100644 --- a/api/src/main/java/io/github/wulkanowy/api/login/Login.java +++ b/api/src/main/java/io/github/wulkanowy/api/login/Login.java @@ -5,6 +5,8 @@ import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.parser.Parser; import org.jsoup.select.Elements; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; @@ -21,6 +23,8 @@ public class Login { private Client client; + private static final Logger logger = LoggerFactory.getLogger(Login.class); + public Login(Client client) { this.client = client; } @@ -29,7 +33,8 @@ public class Login { Document certDoc = sendCredentials(email, password); if ("Błąd".equals(certDoc.title())) { - throw new NotLoggedInErrorException(certDoc.selectFirst("body").text()); + client.clearCookies(); + throw new NotLoggedInErrorException(certDoc.body().text()); } return sendCertificate(certDoc, symbol); @@ -88,6 +93,7 @@ public class Login { String title = targetDoc.title(); if ("Working...".equals(title)) { // on adfs login + logger.info("ADFS login"); title = sendCertData(targetDoc).title(); } @@ -96,6 +102,7 @@ public class Login { } if (!"Uonet+".equals(title)) { + logger.debug("Login failed. Body: {}", targetDoc.body()); throw new LoginErrorException("Expected page title `UONET+`, got " + title); } diff --git a/api/src/main/java/io/github/wulkanowy/api/timetable/Timetable.java b/api/src/main/java/io/github/wulkanowy/api/timetable/Timetable.java index 8a2b9e14..95c40167 100644 --- a/api/src/main/java/io/github/wulkanowy/api/timetable/Timetable.java +++ b/api/src/main/java/io/github/wulkanowy/api/timetable/Timetable.java @@ -3,6 +3,8 @@ package io.github.wulkanowy.api.timetable; import org.apache.commons.lang3.StringUtils; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.ArrayList; @@ -13,8 +15,8 @@ import io.github.wulkanowy.api.VulcanException; import io.github.wulkanowy.api.generic.Lesson; import io.github.wulkanowy.api.generic.Week; -import static io.github.wulkanowy.api.DateTimeUtilsKt.getFormattedDate; import static io.github.wulkanowy.api.DateTimeUtilsKt.getDateAsTick; +import static io.github.wulkanowy.api.DateTimeUtilsKt.getFormattedDate; public class Timetable { @@ -22,6 +24,8 @@ public class Timetable { private SnP snp; + private static final Logger logger = LoggerFactory.getLogger(Timetable.class); + public Timetable(SnP snp) { this.snp = snp; } @@ -45,8 +49,13 @@ public class Timetable { private List getDays(Elements tableHeaderCells) { List days = new ArrayList<>(); + int numberOfDays = tableHeaderCells.size(); - for (int i = 2; i < 7; i++) { + if (numberOfDays > 7) { + logger.info("Number of days: {}", numberOfDays); + } + + for (int i = 2; i < numberOfDays; i++) { String[] dayHeaderCell = tableHeaderCells.get(i).html().split("
"); TimetableDay day = new TimetableDay(); @@ -127,6 +136,11 @@ public class Timetable { private void addLessonInfoFromElement(Lesson lesson, Element e) { Elements spans = e.select("span"); + if (spans.isEmpty()) { + logger.warn("Lesson span is empty"); + return; + } + addTypeInfo(lesson, spans); addNormalLessonInfo(lesson, spans); addChangesInfo(lesson, spans); @@ -213,6 +227,10 @@ public class Timetable { } private String[] getLessonAndGroupInfoFromSpan(Element span) { + if (!span.text().contains("[")) { + return new String[] {span.text(), ""}; + } + String[] subjectNameArray = span.text().split(" "); String groupName = subjectNameArray[subjectNameArray.length - 1]; diff --git a/api/src/test/java/io/github/wulkanowy/api/timetable/TimetableTest.java b/api/src/test/java/io/github/wulkanowy/api/timetable/TimetableTest.java index 1383b830..7b3cce6e 100644 --- a/api/src/test/java/io/github/wulkanowy/api/timetable/TimetableTest.java +++ b/api/src/test/java/io/github/wulkanowy/api/timetable/TimetableTest.java @@ -156,6 +156,7 @@ public class TimetableTest extends StudentAndParentTestCase { Assert.assertEquals("poprzednio: Wychowanie fizyczne", full.getWeekTable().getDay(4).getLesson(2).getDescription()); Assert.assertEquals("egzamin", full.getWeekTable().getDay(3).getLesson(0).getDescription()); Assert.assertEquals("", full.getWeekTable().getDay(4).getLesson(1).getDescription()); + Assert.assertEquals("poprzednio: Zajęcia z wychowawcą", full.getWeekTable().getDay(4).getLesson(5).getDescription()); Assert.assertEquals("opis w uwadze bez klasy w spanie", full.getWeekTable().getDay(4).getLesson(4).getDescription()); Assert.assertEquals("", holidays.getWeekTable().getDay(3).getLesson(3).getDescription()); } diff --git a/api/src/test/resources/io/github/wulkanowy/api/timetable/PlanLekcji-full.html b/api/src/test/resources/io/github/wulkanowy/api/timetable/PlanLekcji-full.html index c6ade3a5..c3dbfebe 100644 --- a/api/src/test/resources/io/github/wulkanowy/api/timetable/PlanLekcji-full.html +++ b/api/src/test/resources/io/github/wulkanowy/api/timetable/PlanLekcji-full.html @@ -364,7 +364,20 @@ - + +
+ Tworzenie i administrowanie bazami danych [zaw2] + + + +
+
+ Zajęcia z wychowawcą + Małgorzata Kowal + 43 + (zmiana organizacji zajęć) +
+ 6