/* * cordova-plugin-argo-api - Cordova plugin for reading marks from the eDiary Argo Scuolanext * Copyright (C) 2017 Claudio Maggioni * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package xyz.maggioni.ProvaArgo; import org.jsoup.Connection.*; import org.jsoup.Connection; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import java.io.IOException; import java.util.HashMap; import java.util.Map; class ArgoAPI { private ArgoAPI() { } private static void logResponse(Response res) { PluginLOG.i("ArgoAPI", "Response URL: " + res.url()); PluginLOG.i("ArgoAPI", "Response code: " + res.statusCode()); PluginLOG.i("ArgoAPI", "Response status: " + res.statusMessage()); for (Map.Entry entry : res.cookies().entrySet()) { PluginLOG.i("ArgoAPI", "Response cookie: " + entry.getKey() + "=" + entry.getValue()); } } private static void updateCookies(Map old, Map returned) { for (Map.Entry entry : returned.entrySet()) { old.put(entry.getKey(), entry.getValue()); } } enum ArgoDocument { MARKS("[evt=menu-servizialunno:vot|event|custom]"), HOMEWORK("[evt=menu-serviziclasse:_idJsp25|event|submit]"), LESSONS("[evt=menu-serviziclasse:_idJsp27|event|submit]"); private String backbaseDelta; ArgoDocument(String s) { backbaseDelta = s; } } static Document getDocument(Map cookies, ArgoDocument d) { try { Response markRes = Jsoup.connect("https://www.portaleargo.it/argoweb/famiglia/index.jsf") .cookies(cookies) .referrer("https://www.portaleargo.it/argoweb/famiglia/index.jsf") .data("BackbaseClientDelta", d.backbaseDelta) .followRedirects(true) .method(Connection.Method.POST) .execute(); updateCookies(cookies, markRes.cookies()); logResponse(markRes); return Jsoup.parse(markRes.body().replaceAll("Â", "")); } catch (IOException e) { PluginLOG.e("ArgoAPI", e.getLocalizedMessage(), e); return null; } } static boolean login(Map cookies, User user) { try { Response initRes = Jsoup.connect("https://www.portaleargo.it/argoweb/famiglia/?cod_utente=" + user.getSchoolCode()) .followRedirects(true) .method(Connection.Method.GET) .execute(); updateCookies(cookies, initRes.cookies()); logResponse(initRes); PluginLOG.i("ArgoAPI", "Cookie init complete"); Map params = new HashMap<>(); params.put("utente", user.getUsername()); params.put("j_password", user.getPassword()); params.put("j_username", user.getUsername() + "#" + user.getSchoolCode()); params.put("submit", "Entra"); Response loginRes = Jsoup.connect("https://www.portaleargo.it/argoweb/famiglia/common/j_security_check") .cookies(cookies) .referrer("https://www.portaleargo.it/argoweb/famiglia/common/login_form2.jsp") .data(params) .method(Connection.Method.POST) .followRedirects(true) .execute(); updateCookies(cookies, loginRes.cookies()); logResponse(loginRes); PluginLOG.i("ArgoAPI", "login complete"); Document d = loginRes.parse(); return !d.select("html > head > title").isEmpty() && "Argo - Famiglia".equalsIgnoreCase(d.select("html > head > title").first().text()); } catch (IOException e) { PluginLOG.e("ArgoAPI", e.getLocalizedMessage(), e); return false; } } static void logout(Map cookies) { try { Response loginRes = Jsoup.connect("https://www.portaleargo.it/argoweb/famiglia/index.jsf") .cookies(cookies) .referrer("https://www.portaleargo.it/argoweb/famiglia/index.jsf") .data("BackbaseClientDelta", "[evt=_idJsp66|event|custom|eventSrc|Uscire%20dal%20programma%3F|param1||param2||param3||param4|]") .followRedirects(true) .method(Connection.Method.POST) .execute(); updateCookies(cookies, loginRes.cookies()); logResponse(loginRes); } catch (IOException e) { PluginLOG.e("ArgoAPI", e.getLocalizedMessage(), e); } } }