Add experimental/partial downloading marks data

This commit is contained in:
Mikołaj Pich 2017-07-19 21:24:27 +02:00
parent 2a0134d8f4
commit 7d00f92b83
3 changed files with 92 additions and 2 deletions

View File

@ -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<Void, Void, Void> {
private Context mContext;
private Map<String, String> 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<String, String>) 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;
}
}
}

View File

@ -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<Void, Void, Void> {
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<Void, Void, Void> {
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) {

View File

@ -31,7 +31,7 @@ public class LoadingTask extends AsyncTask<Void, Void, Void> {
/* 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);
}
}