diff --git a/app/src/main/java/io/github/wulkanowy/activity/dashboard/marks/MarksFragment.java b/app/src/main/java/io/github/wulkanowy/activity/dashboard/marks/MarksFragment.java index be7ca911..485f44d9 100644 --- a/app/src/main/java/io/github/wulkanowy/activity/dashboard/marks/MarksFragment.java +++ b/app/src/main/java/io/github/wulkanowy/activity/dashboard/marks/MarksFragment.java @@ -1,6 +1,5 @@ package io.github.wulkanowy.activity.dashboard.marks; - import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; @@ -11,48 +10,37 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import org.jsoup.Connection; -import org.jsoup.Jsoup; -import org.jsoup.nodes.Document; -import org.jsoup.nodes.Element; -import org.jsoup.select.Elements; - import java.io.FileInputStream; -import java.io.IOException; import java.io.ObjectInputStream; import java.util.ArrayList; -import java.util.HashSet; +import java.util.List; import java.util.Map; -import java.util.Set; import io.github.wulkanowy.R; +import io.github.wulkanowy.api.Cookies; +import io.github.wulkanowy.api.grades.*; public class MarksFragment extends Fragment { - private ArrayList subject = new ArrayList<>(); + private View view; public MarksFragment() { } - @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_marks, container, false); if (subject.size() == 0) { - - new MarksModel(container.getContext()).execute(); - } - else if (subject.size() > 1) { - + new MarksTask(container.getContext()).execute(); + } else if (subject.size() > 1) { createGrid(); view.findViewById(R.id.loadingPanel).setVisibility(View.GONE); } - return view; } @@ -67,12 +55,12 @@ public class MarksFragment extends Fragment { recyclerView.setAdapter(adapter); } - public class MarksModel extends AsyncTask { + public class MarksTask extends AsyncTask { private Context mContext; private Map loginCookies; - MarksModel(Context context) { + MarksTask(Context context) { mContext = context; } @@ -83,57 +71,25 @@ public class MarksFragment extends Fragment { try { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(cookiesPath)); loginCookies = (Map) ois.readObject(); - - } catch (ClassNotFoundException e) { + } catch (Exception e) { e.printStackTrace(); - } catch (IOException e){ - e.toString(); } - // get link to uonetplus-opiekun.vulcan.net.pl module - String startPageUrl = "https://uonetplus.vulcan.net.pl/powiatjaroslawski/Start.mvc/Index"; try { - Document startPage = Jsoup.connect(startPageUrl) - .followRedirects(true) - .cookies(loginCookies) - .get(); - Elements studentTileLink = startPage.select(".panel.linkownia.pracownik.klient > a"); - String uonetPlusOpiekunUrl = studentTileLink.attr("href"); - - // get context module cookie - Connection.Response res = Jsoup.connect(uonetPlusOpiekunUrl) - .followRedirects(true) - .cookies(loginCookies) - .execute(); - loginCookies = res.cookies(); - - //get subject name - String subjectPageurl = "https://uonetplus-opiekun.vulcan.net.pl/powiatjaroslawski/005791/Oceny/Wszystkie?details=1"; - Document subjectPage = Jsoup.connect(subjectPageurl) - .cookies(loginCookies) - .get(); - Elements subjectTile = subjectPage.select(".ocenyZwykle-table > tbody > tr"); - for (Element titlelement : subjectTile){ - subject.add(titlelement.select("td:nth-child(1)").text()); + Cookies cookies = new Cookies(); + cookies.setItems(loginCookies); + SubjectsList subjectsList = new SubjectsList(cookies, "powiatjaroslawski"); + List subjects = subjectsList.getAll(); + for (Subject item : subjects){ + subject.add(item.getName()); } - // get marks view - String marksPageUrl = "https://uonetplus-opiekun.vulcan.net.pl/powiatjaroslawski/005791/Oceny/Wszystkie?details=2"; - Document marksPage = Jsoup.connect(marksPageUrl) - .cookies(loginCookies) - .get(); - Elements marksRows = marksPage.select(".ocenySzczegoly-table > tbody > tr"); - for (Element element : marksRows) { - System.out.println("----------"); - System.out.println("Subject" + element.select("td:nth-child(1)").text()); - System.out.println("Grade: " + element.select("td:nth-child(2)").text()); - System.out.println("Description: " + element.select("td:nth-child(3)").text()); - System.out.println("Weight: " + element.select("td:nth-child(4)").text()); - System.out.println("Date: " + element.select("td:nth-child(5)").text()); - System.out.println("Teacher: " + element.select("td:nth-child(6)").text()); - + GradesList gradesList = new GradesList(cookies, "powiatjaroslawski"); + List grades = gradesList.getAll(); + for (Grade item : grades){ + System.out.println(item.getSubject() + ": " + item.getValue()); } - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } @@ -141,12 +97,6 @@ public class MarksFragment extends Fragment { } protected void onPostExecute(Void result) { - - Set hs = new HashSet<>(); - hs.addAll(subject); - subject.clear(); - subject.addAll(hs); - createGrid(); view.findViewById(R.id.loadingPanel).setVisibility(View.GONE); @@ -154,6 +104,4 @@ public class MarksFragment extends Fragment { super.onPostExecute(result); } } - - } diff --git a/app/src/main/java/io/github/wulkanowy/api/Cookies.java b/app/src/main/java/io/github/wulkanowy/api/Cookies.java index 189ef02b..2617dffe 100644 --- a/app/src/main/java/io/github/wulkanowy/api/Cookies.java +++ b/app/src/main/java/io/github/wulkanowy/api/Cookies.java @@ -10,6 +10,10 @@ public class Cookies { cookies = items; } + public void addItems(Map items) { + cookies.putAll(items); + } + public Map getAll() { return cookies; } diff --git a/app/src/main/java/io/github/wulkanowy/api/StudentAndParent.java b/app/src/main/java/io/github/wulkanowy/api/StudentAndParent.java new file mode 100644 index 00000000..cb3025e0 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/api/StudentAndParent.java @@ -0,0 +1,67 @@ +package io.github.wulkanowy.api; + +import org.jsoup.Connection; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; + +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import io.github.wulkanowy.api.login.LoginErrorException; + +public class StudentAndParent extends Vulcan { + + private String startPageUrl = "https://uonetplus.vulcan.net.pl/{locationID}/Start.mvc/Index"; + + private String locationID; + + private String uonetPlusOpiekunUrl; + + public StudentAndParent(Cookies cookies, String locID) throws IOException { + super(cookies); + + locationID = locID; + + setUp(); + } + + private void setUp() throws IOException { + startPageUrl = startPageUrl.replace("{locationID}", locationID); + + // get link to uonetplus-opiekun.vulcan.net.pl module + Document startPage = Jsoup.connect(startPageUrl) + .followRedirects(true) + .cookies(getJar()) + .get(); + Element studentTileLink = startPage.select(".panel.linkownia.pracownik.klient > a").first(); + uonetPlusOpiekunUrl = studentTileLink.attr("href"); + + //get context module cookie + Connection.Response res = Jsoup.connect(uonetPlusOpiekunUrl) + .followRedirects(true) + .cookies(getJar()) + .execute(); + + addCookies(res.cookies()); + } + + protected String getLocationID() { + return locationID; + } + + public String getID() throws LoginErrorException { + Pattern pattern = Pattern.compile("([0-9]{6})"); + Matcher matcher = pattern.matcher(uonetPlusOpiekunUrl); + + // Finds all the matches until found by moving the `matcher` forward + if (!matcher.find()) { + throw new LoginErrorException(); + } + + String match = matcher.group(1); + + return match; + } +} diff --git a/app/src/main/java/io/github/wulkanowy/api/Vulcan.java b/app/src/main/java/io/github/wulkanowy/api/Vulcan.java index f6d987cf..a62b389c 100644 --- a/app/src/main/java/io/github/wulkanowy/api/Vulcan.java +++ b/app/src/main/java/io/github/wulkanowy/api/Vulcan.java @@ -14,7 +14,15 @@ public abstract class Vulcan { return cookies.getAll(); } + public Cookies getCookies() { + return cookies; + } + public void setCookies(Map items) { cookies.setItems(items); } + + public void addCookies(Map items) { + cookies.addItems(items); + } } diff --git a/app/src/main/java/io/github/wulkanowy/api/grades/Grade.java b/app/src/main/java/io/github/wulkanowy/api/grades/Grade.java new file mode 100644 index 00000000..3cace9f4 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/api/grades/Grade.java @@ -0,0 +1,88 @@ +package io.github.wulkanowy.api.grades; + +public class Grade { + + private String subject; + + private String value; + + private String color; + + private String description; + + private String weight; + + private String date; + + private String teacher; + + public Grade setSubject(String subject) { + this.subject = subject; + + return this; + } + + public Grade setValue(String value) { + this.value = value; + + return this; + } + + public Grade setColor(String color) { + this.color = color; + + return this; + } + + public Grade setDescription(String description) { + this.description = description; + + return this; + } + + public Grade setWeight(String weight) { + this.weight = weight; + + return this; + } + + public Grade setDate(String date) { + this.date = date; + + return this; + } + + public Grade setTeacher(String teacher) { + this.teacher = teacher; + + return this; + } + + public String getSubject() { + return subject; + } + + public String getValue() { + return value; + } + + public String getColor() { + return color; + } + + public String getDescription() { + return description; + } + + public String getWeight() { + return weight; + } + + public String getDate() { + return date; + } + + public String getTeacher() { + return teacher; + } +} diff --git a/app/src/main/java/io/github/wulkanowy/api/grades/GradesList.java b/app/src/main/java/io/github/wulkanowy/api/grades/GradesList.java new file mode 100644 index 00000000..3ac4c8a5 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/api/grades/GradesList.java @@ -0,0 +1,61 @@ +package io.github.wulkanowy.api.grades; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import io.github.wulkanowy.api.Cookies; +import io.github.wulkanowy.api.StudentAndParent; +import io.github.wulkanowy.api.login.LoginErrorException; + +public class GradesList extends StudentAndParent { + + private String gradesPageUrl = "https://uonetplus-opiekun.vulcan.net.pl/{locationID}/{ID}/Oceny/Wszystkie?details=2"; + + private List grades = new ArrayList<>(); + + public GradesList(Cookies cookies, String locationID) throws IOException { + super(cookies, locationID); + } + + public List getAll() throws IOException, LoginErrorException { + gradesPageUrl = gradesPageUrl.replace("{locationID}", getLocationID()); + gradesPageUrl = gradesPageUrl.replace("{ID}", getID()); + + Document marksPage = Jsoup.connect(gradesPageUrl) + .cookies(getJar()) + .get(); + + Elements marksRows = marksPage.select(".ocenySzczegoly-table > tbody > tr"); + + for (Element row : marksRows) { + Pattern pattern = Pattern.compile("#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})"); + Matcher matcher = pattern.matcher(row.select("td:nth-child(2) span.ocenaCzastkowa").attr("style")); + String color; + if (!matcher.find()) { + color = "000000"; + } else { + color = matcher.group(1); + } + + grades.add(new Grade() + .setSubject(row.select("td:nth-child(1)").text()) + .setValue(row.select("td:nth-child(2)").text()) + .setColor(color) + .setDescription(row.select("td:nth-child(3)").text()) + .setWeight(row.select("td:nth-child(4)").text()) + .setDate(row.select("td:nth-child(5)").text()) + .setTeacher(row.select("td:nth-child(6)").text()) + ); + } + + return grades; + } +} diff --git a/app/src/main/java/io/github/wulkanowy/api/grades/Subject.java b/app/src/main/java/io/github/wulkanowy/api/grades/Subject.java new file mode 100644 index 00000000..9f52f1a8 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/api/grades/Subject.java @@ -0,0 +1,38 @@ +package io.github.wulkanowy.api.grades; + +public class Subject { + + private String name; + private String predictedRating; + private String finalRating; + + public Subject setName(String name) { + this.name = name; + + return this; + } + + public Subject setPredictedRating(String predictedRating) { + this.predictedRating = predictedRating; + + return this; + } + + public Subject setFinalRating(String finalRating) { + this.finalRating = finalRating; + + return this; + } + + public String getName() { + return name; + } + + public String getPredictedRating() { + return predictedRating; + } + + public String getFinalRating() { + return finalRating; + } +} diff --git a/app/src/main/java/io/github/wulkanowy/api/grades/SubjectsList.java b/app/src/main/java/io/github/wulkanowy/api/grades/SubjectsList.java new file mode 100644 index 00000000..8b62879f --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/api/grades/SubjectsList.java @@ -0,0 +1,46 @@ +package io.github.wulkanowy.api.grades; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import io.github.wulkanowy.api.Cookies; +import io.github.wulkanowy.api.StudentAndParent; +import io.github.wulkanowy.api.login.LoginErrorException; + +public class SubjectsList extends StudentAndParent { + + private String subjectsPageUrl = "https://uonetplus-opiekun.vulcan.net.pl/{locationID}/{ID}/Oceny/Wszystkie?details=1"; + + private List subjects = new ArrayList<>(); + + public SubjectsList(Cookies cookies, String locationID) throws IOException { + super(cookies, locationID); + } + + public List getAll() throws IOException, LoginErrorException { + subjectsPageUrl = subjectsPageUrl.replace("{locationID}", getLocationID()); + subjectsPageUrl = subjectsPageUrl.replace("{ID}", getID()); + + Document subjectPage = Jsoup.connect(subjectsPageUrl) + .cookies(getJar()) + .get(); + + Elements rows = subjectPage.select(".ocenyZwykle-table > tbody > tr"); + + for (Element subjectRow : rows){ + subjects.add(new Subject() + .setName(subjectRow.select("td:nth-child(1)").text()) + .setPredictedRating(subjectRow.select("td:nth-child(3)").text()) + .setFinalRating(subjectRow.select("td:nth-child(4)").text()) + ); + } + + return subjects; + } +}