Search in sources :

Example 31 with ReservedItem

use of de.geeksfactory.opacclient.objects.ReservedItem 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 32 with ReservedItem

use of de.geeksfactory.opacclient.objects.ReservedItem in project opacclient by opacapp.

the class Adis method parseReservationList.

static boolean parseReservationList(Document doc, String[] rlink, boolean split_title_author, List<ReservedItem> res, DateTimeFormatter fmt, StringProvider stringProvider) {
    boolean error = false;
    boolean interlib = doc.html().contains("Ihre Fernleih-Bestellung");
    boolean stacks = doc.html().contains("aus dem Magazin");
    boolean provision = doc.html().contains("Ihre Bereitstellung");
    Map<String, Integer> colmap = new HashMap<>();
    colmap.put("title", 2);
    colmap.put("branch", 1);
    colmap.put("expirationdate", 0);
    int i = 0;
    for (Element th : doc.select(".rTable_div thead tr th")) {
        if (th.text().contains("Bis")) {
            colmap.put("expirationdate", i);
        }
        if (th.text().contains("Ausgabeort")) {
            colmap.put("branch", i);
        }
        if (th.text().contains("Titel")) {
            colmap.put("title", i);
        }
        if (th.text().contains("Hinweis")) {
            colmap.put("status", i);
        }
        i++;
    }
    for (Element tr : doc.select(".rTable_div tbody tr")) {
        if (tr.children().size() >= colmap.size()) {
            ReservedItem item = new ReservedItem();
            String text = tr.child(colmap.get("title")).html();
            text = Jsoup.parse(text.replaceAll("(?i)<br[^>]*>", ";")).text();
            if (split_title_author) {
                String[] split = text.split("[:/;\n]");
                item.setTitle(split[0].replaceFirst("([^:;\n]+)[:;\n](.*)$", "$1").trim());
                if (split.length > 1) {
                    item.setAuthor(split[1].replaceFirst("([^:;\n]+)[:;\n](.*)$", "$1").trim());
                }
            } else {
                item.setTitle(text);
            }
            String branch = tr.child(colmap.get("branch")).text().trim();
            if (interlib) {
                branch = stringProvider.getFormattedString(StringProvider.INTERLIB_BRANCH, branch);
            } else if (stacks) {
                branch = stringProvider.getFormattedString(StringProvider.STACKS_BRANCH, branch);
            } else if (provision) {
                branch = stringProvider.getFormattedString(StringProvider.PROVISION_BRANCH, branch);
            }
            item.setBranch(branch);
            if (colmap.containsKey("status")) {
                String status = tr.child(colmap.get("status")).text().trim();
                if (!"".equals(status))
                    item.setStatus(status);
            }
            if (rlink[0].contains("Abholbereit") || rlink[0].contains("Bereitstellung")) {
                // Abholbereite Bestellungen
                item.setStatus("bereit");
                if (tr.child(0).text().trim().length() >= 10) {
                    item.setExpirationDate(fmt.parseLocalDate(tr.child(colmap.get("expirationdate")).text().trim().substring(0, 10)));
                }
            } else {
                // Nicht abholbereite
                if (tr.select("input[type=checkbox]").size() > 0 && (rlink[1].toUpperCase(Locale.GERMAN).contains("SP=SZM") || rlink[1].toUpperCase(Locale.GERMAN).contains("SP=SZW") || rlink[1].toUpperCase(Locale.GERMAN).contains("SP=SZB"))) {
                    item.setCancelData(tr.select("input[type=checkbox]").attr("name") + "|" + rlink[1]);
                }
            }
            res.add(item);
        } else {
            // This is a strange bug where sometimes there is only three
            // columns
            error = true;
        }
    }
    return error;
}
Also used : HashMap(java.util.HashMap) Element(org.jsoup.nodes.Element) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem)

Example 33 with ReservedItem

use of de.geeksfactory.opacclient.objects.ReservedItem 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)

Example 34 with ReservedItem

use of de.geeksfactory.opacclient.objects.ReservedItem in project opacclient by opacapp.

the class IOpac method account.

@Override
public AccountData account(Account account) throws IOException, JSONException, OpacErrorException {
    if (!initialised) {
        start();
    }
    Document doc = getAccountPage(account);
    AccountData res = new AccountData(account.getId());
    List<LentItem> media = new ArrayList<>();
    List<ReservedItem> reserved = new ArrayList<>();
    parseMediaList(media, doc, data);
    parseResList(reserved, doc, data);
    res.setLent(media);
    res.setReservations(reserved);
    if (doc.select("h4:contains(Kontostand)").size() > 0) {
        Element h4 = doc.select("h4:contains(Kontostand)").first();
        Pattern regex = Pattern.compile("Kontostand (-?\\d+\\.\\d\\d EUR)");
        Matcher matcher = regex.matcher(h4.text());
        if (matcher.find())
            res.setPendingFees(matcher.group(1));
    }
    if (doc.select("h4:contains(Ausweis g)").size() > 0) {
        Element h4 = doc.select("h4:contains(Ausweis g)").first();
        Pattern regex = Pattern.compile("Ausweis g.+ltig bis\\s*.\\s*(\\d\\d.\\d\\d.\\d\\d\\d\\d)");
        Matcher matcher = regex.matcher(h4.text());
        if (matcher.find())
            res.setValidUntil(matcher.group(1));
    }
    if (doc.select(".ReaderAccount_expiredID").size() > 0) {
        res.setWarning(doc.select(".ReaderAccount_expiredID").text());
    }
    if (media.isEmpty() && reserved.isEmpty()) {
        if (doc.select("h1").size() > 0) {
            // noinspection StatementWithEmptyBody
            if (doc.select("h4").text().trim().contains("keine ausgeliehenen Medien")) {
            // There is no lent media, but the server is working
            // correctly
            } else if (doc.select("h1").text().trim().contains("RUNTIME ERROR")) {
                // Server Error
                throw new NotReachableException("IOPAC RUNTIME ERROR");
            } else {
                throw new OpacErrorException(stringProvider.getFormattedString(StringProvider.UNKNOWN_ERROR_ACCOUNT_WITH_DESCRIPTION, doc.select("h1").text().trim()));
            }
        } else {
            throw new OpacErrorException(stringProvider.getString(StringProvider.UNKNOWN_ERROR_ACCOUNT));
        }
    }
    return res;
}
Also used : Pattern(java.util.regex.Pattern) AccountData(de.geeksfactory.opacclient.objects.AccountData) Matcher(java.util.regex.Matcher) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) LentItem(de.geeksfactory.opacclient.objects.LentItem) Document(org.jsoup.nodes.Document)

Aggregations

ReservedItem (de.geeksfactory.opacclient.objects.ReservedItem)34 ArrayList (java.util.ArrayList)20 Element (org.jsoup.nodes.Element)17 LentItem (de.geeksfactory.opacclient.objects.LentItem)16 AccountData (de.geeksfactory.opacclient.objects.AccountData)13 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)8 Document (org.jsoup.nodes.Document)8 Elements (org.jsoup.select.Elements)8 JSONObject (org.json.JSONObject)7 Test (org.junit.Test)7 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)6 DummyStringProvider (de.geeksfactory.opacclient.i18n.DummyStringProvider)4 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 Pattern (java.util.regex.Pattern)3 JSONException (org.json.JSONException)3 Intent (android.content.Intent)2 Account (de.geeksfactory.opacclient.objects.Account)2 Report (de.geeksfactory.opacclient.reporting.Report)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2