1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2025-01-19 02:46:44 -06:00

Add api for grades

This commit is contained in:
Mikołaj Pich 2017-07-22 14:37:00 +02:00
parent f4a8ad9b55
commit df7153920d
8 changed files with 332 additions and 72 deletions

View File

@ -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<String> 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<Void, Void, Void> {
public class MarksTask extends AsyncTask<Void, Void, Void> {
private Context mContext;
private Map<String, String> 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<String, String>) 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<Subject> 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<Grade> 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<String> 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);
}
}
}

View File

@ -10,6 +10,10 @@ public class Cookies {
cookies = items;
}
public void addItems(Map<String, String> items) {
cookies.putAll(items);
}
public Map<String, String> getAll() {
return cookies;
}

View File

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

View File

@ -14,7 +14,15 @@ public abstract class Vulcan {
return cookies.getAll();
}
public Cookies getCookies() {
return cookies;
}
public void setCookies(Map<String, String> items) {
cookies.setItems(items);
}
public void addCookies(Map<String, String> items) {
cookies.addItems(items);
}
}

View File

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

View File

@ -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<Grade> grades = new ArrayList<>();
public GradesList(Cookies cookies, String locationID) throws IOException {
super(cookies, locationID);
}
public List<Grade> 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;
}
}

View File

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

View File

@ -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<Subject> subjects = new ArrayList<>();
public SubjectsList(Cookies cookies, String locationID) throws IOException {
super(cookies, locationID);
}
public List<Subject> 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;
}
}