Search in sources :

Example 1 with Report

use of de.geeksfactory.opacclient.reporting.Report in project opacclient by opacapp.

the class BiBer1992 method parseMediaList.

static List<LentItem> parseMediaList(AccountData res, Account account, Document doc, JSONObject data, ReportHandler reportHandler, JSONObject headers_lent) throws JSONException {
    List<LentItem> media = new ArrayList<>();
    if (doc == null) {
        return media;
    }
    if (doc.select("form[name=medkl] table").size() == 0) {
        return new ArrayList<LentItem>();
    }
    // parse result list
    Map<String, Integer> copymap = new HashMap<>();
    Map<String, Integer> colspanmap = new HashMap<>();
    Elements headerCells = doc.select("form[name=medkl] table tr:has(th)").last().select("th");
    JSONArray headersList = new JSONArray();
    JSONArray unknownHeaders = new JSONArray();
    int j = 0;
    for (Element headerCell : headerCells) {
        String header = headerCell.text();
        String colspan_str = headerCell.attr("colspan");
        int colspan = 1;
        if (!colspan_str.equals("")) {
            colspan = Integer.valueOf(colspan_str);
        }
        headersList.put(header);
        if (headers_lent.has(header)) {
            if (!headers_lent.isNull(header)) {
                copymap.put(headers_lent.getString(header), j);
                colspanmap.put(headers_lent.getString(header), colspan);
            }
        } else {
            unknownHeaders.put(header);
        }
        j += colspan;
    }
    if (unknownHeaders.length() > 0) {
        // send report
        JSONObject reportData = new JSONObject();
        reportData.put("headers", headersList);
        reportData.put("unknown_headers", unknownHeaders);
        Report report = new Report(account.getLibrary(), "biber1992", "unknown header - lent", DateTime.now(), reportData);
        reportHandler.sendReport(report);
        // fallback to JSON
        JSONObject accounttable = data.getJSONObject("accounttable");
        copymap = jsonToMap(accounttable);
    }
    Pattern expire = Pattern.compile("Ausweisg.ltigkeit: ([0-9.]+)");
    Pattern fees = Pattern.compile("([0-9,.]+) .");
    for (Element td : doc.select(".td01x09n")) {
        String text = td.text().trim();
        if (expire.matcher(text).matches()) {
            res.setValidUntil(expire.matcher(text).replaceAll("$1"));
        } else if (fees.matcher(text).matches()) {
            res.setPendingFees(text);
        }
    }
    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
    Elements rowElements = doc.select("form[name=medkl] table tr");
    // rows: skip 1st row -> title row
    for (int i = 1; i < rowElements.size(); i++) {
        Element tr = rowElements.get(i);
        if (tr.child(0).tagName().equals("th")) {
            continue;
        }
        LentItem item = new LentItem();
        Elements mediatypeImg = tr.select("td img");
        if (mediatypeImg.size() > 0) {
            item.setMediaType(getMediaTypeFromImageFilename(null, mediatypeImg.get(0).attr("src"), data));
        }
        Pattern itemIdPat = Pattern.compile("javascript:(?:smAcc|smMedk)\\('[a-z]+','[a-z]+','([A-Za-z0-9]+)'\\)");
        // columns: all elements of one media
        for (Map.Entry<String, Integer> entry : copymap.entrySet()) {
            String key = entry.getKey();
            int index = entry.getValue();
            if (tr.child(index).select("a").size() == 1) {
                Matcher matcher = itemIdPat.matcher(tr.child(index).select("a").attr("href"));
                if (matcher.find())
                    item.setId(matcher.group(1));
            }
            String value = tr.child(index).text().trim().replace("\u00A0", "");
            if (colspanmap.get(key) > 1) {
                for (int k = 1; k < colspanmap.get(key); k++) {
                    value = value + " " + tr.child(index + k).text().trim().replace("\u00A0", "");
                }
                value = value.trim();
            }
            switch(key) {
                case "author+title":
                    item.setTitle(findTitleAndAuthor(value)[0]);
                    item.setAuthor(findTitleAndAuthor(value)[1]);
                    continue;
                case "returndate":
                    try {
                        value = fmt.parseLocalDate(value).toString();
                    } catch (IllegalArgumentException e1) {
                        e1.printStackTrace();
                    }
                    break;
                case "renewals_number":
                case "status":
                    if (value != null && value.length() != 0) {
                        if (item.getStatus() == null) {
                            item.setStatus(value);
                        } else {
                            item.setStatus(item.getStatus() + ", " + value);
                        }
                    }
                    continue;
            }
            if (value != null && value.length() != 0)
                item.set(key, value);
        }
        if (tr.select("input[type=checkbox][value=YES]").size() > 0) {
            item.setProlongData(tr.select("input[type=checkbox][value=YES]").attr("name"));
        }
        media.add(item);
    }
    return media;
}
Also used : Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) Report(de.geeksfactory.opacclient.reporting.Report) Matcher(java.util.regex.Matcher) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) Elements(org.jsoup.select.Elements) JSONObject(org.json.JSONObject) LentItem(de.geeksfactory.opacclient.objects.LentItem) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with Report

use of de.geeksfactory.opacclient.reporting.Report in project opacclient by opacapp.

the class Bibliotheca method parse_account.

public static AccountData parse_account(Account acc, Document doc, JSONObject data, ReportHandler reportHandler, JSONObject headers_lent, JSONObject headers_reservations) throws JSONException, NotReachableException {
    if (doc.select(".kontozeile_center table").size() == 0) {
        throw new NotReachableException();
    }
    Map<String, Integer> copymap = new HashMap<>();
    Elements headerCells = doc.select(".kontozeile_center table").get(0).select("tr.exemplarmenubar").get(0).children();
    JSONArray headersList = new JSONArray();
    JSONArray unknownHeaders = new JSONArray();
    int i = 0;
    for (Element headerCell : headerCells) {
        String header = headerCell.text();
        headersList.put(header);
        if (headers_lent.has(header)) {
            if (!headers_lent.isNull(header))
                copymap.put(headers_lent.getString(header), i);
        } else {
            unknownHeaders.put(header);
        }
        i++;
    }
    if (unknownHeaders.length() > 0) {
        // send report
        JSONObject reportData = new JSONObject();
        reportData.put("headers", headersList);
        reportData.put("unknown_headers", unknownHeaders);
        Report report = new Report(acc.getLibrary(), "bibliotheca", "unknown header - lent", DateTime.now(), reportData);
        reportHandler.sendReport(report);
        // fallback to JSON
        JSONObject accounttable = data.getJSONObject("accounttable");
        copymap = jsonToMap(accounttable);
    }
    List<LentItem> media = new ArrayList<>();
    Elements exemplartrs = doc.select(".kontozeile_center table").get(0).select("tr.tabKonto");
    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
    DateTimeFormatter fmt2 = DateTimeFormat.forPattern("d/M/yyyy").withLocale(Locale.GERMAN);
    for (Element tr : exemplartrs) {
        LentItem item = new LentItem();
        for (Entry<String, Integer> entry : copymap.entrySet()) {
            String key = entry.getKey();
            int index = entry.getValue();
            if (key.equals("prolongurl")) {
                if (tr.child(index).children().size() > 0) {
                    item.setProlongData(tr.child(index).child(0).attr("href"));
                    item.setRenewable(!tr.child(index).child(0).attr("href").contains("vermsg"));
                }
            } else if (key.equals("returndate")) {
                try {
                    item.setDeadline(fmt.parseLocalDate(tr.child(index).text()));
                } catch (IllegalArgumentException e1) {
                    try {
                        item.setDeadline(fmt2.parseLocalDate(tr.child(index).text()));
                    } catch (IllegalArgumentException e2) {
                        e2.printStackTrace();
                    }
                }
            } else {
                item.set(key, tr.child(index).text());
            }
        }
        media.add(item);
    }
    copymap = new HashMap<>();
    headerCells = doc.select(".kontozeile_center table").get(1).select("tr.exemplarmenubar").get(0).children();
    headersList = new JSONArray();
    unknownHeaders = new JSONArray();
    i = 0;
    for (Element headerCell : headerCells) {
        String header = headerCell.text();
        headersList.put(header);
        if (headers_reservations.has(header)) {
            if (!headers_reservations.isNull(header)) {
                copymap.put(headers_reservations.getString(header), i);
            }
        } else {
            unknownHeaders.put(header);
        }
        i++;
    }
    if (unknownHeaders.length() > 0) {
        // send report
        JSONObject reportData = new JSONObject();
        reportData.put("headers", headersList);
        reportData.put("unknown_headers", unknownHeaders);
        Report report = new Report(acc.getLibrary(), "bibliotheca", "unknown header - reservations", DateTime.now(), reportData);
        reportHandler.sendReport(report);
        // fallback to JSON
        JSONObject reservationtable = data.getJSONObject("reservationtable");
        copymap = jsonToMap(reservationtable);
    }
    List<ReservedItem> reservations = new ArrayList<>();
    exemplartrs = doc.select(".kontozeile_center table").get(1).select("tr.tabKonto");
    for (Element tr : exemplartrs) {
        ReservedItem item = new ReservedItem();
        for (Entry<String, Integer> entry : copymap.entrySet()) {
            String key = entry.getKey();
            int index = entry.getValue();
            if (key.equals("cancelurl")) {
                if (tr.child(index).children().size() > 0) {
                    item.setCancelData(tr.child(index).child(0).attr("href"));
                }
            } else if (key.equals("availability")) {
                try {
                    item.setReadyDate(fmt.parseLocalDate(tr.child(index).text()));
                } catch (IllegalArgumentException e1) {
                    try {
                        item.setReadyDate(fmt2.parseLocalDate(tr.child(index).text()));
                    } catch (IllegalArgumentException e2) {
                        e2.printStackTrace();
                    }
                }
            } else if (key.equals("expirationdate")) {
                try {
                    item.setExpirationDate(fmt.parseLocalDate(tr.child(index).text()));
                } catch (IllegalArgumentException e1) {
                    try {
                        item.setExpirationDate(fmt2.parseLocalDate(tr.child(index).text()));
                    } catch (IllegalArgumentException e2) {
                        item.setStatus(tr.child(index).text());
                    }
                }
            } else {
                item.set(key, tr.child(index).text());
            }
        }
        reservations.add(item);
    }
    AccountData res = new AccountData(acc.getId());
    for (Element row : doc.select(".kontozeile_center, div[align=center]")) {
        String text = row.text().trim();
        if (text.matches(".*Ausstehende Geb.+hren:[^0-9]+([0-9.,]+)[^0-9€A-Z]*(€|EUR|CHF|Fr.).*")) {
            text = text.replaceAll(".*Ausstehende Geb.+hren:[^0-9]+([0-9.," + "]+)[^0-9€A-Z]*(€|EUR|CHF|Fr.).*", "$1 $2");
            res.setPendingFees(text);
        }
        if (text.matches("Ihr Ausweis ist g.ltig bis:.*")) {
            text = text.replaceAll("Ihr Ausweis ist g.ltig bis:[^A-Za-z0-9]+", "");
            res.setValidUntil(text);
        } else if (text.matches("Ausweis g.ltig bis:.*")) {
            text = text.replaceAll("Ausweis g.ltig bis:[^A-Za-z0-9]+", "");
            res.setValidUntil(text);
        }
    }
    res.setLent(media);
    res.setReservations(reservations);
    return res;
}
Also used : NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) HashMap(java.util.HashMap) Report(de.geeksfactory.opacclient.reporting.Report) Element(org.jsoup.nodes.Element) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) Elements(org.jsoup.select.Elements) JSONObject(org.json.JSONObject) AccountData(de.geeksfactory.opacclient.objects.AccountData) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) LentItem(de.geeksfactory.opacclient.objects.LentItem) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 3 with Report

use of de.geeksfactory.opacclient.reporting.Report in project opacclient by opacapp.

the class BiBer1992 method parseResList.

static List<ReservedItem> parseResList(Account account, Document doc, JSONObject data, ReportHandler reportHandler, JSONObject headers_reservations) throws JSONException {
    List<ReservedItem> reservations = new ArrayList<>();
    if (doc == null) {
        // error message as html result
        return reservations;
    }
    if (doc.select("form[name=vorml] table").size() == 0) {
        return new ArrayList<ReservedItem>();
    }
    // parse result list
    Map<String, Integer> copymap = new HashMap<>();
    Elements headerCells = doc.select("form[name=vorml] table tr:has(th)").last().select("th");
    JSONArray headersList = new JSONArray();
    JSONArray unknownHeaders = new JSONArray();
    int j = 0;
    for (Element headerCell : headerCells) {
        String header = headerCell.text();
        headersList.put(header);
        if (headers_reservations.has(header)) {
            if (!headers_reservations.isNull(header)) {
                copymap.put(headers_reservations.getString(header), j);
            }
        } else {
            unknownHeaders.put(header);
        }
        String colspan = headerCell.attr("colspan");
        j += !colspan.equals("") ? Integer.valueOf(colspan) : 1;
    }
    if (unknownHeaders.length() > 0) {
        // send report
        JSONObject reportData = new JSONObject();
        reportData.put("headers", headersList);
        reportData.put("unknown_headers", unknownHeaders);
        Report report = new Report(account.getLibrary(), "biber1992", "unknown header - reservations", DateTime.now(), reportData);
        reportHandler.sendReport(report);
        // fallback to JSON
        JSONObject reservationtable;
        if (data.has("reservationtable")) {
            reservationtable = data.getJSONObject("reservationtable");
        } else {
            // reservations not specifically supported, let's just try it
            // with default values but fail silently
            reservationtable = new JSONObject();
            reservationtable.put("author", 3);
            reservationtable.put("availability", 6);
            reservationtable.put("branch", -1);
            reservationtable.put("cancelurl", -1);
            reservationtable.put("expirationdate", 5);
            reservationtable.put("title", 3);
        }
        copymap = jsonToMap(reservationtable);
    }
    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
    Elements rowElements = doc.select("form[name=vorml] table tr");
    // rows: skip 1st row -> title row
    for (int i = 1; i < rowElements.size(); i++) {
        Element tr = rowElements.get(i);
        if (tr.child(0).tagName().equals("th")) {
            continue;
        }
        ReservedItem item = new ReservedItem();
        item.setCancelData(tr.select("input[type=checkbox]").attr("name"));
        Elements mediatypeImg = tr.select("td img");
        if (mediatypeImg.size() > 0) {
            item.setMediaType(getMediaTypeFromImageFilename(null, mediatypeImg.get(0).attr("src"), data));
        }
        // columns: all elements of one media
        for (Map.Entry<String, Integer> entry : copymap.entrySet()) {
            String key = entry.getKey();
            int index = entry.getValue();
            String value = tr.child(index).text().trim();
            switch(key) {
                case "author+title":
                    item.setTitle(findTitleAndAuthor(value)[0]);
                    item.setAuthor(findTitleAndAuthor(value)[1]);
                    continue;
                case "availability":
                    try {
                        value = fmt.parseLocalDate(value).toString();
                    } catch (IllegalArgumentException e1) {
                        key = "status";
                    }
                    break;
                case "expirationdate":
                    try {
                        value = fmt.parseLocalDate(value).toString();
                    } catch (IllegalArgumentException e1) {
                        key = "status";
                    }
                    break;
            }
            if (value != null && value.length() != 0) {
                item.set(key, value);
            }
        }
        reservations.add(item);
    }
    return reservations;
}
Also used : HashMap(java.util.HashMap) Report(de.geeksfactory.opacclient.reporting.Report) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) Elements(org.jsoup.select.Elements) JSONObject(org.json.JSONObject) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Report (de.geeksfactory.opacclient.reporting.Report)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)3 JSONArray (org.json.JSONArray)3 JSONObject (org.json.JSONObject)3 Element (org.jsoup.nodes.Element)3 Elements (org.jsoup.select.Elements)3 LentItem (de.geeksfactory.opacclient.objects.LentItem)2 ReservedItem (de.geeksfactory.opacclient.objects.ReservedItem)2 Map (java.util.Map)2 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)1 AccountData (de.geeksfactory.opacclient.objects.AccountData)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1