Search in sources :

Example 1 with SubstitutionScheduleDay

use of me.vertretungsplan.objects.SubstitutionScheduleDay in project substitution-schedule-parser by vertretungsplanme.

the class IndiwareParser method parseIndiwareDay.

SubstitutionScheduleDay parseIndiwareDay(Element doc, boolean html) throws IOException, JSONException {
    SubstitutionScheduleDay day = new SubstitutionScheduleDay();
    DataSource ds;
    if (html) {
        ds = new HTMLDataSource(doc);
    } else {
        ds = new XMLDataSource(doc);
    }
    Matcher matcher = datePattern.matcher(ds.titel().text());
    if (!matcher.find())
        throw new IOException("malformed date: " + ds.titel().text());
    String date = matcher.group();
    day.setDate(DateTimeFormat.forPattern("EEEE, dd. MMMM yyyy").withLocale(Locale.GERMAN).parseLocalDate(date));
    matcher = lastChangePattern.matcher(ds.datum().text());
    if (!matcher.find())
        throw new IOException("malformed date: " + ds.datum().text());
    String lastChange = matcher.group();
    day.setLastChange(DateTimeFormat.forPattern("dd.MM.yyyy, HH:mm").withLocale(Locale.GERMAN).parseLocalDateTime(lastChange));
    if (ds.kopfinfos().size() > 0) {
        for (Element kopfinfo : ds.kopfinfos()) {
            String title = html ? kopfinfo.select("th").text() : kopfinfoTitle(kopfinfo.tagName()) + ":";
            StringBuilder message = new StringBuilder();
            if (title != null && !title.isEmpty()) {
                message.append("<b>").append(title).append("</b>").append(" ");
            }
            message.append(html ? kopfinfo.select("td").text() : kopfinfo.text());
            day.addMessage(message.toString());
        }
    }
    if (ds.fuss() != null) {
        StringBuilder message = new StringBuilder();
        boolean first = true;
        for (Element fusszeile : ds.fusszeilen()) {
            if (first) {
                first = false;
            } else {
                message.append("<br>\n");
            }
            message.append(fusszeile.text());
        }
        day.addMessage(message.toString());
    }
    if (ds.aufsichten() != null) {
        StringBuilder message = new StringBuilder();
        message.append("<b>").append("GeƤnderte Aufsichten:").append("</b>");
        for (Element aufsicht : ds.aufsichtzeilen()) {
            message.append("<br>\n");
            message.append(aufsicht.text());
        }
        day.addMessage(message.toString());
    }
    List<String> columnTypes = null;
    if (html) {
        columnTypes = new ArrayList<>();
        for (Element th : ((HTMLDataSource) ds).headers()) {
            Set<String> classNames = th.classNames();
            for (String className : classNames) {
                if (className.contains("thplan") || className.contains("thlplan")) {
                    columnTypes.add(className.replace("thplan", "").replace("thlplan", "").replace("_scheuler", // sic! -> http://www.hildebrand-gymnasium.de/index.php/klasse-5.html
                    ""));
                    break;
                }
            }
        }
    }
    for (Element aktion : ds.aktionen()) {
        Substitution substitution = new Substitution();
        String course = null;
        int i = 0;
        for (Element info : aktion.children()) {
            String value = info.text().replace("\u00a0", "");
            if (value.equals("---")) {
                i++;
                continue;
            }
            final String columnType = html ? columnTypes.get(i) : info.tagName();
            switch(columnType) {
                case "klasse":
                    ClassAndCourse cac = new ClassAndCourse(value, data);
                    course = cac.course;
                    substitution.setClasses(cac.classes);
                    break;
                case "stunde":
                    substitution.setLesson(value);
                    break;
                case "fach":
                    String subject = subjectAndCourse(course, value);
                    if (columnTypes != null && columnTypes.contains("vfach")) {
                        substitution.setPreviousSubject(subject);
                    } else {
                        substitution.setSubject(subject);
                    }
                    break;
                case "vfach":
                    substitution.setSubject(subjectAndCourse(course, value));
                case "lehrer":
                    Matcher bracesMatcher = bracesPattern.matcher(value);
                    if (bracesMatcher.matches()) {
                        value = bracesMatcher.group(1);
                        substitution.setPreviousTeachers(new HashSet<>(Arrays.asList(value.split(", "))));
                    } else {
                        substitution.setTeachers(new HashSet<>(Arrays.asList(value.split(", "))));
                    }
                    break;
                case "raum":
                    if (columnTypes != null && columnTypes.contains("vraum")) {
                        substitution.setPreviousRoom(value);
                    } else {
                        substitution.setRoom(value);
                    }
                    break;
                case "vraum":
                    substitution.setRoom(value);
                case "info":
                    handleDescription(substitution, value);
                    break;
            }
            i++;
        }
        if (substitution.getType() == null)
            substitution.setType("Vertretung");
        substitution.setColor(colorProvider.getColor(substitution.getType()));
        if (course != null && substitution.getSubject() == null) {
            substitution.setSubject(course);
        }
        day.addSubstitution(substitution);
    }
    return day;
}
Also used : Matcher(java.util.regex.Matcher) Element(org.jsoup.nodes.Element) IOException(java.io.IOException) Substitution(me.vertretungsplan.objects.Substitution) SubstitutionScheduleDay(me.vertretungsplan.objects.SubstitutionScheduleDay)

Example 2 with SubstitutionScheduleDay

use of me.vertretungsplan.objects.SubstitutionScheduleDay in project substitution-schedule-parser by vertretungsplanme.

the class LegionBoardParser method parseLegionBoard.

void parseLegionBoard(SubstitutionSchedule substitutionSchedule, JSONArray changes, JSONArray courses, JSONArray teachers) throws IOException, JSONException {
    if (changes == null) {
        return;
    }
    // Link course IDs to their names
    HashMap<String, String> coursesHashMap = null;
    if (courses != null) {
        coursesHashMap = new HashMap<>();
        for (int i = 0; i < courses.length(); i++) {
            JSONObject course = courses.getJSONObject(i);
            coursesHashMap.put(course.getString("id"), course.getString("name"));
        }
    }
    // Link teacher IDs to their names
    HashMap<String, String> teachersHashMap = null;
    if (teachers != null) {
        teachersHashMap = new HashMap<>();
        for (int i = 0; i < teachers.length(); i++) {
            JSONObject teacher = teachers.getJSONObject(i);
            teachersHashMap.put(teacher.getString("id"), teacher.getString("name"));
        }
    }
    // Add changes to SubstitutionSchedule
    LocalDate currentDate = LocalDate.now();
    SubstitutionScheduleDay substitutionScheduleDay = new SubstitutionScheduleDay();
    substitutionScheduleDay.setDate(currentDate);
    for (int i = 0; i < changes.length(); i++) {
        final JSONObject change = changes.getJSONObject(i);
        final Substitution substitution = getSubstitution(change, coursesHashMap, teachersHashMap);
        final LocalDate startingDate = new LocalDate(change.getString("startingDate"));
        final LocalDate endingDate = new LocalDate(change.getString("endingDate"));
        // Handle multi-day changes
        if (!startingDate.isEqual(endingDate)) {
            if (!substitutionScheduleDay.getSubstitutions().isEmpty()) {
                substitutionSchedule.addDay(substitutionScheduleDay);
            }
            for (int k = 0; k < 8; k++) {
                final LocalDate date = LocalDate.now().plusDays(k);
                if ((date.isAfter(startingDate) || date.isEqual(startingDate)) && (date.isBefore(endingDate) || date.isEqual(endingDate))) {
                    substitutionScheduleDay = new SubstitutionScheduleDay();
                    substitutionScheduleDay.setDate(date);
                    substitutionScheduleDay.addSubstitution(substitution);
                    substitutionSchedule.addDay(substitutionScheduleDay);
                    currentDate = date;
                }
            }
            continue;
        }
        // If starting date of change does not equal date of SubstitutionScheduleDay
        if (!startingDate.isEqual(currentDate)) {
            if (!substitutionScheduleDay.getSubstitutions().isEmpty()) {
                substitutionSchedule.addDay(substitutionScheduleDay);
            }
            substitutionScheduleDay = new SubstitutionScheduleDay();
            substitutionScheduleDay.setDate(startingDate);
            currentDate = startingDate;
        }
        substitutionScheduleDay.addSubstitution(substitution);
    }
    substitutionSchedule.addDay(substitutionScheduleDay);
}
Also used : JSONObject(org.json.JSONObject) Substitution(me.vertretungsplan.objects.Substitution) SubstitutionScheduleDay(me.vertretungsplan.objects.SubstitutionScheduleDay) LocalDate(org.joda.time.LocalDate)

Example 3 with SubstitutionScheduleDay

use of me.vertretungsplan.objects.SubstitutionScheduleDay in project substitution-schedule-parser by vertretungsplanme.

the class DaVinciParser method parsePage.

@NotNull
static void parsePage(Element doc, SubstitutionSchedule schedule, ColorProvider colorProvider) throws IOException {
    SubstitutionScheduleDay day = new SubstitutionScheduleDay();
    Element titleElem;
    if (doc.select("h1.list-table-caption").size() > 0) {
        titleElem = doc.select("h1.list-table-caption").first();
    } else {
        // DaVinci 5
        titleElem = doc.select("h2").first();
    }
    String title = titleElem.text();
    String klasse = null;
    // title can either be date or class
    Pattern datePattern = Pattern.compile("\\d+\\.\\d+.\\d{4}");
    Matcher dateMatcher = datePattern.matcher(title);
    if (dateMatcher.find()) {
        day.setDateString(dateMatcher.group());
        day.setDate(ParserUtils.parseDate(dateMatcher.group()));
    } else {
        klasse = title;
        String nextText = titleElem.nextElementSibling().text();
        if (nextText.matches("\\w+ \\d+\\.\\d+.\\d{4}")) {
            day.setDateString(nextText);
            day.setDate(ParserUtils.parseDate(nextText));
        } else {
            // could not find date, must be multiple days
            day = null;
        }
    }
    for (Element p : doc.select(".row:has(h1.list-table-caption) p")) {
        for (TextNode node : p.textNodes()) {
            if (!node.text().trim().isEmpty() && day != null)
                day.addMessage(node.text().trim());
        }
    }
    for (Element message : doc.select(".callout")) {
        for (TextNode node : message.textNodes()) {
            if (!node.text().trim().isEmpty())
                day.addMessage(node.text().trim());
        }
    }
    Element lastChangeElem = doc.select(".row.copyright div").first();
    if (lastChangeElem == null) {
        // DaVinci 5
        lastChangeElem = doc.select("h1").first();
    }
    if (lastChangeElem != null) {
        String lastChange = lastChangeElem.ownText();
        Pattern pattern = Pattern.compile("(\\d{2}-\\d{2}-\\d{4} \\d{2}:\\d{2}) \\|");
        Matcher matcher = pattern.matcher(lastChange);
        if (matcher.find()) {
            LocalDateTime lastChangeTime = DateTimeFormat.forPattern("dd-MM-yyyy HH:mm").parseLocalDateTime(matcher.group(1));
            if (day != null) {
                day.setLastChange(lastChangeTime);
            } else {
                schedule.setLastChange(lastChangeTime);
            }
        } else {
            Pattern pattern2 = Pattern.compile("(\\d{2}.\\d{2}.\\d{4} \\| \\d+:\\d{2})");
            Matcher matcher2 = pattern2.matcher(lastChange);
            if (matcher2.find()) {
                LocalDateTime lastChangeTime = DateTimeFormat.forPattern("dd.MM.yyyy | HH:mm").parseLocalDateTime(matcher2.group(1));
                if (day != null) {
                    day.setLastChange(lastChangeTime);
                } else {
                    schedule.setLastChange(lastChangeTime);
                }
            }
        }
    } else {
        Pattern pattern = Pattern.compile("<!-- Created by daVinci 5 \\| (\\d+\\.\\d+\\.\\d+ \\| \\d+:\\d+) \\| " + "www.stueber.de -->");
        Matcher matcher = pattern.matcher(doc.html());
        if (matcher.find()) {
            String str = matcher.group(1);
            LocalDateTime date = DateTimeFormat.forPattern("dd.MM.yyyy | HH:mm").parseLocalDateTime(str);
            if (day != null) {
                day.setLastChange(date);
            } else {
                schedule.setLastChange(date);
            }
        }
    }
    if (doc.select(".list-table").size() > 0 || !doc.select(".callout").text().contains("Es liegen keine")) {
        Element table = doc.select(".list-table, table").first();
        parseDaVinciTable(table, schedule, klasse, day, colorProvider);
    }
    if (day != null) {
        schedule.addDay(day);
    }
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Element(org.jsoup.nodes.Element) SubstitutionScheduleDay(me.vertretungsplan.objects.SubstitutionScheduleDay) TextNode(org.jsoup.nodes.TextNode) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with SubstitutionScheduleDay

use of me.vertretungsplan.objects.SubstitutionScheduleDay in project substitution-schedule-parser by vertretungsplanme.

the class UntisCommonParser method parseSubstitutionTable.

/**
 * Parses an Untis substitution table ({@link UntisSubstitutionParser}).
 *
 * @param v
 * @param lastChange
 * @param doc
 * @throws JSONException
 * @throws CredentialInvalidException
 */
protected void parseSubstitutionTable(SubstitutionSchedule v, String lastChange, Document doc, String className) throws JSONException, CredentialInvalidException, IOException {
    JSONObject data = scheduleData.getData();
    LocalDateTime lastChangeDate = ParserUtils.parseDateTime(lastChange);
    Pattern dayPattern = Pattern.compile("\\d\\d?.\\d\\d?. / \\w+");
    int dateColumn = -1;
    JSONArray columns = data.getJSONArray("columns");
    for (int i = 0; i < columns.length(); i++) {
        if (columns.getString(i).equals("date")) {
            dateColumn = i;
            break;
        }
    }
    Element table = doc.select("table[rules=all], table:has(tr:has(td[align=center]))").first();
    if (table == null || table.text().replace("\u00a0", "").trim().equals("Keine Vertretungen")) {
        return;
    }
    if (dateColumn == -1) {
        SubstitutionScheduleDay day = new SubstitutionScheduleDay();
        day.setLastChangeString(lastChange);
        day.setLastChange(lastChangeDate);
        String title = doc.select("font[size=5], font[size=4], font[size=3] b").text();
        Matcher matcher = dayPattern.matcher(title);
        if (matcher.find()) {
            String date = matcher.group();
            day.setDateString(date);
            day.setDate(ParserUtils.parseDate(date));
        }
        parseSubstitutionScheduleTable(table, data, day, className, getAllClasses());
        v.addDay(day);
    } else {
        for (Element line : table.select("tr.list.odd:not(:has(td.inline_header)), " + "tr.list.even:not(:has(td.inline_header)), " + "tr:has(td[align=center]):gt(0)")) {
            SubstitutionScheduleDay day = null;
            String date = line.select("td").get(dateColumn).text().replace("\u00a0", "").trim();
            if (date.isEmpty())
                continue;
            if (date.indexOf("-") > 0) {
                date = date.substring(0, date.indexOf("-") - 1).trim();
            }
            LocalDate parsedDate = ParserUtils.parseDate(date);
            for (SubstitutionScheduleDay search : v.getDays()) {
                if (Objects.equals(search.getDate(), parsedDate) || Objects.equals(search.getDateString(), date)) {
                    day = search;
                    break;
                }
            }
            if (day == null) {
                day = new SubstitutionScheduleDay();
                day.setDateString(date);
                day.setDate(parsedDate);
                day.setLastChangeString(lastChange);
                day.setLastChange(lastChangeDate);
                v.addDay(day);
            }
            parseSubstitutionScheduleTable(line, data, day, className, getAllClasses());
        }
    }
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) Pattern(java.util.regex.Pattern) JSONObject(org.json.JSONObject) Matcher(java.util.regex.Matcher) JSONArray(org.json.JSONArray) SubstitutionScheduleDay(me.vertretungsplan.objects.SubstitutionScheduleDay) LocalDate(org.joda.time.LocalDate)

Example 5 with SubstitutionScheduleDay

use of me.vertretungsplan.objects.SubstitutionScheduleDay in project substitution-schedule-parser by vertretungsplanme.

the class UntisCommonParser method parseMonitorDay.

SubstitutionScheduleDay parseMonitorDay(Element doc, JSONObject data) throws JSONException, CredentialInvalidException, IOException {
    SubstitutionScheduleDay day = new SubstitutionScheduleDay();
    String date = doc.select(".mon_title").first().text().replaceAll(" \\(Seite \\d+ / \\d+\\)", "");
    day.setDateString(date);
    day.setDate(ParserUtils.parseDate(date));
    if (!scheduleData.getData().has(PARAM_LAST_CHANGE_SELECTOR)) {
        String lastChange = findLastChange(doc, scheduleData);
        day.setLastChangeString(lastChange);
        day.setLastChange(ParserUtils.parseDateTime(lastChange));
    }
    // NACHRICHTEN
    if (doc.select("table.info").size() > 0) {
        parseMessages(doc.select("table.info").first(), day);
    }
    // VERTRETUNGSPLAN
    if (doc.select("table:has(tr.list)").size() > 0) {
        parseSubstitutionScheduleTable(doc.select("table:has(tr.list)").first(), data, day, getAllClasses());
    }
    return day;
}
Also used : SubstitutionScheduleDay(me.vertretungsplan.objects.SubstitutionScheduleDay)

Aggregations

SubstitutionScheduleDay (me.vertretungsplan.objects.SubstitutionScheduleDay)34 LocalDate (org.joda.time.LocalDate)15 Substitution (me.vertretungsplan.objects.Substitution)14 SubstitutionSchedule (me.vertretungsplan.objects.SubstitutionSchedule)14 Test (org.junit.Test)14 LocalDateTime (org.joda.time.LocalDateTime)12 Element (org.jsoup.nodes.Element)10 Matcher (java.util.regex.Matcher)6 Document (org.jsoup.nodes.Document)6 Elements (org.jsoup.select.Elements)6 Pattern (java.util.regex.Pattern)5 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 NotNull (org.jetbrains.annotations.NotNull)3 JSONObject (org.json.JSONObject)3 JSONArray (org.json.JSONArray)2 HashSet (java.util.HashSet)1 AdditionalInfo (me.vertretungsplan.objects.AdditionalInfo)1 SubstitutionScheduleData (me.vertretungsplan.objects.SubstitutionScheduleData)1 TextNode (org.jsoup.nodes.TextNode)1