Search in sources :

Example 6 with GiuaScraper

use of com.giua.webscraper.GiuaScraper in project Giua-App by Giua-app.

the class AccountsActivity method addNewAccountToLayout.

private void addNewAccountToLayout(String username, String password, String email, @ColorInt int color, String siteUrl) {
    AccountCard accountCard = new AccountCard(this, username, email, "", color);
    View pbAccountCard = accountCard.findViewById(R.id.view_account_card_pb);
    pbAccountCard.setVisibility(View.VISIBLE);
    accountCard.setLayoutParams(accountCardParams);
    accountCard.setOnClickListener(this::onAccountCardClick);
    layoutAllAccounts.addView(accountCard);
    new Thread(() -> {
        GiuaScraper gS = new GiuaScraper(username, password, loggerManager);
        gS.setPrivateSiteUrl(siteUrl);
        try {
            gS.login();
            runOnUiThread(() -> {
                pbAccountCard.setVisibility(View.INVISIBLE);
                AccountData.setCredentials(this, username, password, gS.getCookie(), gS.getUserTypeString(), email);
                AccountData.setSiteUrl(this, username, siteUrl);
                AppData.addAccountUsername(this, username);
            });
        } catch (GiuaScraperExceptions.YourConnectionProblems | GiuaScraperExceptions.SiteConnectionProblems e) {
            runOnUiThread(() -> {
                layoutAllAccounts.removeView(accountCard);
                setErrorMessage("Problema di connessione", layoutAllAccounts);
            });
        } catch (GiuaScraperExceptions.SessionCookieEmpty | GiuaScraperExceptions.UnableToLogin e) {
            runOnUiThread(() -> {
                layoutAllAccounts.removeView(accountCard);
                setErrorMessage("Credenziali non valide", layoutAllAccounts);
            });
        } catch (Exception e) {
            runOnUiThread(() -> {
                layoutAllAccounts.removeView(accountCard);
                setErrorMessage("Impossibile aggiungere l'account. Riprova più tardi.", layoutAllAccounts);
            });
        }
    }).start();
}
Also used : ImageView(android.widget.ImageView) SwipeView(com.giua.app.ui.views.SwipeView) View(android.view.View) TextView(android.widget.TextView) GiuaScraperExceptions(com.giua.webscraper.GiuaScraperExceptions) GiuaScraper(com.giua.webscraper.GiuaScraper)

Example 7 with GiuaScraper

use of com.giua.webscraper.GiuaScraper in project Giua-App by Giua-app.

the class AutomaticLoginActivity method loginWithPreviousCredentials.

private void loginWithPreviousCredentials() {
    if (GlobalVariables.gsThread == null || GlobalVariables.gsThread.isInterrupted() || GlobalVariables.gsThread.isInterrupting())
        GlobalVariables.gsThread = new GiuaScraperThread();
    loggerManager.d("Login automatico con credenziali salvate in corso");
    GlobalVariables.gsThread.addTask(() -> {
        runOnUiThread(() -> pbLoadingScreen.setVisibility(View.VISIBLE));
        try {
            String username = AppData.getActiveUsername(this);
            String password = AccountData.getPassword(this, username);
            String cookie = AccountData.getCookie(this, username);
            String savedSiteUrl = AccountData.getSiteUrl(this, username);
            GlobalVariables.gS = new GiuaScraper(username, password, cookie, true, SettingsData.getSettingBoolean(this, SettingKey.DEMO_MODE), new LoggerManager("GiuaScraper", this));
            if (!savedSiteUrl.equals(""))
                GlobalVariables.gS.setPrivateSiteUrl(savedSiteUrl);
            GlobalVariables.gS.login();
            AccountData.setCredentials(this, username, password, GlobalVariables.gS.getCookie(), GlobalVariables.gS.getUserTypeString(), GlobalVariables.gS.getProfilePage(false).getProfileInformation()[2]);
            AccountData.setSiteUrl(this, username, GlobalVariables.gS.getSiteUrl());
            if (!AppData.getAllAccountUsernames(this).contains(username))
                AppData.addAccountUsername(this, username);
            startDrawerActivity();
        } catch (GiuaScraperExceptions.YourConnectionProblems | GiuaScraperExceptions.SiteConnectionProblems e) {
            loggerManager.e("Errore di connessione - " + e.getMessage());
            loggerManager.e(Arrays.toString(e.getStackTrace()));
            runOnUiThread(() -> btnLogout.setVisibility(View.VISIBLE));
            runOnUiThread(() -> btnOffline.setVisibility(View.VISIBLE));
            if (SettingsData.getSettingBoolean(this, SettingKey.EXP_MODE))
                runOnUiThread(() -> btnOffline.setVisibility(View.VISIBLE));
            if (e.getClass() == GiuaScraperExceptions.YourConnectionProblems.class)
                runOnUiThread(() -> setErrorMessage(getString(R.string.your_connection_error)));
            else if (e.getClass() == GiuaScraperExceptions.SiteConnectionProblems.class)
                runOnUiThread(() -> setErrorMessage(getString(R.string.site_connection_error)));
            else
                runOnUiThread(() -> setErrorMessage("E' stato riscontrato qualche problema sconosciuto riguardo la rete"));
            runOnUiThread(() -> pbLoadingScreen.setVisibility(View.INVISIBLE));
            threadSleepWithTextUpdates();
            if (waitToReLogin < 30)
                waitToReLogin += 5;
            loginWithPreviousCredentials();
        } catch (GiuaScraperExceptions.SessionCookieEmpty sce) {
            // Se il login non dovesse funzionare lancia l acitvity di login ed elimina le credenziali salvate
            if (AppData.getActiveUsername(this).equals("gsuite")) {
                // Questa condizione si verifica quando è presente un acccount studente con il cookie scaduto
                loggerManager.w("Cookie gS dell'account studente scaduto, avvio StudentLoginActivity");
                startStudentLoginActivity();
            } else {
                loggerManager.w("Cookie gS scaduto, avvio ActivityManager");
                AppData.removeAccountUsername(this, AppData.getActiveUsername(this));
                AppData.saveActiveUsername(this, "");
                setErrorMessage("Le credenziali di accesso non sono più valide");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                startActivity(new Intent(AutomaticLoginActivity.this, ActivityManager.class));
            }
        } catch (GiuaScraperExceptions.MaintenanceIsActiveException e) {
            loggerManager.e("Errore: sito in manutenzione");
            try {
                Maintenance maintenance = GlobalVariables.gS.getMaintenanceInfo();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
                runOnUiThread(() -> setErrorMessage("Il sito è in manutenzione sino alle " + simpleDateFormat.format(maintenance.end)));
            } catch (Exception e2) {
                loggerManager.e("Errore nel ricevere informazioni manutenzione: " + e.getMessage());
                runOnUiThread(() -> setErrorMessage(getString(R.string.maintenance_is_active_error)));
            }
            runOnUiThread(() -> btnLogout.setVisibility(View.VISIBLE));
            if (SettingsData.getSettingBoolean(this, SettingKey.EXP_MODE))
                runOnUiThread(() -> btnOffline.setVisibility(View.VISIBLE));
            runOnUiThread(() -> pbLoadingScreen.setVisibility(View.GONE));
            runOnUiThread(() -> tvAutoLogin.setText("Accesso fallito."));
        }
    });
    if (SettingsData.getSettingBoolean(this, SettingKey.EXP_MODE)) {
        new Thread(() -> {
            // mostra il pulsante per l'offline dopo 5 secondi
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            runOnUiThread(() -> {
                btnOffline.setVisibility(View.INVISIBLE);
                btnOffline.setAlpha(0f);
                btnOffline.animate().alpha(1f).setDuration(400);
                btnOffline.setVisibility(View.VISIBLE);
            });
        }).start();
    }
}
Also used : Intent(android.content.Intent) Maintenance(com.giua.objects.Maintenance) GiuaScraperThread(com.giua.app.GiuaScraperThread) GiuaScraperThread(com.giua.app.GiuaScraperThread) LoggerManager(com.giua.app.LoggerManager) GiuaScraperExceptions(com.giua.webscraper.GiuaScraperExceptions) SimpleDateFormat(java.text.SimpleDateFormat) GiuaScraper(com.giua.webscraper.GiuaScraper)

Example 8 with GiuaScraper

use of com.giua.webscraper.GiuaScraper in project Giua-App by Giua-app.

the class LoginActivity method onBackPressed.

/**
 * Esci dall'applicazione simulando la pressione del tasto home
 */
@Override
public void onBackPressed() {
    if (!isAddingAccount) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    } else {
        GlobalVariables.gsThread.addTask(() -> {
            String username = AppData.getActiveUsername(this);
            GlobalVariables.gS = new GiuaScraper(username, AccountData.getPassword(this, username), AccountData.getCookie(this, username), true, SettingsData.getSettingBoolean(this, SettingKey.DEMO_MODE), new LoggerManager("GiuaScraper", this));
            GlobalVariables.gS.login();
            runOnUiThread(this::startDrawerActivity);
        });
    }
}
Also used : LoggerManager(com.giua.app.LoggerManager) Intent(android.content.Intent) GiuaScraper(com.giua.webscraper.GiuaScraper)

Example 9 with GiuaScraper

use of com.giua.webscraper.GiuaScraper in project Giua-App by Giua-app.

the class StudentLoginActivity method onStoppedWebView.

private void onStoppedWebView(boolean increaseVisitCount) {
    loggerManager.d("onStoppedWebView chiamato");
    if (increaseVisitCount)
        new Analytics.Builder(Analytics.LOG_IN).addCustomValue("account_type", "Studente (Google)").send();
    webView.setVisibility(View.INVISIBLE);
    obscureLayoutView.setVisibility(View.VISIBLE);
    loggerManager.d("Creazione credenziali con cookie ottenuto da google");
    GlobalVariables.gS = new GiuaScraper("gsuite", "gsuite", cookie, true, new LoggerManager("GiuaScraper", this));
    AccountData.setCredentials(this, "gsuite", "gsuite", cookie, "Studente", "");
    AccountData.setSiteUrl(this, "gsuite", GiuaScraper.getGlobalSiteUrl());
    GlobalVariables.gsThread.addTask(() -> {
        try {
            String email = GlobalVariables.gS.getProfilePage(false).getProfileInformation()[2];
            runOnUiThread(() -> AccountData.setEmail(this, "gsuite", email));
        } catch (Exception ignored) {
        }
    });
    obscureLayoutView.setVisibility(View.GONE);
    loggerManager.d("Avvio DrawerActivity");
    if (!AppData.getAllAccountUsernames(this).contains("gsuite")) {
        AppData.addAccountUsername(this, "gsuite");
        if (!isRequestedFromAccounts)
            AppData.saveActiveUsername(this, "gsuite");
    }
    if (goTo == null)
        goTo = "";
    if (isRequestedFromAccounts) {
        webView.clearHistory();
        onBackPressed();
        return;
    }
    Intent intent = new Intent(this, DrawerActivity.class).putExtra("goTo", goTo);
    startActivity(intent);
    finish();
}
Also used : LoggerManager(com.giua.app.LoggerManager) Intent(android.content.Intent) Analytics(com.giua.app.Analytics) GiuaScraper(com.giua.webscraper.GiuaScraper)

Example 10 with GiuaScraper

use of com.giua.webscraper.GiuaScraper in project Giua-Webscraper by Giua-app.

the class TestClasses method testAll.

private static void testAll() {
    if (speedTest) {
        System.out.println("--------STARTING SPEED TEST-----");
        testSpeed();
        return;
    }
    long t1;
    long t2;
    long tSite;
    long tPhase1 = 0;
    long tPhase2 = 0;
    long tPhase3 = 0;
    t1 = System.currentTimeMillis();
    logln("My internet work: " + GiuaScraper.isMyInternetWorking());
    t2 = System.currentTimeMillis();
    logln("Tempo: " + (t2 - t1));
    t1 = System.currentTimeMillis();
    logln("The site work: " + GiuaScraper.isSiteWorking());
    t2 = System.currentTimeMillis();
    logln("Tempo: " + (t2 - t1));
    tSite = t2 - t1;
    // ///////////////////////////////////////////////////////////////////
    // NO CACHE
    // In questa prima parte vengono generate tutte le cose mentre nella seconda viene usata la cache
    System.out.println("\n\n----------------------Phase 1 - Testing all webscraper functions-----------------------------\n\n");
    t1 = nanoTime();
    startLogin();
    System.out.println("\n-------------------\nConnecting to " + gS.getSiteUrl() + "\n-------------------\n");
    System.out.println("--------NEWS--------");
    testNews(true);
    System.out.println("--------VOTI--------");
    testVotes(true);
    System.out.println("--------AVVISI---------");
    testAlerts(true);
    System.out.println("--------AGENDA--------");
    testAgendaPage(true);
    System.out.println("--------CIRCOLARI--------");
    testNewsletters(true);
    System.out.println("--------LEZIONI--------");
    testLessons(true);
    System.out.println("--------PAGELLA--------");
    System.out.println("Non disponibile");
    testReportCard(true);
    System.out.println("--------NOTE--------");
    testNotes(true);
    System.out.println("--------ASSENZE--------");
    testAbsences(true);
    System.out.println("--------AUTORIZZAZIONI--------");
    testAuthorization(true);
    System.out.println("--------DOCUMENTI--------");
    testDocuments(true);
    System.out.println("--------ARGOMENTI E ATTIVITA--------");
    testArgumentsActivities(true);
    t2 = nanoTime();
    tPhase1 = t2 - t1;
    System.out.println("---------------------------------------------------");
    System.out.println("Tempo: " + tPhase1);
    System.out.println("---------------------------------------------------");
    // //////////////////////////////////////////////////////////
    // CACHE
    System.out.println("\n\n----------------------Phase 2 - Testing cache-----------------------------\n\n");
    t1 = nanoTime();
    // gS.setSiteURL("https://registroasiaiai.giua.edu.it");
    // Document doc = gS.getPage("");
    System.out.println("Account type: " + gS.getUserTypeEnum());
    logln("Ultimo accesso: " + gS.getHomePage(false).getLastAccessTime().toString());
    System.out.println("\n-------------------\nConnecting to " + gS.getSiteUrl() + "\n-------------------\n");
    System.out.println("--------NEWS--------");
    testNews(false);
    System.out.println("--------VOTI--------");
    testVotes(false);
    System.out.println("--------AVVISI---------");
    testAlerts(false);
    System.out.println("--------AGENDA--------");
    testAgendaPage(true);
    System.out.println("--------CIRCOLARI--------");
    testNewsletters(false);
    System.out.println("--------LEZIONI--------");
    testLessons(false);
    System.out.println("--------PAGELLA--------");
    System.out.println("Non disponibile");
    // testReportCard(false);
    System.out.println("--------NOTE--------");
    testNotes(false);
    System.out.println("--------ASSENZE--------");
    testAbsences(false);
    System.out.println("--------AUTORIZZAZIONI--------");
    testAuthorization(false);
    System.out.println("--------DOCUMENTI--------");
    testDocuments(false);
    System.out.println("--------ARGOMENTI E ATTIVITA--------");
    testArgumentsActivities(false);
    t2 = nanoTime();
    tPhase2 = t2 - t1;
    System.out.println("---------------------------------------------------");
    System.out.println("Tempo: " + t2 + "-" + t1);
    System.out.println("---------------------------------------------------");
    System.out.println("\n\n----------------------Phase 3 - Testing login with valid session-----------------------------\n\n");
    t1 = nanoTime();
    System.out.println("Logout...");
    String phpsessid = gS.getCookie();
    gS = new GiuaScraper(user, password, phpsessid, true, demoMode, null);
    gS.login();
    System.out.println("Created new gS variable");
    System.out.println("Account type: " + gS.getUserTypeEnum());
    logln("Ultimo accesso: " + gS.getHomePage(false).getLastAccessTime().toString());
    System.out.println("\n-------------------\nConnecting to " + gS.getSiteUrl() + "\n-------------------\n");
    System.out.println("--------NEWS--------");
    testNews(true);
    System.out.println("--------VOTI--------");
    testVotes(true);
    System.out.println("--------AVVISI---------");
    testAlerts(true);
    System.out.println("--------AGENDA--------");
    testAgendaPage(true);
    System.out.println("--------CIRCOLARI--------");
    testNewsletters(true);
    System.out.println("--------LEZIONI--------");
    testLessons(true);
    System.out.println("--------PAGELLA--------");
    // testReportCard(true);
    System.out.println("--------NOTE--------");
    testNotes(true);
    System.out.println("--------ASSENZE--------");
    testAbsences(true);
    System.out.println("--------AUTORIZZAZIONI--------");
    testAuthorization(true);
    System.out.println("--------DOCUMENTI--------");
    testDocuments(true);
    System.out.println("--------ARGOMENTI E ATTIVITA--------");
    testArgumentsActivities(true);
    t2 = nanoTime();
    tPhase3 = t2 - t1;
    System.out.println("---------------------------------------------------");
    System.out.println("Tempo: " + tPhase3);
    System.out.println("---------------------------------------------------");
    System.out.println("\n\n");
    System.out.println("/---------------------RESULTS----------------------------");
    System.out.println("|    Fase 1 (login iniziale):        " + (tPhase1 / 1000000) + "ms");
    System.out.println("|    Fase 2 (cache):                 " + (tPhase2 / 1000000) + "ms");
    System.out.println("|    Fase 3 (riutilizzo sessione):   " + (tPhase3 / 1000000) + "ms");
    System.out.println("|");
    System.out.println("|    Totale:                         " + (tPhase1 / 1000000 + tPhase2 / 1000000 + tPhase3 / 1000000) + "ms");
    System.out.println("\\--------------------------------------------------------");
}
Also used : GiuaScraper(com.giua.webscraper.GiuaScraper)

Aggregations

GiuaScraper (com.giua.webscraper.GiuaScraper)11 LoggerManager (com.giua.app.LoggerManager)4 GiuaScraperExceptions (com.giua.webscraper.GiuaScraperExceptions)4 Intent (android.content.Intent)3 GiuaScraperThread (com.giua.app.GiuaScraperThread)2 Maintenance (com.giua.objects.Maintenance)2 SimpleDateFormat (java.text.SimpleDateFormat)2 View (android.view.View)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 Analytics (com.giua.app.Analytics)1 SwipeView (com.giua.app.ui.views.SwipeView)1 JsonBuilder (com.giua.utils.JsonBuilder)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 ParseException (java.text.ParseException)1