Search in sources :

Example 41 with LentItem

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

the class Adis method parseMediaList.

static void parseMediaList(Document adoc, String alink, List<LentItem> lent, boolean split_title_author) {
    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
    for (Element tr : adoc.select(".rTable_div tbody tr")) {
        LentItem item = new LentItem();
        String text = Jsoup.parse(tr.child(3).html().replaceAll("(?i)<br[^>]*>", "#")).text();
        if (text.contains(" / ")) {
            // Format "Titel / Autor #Sig#Nr", z.B. normale Ausleihe in Berlin
            String[] split = text.split("[/#\n]");
            String title = split[0];
            // Is always the last one...
            String id = split[split.length - 1];
            item.setId(id);
            if (split_title_author) {
                title = title.replaceFirst("([^:;\n]+)[:;\n](.*)$", "$1");
            }
            item.setTitle(title.trim());
            if (split.length > 1) {
                item.setAuthor(split[1].replaceFirst("([^:;\n]+)[:;\n](.*)$", "$1").trim());
            }
        } else {
            // Format "Autor: Titel - Verlag - ISBN:... #Nummer", z.B. Fernleihe in Berlin
            String[] split = text.split("#");
            String[] aut_tit = split[0].split(": ");
            item.setAuthor(aut_tit[0].replaceFirst("([^:;\n]+)[:;\n](.*)$", "$1").trim());
            if (aut_tit.length > 1) {
                item.setTitle(aut_tit[1].replaceFirst("([^:;\n]+)[:;\n](.*)$", "$1").trim());
            }
            // Is always the last one...
            String id = split[split.length - 1];
            item.setId(id);
        }
        String date = tr.child(1).text().trim();
        if (date.contains("-")) {
            // Nürnberg: "29.03.2016 - 26.04.2016"
            // for beginning and end date in one field
            date = date.split("-")[1].trim();
        }
        try {
            item.setDeadline(fmt.parseLocalDate(date));
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        item.setHomeBranch(tr.child(2).text().trim());
        if (tr.select("input[type=checkbox]").hasAttr("disabled")) {
            item.setRenewable(false);
        } else {
            try {
                item.setRenewable(!tr.child(4).text().matches(".*nicht verl.+ngerbar.*") && !tr.child(4).text().matches(".*Verl.+ngerung nicht m.+glich.*"));
            } catch (Exception e) {
            }
            item.setProlongData(tr.select("input[type=checkbox]").attr("name") + "|" + alink);
        }
        lent.add(item);
    }
}
Also used : Element(org.jsoup.nodes.Element) LentItem(de.geeksfactory.opacclient.objects.LentItem) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) JSONException(org.json.JSONException) SSLSecurityException(de.geeksfactory.opacclient.networking.SSLSecurityException) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 42 with LentItem

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

the class IOpac method parseMediaList.

static void parseMediaList(List<LentItem> media, Document doc, JSONObject data) {
    if (doc.select("a[name=AUS]").size() == 0)
        return;
    Elements copytrs = doc.select("a[name=AUS] ~ table, a[name=AUS] ~ form table").first().select("tr");
    doc.setBaseUri(data.optString("baseurl"));
    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
    int trs = copytrs.size();
    if (trs < 2) {
        return;
    }
    assert (trs > 0);
    JSONObject copymap = new JSONObject();
    try {
        if (data.has("accounttable")) {
            copymap = data.getJSONObject("accounttable");
        }
    } catch (JSONException e) {
    }
    Pattern datePattern = Pattern.compile("\\d{2}\\.\\d{2}\\.\\d{4}");
    Pattern reservedPattern = Pattern.compile("\\d+ x reserv.");
    for (int i = 1; i < trs; i++) {
        Element tr = copytrs.get(i);
        LentItem item = new LentItem();
        if (copymap.optInt("title", 0) >= 0) {
            item.setTitle(tr.child(copymap.optInt("title", 0)).text().trim().replace("\u00a0", ""));
        }
        if (copymap.optInt("author", 1) >= 0) {
            item.setAuthor(tr.child(copymap.optInt("author", 1)).text().trim().replace("\u00a0", ""));
        }
        if (copymap.optInt("format", 2) >= 0) {
            item.setFormat(tr.child(copymap.optInt("format", 2)).text().trim().replace("\u00a0", ""));
        }
        int prolongCount = 0;
        if (copymap.optInt("prolongcount", 3) >= 0) {
            prolongCount = Integer.parseInt(tr.child(copymap.optInt("prolongcount", 3)).text().trim().replace("\u00a0", ""));
            item.setStatus(String.valueOf(prolongCount) + "x verl.");
        }
        if (data.optInt("maxprolongcount", -1) != -1) {
            item.setRenewable(prolongCount < data.optInt("maxprolongcount", -1));
        }
        if (copymap.optInt("returndate", 4) >= 0) {
            String value = tr.child(copymap.optInt("returndate", 4)).text().trim().replace("\u00a0", "");
            Matcher matcher = datePattern.matcher(value);
            if (matcher.find()) {
                try {
                    item.setDeadline(fmt.parseLocalDate(matcher.group()));
                } catch (IllegalArgumentException e1) {
                    e1.printStackTrace();
                }
            }
            matcher = reservedPattern.matcher(value);
            if (matcher.find()) {
                if (item.getStatus() != null) {
                    item.setStatus(item.getStatus() + ", " + matcher.group());
                } else {
                    item.setStatus(matcher.group());
                }
            }
        }
        if (copymap.optInt("prolongurl", 5) >= 0) {
            if (tr.children().size() > copymap.optInt("prolongurl", 5)) {
                Element cell = tr.child(copymap.optInt("prolongurl", 5));
                if (cell.select("input[name=MedNrVerlAll]").size() > 0) {
                    // new iOPAC Version 1.45 - checkboxes to prolong multiple items
                    // internal convention: We add "NEW" to the media ID to show that we have
                    // the new iOPAC version
                    Element input = cell.select("input[name=MedNrVerlAll]").first();
                    String value = input.val();
                    item.setProlongData("NEW" + value);
                    item.setId(value.split(";")[0]);
                    if (input.hasAttr("disabled"))
                        item.setRenewable(false);
                } else {
                    // previous versions - link for prolonging on every medium
                    String link = cell.select("a").attr("href");
                    item.setProlongData(link);
                    // find media number with regex
                    Pattern pattern = Pattern.compile("mednr=([^&]*)&");
                    Matcher matcher = pattern.matcher(link);
                    if (matcher.find() && matcher.group() != null)
                        item.setId(matcher.group(1));
                }
            }
        }
        media.add(item);
    }
    assert (media.size() == trs - 1);
}
Also used : Pattern(java.util.regex.Pattern) JSONObject(org.json.JSONObject) Matcher(java.util.regex.Matcher) Element(org.jsoup.nodes.Element) JSONException(org.json.JSONException) LentItem(de.geeksfactory.opacclient.objects.LentItem) Elements(org.jsoup.select.Elements) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 43 with LentItem

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

LentItem (de.geeksfactory.opacclient.objects.LentItem)43 ArrayList (java.util.ArrayList)26 ReservedItem (de.geeksfactory.opacclient.objects.ReservedItem)16 Element (org.jsoup.nodes.Element)16 AccountData (de.geeksfactory.opacclient.objects.AccountData)14 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)12 Test (org.junit.Test)10 JSONObject (org.json.JSONObject)9 Document (org.jsoup.nodes.Document)8 Elements (org.jsoup.select.Elements)8 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)7 Pattern (java.util.regex.Pattern)5 Intent (android.content.Intent)4 Cursor (android.database.Cursor)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Matcher (java.util.regex.Matcher)4 LocalDate (org.joda.time.LocalDate)4 JSONException (org.json.JSONException)4 Account (de.geeksfactory.opacclient.objects.Account)3