More refactor

This commit is contained in:
Claudio Maggioni 2018-01-22 20:08:32 +01:00
parent 52571565e5
commit 24129b1f11
4 changed files with 126 additions and 89 deletions

View file

@ -18,6 +18,9 @@
package xyz.maggioni.ProvaArgo; package xyz.maggioni.ProvaArgo;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Connection.*; import org.jsoup.Connection.*;
import org.jsoup.Connection; import org.jsoup.Connection;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
@ -47,37 +50,7 @@ class ArgoAPI {
} }
} }
enum ArgoDocument { private static boolean login(Map<String, String> cookies, User user) {
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<String, String> 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<String, String> cookies, User user) {
try { try {
Response initRes = Jsoup.connect("https://www.portaleargo.it/argoweb/famiglia/?cod_utente=" + user.getSchoolCode()) Response initRes = Jsoup.connect("https://www.portaleargo.it/argoweb/famiglia/?cod_utente=" + user.getSchoolCode())
.followRedirects(true) .followRedirects(true)
@ -118,20 +91,91 @@ class ArgoAPI {
} }
} }
static void logout(Map<String, String> cookies) { enum ArgoDocument {
MARKS("[evt=menu-servizialunno:vot|event|custom]"),
HOMEWORK("[evt=menu-serviziclasse:_idJsp25|event|submit]"),
LESSONS("[evt=menu-serviziclasse:_idJsp27|event|submit]"),
LOGOUT("[evt=_idJsp66|event|custom|eventSrc|Uscire%20dal%20programma%3F|param1||param2||param3||param4|]");
private String backbaseDelta;
ArgoDocument(String s) {
backbaseDelta = s;
}
}
interface DocumentParser {
JSONObject parseDocument(Document document) throws Exception;
}
private static Document fetchHTML(ArgoDocument document, Map<String, String> cookies) throws IOException {
try { try {
Response loginRes = Jsoup.connect("https://www.portaleargo.it/argoweb/famiglia/index.jsf") Response markRes = Jsoup.connect("https://www.portaleargo.it/argoweb/famiglia/index.jsf")
.cookies(cookies) .cookies(cookies)
.referrer("https://www.portaleargo.it/argoweb/famiglia/index.jsf") .referrer("https://www.portaleargo.it/argoweb/famiglia/index.jsf")
.data("BackbaseClientDelta", "[evt=_idJsp66|event|custom|eventSrc|Uscire%20dal%20programma%3F|param1||param2||param3||param4|]") .data("BackbaseClientDelta", document.backbaseDelta)
.followRedirects(true) .followRedirects(true)
.method(Connection.Method.POST) .method(Connection.Method.POST)
.execute(); .execute();
updateCookies(cookies, loginRes.cookies()); updateCookies(cookies, markRes.cookies());
logResponse(loginRes); logResponse(markRes);
return Jsoup.parse(markRes.body().replaceAll("Â", ""));
} catch (IOException e) { } catch (IOException e) {
PluginLOG.e("ArgoAPI", e.getLocalizedMessage(), e); PluginLOG.e("ArgoAPI", e.getLocalizedMessage(), e);
throw e;
}
}
public static JSONObject fetchDocument(JSONArray args, ArgoDocument document, DocumentParser parser)
throws JSONException, ArgoException {
JSONObject response;
try {
JSONObject jsonObject = args.getJSONObject(0);
// parse user credentials
User user = new User(jsonObject.getString("username"),
jsonObject.getString("password"),
jsonObject.getString("schoolCode"));
Map<String, String> cookies = new HashMap<>();
// login
if (!ArgoAPI.login(cookies, user)) {
throw new IllegalStateException("Login on Argo failed");
}
// fetch the HTML page
Document html = fetchHTML(document, cookies);
// parse the HTML into JSON
response = parser.parseDocument(html);
// log out
fetchHTML(ArgoDocument.LOGOUT, cookies);
} catch (Exception e) {
PluginLOG.e("ArgoAPI", e.getMessage(), e);
try {
response = new JSONObject();
response.put("success", false);
response.put("subjects", new JSONArray());
response.put("message", e.getLocalizedMessage());
} catch (JSONException e1) {
PluginLOG.e("ArgoAPI", e1.getMessage(), e1);
throw e1;
}
}
try {
if (response.get("success").equals(true)) {
return response;
} else {
throw new ArgoException(response);
}
} catch (JSONException | ArgoException e) {
PluginLOG.e("ArgoAPI", e.getMessage(), e);
throw e;
} }
} }
} }

View file

@ -23,12 +23,9 @@ import org.apache.cordova.*;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -74,33 +71,15 @@ public class ArgoAPIPlugin extends CordovaPlugin {
return false; return false;
} }
private JSONObject getMarks(JSONArray args) throws JSONException, ArgoException { private JSONObject getMarks(final JSONArray args) throws JSONException, ArgoException {
return ArgoAPI.fetchDocument(args, ArgoAPI.ArgoDocument.MARKS, document -> {
JSONObject response = new JSONObject(); JSONObject toReturn = new JSONObject();
JSONArray subjects = new JSONArray(); JSONArray subjects = new JSONArray();
toReturn.put("success", true);
toReturn.put("subjects", subjects);
toReturn.put("message", "Success");
try { Element e = document.getElementById("sheet-sezioneDidargo:panel-votiGiornalieri:pannello");
response.put("success", true);
response.put("subjects", subjects);
JSONObject jsonObject = args.getJSONObject(0);
User user = new User(jsonObject.getString("username"),
jsonObject.getString("password"),
jsonObject.getString("schoolCode"));
Map<String, String> cookies = new HashMap<>();
if (!ArgoAPI.login(cookies, user)) {
throw new IllegalStateException("Login on Argo failed");
}
Document marksDocument = ArgoAPI.getDocument(cookies, ArgoAPI.ArgoDocument.MARKS);
if (marksDocument == null) {
throw new IllegalStateException("Failed to get marks document");
}
Element e = marksDocument.getElementById("sheet-sezioneDidargo:panel-votiGiornalieri:pannello");
for (Element el : e.select("fieldset")) { for (Element el : e.select("fieldset")) {
@ -136,30 +115,8 @@ public class ArgoAPIPlugin extends CordovaPlugin {
subjects.put(subject); subjects.put(subject);
} }
ArgoAPI.logout(cookies); return toReturn;
});
} catch (Exception e) {
PluginLOG.e("ArgoAPIPlugin", e.getMessage(), e);
try {
response.put("success", false);
response.put("subjects", new JSONArray());
response.put("message", e.getLocalizedMessage());
} catch (JSONException e1) {
PluginLOG.e("ArgoAPIPlugin", e1.getMessage(), e1);
throw e1;
}
}
try {
if (response.get("success").equals(true)) {
return response;
} else {
throw new ArgoException(response);
}
} catch (JSONException | ArgoException e) {
PluginLOG.e("ArgoAPIPlugin", e.getMessage(), e);
throw e;
}
} }
} }

View file

@ -1,3 +1,21 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package xyz.maggioni.ProvaArgo; package xyz.maggioni.ProvaArgo;
import org.json.JSONObject; import org.json.JSONObject;

View file

@ -1,3 +1,21 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package xyz.maggioni.ProvaArgo; package xyz.maggioni.ProvaArgo;
import org.apache.cordova.LOG; import org.apache.cordova.LOG;