Initial commit
This commit is contained in:
commit
a66d67676f
33 files changed
+793
No files matched your search
@@ -0,0 +1,31 @@
|
||||
package xyz.thastertyn.Jecna;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.jsoup.nodes.Document;
|
||||
|
||||
import xyz.thastertyn.Connection;
|
||||
import xyz.thastertyn.Tuple.Pair;
|
||||
|
||||
public class Dochazka {
|
||||
|
||||
private HashMap<String, Pair<String, String>> absence = new HashMap<>();
|
||||
|
||||
public byte downloadAbsence(String JSESSIOND)
|
||||
{
|
||||
Document absenceDoc;
|
||||
|
||||
try {
|
||||
// absence-list
|
||||
absenceDoc = Connection.getConnection()
|
||||
.cookie("JSESSIOND", JSESSIOND)
|
||||
.url("https://www.spsejecna.cz" + "/absence/passing-student")
|
||||
.get();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package xyz.thastertyn.Jecna;
|
||||
|
||||
import org.jsoup.Connection;
|
||||
import org.jsoup.Jsoup;
|
||||
|
||||
public class Downloader {
|
||||
|
||||
public static Connection download(String url, String Jsessionid)
|
||||
{
|
||||
return Jsoup.connect(url)
|
||||
.header("Connection", "keep-alive")
|
||||
.cookie("role", "student")
|
||||
.cookie("JSESSIONID", Jsessionid);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package xyz.thastertyn.Jecna;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.jsoup.Connection;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.Connection.Method;
|
||||
|
||||
public class Login {
|
||||
|
||||
private String Jsessionid = null;
|
||||
|
||||
// Check for session expiration
|
||||
private long start;
|
||||
private long lastCheck;
|
||||
|
||||
/**
|
||||
* Zkontroluje zda jiz neexistuji ulozene udaje na systemu
|
||||
* @return <ul>
|
||||
* <li> {@code true} soubor existuje a neni prazdny </li>
|
||||
* <li> {@code false} neexistuje, nebo jsou prazdne </li>
|
||||
* </ul>
|
||||
*/
|
||||
public boolean getCredentials()
|
||||
{
|
||||
File credentials = null;
|
||||
|
||||
if(System.getProperty("os.name").equals("Linux"))
|
||||
{
|
||||
credentials = new File("~/.local/share/jecnak/credentials.json");
|
||||
|
||||
}else if(System.getProperty("os.name").contains("Windows"))
|
||||
{
|
||||
credentials = new File(System.getenv("APPDATA\\jecnak\\"));
|
||||
}
|
||||
|
||||
if(!credentials.exists() || credentials.length() == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public String getJSESSIONID()
|
||||
{
|
||||
return Jsessionid;
|
||||
}
|
||||
|
||||
public byte login(String user, String pass)
|
||||
{
|
||||
try {
|
||||
//#region JSESSIONID
|
||||
Connection.Response response = Jsoup.connect("https://www.spsejecna.cz")
|
||||
.header("Connection", "keep-alive")
|
||||
.method(Method.HEAD)
|
||||
.execute();
|
||||
|
||||
Jsessionid = response.cookie("JSESSIONID");
|
||||
//#endregion
|
||||
|
||||
//#region Token3
|
||||
String token3 = Jsoup.connect("https://www.spsejecna.cz/user/role?role=student")
|
||||
.header("Connection", "keep-alive")
|
||||
.cookie("JSESSIONID", Jsessionid)
|
||||
.get()
|
||||
.select("input[name=token3]")
|
||||
.attr("value");
|
||||
//#endregion
|
||||
|
||||
//#region Login
|
||||
Connection.Response login = Jsoup.connect("https://www.spsejecna.cz/user/login")
|
||||
.method(Connection.Method.POST)
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
//.header("Content-Length", "71") Adds 10 seconds to total request time
|
||||
.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)
|
||||
.data("submit", "P%C5%99ihl%C3%A1sit+se")
|
||||
.followRedirects(true)
|
||||
.execute();
|
||||
//#endregion
|
||||
|
||||
start = System.currentTimeMillis() / 1000L;
|
||||
lastCheck = start;
|
||||
|
||||
return 0;
|
||||
} catch(UnknownHostException e)
|
||||
{
|
||||
// Not connected to internet
|
||||
return 127;
|
||||
} catch(IOException e)
|
||||
{
|
||||
return 126;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package xyz.thastertyn.Jecna;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.HashSet;
|
||||
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.select.Elements;
|
||||
|
||||
/**
|
||||
* Jeden radek v rozvrhu
|
||||
*/
|
||||
public class Rozvrh {
|
||||
|
||||
private String[][] rozvrh = new String[5][10];
|
||||
int pos = 0;
|
||||
|
||||
|
||||
public void setPredmet(String pr, int d, int i)
|
||||
{
|
||||
rozvrh[d][i] = pr;
|
||||
}
|
||||
|
||||
public String[][] getRozvrh()
|
||||
{
|
||||
return rozvrh;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String s = "";
|
||||
for(String[] st : rozvrh)
|
||||
{
|
||||
s += ("| ");
|
||||
for(String str : st)
|
||||
{
|
||||
s += String.format("%-5s", str) + " | ";
|
||||
}
|
||||
s += "\n";
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stahne rozvrh z www.spsejecna.cz a dale ho zpracuje do formy
|
||||
* se kterou da pracovat
|
||||
* @param Jsessionid ze stranek
|
||||
* @return Status code
|
||||
* <newline>
|
||||
* <ul>
|
||||
* <li> 0 - Operace probehla bez problemu</li>
|
||||
* </ul>
|
||||
* @throws UnknownHostException kdyz neni pripojeni k internetu
|
||||
* @throws IOException ostatni exceptiony nejsou dulezite, tak jsou zahrnuty v jednom
|
||||
*/
|
||||
public void downloadRozvrh(final String Jsessionid) throws UnknownHostException, IOException
|
||||
{
|
||||
Document rozvrhDoc = Downloader.download("https://www.spsejecna.cz" + "/timetable/class", Jsessionid).get();
|
||||
|
||||
Elements rozvrhTable = rozvrhDoc.select("table.timetable").select("tr");
|
||||
Elements[] rozvrhDny = new Elements[rozvrhTable.size()];
|
||||
|
||||
for(int i = 0; i < rozvrhTable.size(); i++)
|
||||
{
|
||||
rozvrhDny[i] = rozvrhTable.get(i).children();
|
||||
}
|
||||
|
||||
for(int i = 1; i < 6; i++)
|
||||
{
|
||||
for(int j = 1; j < 11; j++)
|
||||
{
|
||||
String predmet = rozvrhDny[i].get(j).select("span.subject").text().replaceAll(" ", "/");
|
||||
|
||||
// Predmety jako CEL jsou trikrat, staci ale jen jednou
|
||||
// V hashsetu se neopakuji prvky
|
||||
HashSet<String> set = new HashSet<>();
|
||||
String[] split = predmet.split("/");
|
||||
|
||||
for(String s : split)
|
||||
{
|
||||
set.add(s);
|
||||
}
|
||||
|
||||
String pr = "";
|
||||
|
||||
if(set.size() > 1)
|
||||
{
|
||||
for(String s : set)
|
||||
{
|
||||
pr += s + "/";
|
||||
}
|
||||
pr = pr.substring(0, pr.length() - 1);
|
||||
}else{
|
||||
for(String s : set) { pr = s; break; }
|
||||
}
|
||||
|
||||
rozvrh[i-1][j-1] = pr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package xyz.thastertyn.Jecna;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.nodes.Element;
|
||||
import org.jsoup.select.Elements;
|
||||
|
||||
import xyz.thastertyn.Tuple.Pair;
|
||||
|
||||
public class Znamky {
|
||||
|
||||
private boolean wasDownloaded;
|
||||
|
||||
// key (String) reprezentuje predmet, value (arraylist) znamky a pair znamku s jeji vahou
|
||||
private HashMap<String, ArrayList<Pair<Integer, Double>>> grades = new HashMap<>();
|
||||
|
||||
public void addGrade(String subject, int grade, double weight)
|
||||
{
|
||||
grades.get(subject).add(new Pair<Integer,Double>(grade, weight));
|
||||
}
|
||||
|
||||
public void addSubject(String subject)
|
||||
{
|
||||
grades.put(subject, new ArrayList<Pair<Integer, Double>>());
|
||||
}
|
||||
|
||||
private double getAverage(ArrayList<Pair<Integer, Double>> a)
|
||||
{
|
||||
try{
|
||||
int divident = 0;
|
||||
int divisor = 0;
|
||||
for(Pair<Integer, Double> p : a)
|
||||
{
|
||||
divident += p.getValue0() * p.getValue1();
|
||||
divisor += p.getValue1();
|
||||
}
|
||||
|
||||
return (double) divident / divisor;
|
||||
|
||||
}catch(ArithmeticException e)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public byte downloadZnamky(final String Jsessionid)
|
||||
{
|
||||
wasDownloaded = true;
|
||||
Document znamkyDoc = null;
|
||||
|
||||
try {
|
||||
znamkyDoc = Downloader.download("https://www.spsejecna.cz" + "/score/student", Jsessionid).get();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return 127;
|
||||
}
|
||||
|
||||
Elements rows = znamkyDoc.select("table.score").select("tr");
|
||||
|
||||
Elements[] rows_split = new Elements[rows.size()];
|
||||
|
||||
for (int i = 0; i < rows.size(); i++)
|
||||
{
|
||||
rows_split[i] = rows.get(i).children();
|
||||
}
|
||||
|
||||
for(int i = 1; i < rows_split.length; i++)
|
||||
{
|
||||
String predmet = rows_split[i].get(0).text();
|
||||
addSubject(predmet);
|
||||
|
||||
for(Element e : rows_split[i].get(1).select("a.score"))
|
||||
{
|
||||
int znamka = Integer.parseInt(e.select("span.value").text());
|
||||
|
||||
// Mala znamka se bude pocitat jako polovicni vaha
|
||||
if(e.hasClass("scoreSmall"))
|
||||
{
|
||||
addGrade(predmet, znamka, 0.5);
|
||||
}else{
|
||||
addGrade(predmet, znamka, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
if(wasDownloaded)
|
||||
{
|
||||
String s = "";
|
||||
|
||||
for(Map.Entry<String, ArrayList<Pair<Integer, Double>>> entry : grades.entrySet())
|
||||
{
|
||||
double prumer = getAverage(entry.getValue());
|
||||
double p = (double) Math.round(prumer * 100) / 100;
|
||||
s += entry.getKey() + " | " + entry.getValue().toString() + " { " + p + " }\n";
|
||||
}
|
||||
return s;
|
||||
}else{
|
||||
return "Nothing was downloaded";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user