1
0

Add timetable api (#9)

This commit is contained in:
Mikołaj Pich
2017-08-05 14:05:57 +02:00
committed by GitHub
parent 4539a62de9
commit 1a244e3995
11 changed files with 1582 additions and 0 deletions

View File

@ -0,0 +1,55 @@
package io.github.wulkanowy.api.timetable;
import java.util.ArrayList;
import java.util.List;
public class Day {
private List<Lesson> lessons = new ArrayList<>();
private String date = "";
private boolean isFreeDay = false;
private String freeDayName = "";
public Lesson getLesson(int index) {
return lessons.get(index);
}
public List<Lesson> getLessons() {
return lessons;
}
public Day setLesson(Lesson lesson) {
this.lessons.add(lesson);
return this;
}
public String getDate() {
return date;
}
public Day setDate(String date) {
this.date = date;
return this;
}
public boolean isFreeDay() {
return isFreeDay;
}
public Day setFreeDay(boolean freeDay) {
isFreeDay = freeDay;
return this;
}
public String getFreeDayName() {
return freeDayName;
}
public Day setFreeDayName(String freeDayName) {
this.freeDayName = freeDayName;
return this;
}
}

View File

@ -0,0 +1,141 @@
package io.github.wulkanowy.api.timetable;
public class Lesson {
public static final String CLASS_PLANNING = "x-treelabel-ppl";
public static final String CLASS_REALIZED = "x-treelabel-rlz";
public static final String CLASS_MOVED_OR_CANCELED = "x-treelabel-inv";
public static final String CLASS_NEW_MOVED_IN_OR_CHANGED = "x-treelabel-zas";
private String subject = "";
private String teacher = "";
private String room = "";
private String description = "";
private String groupName = "";
private String startTime = "";
private String endTime = "";
private boolean isEmpty = false;
private boolean isDivisionIntoGroups = false;
private boolean isPlanning = false;
private boolean isRealized = false;
private boolean isMovedOrCanceled = false;
private boolean isNewMovedInOrChanged = false;
public String getSubject() {
return subject;
}
public Lesson setSubject(String subject) {
this.subject = subject;
return this;
}
public String getTeacher() {
return teacher;
}
public Lesson setTeacher(String teacher) {
this.teacher = teacher;
return this;
}
public String getRoom() {
return room;
}
public Lesson setRoom(String room) {
this.room = room;
return this;
}
public String getDescription() {
return description;
}
public Lesson setDescription(String description) {
this.description = description;
return this;
}
public String getGroupName() {
return groupName;
}
public Lesson setGroupName(String groupName) {
this.groupName = groupName;
return this;
}
public String getStartTime() {
return startTime;
}
public Lesson setStartTime(String startTime) {
this.startTime = startTime;
return this;
}
public String getEndTime() {
return endTime;
}
public Lesson setEndTime(String endTime) {
this.endTime = endTime;
return this;
}
public boolean isEmpty() {
return isEmpty;
}
public Lesson setEmpty(boolean empty) {
isEmpty = empty;
return this;
}
public boolean isDivisionIntoGroups() {
return isDivisionIntoGroups;
}
public Lesson setDivisionIntoGroups(boolean divisionIntoGroups) {
isDivisionIntoGroups = divisionIntoGroups;
return this;
}
public boolean isPlanning() {
return isPlanning;
}
public Lesson setPlanning(boolean planning) {
isPlanning = planning;
return this;
}
public boolean isRealized() {
return isRealized;
}
public Lesson setRealized(boolean realized) {
isRealized = realized;
return this;
}
public boolean isMovedOrCanceled() {
return isMovedOrCanceled;
}
public Lesson setMovedOrCanceled(boolean movedOrCanceled) {
isMovedOrCanceled = movedOrCanceled;
return this;
}
public boolean isNewMovedInOrChanged() {
return isNewMovedInOrChanged;
}
public Lesson setNewMovedInOrChanged(boolean newMovedInOrChanged) {
isNewMovedInOrChanged = newMovedInOrChanged;
return this;
}
}

View File

@ -0,0 +1,143 @@
package io.github.wulkanowy.api.timetable;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.api.login.LoginErrorException;
public class Table extends Vulcan {
private Timetable timetable;
public Table(Timetable timetable) {
this.timetable = timetable;
}
public Week getWeekTable() throws IOException, LoginErrorException {
return getWeekTable("");
}
public Week getWeekTable(String tick) throws IOException, LoginErrorException {
Element table = timetable.getTablePageDocument(tick)
.select(".mainContainer .presentData").first();
Elements tableHeaderCells = table.select("thead th");
List<Day> days = new ArrayList<>();
for (int i = 2; i < 7; i++) {
String[] dayHeaderCell = tableHeaderCells.get(i).html().split("<br>");
boolean isFreeDay = tableHeaderCells.get(i).hasClass("free-day");
Day day = new Day();
day.setDate(dayHeaderCell[1]);
if (isFreeDay) {
day.setFreeDay(isFreeDay);
day.setFreeDayName(dayHeaderCell[2]);
}
days.add(day);
}
Elements hoursInDays = table.select("tbody tr");
// fill days in week with lessons
for (Element row : hoursInDays) {
Elements hours = row.select("td");
// fill hours in day
for (int i = 2; i < hours.size(); i++) {
Lesson lesson = new Lesson();
Elements e = hours.get(i).select("div");
switch (e.size()) {
case 1:
lesson = getLessonFromElement(e.first());
break;
case 3:
lesson = getLessonFromElement(e.get(1));
break;
default:
lesson.setEmpty(true);
break;
}
String[] startEndEnd = hours.get(1).text().split(" ");
lesson.setStartTime(startEndEnd[0]);
lesson.setEndTime(startEndEnd[1]);
days.get(i - 2).setLesson(lesson);
}
}
Element startDayCellHeader = tableHeaderCells.get(2);
String[] dayDescription = startDayCellHeader.html().split("<br>");
return new Week()
.setStartDayDate(dayDescription[1])
.setDays(days);
}
public Lesson getLessonFromElement(Element e) {
Lesson lesson = new Lesson();
Elements spans = e.select("span");
lesson.setSubject(spans.get(0).text());
lesson.setTeacher(spans.get(1).text());
lesson.setRoom(spans.get(2).text());
lesson = getLessonGroupDivisionInfo(lesson, spans);
lesson = getLessonTypeInfo(lesson, spans);
lesson = getLessonDescriptionInfo(lesson, spans);
return lesson;
}
public Lesson getLessonGroupDivisionInfo(Lesson lesson, Elements e) {
if ((4 == e.size() && (e.first().attr("class").equals("")) ||
(5 == e.size() && e.first().hasClass(Lesson.CLASS_NEW_MOVED_IN_OR_CHANGED)))) {
lesson.setDivisionIntoGroups(true);
String[] subjectNameArray = lesson.getSubject().split(" ");
String groupName = subjectNameArray[subjectNameArray.length - 1];
lesson.setSubject(lesson.getSubject().replace(" " + groupName, ""));
lesson.setGroupName(StringUtils.substringBetween(groupName, "[", "]"));
lesson.setTeacher(e.get(2).text());
lesson.setRoom(e.get(3).text());
}
return lesson;
}
public Lesson getLessonTypeInfo(Lesson lesson, Elements e) {
if (e.first().hasClass(Lesson.CLASS_MOVED_OR_CANCELED)) {
lesson.setMovedOrCanceled(true);
} else if (e.first().hasClass(Lesson.CLASS_NEW_MOVED_IN_OR_CHANGED)) {
lesson.setNewMovedInOrChanged(true);
} else if (e.first().hasClass(Lesson.CLASS_PLANNING)) {
lesson.setPlanning(true);
}
if (e.last().hasClass(Lesson.CLASS_REALIZED)
|| e.first().attr("class").equals("")) {
lesson.setRealized(true);
}
return lesson;
}
public Lesson getLessonDescriptionInfo(Lesson lesson, Elements e) {
if ((4 == e.size() || 5 == e.size())
&& (e.first().hasClass(Lesson.CLASS_MOVED_OR_CANCELED)
|| e.first().hasClass(Lesson.CLASS_NEW_MOVED_IN_OR_CHANGED))) {
lesson.setDescription(StringUtils.substringBetween(e.last().text(), "(", ")"));
}
return lesson;
}
}

View File

@ -0,0 +1,33 @@
package io.github.wulkanowy.api.timetable;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
import io.github.wulkanowy.api.Cookies;
import io.github.wulkanowy.api.StudentAndParent;
import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.api.login.LoginErrorException;
public class Timetable extends Vulcan {
private StudentAndParent snp;
private String timetablePageurl =
"https://uonetplus-opiekun.vulcan.net.pl/{locationID}/{ID}/Lekcja.mvc/PlanLekcji?data=";
public Timetable(Cookies cookies, StudentAndParent snp) {
this.cookies = cookies;
this.snp = snp;
}
public Document getTablePageDocument(String tick) throws IOException, LoginErrorException {
timetablePageurl = timetablePageurl.replace("{locationID}", snp.getLocationID());
timetablePageurl = timetablePageurl.replace("{ID}", snp.getID());
return Jsoup.connect(timetablePageurl + tick)
.cookies(getCookies())
.get();
}
}

View File

@ -0,0 +1,33 @@
package io.github.wulkanowy.api.timetable;
import java.util.ArrayList;
import java.util.List;
public class Week {
private List<Day> days = new ArrayList<>();
private String startDayDate = "";
public Day getDay(int index) {
return days.get(index);
}
public List<Day> getDays() {
return days;
}
public Week setDays(List<Day> days) {
this.days = days;
return this;
}
public String getStartDayDate() {
return startDayDate;
}
public Week setStartDayDate(String startDayDate) {
this.startDayDate = startDayDate;
return this;
}
}

View File

@ -0,0 +1,37 @@
package io.github.wulkanowy.utilities;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
abstract public class DateHelper {
private static final long TICKS_AT_EPOCH = 621355968000000000L;
private static final long TICKS_PER_MILLISECOND = 10000;
public static long getTicks(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return (calendar.getTimeInMillis() * TICKS_PER_MILLISECOND) + TICKS_AT_EPOCH;
}
public static long getTics(String dateString) throws ParseException {
return getTics(dateString, "dd.MM.yyyy");
}
public static long getTics(String dateString, String dateFormat) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat(dateFormat, Locale.ROOT);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
Date dateObject = format.parse(dateString);
return getTicks(dateObject);
}
public static Date getDate(long ticks) {
return new Date((ticks - TICKS_AT_EPOCH) / TICKS_PER_MILLISECOND);
}
}