142 lines
4.9 KiB
Java
142 lines
4.9 KiB
Java
/*
|
|
* 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;
|
|
|
|
import org.apache.cordova.CordovaPlugin;
|
|
import org.apache.cordova.CallbackContext;
|
|
import org.apache.cordova.LOG;
|
|
|
|
import org.json.JSONArray;
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
import org.jsoup.nodes.Document;
|
|
import org.jsoup.nodes.Element;
|
|
import org.jsoup.select.Elements;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
public class ArgoAPIPlugin extends CordovaPlugin {
|
|
|
|
@Override
|
|
public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
|
if (action.equals("getMarks")) {
|
|
|
|
final ArgoAPIPlugin this__ = this;
|
|
|
|
new Thread(new Runnable() {
|
|
public void run() {
|
|
this__.getMarks(args, callbackContext);
|
|
}
|
|
}).start();
|
|
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private void getMarks(JSONArray args, CallbackContext c) {
|
|
|
|
JSONObject response = new JSONObject();
|
|
JSONArray subjects = new JSONArray();
|
|
|
|
try {
|
|
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<String, String>();
|
|
|
|
if (!ArgoAPI.login(cookies, user)) {
|
|
throw new IllegalStateException("Login on Argo failed");
|
|
}
|
|
|
|
Document marksDocument = ArgoAPI.getMarksDocument(cookies);
|
|
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")) {
|
|
|
|
JSONObject subject = new JSONObject();
|
|
JSONArray marks = new JSONArray();
|
|
subject.put("name", el.select("legend").first().text());
|
|
subject.put("marks", marks);
|
|
|
|
for (Element el2 : el.select("table > tbody > tr")) {
|
|
Elements cells = el2.select("td");
|
|
Element[] cellsArr = cells.toArray(new Element[3]);
|
|
|
|
JSONObject mark = new JSONObject();
|
|
mark.put("date", new SimpleDateFormat("yyyy-MM-dd").parse(cellsArr[1].text()));
|
|
|
|
String markStr = cellsArr[2].text();
|
|
|
|
Pattern regMark = Pattern.compile("Voto ((?:[Ss]critto)|(?:[Oo]rale)|(?:[Pp]ratico)) .*\\(([0-9.]+)\\)");
|
|
Matcher m = regMark.matcher(markStr);
|
|
|
|
if (m.find()) {
|
|
mark.put("type", m.group(1));
|
|
mark.put("value", Double.parseDouble(m.group(2)));
|
|
} else {
|
|
throw new IllegalStateException("Regex for mark does not match with markStr \"" + markStr + "\"");
|
|
}
|
|
|
|
marks.put(mark);
|
|
}
|
|
|
|
subjects.put(subject);
|
|
}
|
|
|
|
ArgoAPI.logout(cookies);
|
|
|
|
} catch (Exception e) {
|
|
LOG.e("ArgoAPIPlugin", e.getMessage(), e);
|
|
try {
|
|
response.put("success", false);
|
|
response.put("subjects", new JSONArray());
|
|
response.put("message", e.getLocalizedMessage());
|
|
} catch (JSONException e1) {
|
|
LOG.e("ArgoAPIPlugin", e1.getMessage(), e1);
|
|
c.error(e1.getMessage());
|
|
}
|
|
}
|
|
|
|
try {
|
|
if (response.get("success").equals(true)) {
|
|
c.success(response);
|
|
} else {
|
|
c.error(response);
|
|
}
|
|
} catch (JSONException e) {
|
|
LOG.e("ArgoAPIPlugin", e.getMessage(), e);
|
|
c.error(e.getMessage());
|
|
}
|
|
}
|
|
} |