Search in sources :

Example 1 with SubstitutionSchedule

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

the class IndiwareParser method getSubstitutionSchedule.

@Override
public SubstitutionSchedule getSubstitutionSchedule() throws IOException, JSONException, CredentialInvalidException {
    new LoginHandler(scheduleData, credential, cookieProvider).handleLogin(executor, cookieStore);
    JSONArray urls = data.getJSONArray(PARAM_URLS);
    String encoding = data.optString(PARAM_ENCODING, null);
    List<String> docs = new ArrayList<>();
    SubstitutionSchedule v = SubstitutionSchedule.fromData(scheduleData);
    int successfulSchedules = 0;
    IOException lastException = null;
    for (int i = 0; i < urls.length(); i++) {
        if (urls.optJSONObject(i) != null) {
            try {
                JSONObject obj = urls.getJSONObject(i);
                String url = obj.getString("url");
                if (obj.has("postData")) {
                    JSONObject postParams = obj.getJSONObject("postData");
                    List<NameValuePair> nvps = new ArrayList<>();
                    for (String name : JSONObject.getNames(postParams)) {
                        String value = postParams.getString(name);
                        nvps.add(new BasicNameValuePair(name, value));
                    }
                    docs.add(httpPost(url, encoding, nvps));
                    successfulSchedules++;
                }
            } catch (IOException e) {
                lastException = e;
            }
        } else {
            for (String url : ParserUtils.handleUrlWithDateFormat(urls.getString(i))) {
                try {
                    docs.add(httpGet(url, encoding));
                    successfulSchedules++;
                } catch (IOException e) {
                    lastException = e;
                }
            }
        }
    }
    if (successfulSchedules == 0 && lastException != null) {
        throw lastException;
    }
    successfulSchedules = 0;
    lastException = null;
    for (String response : docs) {
        try {
            parseIndiwarePage(v, response);
            successfulSchedules++;
        } catch (IOException e) {
            lastException = e;
        }
    }
    if (successfulSchedules == 0 && lastException != null) {
        throw lastException;
    }
    v.setWebsite(urls.getString(0));
    v.setClasses(getAllClasses());
    v.setTeachers(getAllTeachers());
    return v;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) JSONObject(org.json.JSONObject) SubstitutionSchedule(me.vertretungsplan.objects.SubstitutionSchedule) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) JSONArray(org.json.JSONArray) IOException(java.io.IOException)

Example 2 with SubstitutionSchedule

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

the class SVPlanParser method getSubstitutionSchedule.

public SubstitutionSchedule getSubstitutionSchedule() throws IOException, JSONException, CredentialInvalidException {
    // 
    new LoginHandler(scheduleData, credential, cookieProvider).handleLogin(executor, cookieStore);
    JSONArray urls = data.getJSONArray(PARAM_URLS);
    String encoding = data.optString(PARAM_ENCODING, null);
    List<Document> docs = new ArrayList<>();
    int successfulSchedules = 0;
    IOException lastException = null;
    for (int i = 0; i < urls.length(); i++) {
        String url;
        if (urls.get(i) instanceof JSONObject) {
            // backwards compatibility
            final JSONObject obj = urls.getJSONObject(i);
            url = obj.getString("url");
            if (obj.has("postData")) {
                JSONObject postParams = obj.getJSONObject("postData");
                List<NameValuePair> nvps = new ArrayList<>();
                for (String name : JSONObject.getNames(postParams)) {
                    String value = postParams.getString(name);
                    nvps.add(new BasicNameValuePair(name, value));
                }
                try {
                    docs.add(Jsoup.parse(httpPost(url, encoding, nvps).replace("&nbsp;", "")));
                    successfulSchedules++;
                } catch (IOException e) {
                    lastException = e;
                }
            } else {
                try {
                    docs.add(Jsoup.parse(httpGet(url, encoding).replace("&nbsp;", "")));
                    successfulSchedules++;
                } catch (IOException e) {
                    lastException = e;
                }
            }
        } else {
            url = urls.getString(i);
            try {
                docs.add(Jsoup.parse(httpGet(url, encoding).replace("&nbsp;", "")));
                successfulSchedules++;
            } catch (IOException e) {
                lastException = e;
            }
        }
    }
    if (successfulSchedules == 0 && lastException != null) {
        throw lastException;
    }
    SubstitutionSchedule v = parseSVPlanSchedule(docs);
    return v;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) JSONObject(org.json.JSONObject) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) SubstitutionSchedule(me.vertretungsplan.objects.SubstitutionSchedule)

Example 3 with SubstitutionSchedule

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

the class SVPlanParser method parseSVPlanSchedule.

@NotNull
SubstitutionSchedule parseSVPlanSchedule(List<Document> docs) throws IOException, JSONException {
    SubstitutionSchedule v = SubstitutionSchedule.fromData(scheduleData);
    for (Document doc : docs) {
        if (doc.select(".svp").size() > 0) {
            for (Element svp : doc.select(".svp")) {
                parseSvPlanDay(v, svp, doc);
            }
        } else if (doc.select(".Trennlinie").size() > 0) {
            Element div = new Element(Tag.valueOf("div"), "");
            for (Node node : doc.body().childNodesCopy()) {
                if (node instanceof Element && ((Element) node).hasClass("Trennlinie") && div.select("table").size() > 0) {
                    parseSvPlanDay(v, div, doc);
                    div = new Element(Tag.valueOf("div"), "");
                } else {
                    div.appendChild(node);
                }
            }
            parseSvPlanDay(v, div, doc);
        } else {
            parseSvPlanDay(v, doc, doc);
        }
    }
    v.setClasses(getAllClasses());
    v.setTeachers(getAllTeachers());
    return v;
}
Also used : SubstitutionSchedule(me.vertretungsplan.objects.SubstitutionSchedule) Element(org.jsoup.nodes.Element) TextNode(org.jsoup.nodes.TextNode) Node(org.jsoup.nodes.Node) Document(org.jsoup.nodes.Document) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with SubstitutionSchedule

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

the class ESchoolParser method getSubstitutionSchedule.

@Override
public SubstitutionSchedule getSubstitutionSchedule() throws IOException, JSONException, CredentialInvalidException {
    if (!(scheduleData.getAuthenticationData() instanceof NoAuthenticationData) && (credential == null || !(credential instanceof PasswordCredential) || ((PasswordCredential) credential).getPassword() == null || ((PasswordCredential) credential).getPassword().isEmpty())) {
        throw new IOException("no login");
    }
    List<NameValuePair> nvps = new ArrayList<>();
    nvps.add(new BasicNameValuePair("wp", scheduleData.getData().getString(PARAM_ID)));
    nvps.add(new BasicNameValuePair("go", "vplan"));
    nvps.add(new BasicNameValuePair("content", "x14"));
    nvps.add(new BasicNameValuePair("sortby", "S"));
    String url = BASE_URL + "?" + URLEncodedUtils.format(nvps, "UTF-8");
    Document doc = Jsoup.parse(httpGet(url, ENCODING));
    if (doc.select("form[name=loginform]").size() > 0 && scheduleData.getAuthenticationData() instanceof PasswordAuthenticationData) {
        // Login required
        List<NameValuePair> formParams = new ArrayList<>();
        formParams.add(new BasicNameValuePair("password", ((PasswordCredential) credential).getPassword()));
        formParams.add(new BasicNameValuePair("login", ""));
        doc = Jsoup.parse(httpPost(url, ENCODING, formParams));
        if (doc.select("font[color=red]").text().contains("fehlgeschlagen")) {
            throw new CredentialInvalidException();
        }
    }
    SubstitutionSchedule schedule = parseESchoolSchedule(doc);
    return schedule;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) NoAuthenticationData(me.vertretungsplan.objects.authentication.NoAuthenticationData) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) SubstitutionSchedule(me.vertretungsplan.objects.SubstitutionSchedule) PasswordCredential(me.vertretungsplan.objects.credential.PasswordCredential) ArrayList(java.util.ArrayList) PasswordAuthenticationData(me.vertretungsplan.objects.authentication.PasswordAuthenticationData) CredentialInvalidException(me.vertretungsplan.exception.CredentialInvalidException) IOException(java.io.IOException) Document(org.jsoup.nodes.Document)

Example 5 with SubstitutionSchedule

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

the class IndiwareMobileParser method getSubstitutionSchedule.

@Override
public SubstitutionSchedule getSubstitutionSchedule() throws IOException, JSONException, CredentialInvalidException {
    new LoginHandler(scheduleData, credential, cookieProvider).handleLogin(executor, cookieStore);
    String baseurl = data.getString(PARAM_BASEURL) + "/";
    List<Document> docs = new ArrayList<>();
    for (int i = 0; i < MAX_DAYS; i++) {
        LocalDate date = LocalDate.now().plusDays(i);
        String dateStr = DateTimeFormat.forPattern("yyyyMMdd").print(date);
        String filePrefix = scheduleData.getType() == SubstitutionSchedule.Type.TEACHER ? "PlanLe" : "PlanKl";
        String url = baseurl + "mobdaten/" + filePrefix + dateStr + "" + ".xml?_=" + System.currentTimeMillis();
        HttpResponseException lastException = null;
        try {
            String xml = httpGet(url, "UTF-8");
            Document doc = Jsoup.parse(xml, url, Parser.xmlParser());
            if (doc.select("kopf datei").text().equals(filePrefix + dateStr + ".xml")) {
                docs.add(doc);
            }
        } catch (HttpResponseException e) {
            lastException = e;
        }
        if (docs.size() == 0 && lastException != null) {
            throw lastException;
        }
    }
    SubstitutionSchedule v = SubstitutionSchedule.fromData(scheduleData);
    for (Document doc : docs) {
        v.addDay(parseDay(doc, colorProvider, scheduleData));
    }
    v.setClasses(getAllClasses());
    v.setTeachers(getAllTeachers());
    v.setWebsite(baseurl + "plankl.html");
    return v;
}
Also used : SubstitutionSchedule(me.vertretungsplan.objects.SubstitutionSchedule) ArrayList(java.util.ArrayList) HttpResponseException(org.apache.http.client.HttpResponseException) Document(org.jsoup.nodes.Document) LocalDate(org.joda.time.LocalDate)

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