From 12f06c45ff54ea7308cdd409e984ef0bff85a30d Mon Sep 17 00:00:00 2001 From: Thastertyn Date: Wed, 12 Apr 2023 19:03:28 +0200 Subject: [PATCH] Mensi upravy, novy typ --- .../thastertyn/Display/Content/Znamky.java | 3 +- .../Display/Dialogs/OptionsDialog.java | 25 ++++++++------- .../thastertyn/Display/LoginController.java | 7 +++- .../xyz/thastertyn/Scrape/Downloader.java | 3 +- .../java/xyz/thastertyn/Scrape/Login.java | 17 +++------- .../java/xyz/thastertyn/Scrape/Znamky.java | 15 +++++---- .../java/xyz/thastertyn/Tuples/Triplet.java | 2 +- .../java/xyz/thastertyn/Types/Option.java | 27 ++++++++++++++++ .../java/xyz/thastertyn/Types/Options.java | 32 +++++++++++++++++++ 9 files changed, 95 insertions(+), 36 deletions(-) create mode 100644 src/main/java/xyz/thastertyn/Types/Option.java create mode 100644 src/main/java/xyz/thastertyn/Types/Options.java diff --git a/src/main/java/xyz/thastertyn/Display/Content/Znamky.java b/src/main/java/xyz/thastertyn/Display/Content/Znamky.java index 98faf63..82420b1 100644 --- a/src/main/java/xyz/thastertyn/Display/Content/Znamky.java +++ b/src/main/java/xyz/thastertyn/Display/Content/Znamky.java @@ -11,7 +11,6 @@ import com.googlecode.lanterna.gui2.Label; import com.googlecode.lanterna.gui2.LinearLayout; import com.googlecode.lanterna.gui2.Panel; import com.googlecode.lanterna.gui2.WindowBasedTextGUI; -import com.googlecode.lanterna.gui2.dialogs.MessageDialog; import xyz.thastertyn.Display.Dialogs.OptionsDialog; import xyz.thastertyn.Tuples.Pair; @@ -162,7 +161,7 @@ public class Znamky extends JecnaContent { @Override public void showOptions(WindowBasedTextGUI textGUI) { - OptionsDialog d = new OptionsDialog(znamky.getOptions()); + OptionsDialog d = new OptionsDialog(znamky.getOptions().getValue0(), znamky.getOptions().getValue1()); d.showDialog(textGUI); } diff --git a/src/main/java/xyz/thastertyn/Display/Dialogs/OptionsDialog.java b/src/main/java/xyz/thastertyn/Display/Dialogs/OptionsDialog.java index e7db0ad..62a09b5 100644 --- a/src/main/java/xyz/thastertyn/Display/Dialogs/OptionsDialog.java +++ b/src/main/java/xyz/thastertyn/Display/Dialogs/OptionsDialog.java @@ -1,6 +1,5 @@ package xyz.thastertyn.Display.Dialogs; -import java.util.ArrayList; import java.util.Arrays; import com.googlecode.lanterna.gui2.Button; @@ -13,16 +12,11 @@ import com.googlecode.lanterna.gui2.Window; import com.googlecode.lanterna.gui2.WindowBasedTextGUI; import com.googlecode.lanterna.gui2.dialogs.DialogWindow; -import xyz.thastertyn.Tuples.Pair; +import xyz.thastertyn.Types.Options; public class OptionsDialog extends DialogWindow { - - - private int option0 = 0; - private int option1 = 0; - - public OptionsDialog(Pair, ArrayList> options) + public OptionsDialog(Options... options) { super("Choose from below"); @@ -31,12 +25,19 @@ public class OptionsDialog extends DialogWindow { .setLeftMarginSize(1) .setRightMarginSize(1)); - new Panel().setLayoutManager(new GridLayout(3)) - .addComponent(new ComboBox<>(options.getValue0(), options.getValue0().size() - 1)) - .addComponent(new EmptySpace()) - .addComponent(new ComboBox<>(options.getValue1(), options.getValue1().size() - 1)) + Panel optionPanel = new Panel().setLayoutManager(new GridLayout(3)) .addTo(mainPanel); + for(int i = 0; i < options.length; i++) + { + optionPanel.addComponent(new ComboBox<>(options[i].getOptions())); + + if(i != options.length - 1) + { + optionPanel.addComponent(new EmptySpace()); + } + } + new Panel() .setLayoutManager( new GridLayout(2).setHorizontalSpacing(1)) diff --git a/src/main/java/xyz/thastertyn/Display/LoginController.java b/src/main/java/xyz/thastertyn/Display/LoginController.java index ab16ee2..375f972 100644 --- a/src/main/java/xyz/thastertyn/Display/LoginController.java +++ b/src/main/java/xyz/thastertyn/Display/LoginController.java @@ -7,6 +7,7 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.net.UnknownHostException; +import java.util.concurrent.TimeoutException; import javax.security.auth.login.CredentialException; @@ -43,6 +44,10 @@ public class LoginController { try { login.loginJecna(data.getValue0(), data.getValue1()); + }catch (TimeoutException e) + { + MessageDialog.showMessageDialog(textGUI, "Timeout", "The attempt to connect took too long.", MessageDialogButton.Retry, + MessageDialogButton.Abort); } catch (UnknownHostException e) { MessageDialog.showMessageDialog(textGUI, "No Internet connection", "There seems to be no internet connection, reverting to cached data", @@ -178,7 +183,7 @@ public class LoginController { try { login.loginJecna(user, pass); - } catch (CredentialException | IOException e) { + } catch (CredentialException | IOException | TimeoutException e) { e.printStackTrace(); return false; } diff --git a/src/main/java/xyz/thastertyn/Scrape/Downloader.java b/src/main/java/xyz/thastertyn/Scrape/Downloader.java index 449ac84..ed3316c 100644 --- a/src/main/java/xyz/thastertyn/Scrape/Downloader.java +++ b/src/main/java/xyz/thastertyn/Scrape/Downloader.java @@ -12,7 +12,8 @@ public class Downloader { Connection c = Jsoup.connect(url) .header("Connection", "keep-alive") .cookie("role", "student") - .cookie("JSESSIONID", JsessionId); + .cookie("JSESSIONID", JsessionId) + .timeout(10000); return c; } diff --git a/src/main/java/xyz/thastertyn/Scrape/Login.java b/src/main/java/xyz/thastertyn/Scrape/Login.java index e5e7a54..f3b5b91 100644 --- a/src/main/java/xyz/thastertyn/Scrape/Login.java +++ b/src/main/java/xyz/thastertyn/Scrape/Login.java @@ -2,6 +2,7 @@ package xyz.thastertyn.Scrape; import java.io.IOException; import java.net.UnknownHostException; +import java.util.concurrent.TimeoutException; import javax.security.auth.login.CredentialException; @@ -18,7 +19,7 @@ public class Login { private long start; private long lastCheck; - public void loginJecna(String user, String pass) throws UnknownHostException, IOException, CredentialException + public void loginJecna(String user, String pass) throws UnknownHostException, IOException, CredentialException, TimeoutException { //#region JSESSIONID Connection.Response response = Jsoup.connect("https://www.spsejecna.cz") @@ -31,22 +32,17 @@ public class Login { //#endregion //#region Token3 - String token3 = Jsoup.connect("https://www.spsejecna.cz/user/role?role=student") - .header("Connection", "keep-alive") - .cookie("JSESSIONID", Jsessionid) + String token3 = Downloader.download("https://www.spsejecna.cz/user/role?role=student") .get() .select("input[name=token3]") .attr("value"); //#endregion //#region Login - Jsoup.connect("https://www.spsejecna.cz/user/login") + Downloader.download("https://www.spsejecna.cz/user/login") .method(Connection.Method.POST) .header("Content-Type", "application/x-www-form-urlencoded") .header("Origin", "https://www.spsejecna.cz") - .header("Connection", "keep-alive") - .cookie("JSESSIONID", Jsessionid) - .cookie("role", "student") .data("token3", token3) .data("user", user) .data("pass", pass) @@ -55,10 +51,7 @@ public class Login { .execute(); //#endregion - Document test = Jsoup.connect("https://www.spsejecna.cz/score/student") - .header("Connection", "keep-alive") - .cookie("JSESSIONID", Jsessionid) - .cookie("role", "student") + Document test = Downloader.download("https://www.spsejecna.cz/score/student") .get(); if(test.toString().contains("Pro pokračování se přihlaste do systému")) diff --git a/src/main/java/xyz/thastertyn/Scrape/Znamky.java b/src/main/java/xyz/thastertyn/Scrape/Znamky.java index 6c454e7..20b3ba5 100644 --- a/src/main/java/xyz/thastertyn/Scrape/Znamky.java +++ b/src/main/java/xyz/thastertyn/Scrape/Znamky.java @@ -11,18 +11,19 @@ import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import xyz.thastertyn.Tuples.Pair; -import xyz.thastertyn.Tuples.Triplet; +import xyz.thastertyn.Types.Option; +import xyz.thastertyn.Types.Options; public class Znamky { private boolean wasDownloaded; // schoolYear, schoolYearId - private ArrayList>, Double>> test = new ArrayList<>(); private ArrayList>,Double>>> grades = new ArrayList<>(); // Integer znaci id roku, boolean jestli je jen prvni nebo i druhe pololeti - private Pair>, ArrayList>> options = new Pair<>(new ArrayList>(), new ArrayList>()); + private Options schoolYearOptions = new Options("Skolni R."); + private Options schoolHalfYearOptions = new Options("Pololeti"); public void downloadZnamky(/*, int schoolYearId, int schoolYearHalfId*/) throws UnknownHostException, IOException { @@ -85,12 +86,12 @@ public class Znamky { for(Element e : schoolYear) { - options.getValue0().add(e.attr("value")); + this.schoolYearOptions.addOption(new Option(e.text(), e.attr("value"))); } for(Element e : schoolYearId) { - options.getValue1().add(new Pair<>(e.attr("value", e.text())); + this.schoolHalfYearOptions.addOption(new Option(e.text(), e.attr("value"))); } wasDownloaded = true; @@ -141,9 +142,9 @@ public class Znamky { return grades; } - public Pair, ArrayList> getOptions() + public Pair getOptions() { - return options; + return new Pair(schoolYearOptions, schoolHalfYearOptions); } @Override diff --git a/src/main/java/xyz/thastertyn/Tuples/Triplet.java b/src/main/java/xyz/thastertyn/Tuples/Triplet.java index 31e10aa..7814728 100644 --- a/src/main/java/xyz/thastertyn/Tuples/Triplet.java +++ b/src/main/java/xyz/thastertyn/Tuples/Triplet.java @@ -61,6 +61,6 @@ public class Triplet { @Override public String toString() { - return "[" + value0 + ", " + value1 + "]"; + return "[" + value0 + ", " + value1 + "," + value2 + "]"; } } diff --git a/src/main/java/xyz/thastertyn/Types/Option.java b/src/main/java/xyz/thastertyn/Types/Option.java new file mode 100644 index 0000000..3b369d4 --- /dev/null +++ b/src/main/java/xyz/thastertyn/Types/Option.java @@ -0,0 +1,27 @@ +package xyz.thastertyn.Types; + +public class Option { + + private String text; + private String value; + public Option(String text, String value) { + this.text = text; + this.value = value; + } + public String getText() { + return text; + } + public void setText(String text) { + this.text = text; + } + public String getValue() { + return value; + } + public void setValue(String value) { + this.value = value; + } + @Override + public String toString() { + return text; + } +} diff --git a/src/main/java/xyz/thastertyn/Types/Options.java b/src/main/java/xyz/thastertyn/Types/Options.java new file mode 100644 index 0000000..daaa765 --- /dev/null +++ b/src/main/java/xyz/thastertyn/Types/Options.java @@ -0,0 +1,32 @@ +package xyz.thastertyn.Types; + +import java.util.ArrayList; + +public class Options { + + private ArrayList