From 7d00f92b83b5d8e71c77efa714424e4cbea84f05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Wed, 19 Jul 2017 21:24:27 +0200 Subject: [PATCH] Add experimental/partial downloading marks data --- .../dashboard/marks/MarksFragment.java | 78 +++++++++++++++++++ .../github/wulkanowy/activity/main/Login.java | 14 +++- .../activity/started/LoadingTask.java | 2 +- 3 files changed, 92 insertions(+), 2 deletions(-) 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 532dd4e9..9953e8f0 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 @@ -2,6 +2,8 @@ package io.github.wulkanowy.activity.dashboard.marks; import android.app.Activity; +import android.content.Context; +import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.widget.GridLayoutManager; @@ -10,8 +12,18 @@ 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.Arrays; +import java.util.Map; import io.github.wulkanowy.R; @@ -39,6 +51,8 @@ public class MarksFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + new MarksModel(container.getContext()).execute(); + View view = inflater.inflate(R.layout.fragment_marks, container, false); RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.card_recycler_view); @@ -53,4 +67,68 @@ public class MarksFragment extends Fragment { return view; } + public class MarksModel extends AsyncTask { + + private Context mContext; + private Map loginCookies; + + MarksModel(Context context) { + mContext = context; + } + + @Override + protected Void doInBackground(Void... params) { + String cookiesPath = mContext.getFilesDir().getPath() + "/cookies.txt"; + + try { + ObjectInputStream ois = new ObjectInputStream(new FileInputStream(cookiesPath)); + loginCookies = (Map) ois.readObject(); + + } catch (ClassNotFoundException 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 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()); + } + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } + } + + } diff --git a/app/src/main/java/io/github/wulkanowy/activity/main/Login.java b/app/src/main/java/io/github/wulkanowy/activity/main/Login.java index 1e33985b..53943f5b 100644 --- a/app/src/main/java/io/github/wulkanowy/activity/main/Login.java +++ b/app/src/main/java/io/github/wulkanowy/activity/main/Login.java @@ -11,7 +11,9 @@ import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.ObjectOutputStream; import java.util.Map; import io.github.wulkanowy.R; @@ -83,6 +85,12 @@ public class Login extends AsyncTask { String helloText = dashboardHtml.getElementsByClass("welcome").text(); if (helloText.equals("DzieƄ dobry!")) { + String cookiesPath = activity.getFilesDir().getPath() + "/cookies.txt"; + FileOutputStream out = new FileOutputStream(cookiesPath); + ObjectOutputStream outputStream = new ObjectOutputStream(out); + outputStream.writeObject(loginCookies); + outputStream.flush(); + userMesage = activity.getString(R.string.login_accepted); } else { @@ -129,13 +137,17 @@ public class Login extends AsyncTask { urlForStepThree = urlForStepThree.replace("{locationID}", county); - return Jsoup.connect(urlForStepThree) + Connection.Response res = Jsoup.connect(urlForStepThree) .data("wa", wa) .data("wresult", wresults) .cookies(loginCookies) .followRedirects(true) .method(Connection.Method.POST) .execute(); + + loginCookies = res.cookies(); + + return res; } protected void onPostExecute(Void result) { diff --git a/app/src/main/java/io/github/wulkanowy/activity/started/LoadingTask.java b/app/src/main/java/io/github/wulkanowy/activity/started/LoadingTask.java index bc57ffc0..e6eb602b 100644 --- a/app/src/main/java/io/github/wulkanowy/activity/started/LoadingTask.java +++ b/app/src/main/java/io/github/wulkanowy/activity/started/LoadingTask.java @@ -31,7 +31,7 @@ public class LoadingTask extends AsyncTask { /* Intent intent = new Intent(activity,MainActivity.class); activity.startActivity(intent); */ - Intent intent = new Intent(activity,DashboardActivity.class); + Intent intent = new Intent(activity,MainActivity.class); activity.startActivity(intent); } }