Search in sources :

Example 6 with SubstitutionSchedule

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

the class ParserUtil method parseSubstitutionSchedule.

/**
 * Parses a substitution schedule.
 *
 * @param data       A <code>SubstitutionScheduleData</code> instance containing information about the schedule you
 *                   want to parse.
 * @param credential A <code>Credential</code> subclass for authentication. If the schedule requires no
 *                   authentication, use <code>null</code>.
 * @param cp         An optional <code>CookieProvider</code> implementation. This can be used if you want to reuse
 *                   session cookies the next time you load the schedule. If you don't need it, pass
 *                   <code>null</code>.
 * @param handler    An optional <code>DebuggingDataHandler</code> implementation. If you don't need it, pass
 *                   <code>null</code>.
 * @return The parsed substitution schedule.
 * @throws JSONException              When there's an error with your JSON configuration
 * @throws CredentialInvalidException When the <code>Credential</code> you supplied was invalid
 * @throws IOException                When there was another error while loading or parsing the schedule
 */
public static SubstitutionSchedule parseSubstitutionSchedule(SubstitutionScheduleData data, Credential credential, CookieProvider cp, DebuggingDataHandler handler) throws JSONException, CredentialInvalidException, IOException {
    SubstitutionScheduleParser parser = BaseParser.getInstance(data, cp);
    if (credential != null)
        parser.setCredential(credential);
    if (handler != null)
        ((BaseParser) parser).setDebuggingDataHandler(handler);
    SubstitutionSchedule schedule = parser.getSubstitutionSchedule();
    for (String a : data.getAdditionalInfos()) {
        BaseAdditionalInfoParser aParser = BaseAdditionalInfoParser.getInstance(a);
        schedule.addAdditionalInfo(aParser.getAdditionalInfo());
    }
    return schedule;
}
Also used : SubstitutionScheduleParser(me.vertretungsplan.parser.SubstitutionScheduleParser) SubstitutionSchedule(me.vertretungsplan.objects.SubstitutionSchedule) BaseAdditionalInfoParser(me.vertretungsplan.additionalinfo.BaseAdditionalInfoParser)

Example 7 with SubstitutionSchedule

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

the class UntisInfoHeadlessParser method getSubstitutionSchedule.

@Override
public SubstitutionSchedule getSubstitutionSchedule() throws IOException, JSONException, CredentialInvalidException {
    new LoginHandler(scheduleData, credential, cookieProvider).handleLogin(executor, cookieStore);
    SubstitutionSchedule v = SubstitutionSchedule.fromData(scheduleData);
    Document doc = Jsoup.parse(httpGet(url, data.optString(PARAM_ENCODING, null)));
    doc.setBaseUri(url);
    Elements dayElems = doc.select("#vertretung > p > b, #vertretung > b");
    Elements frames = doc.select("frame[src*=w00]");
    if (dayElems.size() == 0 && frames.size() > 0) {
        // doc is embedded in frame
        doc = Jsoup.parse(httpGet(frames.get(0).absUrl("src"), data.optString(PARAM_ENCODING, null)));
        dayElems = doc.select("#vertretung > p > b, #vertretung > b");
    }
    final List<String> allClasses = getAllClasses();
    if (dayElems.size() > 0) {
        // untis-info days
        for (Element dayElem : dayElems) {
            SubstitutionScheduleDay day = new SubstitutionScheduleDay();
            day.setLastChangeString("");
            String date = dayElem.text();
            day.setDateString(date);
            day.setDate(ParserUtils.parseDate(date));
            Element next;
            if (dayElem.parent().tagName().equals("p")) {
                next = dayElem.parent().nextElementSibling().nextElementSibling();
            } else {
                next = dayElem.parent().select("p").first().nextElementSibling();
            }
            parseDay(day, next, v, null, allClasses);
        }
    } else if (doc.select("tr:has(td[align=center]):gt(0)").size() > 0) {
        // untis-subst table
        parseSubstitutionTable(v, null, doc);
    }
    v.setClasses(allClasses);
    v.setTeachers(getAllTeachers());
    return v;
}
Also used : SubstitutionSchedule(me.vertretungsplan.objects.SubstitutionSchedule) Element(org.jsoup.nodes.Element) SubstitutionScheduleDay(me.vertretungsplan.objects.SubstitutionScheduleDay) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements)

Example 8 with SubstitutionSchedule

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

the class UntisInfoParser method getSubstitutionSchedule.

@Override
public SubstitutionSchedule getSubstitutionSchedule() throws IOException, JSONException, CredentialInvalidException {
    new LoginHandler(scheduleData, credential, cookieProvider).handleLogin(executor, cookieStore);
    Document navbarDoc = Jsoup.parse(getNavbarDoc().replace("&nbsp;", ""));
    Element select = navbarDoc.select("select[name=week]").first();
    SubstitutionSchedule v = SubstitutionSchedule.fromData(scheduleData);
    String info = navbarDoc.select(".description").text();
    String lastChange;
    try {
        lastChange = info.substring(info.indexOf("Stand:") + "Stand:".length()).trim();
    } catch (Exception e) {
        try {
            String infoHtml = httpGet(baseUrl + "/frames/title.htm", data.optString(PARAM_ENCODING, null));
            Document infoDoc = Jsoup.parse(infoHtml);
            String info2 = infoDoc.select(".description").text();
            lastChange = info2.substring(info2.indexOf("Stand:") + "Stand:".length()).trim();
        } catch (Exception e1) {
            lastChange = "";
        }
    }
    List<String> allClasses = new ArrayList<>();
    try {
        allClasses = getAllClasses();
    } catch (IOException e) {
        // we only really need this for teacher schedules
        if (!getLetter(data).equals("v")) {
            throw e;
        }
    }
    int successfulWeeks = 0;
    HttpResponseException lastException = null;
    for (Element option : select.children()) {
        String week = option.attr("value");
        String weekName = option.text();
        if (data.optBoolean(PARAM_SINGLE_CLASSES, // backwards compatibility
        data.optBoolean("single_classes", false)) || data.optString(PARAM_SCHEDULE_TYPE, "substitution").equals("timetable")) {
            int classNumber = 1;
            List<String> classesToSelect;
            if (getLetter(data).equals("v")) {
                classesToSelect = parseTeachers(getNavbarDoc(), data);
            } else {
                classesToSelect = parseClasses(getNavbarDoc(), data);
            }
            for (String klasse : classesToSelect) {
                String url = getScheduleUrl(week, classNumber, data);
                try {
                    parsePage(v, lastChange, klasse, url, weekName, allClasses);
                } catch (HttpResponseException e) {
                    if (e.getStatusCode() == 500) {
                        // occurs in Hannover_MMBS
                        classNumber++;
                        continue;
                    } else {
                        throw e;
                    }
                }
                classNumber++;
            }
            successfulWeeks++;
        } else {
            String url = getScheduleUrl(week, 0, data);
            try {
                parsePage(v, lastChange, null, url, weekName, allClasses);
                successfulWeeks++;
            } catch (HttpResponseException e) {
                lastException = e;
            }
        }
    }
    if (successfulWeeks == 0 && lastException != null) {
        throw lastException;
    }
    v.setClasses(allClasses);
    v.setTeachers(getAllTeachers());
    v.setWebsite(data.optString("website", baseUrl + "/default.htm"));
    return v;
}
Also used : Element(org.jsoup.nodes.Element) SubstitutionSchedule(me.vertretungsplan.objects.SubstitutionSchedule) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) JSONException(org.json.JSONException) CredentialInvalidException(me.vertretungsplan.exception.CredentialInvalidException)

Example 9 with SubstitutionSchedule

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

the class UntisMonitorParser method getSubstitutionSchedule.

public SubstitutionSchedule getSubstitutionSchedule() throws IOException, JSONException, CredentialInvalidException {
    loginResponse = new LoginHandler(scheduleData, credential, cookieProvider).handleLoginWithResponse(executor, cookieStore);
    SubstitutionSchedule v = SubstitutionSchedule.fromData(scheduleData);
    JSONArray urls = scheduleData.getData().getJSONArray(PARAM_URLS);
    String encoding = scheduleData.getData().optString(PARAM_ENCODING, null);
    List<Document> docs = new ArrayList<>();
    for (int i = 0; i < urls.length(); i++) {
        JSONObject url = urls.getJSONObject(i);
        final String urlStr = url.getString(SUBPARAM_URL);
        for (String dateUrl : ParserUtils.handleUrlWithDateFormat(urlStr)) {
            loadUrl(dateUrl, encoding, url.getBoolean(SUBPARAM_FOLLOWING), docs);
        }
    }
    for (Document doc : docs) {
        if (scheduleData.getData().has(PARAM_EMBEDDED_CONTENT_SELECTOR)) {
            for (Element part : doc.select(scheduleData.getData().getString(PARAM_EMBEDDED_CONTENT_SELECTOR))) {
                SubstitutionScheduleDay day = parseMonitorDay(part, scheduleData.getData());
                v.addDay(day);
            }
        } else if (doc.title().contains("Untis") || doc.html().contains("<!--<title>Untis")) {
            SubstitutionScheduleDay day = parseMonitorDay(doc, scheduleData.getData());
            v.addDay(day);
        }
        if (scheduleData.getData().has(PARAM_LAST_CHANGE_SELECTOR) && doc.select(scheduleData.getData().getString(PARAM_LAST_CHANGE_SELECTOR)).size() > 0) {
            String text = doc.select(scheduleData.getData().getString(PARAM_LAST_CHANGE_SELECTOR)).first().text();
            String lastChange;
            Pattern pattern = Pattern.compile("\\d\\d\\.\\d\\d\\.\\d\\d\\d\\d,? \\d\\d:\\d\\d");
            Matcher matcher = pattern.matcher(text);
            if (matcher.find()) {
                lastChange = matcher.group();
            } else {
                lastChange = text;
            }
            v.setLastChangeString(lastChange);
            v.setLastChange(ParserUtils.parseDateTime(lastChange));
        }
    }
    if (scheduleData.getData().has(PARAM_WEBSITE)) {
        v.setWebsite(scheduleData.getData().getString(PARAM_WEBSITE));
    } else if (urls.length() == 1) {
        v.setWebsite(urls.getJSONObject(0).getString("url"));
    }
    v.setClasses(getAllClasses());
    v.setTeachers(getAllTeachers());
    return v;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Element(org.jsoup.nodes.Element) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) Document(org.jsoup.nodes.Document) JSONObject(org.json.JSONObject) SubstitutionSchedule(me.vertretungsplan.objects.SubstitutionSchedule) SubstitutionScheduleDay(me.vertretungsplan.objects.SubstitutionScheduleDay)

Example 10 with SubstitutionSchedule

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

the class IphisDemoTest method demoTest.

@Test
public void demoTest() throws IOException, JSONException {
    SubstitutionSchedule schedule = new SubstitutionSchedule();
    parser.parseIphis(schedule, changes, courses, teachers, messages);
    assertEquals(2, schedule.getDays().size());
    SubstitutionScheduleDay firstDay = schedule.getDays().get(0);
    assertEquals(new LocalDate(2017, 9, 29), firstDay.getDate());
    assertEquals(4, firstDay.getSubstitutions().size());
}
Also used : SubstitutionSchedule(me.vertretungsplan.objects.SubstitutionSchedule) SubstitutionScheduleDay(me.vertretungsplan.objects.SubstitutionScheduleDay) LocalDate(org.joda.time.LocalDate) Test(org.junit.Test)

Aggregations

SubstitutionSchedule (me.vertretungsplan.objects.SubstitutionSchedule)31 Document (org.jsoup.nodes.Document)15 SubstitutionScheduleDay (me.vertretungsplan.objects.SubstitutionScheduleDay)14 LocalDate (org.joda.time.LocalDate)14 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)9 LocalDateTime (org.joda.time.LocalDateTime)9 Substitution (me.vertretungsplan.objects.Substitution)8 IOException (java.io.IOException)6 JSONArray (org.json.JSONArray)6 JSONObject (org.json.JSONObject)6 Element (org.jsoup.nodes.Element)6 HttpResponseException (org.apache.http.client.HttpResponseException)4 NameValuePair (org.apache.http.NameValuePair)3 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)3 NotNull (org.jetbrains.annotations.NotNull)3 Elements (org.jsoup.select.Elements)3 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 CredentialInvalidException (me.vertretungsplan.exception.CredentialInvalidException)2