Mensi upravy, novy typ

This commit is contained in:
Thastertyn 2023-04-12 19:03:28 +02:00
parent 7b7c00b866
commit 12f06c45ff
9 changed files with 95 additions and 36 deletions

View File

@ -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);
}

View File

@ -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<String>, ArrayList<String>> 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))

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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"))

View File

@ -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<Triplet<String, ArrayList<Pair<Integer, Double>>, Double>> test = new ArrayList<>();
private ArrayList<Pair<String, Pair<ArrayList<Pair<Integer, Double>>,Double>>> grades = new ArrayList<>();
// Integer znaci id roku, boolean jestli je jen prvni nebo i druhe pololeti
private Pair<ArrayList<Pair<String, String>>, ArrayList<Pair<String, String>>> options = new Pair<>(new ArrayList<Pair<String, String>>(), new ArrayList<Pair<String, String>>());
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<String>, ArrayList<String>> getOptions()
public Pair<Options, Options> getOptions()
{
return options;
return new Pair<Options, Options>(schoolYearOptions, schoolHalfYearOptions);
}
@Override

View File

@ -61,6 +61,6 @@ public class Triplet<T1, T2, T3> {
@Override
public String toString()
{
return "[" + value0 + ", " + value1 + "]";
return "[" + value0 + ", " + value1 + "," + value2 + "]";
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,32 @@
package xyz.thastertyn.Types;
import java.util.ArrayList;
public class Options {
private ArrayList<Option> options = new ArrayList<>();
private String title;
public Options(String title) {
this.title = title;
}
public void addOption(Option o)
{
options.add(o);
}
public ArrayList<Option> getOptions() {
return options;
}
public void setOptions(ArrayList<Option> options) {
this.options = options;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}