Search in sources :

Example 16 with LentItem

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

the class WinBiap method parseMediaList.

static List<LentItem> parseMediaList(Document doc, JSONObject data) {
    List<LentItem> lent = new ArrayList<>();
    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
    // the account page differs between WinBiap versions 4.2 and >= 4.3
    boolean winBiap43;
    if (doc.select(".GridView_RowStyle").size() > 0) {
        winBiap43 = false;
    } else {
        winBiap43 = true;
    }
    // 4.4: id=...DetailItemMain_0_rowBorrow_0
    for (Element tr : doc.select(winBiap43 ? ".detailTable tr[id*=_rowBorrow]" : ".GridView_RowStyle")) {
        LentItem item = new LentItem();
        Element detailsTr = winBiap43 ? tr.nextElementSibling() : tr;
        // the second column contains an img tag with the cover
        if (detailsTr.select(".cover, img[id*=ImageCover]").size() > 0) {
            // find media ID using cover URL
            Element cover = detailsTr.select(".cover, img[id*=ImageCover]").first();
            String src = cover.attr("abs:data-src");
            if (src.equals(""))
                src = cover.attr("abs:src");
            Map<String, String> params = getQueryParamsFirst(src);
            if (params.containsKey("catid"))
                item.setId(params.get("catid"));
            // find media type
            SearchResult.MediaType mt = getMediaType(cover, data);
            item.setMediaType(mt);
            // set cover if it's not the media type image
            if (!src.equals(cover.attr("grp")))
                item.setCover(src);
        }
        item.setAuthor(nullIfEmpty(tr.select("[id$=LabelAutor]").text()));
        item.setTitle(nullIfEmpty(tr.select("[id$=LabelTitel], [id$=LabelTitle], .title").text()));
        item.setBarcode(nullIfEmpty(detailsTr.select("[id$=Label_Mediennr], [id$=labelMediennr], [id*=labelMediennr_]").text()));
        item.setFormat(nullIfEmpty(detailsTr.select("[id$=Label_Mediengruppe], [id$=labelMediagroup], " + "[id*=labelMediagroup_]").text()));
        item.setHomeBranch(nullIfEmpty(detailsTr.select("[id$=Label_Zweigstelle], [id$=labelBranch], [id*=labelBranch_]").text()));
        // Label_Entliehen contains the date when the medium was lent
        try {
            item.setDeadline(fmt.parseLocalDate(tr.select("[id$=LabelFaellig], [id$=LabelMatureDate], .matureDate").text()));
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        if (tr.select("input[id*=_chkSelect]").size() > 0) {
            item.setProlongData(tr.select("input[id*=_chkSelect]").attr("name"));
        } else {
            item.setRenewable(false);
        }
        lent.add(item);
    }
    return lent;
}
Also used : Element(org.jsoup.nodes.Element) FormElement(org.jsoup.nodes.FormElement) ArrayList(java.util.ArrayList) SearchResult(de.geeksfactory.opacclient.objects.SearchResult) LentItem(de.geeksfactory.opacclient.objects.LentItem) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 17 with LentItem

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

the class Zones method account.

@Override
public AccountData account(Account acc) throws IOException, JSONException, OpacErrorException {
    Document login = login(acc);
    if (login == null) {
        return null;
    }
    AccountData res = new AccountData(acc.getId());
    AccountLinks accountLinks = new AccountLinks(login, res);
    String lentLink = accountLinks.getLentLink();
    String resLink = accountLinks.getResLink();
    if (lentLink == null) {
        return null;
    }
    List<LentItem> lentItems = new ArrayList<>();
    String lentUrl = opac_url + "/" + lentLink.replace("utf-8?Method", "utf-8&Method");
    String lentHtml = httpGet(lentUrl, getDefaultEncoding());
    Document lentDoc = Jsoup.parse(lentHtml);
    lentDoc.setBaseUri(lentUrl);
    loadMediaList(lentDoc, lentItems);
    res.setLent(lentItems);
    // In Koeln, the reservations link only doesn't show on the overview page
    if (resLink == null) {
        for (Element a : lentDoc.select("a.AccountMenuLink")) {
            if (a.text().contains("Vormerkungen")) {
                resLink = a.attr("href");
            }
        }
    }
    List<ReservedItem> reservedItems = new ArrayList<>();
    String resUrl = opac_url + "/" + resLink;
    String resHtml = httpGet(resUrl, getDefaultEncoding());
    Document resDoc = Jsoup.parse(resHtml);
    resDoc.setBaseUri(resUrl);
    loadResList(resDoc, reservedItems);
    res.setReservations(reservedItems);
    return res;
}
Also used : AccountData(de.geeksfactory.opacclient.objects.AccountData) 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)

Example 18 with LentItem

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

the class PicaOld method parseMediaList.

static void parseMediaList(List<LentItem> media, Document doc, StringProvider stringProvider, List<String> renewalCounts) throws OpacErrorException {
    Elements copytrs = doc.select("table[summary^=list] > tbody > tr[valign=top]");
    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd-MM-yyyy").withLocale(Locale.GERMAN);
    int trs = copytrs.size();
    if (trs < 1) {
        throw new OpacErrorException(stringProvider.getString(StringProvider.COULD_NOT_LOAD_ACCOUNT));
    }
    assert (trs > 0);
    for (int i = 0; i < trs; i++) {
        Element tr = copytrs.get(i);
        if (tr.select("table[summary=title data]").size() > 0) {
            // According to HTML code from Bug reports (Server TU Darmstadt,
            // Berlin Ibero-Amerikanisches Institut)
            LentItem item = new LentItem();
            // Check if there is a checkbox to prolong this item
            if (tr.select("input").size() > 0) {
                item.setProlongData(tr.select("input").attr("value"));
            } else {
                item.setRenewable(false);
            }
            Elements datatrs = tr.select("table[summary=title data] tr");
            item.setTitle(datatrs.get(0).text());
            String reservations = null;
            for (Element td : datatrs.get(1).select("td")) {
                List<TextNode> textNodes = td.textNodes();
                Elements titles = td.select("span.label-small");
                List<String> values = new ArrayList<>();
                if (td.select("span[name=xxxxx]").size() > 0) {
                    for (Element span : td.select("span[name=xxxxx]")) {
                        values.add(span.text());
                    }
                } else {
                    for (TextNode node : textNodes) {
                        if (!node.text().equals(" ")) {
                            values.add(node.text());
                        }
                    }
                }
                assert (values.size() == titles.size());
                for (int j = 0; j < values.size(); j++) {
                    String title = titles.get(j).text();
                    String value = values.get(j).trim().replace(";", "");
                    // noinspection StatementWithEmptyBody
                    if (title.contains("Signatur") || title.contains("shelf mark") || title.contains("signatuur")) {
                    // not supported
                    } else if (title.contains("Status") || title.contains("status") || title.contains("statut")) {
                        item.setStatus(value);
                    } else if (title.contains("Leihfristende") || title.contains("expiry date") || title.contains("vervaldatum") || title.contains("date d'expiration")) {
                        try {
                            item.setDeadline(fmt.parseLocalDate(value));
                        } catch (IllegalArgumentException e1) {
                            e1.printStackTrace();
                        }
                    } else // noinspection StatementWithEmptyBody
                    if (title.contains("Vormerkungen") || title.contains("reservations") || title.contains("reserveringen") || title.contains("réservations")) {
                        reservations = value;
                    }
                }
            }
            media.add(item);
        } else {
            // like in Kiel
            String prolongCount = "";
            if (renewalCounts.size() == trs && renewalCounts.get(i) != null) {
                prolongCount = renewalCounts.get(i);
            }
            String reminderCount = tr.child(13).text().trim();
            if (reminderCount.contains(" Mahn") && reminderCount.contains("(") && reminderCount.indexOf("(") < reminderCount.indexOf(" Mahn")) {
                reminderCount = reminderCount.substring(reminderCount.indexOf("(") + 1, reminderCount.indexOf(" Mahn"));
            } else {
                reminderCount = "";
            }
            LentItem item = new LentItem();
            if (tr.child(4).text().trim().length() < 5 && tr.child(5).text().trim().length() > 4) {
                item.setTitle(tr.child(5).text().trim());
            } else {
                item.setTitle(tr.child(4).text().trim());
            }
            String status = tr.child(13).text().trim();
            if (!reminderCount.equals("0") && !reminderCount.equals("")) {
                if (!status.equals(""))
                    status += ", ";
                status += reminderCount + " " + stringProvider.getString(StringProvider.REMINDERS) + ", ";
            }
            if (!"".equals(prolongCount)) {
                if (!status.equals(""))
                    status += ", ";
                status += prolongCount + "x " + stringProvider.getString(StringProvider.PROLONGED_ABBR);
            }
            if (tr.children().size() >= 26 && !"".equals(tr.child(25).text().trim())) {
                if (!status.equals(""))
                    status += ", ";
                try {
                    status += stringProvider.getQuantityString(StringProvider.RESERVATIONS_NUMBER, Integer.parseInt(tr.child(25).text().trim()), Integer.parseInt(tr.child(25).text().trim()));
                } catch (NumberFormatException e) {
                // pass
                }
            }
            // + tr.child(25).text().trim() + " Vormerkungen");
            item.setStatus(status);
            try {
                item.setDeadline(fmt.parseLocalDate(tr.child(21).text().trim()));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
            if (tr.child(1).select("input").size() > 0) {
                // If there is no checkbox, the medium is not renewable
                item.setProlongData(tr.child(1).select("input").attr("value"));
            }
            media.add(item);
        }
    }
    assert (media.size() == trs);
}
Also used : Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) TextNode(org.jsoup.nodes.TextNode) Elements(org.jsoup.select.Elements) LentItem(de.geeksfactory.opacclient.objects.LentItem) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 19 with LentItem

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

the class SISIS method account.

@Override
public AccountData account(Account acc) throws IOException, JSONException, OpacErrorException {
    // TODO: Is this necessary?
    start();
    int resultNum;
    if (!login(acc)) {
        return null;
    }
    // Geliehene Medien
    String html = httpGet(opac_url + "/userAccount.do?methodToCall=showAccount&typ=1", ENCODING);
    List<LentItem> medien = new ArrayList<>();
    Document doc = Jsoup.parse(html);
    doc.setBaseUri(opac_url);
    parse_medialist(medien, doc, 1, data);
    // additional pages
    Map<String, Integer> links = getAccountPageLinks(doc);
    for (Map.Entry<String, Integer> link : links.entrySet()) {
        html = httpGet(link.getKey(), ENCODING);
        parse_medialist(medien, Jsoup.parse(html), link.getValue(), data);
    }
    if (doc.select("#label1").size() > 0) {
        resultNum = 0;
        String rNum = doc.select("#label1").first().text().trim().replaceAll(".*\\(([0-9]*)\\).*", "$1");
        if (rNum.length() > 0) {
            resultNum = Integer.parseInt(rNum);
        }
        assert (resultNum == medien.size());
    }
    // Ordered media ("Bestellungen")
    html = httpGet(opac_url + "/userAccount.do?methodToCall=showAccount&typ=6", ENCODING);
    List<ReservedItem> reserved = new ArrayList<>();
    doc = Jsoup.parse(html);
    doc.setBaseUri(opac_url);
    parse_reslist("6", reserved, doc, 1);
    Elements label6 = doc.select("#label6");
    // additional pages
    links = getAccountPageLinks(doc);
    for (Map.Entry<String, Integer> link : links.entrySet()) {
        html = httpGet(link.getKey(), ENCODING);
        parse_reslist("6", reserved, Jsoup.parse(html), link.getValue());
    }
    // Prebooked media ("Vormerkungen")
    html = httpGet(opac_url + "/userAccount.do?methodToCall=showAccount&typ=7", ENCODING);
    doc = Jsoup.parse(html);
    doc.setBaseUri(opac_url);
    parse_reslist("7", reserved, doc, 1);
    // additional pages
    links = getAccountPageLinks(doc);
    for (Map.Entry<String, Integer> link : links.entrySet()) {
        html = httpGet(link.getKey(), ENCODING);
        parse_reslist("7", reserved, Jsoup.parse(html), link.getValue());
    }
    if (label6.size() > 0 && doc.select("#label7").size() > 0) {
        resultNum = 0;
        String rNum = label6.text().trim().replaceAll(".*\\(([0-9]*)\\).*", "$1");
        if (rNum.length() > 0) {
            resultNum = Integer.parseInt(rNum);
        }
        rNum = doc.select("#label7").text().trim().replaceAll(".*\\(([0-9]*)\\).*", "$1");
        if (rNum.length() > 0) {
            resultNum += Integer.parseInt(rNum);
        }
        assert (resultNum == reserved.size());
    }
    AccountData res = new AccountData(acc.getId());
    if (doc.select("#label8").size() > 0) {
        String text = doc.select("#label8").first().text().trim();
        if (text.matches("Geb.+hren[^\\(]+\\(([0-9.,]+)[^0-9€A-Z]*(€|EUR|CHF|Fr)\\)")) {
            text = text.replaceAll("Geb.+hren[^\\(]+\\(([0-9.,]+)[^0-9€A-Z]*(€|EUR|CHF|Fr)\\)", "$1 $2");
            res.setPendingFees(text);
        }
    }
    Pattern p = Pattern.compile("[^0-9.]*", Pattern.MULTILINE);
    if (doc.select(".box3").size() > 0) {
        for (Element box : doc.select(".box3")) {
            if (box.select("strong").size() == 1) {
                String text = box.select("strong").text();
                if (text.equals("Jahresgebühren")) {
                    text = box.text();
                    text = p.matcher(text).replaceAll("");
                    res.setValidUntil(text);
                }
            }
        }
    }
    res.setLent(medien);
    res.setReservations(reserved);
    return res;
}
Also used : Pattern(java.util.regex.Pattern) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) AccountData(de.geeksfactory.opacclient.objects.AccountData) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) LentItem(de.geeksfactory.opacclient.objects.LentItem) Map(java.util.Map) HashMap(java.util.HashMap)

Example 20 with LentItem

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

the class TouchPoint method parse_medialist.

static List<LentItem> parse_medialist(Document doc) {
    List<LentItem> media = new ArrayList<>();
    Elements copytrs = doc.select(".data tr");
    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
    int trs = copytrs.size();
    if (trs == 1) {
        return null;
    }
    assert (trs > 0);
    for (int i = 1; i < trs; i++) {
        Element tr = copytrs.get(i);
        LentItem item = new LentItem();
        if (tr.text().contains("keine Daten")) {
            return null;
        }
        item.setTitle(tr.select(".account-display-title").select("b, strong").text().trim());
        try {
            item.setRenewable(false);
            if (tr.select("a").size() > 0) {
                for (Element link : tr.select("a")) {
                    String href = link.attr("abs:href");
                    Map<String, String> hrefq = getQueryParamsFirst(href);
                    if (hrefq.containsKey("q")) {
                        item.setId(extractIdFromQ(hrefq.get("q")));
                    } else if ("renewal".equals(hrefq.get("methodToCall"))) {
                        item.setProlongData(href.split("\\?")[1]);
                        item.setRenewable(true);
                        link.remove();
                        break;
                    }
                }
            }
            String[] lines = tr.select(".account-display-title").html().split("<br[ /]*>");
            if (lines.length == 4 || lines.length == 5) {
                // Winterthur
                item.setAuthor(Jsoup.parse(lines[1]).text().trim());
                item.setBarcode(Jsoup.parse(lines[2]).text().trim());
                if (lines.length == 5) {
                    // Chemnitz
                    item.setStatus(Jsoup.parse(lines[3] + " " + lines[4]).text().trim());
                } else {
                    // Winterthur
                    item.setStatus(Jsoup.parse(lines[3]).text().trim());
                }
            } else if (lines.length == 3) {
                // We can't really tell the difference between missing author and missing
                // shelfmark. However, all items have shelfmarks, not all have authors.
                item.setBarcode(Parser.unescapeEntities(lines[1].trim(), false));
                item.setStatus(Parser.unescapeEntities(lines[2].trim(), false));
            } else if (lines.length == 2) {
                item.setAuthor(Parser.unescapeEntities(lines[1].trim(), false));
            }
            String[] col3split = tr.select(".account-display-state").html().split("<br[ /]*>");
            String deadline = Jsoup.parse(col3split[0].trim()).text().trim();
            if (deadline.contains(":")) {
                // BSB Munich: <span class="hidden-sm hidden-md hidden-lg">Fälligkeitsdatum :
                // </span>26.02.2016<br>
                deadline = deadline.split(":")[1].trim();
            }
            if (deadline.contains("-")) {
                // Chemnitz: 22.07.2015 - 20.10.2015<br>
                deadline = deadline.split("-")[1].trim();
            }
            try {
                item.setDeadline(fmt.parseLocalDate(deadline).toString());
            } catch (IllegalArgumentException e1) {
                e1.printStackTrace();
            }
            if (col3split.length > 1)
                item.setHomeBranch(col3split[1].trim());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        media.add(item);
    }
    return media;
}
Also used : Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) LentItem(de.geeksfactory.opacclient.objects.LentItem) Elements(org.jsoup.select.Elements) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

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