Search in sources :

Example 1 with GiuaScraper

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

the class CheckNewsReceiver method checkAndMakeLogin.

private void checkAndMakeLogin() {
    String username = AppData.getActiveUsername(context);
    String accountUrl = AccountData.getSiteUrl(context, username);
    String defaultUrl = SettingsData.getSettingString(context, SettingKey.DEFAULT_URL);
    if (accountUrl.equals("") && !defaultUrl.equals(""))
        GiuaScraper.setSiteURL(defaultUrl);
    else if (!accountUrl.equals(""))
        GiuaScraper.setSiteURL(accountUrl);
    loggerManager.d("Username letto: " + username);
    if (!username.equals("gsuite")) {
        // Se l'account non è di gsuite fai il login normale
        loggerManager.d("Account non google rilevato, eseguo login");
        gS = new GiuaScraper(username, AccountData.getPassword(context, username), AccountData.getCookie(context, username), true, new LoggerManager("GiuaScraper", context));
        gS.login();
        AccountData.setCredentials(context, username, AccountData.getPassword(context, username), gS.getCookie());
    } else {
        // Se l'account è di gsuite fai il login con gsuite
        try {
            loggerManager.d("Account google rilevato, provo ad entrare con cookie precedente");
            gS = new GiuaScraper(username, AccountData.getPassword(context, username), AccountData.getCookie(context, username), true, new LoggerManager("GiuaScraper", context));
            gS.login();
            AccountData.setCredentials(context, username, AccountData.getPassword(context, username), gS.getCookie());
        } catch (GiuaScraperExceptions.SessionCookieEmpty e) {
            loggerManager.d("Cookie precedente non valido");
            gS = new GiuaScraper(username, AccountData.getPassword(context, username), makeGsuiteLogin(), true, new LoggerManager("GiuaScraper", context));
            gS.login();
            AccountData.setCredentials(context, username, AccountData.getPassword(context, username), gS.getCookie());
        }
    }
}
Also used : GiuaScraperExceptions(com.giua.webscraper.GiuaScraperExceptions) GiuaScraper(com.giua.webscraper.GiuaScraper)

Example 2 with GiuaScraper

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

the class AppNotifications method makeLogin.

private void makeLogin() {
    loggerManager.d("Faccio il login");
    String password = AccountData.getPassword(context, activeUsername);
    String cookie = AccountData.getCookie(context, activeUsername);
    String siteUrl = AccountData.getSiteUrl(context, activeUsername);
    String defaultUrl = SettingsData.getSettingString(context, SettingKey.DEFAULT_URL);
    gS = new GiuaScraper(activeUsername, password, cookie, true, loggerManager);
    if (// siteUrl non impostato quindi usa defaultUrl
    siteUrl.equals(""))
        // siteUrl non impostato quindi usa defaultUrl
        gS.setPrivateSiteUrl(defaultUrl);
    else
        gS.setPrivateSiteUrl(siteUrl);
    try {
        gS.login();
    } catch (Exception e) {
        logErrorInLoggerManager(e);
        sendErrorNotification(e.toString());
    }
}
Also used : GiuaScraper(com.giua.webscraper.GiuaScraper) URISyntaxException(java.net.URISyntaxException) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 3 with GiuaScraper

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

the class AppNotifications method makeGsuiteLogin.

private void makeGsuiteLogin() {
    String cookie = getCookieForGsuiteLogin();
    // Cè stato un errore e dovrebbe già essere stato segnalato
    if (cookie.equals(""))
        return;
    gS = new GiuaScraper("gsuite", "gsuite", cookie, true, loggerManager);
}
Also used : GiuaScraper(com.giua.webscraper.GiuaScraper)

Example 4 with GiuaScraper

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

the class TestClasses method testSpeed.

// endregion
private static void testSpeed() {
    logEnabled = false;
    List<Long> tSite = new Vector<>();
    List<Long> tInternet = new Vector<>();
    List<Long> tPhase1 = new Vector<>();
    List<Long> tPhase2 = new Vector<>();
    List<Long> tPhase3 = new Vector<>();
    List<Long> tPhasesTot = new Vector<>();
    List<Long> tJsonBuilder = new Vector<>();
    List<Long> tJsonParser = new Vector<>();
    int errors = 0;
    LoggerManager loggerManager = new LoggerManager("GiuaScraper-silent");
    System.out.println("----  AVVIO SPEEDTEST PER SCRAPER ----");
    System.out.println("Legenda:  |   LOGIN    |  CACHE   | SESSIONE |");
    System.out.println("          [############|##########|##########]");
    int i = 0;
    while (i < speedTestAmount) {
        long t1;
        long t2;
        System.out.println("Test " + (i + 1) + "/" + speedTestAmount);
        System.out.print("Progress: [");
        try {
            t1 = nanoTime();
            GiuaScraper.isMyInternetWorking();
            System.out.print("#");
            t2 = nanoTime();
            tInternet.add((t2 / 1000000) - (t1 / 1000000));
            t1 = nanoTime();
            GiuaScraper.isSiteWorking();
            System.out.print("#");
            t2 = nanoTime();
            tSite.add((t2 / 1000000) - (t1 / 1000000));
            // ///////////////////////////////////////////////////////////////////
            // NO CACHE
            // In questa prima parte vengono generate tutte le cose mentre nella seconda viene usata la cache
            t1 = nanoTime();
            startLogin(loggerManager);
            testNews(true);
            System.out.print("#");
            testVotes(true);
            System.out.print("#");
            testAlerts(true);
            System.out.print("#");
            testAgendaPage(true);
            System.out.print("#");
            testNewsletters(true);
            System.out.print("#");
            testLessons(true);
            System.out.print("#");
            testReportCard(true);
            System.out.print("#");
            testNotes(true);
            System.out.print("#");
            testAbsences(true);
            System.out.print("#");
            t2 = nanoTime();
            tPhase1.add((t2 / 1000000) - (t1 / 1000000));
            // //////////////////////////////////////////////////////////
            // CACHE
            t1 = nanoTime();
            System.out.print("|");
            testNews(false);
            System.out.print("#");
            testVotes(false);
            System.out.print("#");
            testAlerts(false);
            System.out.print("#");
            testAgendaPage(false);
            System.out.print("#");
            testNewsletters(false);
            System.out.print("#");
            testLessons(false);
            System.out.print("#");
            testReportCard(false);
            System.out.print("#");
            testNotes(false);
            System.out.print("#");
            testAbsences(false);
            System.out.print("#");
            t2 = nanoTime();
            tPhase2.add((t2 / 1000000) - (t1 / 1000000));
            t1 = nanoTime();
            String phpsessid = gS.getCookie();
            gS = new GiuaScraper(user, password, phpsessid, true, loggerManager);
            gS.login();
            System.out.print("|");
            testNews(true);
            System.out.print("#");
            testVotes(true);
            System.out.print("#");
            testAlerts(true);
            System.out.print("#");
            testAgendaPage(true);
            System.out.print("#");
            testNewsletters(true);
            System.out.print("#");
            testLessons(true);
            System.out.print("#");
            testReportCard(true);
            System.out.print("#");
            testNotes(true);
            System.out.print("#");
            testAbsences(true);
            System.out.print("#");
            t2 = nanoTime();
            tPhase3.add((t2 / 1000000) - (t1 / 1000000));
            tPhasesTot.add(tPhase1.get(i) + tPhase2.get(i) + tPhase3.get(i));
            System.out.println("]");
        } catch (Exception e) {
            System.out.println("/!\\]");
            System.out.println("Error: " + e.getMessage());
            tInternet.add(-1L);
            tSite.add(-1L);
            tPhase1.add(-1L);
            tPhase2.add(-1L);
            tPhase3.add(-1L);
            tPhasesTot.add(-1L);
            errors += 1;
        }
        i++;
    }
    i = 0;
    System.out.println("----  AVVIO SPEEDTEST PER JSON BUILDER ----");
    System.out.println("Legenda:  | Scrittura |");
    System.out.println("          [###########]");
    while (i < speedTestAmount) {
        long t1;
        long t2;
        System.out.println("Test " + (i + 1) + "/" + speedTestAmount);
        System.out.print("Progress: [");
        try {
            JsonBuilder jb = new JsonBuilder("test.json", gS);
            t1 = nanoTime();
            startLogin(loggerManager);
            jb.writeDocuments(gS.getDocumentsPage(false).getDocuments());
            System.out.print("#");
            jb.writeObservations(gS.getObservationsPage(false).getAllObservations());
            System.out.print("#");
            jb.writeAlerts(gS.getAlertsPage(false).getAllAlerts(1));
            System.out.print("#");
            jb.writeAgendaObjects(gS.getAgendaPage(false).getAllAgendaObjectsWithoutDetails(null));
            System.out.print("#");
            jb.writeNews(gS.getHomePage(false).getAllNewsFromHome());
            System.out.print("#");
            jb.writeMaintenance(gS.getMaintenanceInfo());
            System.out.print("#");
            jb.writeLessons(gS.getLessonsPage(false).getAllLessonsFromDate(new Date()));
            System.out.print("#");
            jb.writeAbsences(gS.getAbsencesPage(false).getAllAbsences());
            System.out.print("#");
            jb.writeDisciplinaryNotices(gS.getDisciplinaryNotesPage(false).getAllDisciplinaryNotices());
            System.out.print("#");
            jb.writeDocuments(gS.getDocumentsPage(false).getDocuments());
            System.out.print("#");
            t2 = nanoTime();
            tJsonBuilder.add((t2 / 1000000) - (t1 / 1000000));
            System.out.println("]");
        } catch (Exception e) {
            System.out.println("/!\\]");
            System.out.println("Error: " + e.getMessage());
            tInternet.add(-1L);
            tSite.add(-1L);
            tPhase1.add(-1L);
            tPhase2.add(-1L);
            tPhase3.add(-1L);
            tPhasesTot.add(-1L);
            errors += 1;
        }
        i++;
    }
    System.out.println("\n\n/---------------------LISTS----------------------------");
    Long tInternetAdd = 0L;
    System.out.print("|    Check Internet:                 ");
    for (Long value : tInternet) {
        tInternetAdd += value;
        System.out.print(value + "ms ; ");
    }
    System.out.print("\n");
    Long tSiteAdd = 0L;
    System.out.print("|    Check Site:                     ");
    for (Long value : tSite) {
        tSiteAdd += value;
        System.out.print(value + "ms ; ");
    }
    System.out.print("\n");
    Long tPhase1Add = 0L;
    System.out.print("|    Fase 1 (login iniziale):        ");
    for (Long value : tPhase1) {
        tPhase1Add += value;
        System.out.print(value + "ms ; ");
    }
    System.out.print("\n");
    Long tPhase2Add = 0L;
    System.out.print("|    Fase 2 (cache):                 ");
    for (Long value : tPhase2) {
        tPhase2Add += value;
        System.out.print(value + "ms ; ");
    }
    System.out.print("\n");
    Long tPhase3Add = 0L;
    System.out.print("|    Fase 3 (riutilizzo sessione):   ");
    for (Long value : tPhase3) {
        tPhase3Add += value;
        System.out.print(value + "ms ; ");
    }
    System.out.print("\n");
    Long tJsonBuilder3Add = 0L;
    System.out.print("|    Json Builder (scrittura):       ");
    for (Long value : tJsonBuilder) {
        tJsonBuilder3Add += value;
        System.out.print(value + "ms ; ");
    }
    System.out.print("\n");
    Long tPhasesTotAdd = 0L;
    System.out.print("|    Tempo Totale impiegato:         ");
    for (Long value : tPhasesTot) {
        tPhasesTotAdd += value;
        System.out.print(value + "ms ; ");
    }
    System.out.print("\n");
    System.out.println("\\--------------------------------------------------------");
    System.out.println("\n\n");
    System.out.println("/---------------------FINAL RESULTS " + speedTestAmount + " TESTS-----------------------");
    System.out.println("|    Check Internet:                 " + tInternetAdd / speedTestAmount + "ms");
    System.out.println("|    Check Site:                     " + tSiteAdd / speedTestAmount + "ms");
    System.out.println("|    Fase 1 (login iniziale):        " + tPhase1Add / speedTestAmount + "ms");
    System.out.println("|    Fase 2 (cache):                 " + tPhase2Add / speedTestAmount + "ms");
    System.out.println("|    Fase 3 (riutilizzo sessione):   " + tPhase3Add / speedTestAmount + "ms");
    System.out.println("|");
    System.out.println("|    Tempo impiegato mediamente:     " + tPhasesTotAdd / speedTestAmount + "ms");
    System.out.println("|    Tempo impiegato totalmente:     " + tPhasesTotAdd / 1000 + "s");
    System.out.println("\\--------------------------------------------------------");
    if (errors != 0) {
        System.err.println("Attenzione! " + errors + " su " + speedTestAmount + " hanno fallito con errore");
    }
}
Also used : JsonBuilder(com.giua.utils.JsonBuilder) GiuaScraper(com.giua.webscraper.GiuaScraper)

Example 5 with GiuaScraper

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

the class LoginActivity method login.

private void login() {
    if (GlobalVariables.gsThread == null || GlobalVariables.gsThread.isInterrupted())
        GlobalVariables.gsThread = new GiuaScraperThread();
    GlobalVariables.gsThread.addTask(() -> {
        try {
            if (isAddingAccount && AppData.getAllAccountUsernames(this).contains(etUsername.getText().toString().toLowerCase())) {
                runOnUiThread(() -> {
                    txtLayoutUsername.setError("Username già salvato");
                    etPassword.setText("");
                    pgProgressBar.setVisibility(View.INVISIBLE);
                    btnLogin.setVisibility(View.VISIBLE);
                });
                return;
            }
            loggerManager.d("Eseguo login...");
            GlobalVariables.gS = new GiuaScraper(etUsername.getText().toString(), etPassword.getText().toString(), true, SettingsData.getSettingBoolean(this, SettingKey.DEMO_MODE), new LoggerManager("GiuaScraper", this));
            GlobalVariables.gS.login();
            loggerManager.d("Devo ricordare le credenziali? " + chRememberCredentials.isChecked());
            if (GlobalVariables.gS.isSessionValid(GlobalVariables.gS.getCookie())) {
                if (chRememberCredentials.isChecked()) {
                    AccountData.setCredentials(this, etUsername.getText().toString(), etPassword.getText().toString(), GlobalVariables.gS.getCookie(), GlobalVariables.gS.getUserTypeString(), GlobalVariables.gS.getProfilePage(false).getProfileInformation()[2]);
                    AccountData.setSiteUrl(this, etUsername.getText().toString(), GlobalVariables.gS.getSiteUrl());
                    AppData.addAccountUsername(this, etUsername.getText().toString().toLowerCase());
                    AppData.saveActiveUsername(this, etUsername.getText().toString().toLowerCase());
                }
                startDrawerActivity();
            } else {
                loggerManager.e("Errore sconosciuto, login eseguito ma checkLogin ritorna false");
                setErrorMessage("Qualcosa e' andato storto!");
                this.runOnUiThread(() -> {
                    txtLayoutPassword.setError("Riprova");
                    etPassword.setText("");
                    btnLogin.setVisibility(View.VISIBLE);
                    pgProgressBar.setVisibility(View.INVISIBLE);
                });
            }
        } catch (GiuaScraperExceptions.SessionCookieEmpty e) {
            loggerManager.e("Errore: il sito dice: " + e.siteSays);
            if (e.siteSays.equals("Tipo di utente non ammesso: usare l'autenticazione tramite GSuite.")) {
                loggerManager.d("Rilevato credenziali account google");
                startStudentLoginActivity();
            } else {
                loggerManager.e("Errore: credenziali errate");
                setErrorMessage("Credenziali di login errate!");
                this.runOnUiThread(() -> {
                    etPassword.setText("");
                    txtLayoutPassword.setError("Password o Username errato");
                    pgProgressBar.setVisibility(View.INVISIBLE);
                    btnLogin.setVisibility(View.VISIBLE);
                });
            }
        } catch (GiuaScraperExceptions.UnableToLogin e) {
            loggerManager.e("Errore sconosciuto - " + e.getMessage());
            setErrorMessage("E' stato riscontrato qualche problema sconosciuto");
            this.runOnUiThread(() -> {
                etPassword.setText("");
                pgProgressBar.setVisibility(View.INVISIBLE);
                btnLogin.setVisibility(View.VISIBLE);
            });
        } catch (GiuaScraperExceptions.MaintenanceIsActiveException e) {
            loggerManager.e("Errore: Manutenzione attiva");
            try {
                Maintenance maintenance = GlobalVariables.gS.getMaintenanceInfo();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm", Locale.ITALY);
                runOnUiThread(() -> setErrorMessage("Il sito è in manutenzione sino alle " + simpleDateFormat.format(maintenance.end)));
            } catch (Exception e2) {
                runOnUiThread(() -> setErrorMessage(getString(R.string.maintenance_is_active_error)));
            }
            this.runOnUiThread(() -> {
                etPassword.setText("");
                txtLayoutPassword.setError("In manutenzione, riprova più tardi!");
                pgProgressBar.setVisibility(View.INVISIBLE);
                btnLogin.setVisibility(View.VISIBLE);
            });
        } catch (GiuaScraperExceptions.YourConnectionProblems e) {
            loggerManager.e("Errore di connessione dell'utente");
            setErrorMessage(getString(R.string.your_connection_error));
            this.runOnUiThread(() -> {
                etPassword.setText("");
                txtLayoutPassword.setError("C'è un problema con la tua rete");
                pgProgressBar.setVisibility(View.INVISIBLE);
                btnLogin.setVisibility(View.VISIBLE);
            });
        } catch (GiuaScraperExceptions.SiteConnectionProblems e) {
            loggerManager.e("Errore di connessione da parte del server");
            setErrorMessage(getString(R.string.site_connection_error));
            this.runOnUiThread(() -> {
                etPassword.setText("");
                txtLayoutPassword.setError("Errore di connesione al registro");
                pgProgressBar.setVisibility(View.INVISIBLE);
                btnLogin.setVisibility(View.VISIBLE);
            });
        }
    });
}
Also used : GiuaScraperThread(com.giua.app.GiuaScraperThread) LoggerManager(com.giua.app.LoggerManager) Maintenance(com.giua.objects.Maintenance) GiuaScraperExceptions(com.giua.webscraper.GiuaScraperExceptions) SimpleDateFormat(java.text.SimpleDateFormat) 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