Search in sources :

Example 16 with Alert

use of com.giua.objects.Alert in project Giua-Webscraper by Giua-app.

the class AlertsPage method getAllAlertsWithFilters.

/**
 * Ritorna una lista di {@code Alert} senza {@code details} e {@code creator}.
 * Per generare i dettagli {@link Alert#getDetails(GiuaScraper)}
 * Una volta applicato il filtro {@link #getAllAlerts(int)} ritornerà i risultati con il filtro.
 * Utilizzare {@link #resetFiltersAndRefreshPage()} per resettare il filtro
 * ATTENZIONE: Utilizza una richiesta HTTP
 *
 * @param onlyNotRead true per ottenere soltanto gli avvisi non letti, false altrimenti
 * @param text        Il testo da filtrare
 * @return Lista di Alert
 * @throws IndexOutOfBoundsException Se {@code page} è minore o uguale a 0.
 */
public List<Alert> getAllAlertsWithFilters(boolean onlyNotRead, String text) throws IndexOutOfBoundsException {
    if (gS.isDemoMode()) {
        return GiuaScraperDemo.getAllAlerts();
    }
    List<Alert> allAlerts = new Vector<>();
    try {
        Document newDoc = gS.getSession().newRequest().url(gS.getSiteUrl() + "/" + UrlPaths.ALERTS_PAGE).data("bacheca_avvisi_genitori[visualizza]", onlyNotRead ? "D" : "T").data("bacheca_avvisi_genitori[oggetto]", text).data("bacheca_avvisi_genitori[submit]", "").data("bacheca_avvisi_genitori[_token]", getFilterToken()).post();
        Elements allAlertsHTML = newDoc.getElementsByTag("tbody");
        if (allAlertsHTML.isEmpty())
            return allAlerts;
        allAlertsHTML = allAlertsHTML.get(0).children();
        for (Element alertHTML : allAlertsHTML) {
            allAlerts.add(new Alert(alertHTML.child(0).text(), alertHTML.child(1).text(), alertHTML.child(2).text(), alertHTML.child(3).text(), alertHTML.child(4).child(0).attr("data-href"), 1));
        }
    } catch (IOException e) {
        if (!GiuaScraper.isSiteWorking()) {
            throw new GiuaScraperExceptions.SiteConnectionProblems("Can't get page because the website is down, retry later", e);
        }
        e.printStackTrace();
    }
    return allAlerts;
}
Also used : Element(org.jsoup.nodes.Element) Alert(com.giua.objects.Alert) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Vector(java.util.Vector) GiuaScraperExceptions(com.giua.webscraper.GiuaScraperExceptions)

Example 17 with Alert

use of com.giua.objects.Alert in project Giua-Webscraper by Giua-app.

the class AlertsPage method getAlertsToNotify.

/**
 * Ottieni la differenza degli avvisi tra gli {@code oldAlerts} e gli avvisi nuovi. Serve SOLO per notificare
 * i nuovi compiti e le nuove verifiche
 * @param oldAlerts Una lista degli avvisi già notificati
 * @return Una lista di avvisi nuovi da notificare
 */
/*
    public List<Alert> getNewAlertsFromOldAlerts(List<Alert> oldAlerts) {
        if (!oldAlerts.isEmpty()) {
            lm.d("Alerts vecchie:");
            for (Alert alert : oldAlerts) {
                lm.d(alert.toString());
            }
        }

        List<Alert> newAlerts = getAllAlertsWithFilters(false, "per la materia");
        List<Alert> temp = new Vector<>();

        lm.d("Ottenuto " + newAlerts.size() + " alerts nuove con filtro");
        for (Alert alert : newAlerts) {
            lm.d(alert.toString());
        }

        lm.d("");
        Date date;
        /**
         * codice di debug per inserire una data qualsiasi
         * SimpleDateFormat dF = new SimpleDateFormat("dd/MM/yyyy");
        try {
            date = dF.parse("20/11/2021");
        } catch (ParseException e) {
            e.printStackTrace();
            return;
        }*/
/*
        date=new Date();    //per inserire una data manualmente col codice qui sopra, commentare questa riga

        
        for(int i=0; i<newAlerts.size();i++){
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            Date alertDate=new Date();
            try {
                alertDate = dateFormat.parse(newAlerts.get(i).date);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            if(date.before(alertDate) ||date.equals(alertDate)){ //TODO: controllare se notifica anche i compiti assegnati il giorno odierno
                lm.d("Aggiungo " + newAlerts.get(i).object+ " del giorno "+ newAlerts.get(i).date);
                temp.add(newAlerts.get(i));
            }
        }
        lm.d("");
        newAlerts = temp;
        temp=new Vector<>();
        if(!oldAlerts.isEmpty()){
            int index=0;
            while (newAlerts.get(index).object==oldAlerts.get(index).object){
                index++;
            }
            int l=0;
            for(int i = index; i < newAlerts.size(); i++){
                lm.d("Controllo alert "+ newAlerts.get(i).object+" con "+ oldAlerts.get(i-l).object);
                if(newAlerts.get(i).object!=oldAlerts.get(i-l).object){
                    lm.d("Aggiungo "+ newAlerts.get(i).object);
                    temp.add(newAlerts.get(i));
                    l++;
                }
            }
            newAlerts = temp;
            oldAlerts=newAlerts;
            lm.d("new alerts finali:");
            for (Alert alert : newAlerts) {
                lm.d(alert.toString());
            }
        }
        return newAlerts;
    }*/
/**
 * Ottieni la differenza degli avvisi tra gli {@code oldAlerts} e gli avvisi nuovi. Serve SOLO per notificare
 * i nuovi compiti e le nuove verifiche
 *
 * @param oldAlerts Una lista degli avvisi già notificati
 * @return Una lista di avvisi nuovi da notificare
 */
public List<Alert> getAlertsToNotify(List<Alert> oldAlerts) {
    List<Alert> newAlerts = getAllAlertsWithFilters(false, "per la materia");
    List<Alert> returnDifference = new Vector<>(newAlerts);
    if (oldAlerts.size() == 0)
        // Ritorno newAlerts perché è la loro differenza
        return newAlerts;
    for (Alert newAlert : newAlerts) {
        for (Alert oldAlert : oldAlerts) {
            if (newAlert.toStringWithoutStatus().equals(oldAlert.toStringWithoutStatus()))
                returnDifference.remove(newAlert);
        }
    }
    return returnDifference;
}
Also used : Alert(com.giua.objects.Alert) Vector(java.util.Vector)

Example 18 with Alert

use of com.giua.objects.Alert in project Giua-App by Giua-app.

the class DBController method readAlerts.

public List<Alert> readAlerts() {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM " + ALERTS_TABLE + " ORDER BY " + DBAlert.ALERT_ID + " DESC", null);
    List<Alert> alerts = new Vector<>();
    if (cursor.moveToFirst()) {
        do {
            // 0 = false, 1 = true
            boolean isDetailed = cursor.getInt(DBAlert.IS_DETAILED_COL.ordinal()) != 0;
            if (isDetailed) {
                List<String> attachmentUrls = null;
                try {
                    attachmentUrls = Arrays.asList(cursor.getString(DBAlert.ATTACHMENT_URLS_COL.ordinal()).split(";"));
                } catch (NullPointerException ignored) {
                }
                alerts.add(new Alert(cursor.getString(DBAlert.STATUS_COL.ordinal()), cursor.getString(DBAlert.DATE_COL.ordinal()), cursor.getString(DBAlert.RECEIVERS_COL.ordinal()), cursor.getString(DBAlert.OBJECT_COL.ordinal()), cursor.getString(DBAlert.DETAILS_URL_COL.ordinal()), cursor.getInt(DBAlert.PAGE_COL.ordinal()), attachmentUrls, cursor.getString(DBAlert.DETAILS_COL.ordinal()), cursor.getString(DBAlert.CREATOR_COL.ordinal()), cursor.getString(DBAlert.TYPE_COL.ordinal())));
            } else {
                alerts.add(new Alert(cursor.getString(DBAlert.DATE_COL.ordinal()), cursor.getString(DBAlert.DATE_COL.ordinal()), cursor.getString(DBAlert.RECEIVERS_COL.ordinal()), cursor.getString(DBAlert.OBJECT_COL.ordinal()), cursor.getString(DBAlert.DETAILS_URL_COL.ordinal()), cursor.getInt(DBAlert.PAGE_COL.ordinal())));
            }
        } while (cursor.moveToNext());
    // muovi il cursore nella prossima riga
    }
    cursor.close();
    return alerts;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Alert(com.giua.objects.Alert) Cursor(android.database.Cursor) Vector(java.util.Vector)

Aggregations

Alert (com.giua.objects.Alert)18 Vector (java.util.Vector)11 Cursor (android.database.Cursor)5 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)4 LinearLayout (android.widget.LinearLayout)3 IOException (java.io.IOException)3 TextView (android.widget.TextView)2 GiuaScraperExceptions (com.giua.webscraper.GiuaScraperExceptions)2 URISyntaxException (java.net.URISyntaxException)2 ParseException (java.text.ParseException)2 Document (org.jsoup.nodes.Document)2 Element (org.jsoup.nodes.Element)2 Elements (org.jsoup.select.Elements)2 Notification (android.app.Notification)1 AlertsPage (com.giua.pages.AlertsPage)1 JsonBuilder (com.giua.utils.JsonBuilder)1 JsonParser (com.giua.utils.JsonParser)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1