Search in sources :

Example 21 with Copy

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

the class Heidi method getResultById.

@Override
public DetailedItem getResultById(String id, final String homebranch) throws IOException {
    if (sessid == null) {
        start();
    }
    // Homebranch
    if (homebranch != null && !"".equals(homebranch)) {
        cookieStore.addCookie(new BasicClientCookie("zweig", homebranch));
    }
    String html = httpGet(opac_url + "/titel.cgi?katkey=" + id + "&sess=" + sessid, ENCODING, false, cookieStore);
    Document doc = Jsoup.parse(html);
    DetailedItem item = new DetailedItem();
    item.setId(id);
    Elements table = doc.select(".titelsatz tr");
    for (Element tr : table) {
        if (tr.select("th").size() == 0 || tr.select("td").size() == 0) {
            continue;
        }
        String d = tr.select("th").first().text();
        String c = tr.select("td").first().text();
        if (d.equals("Titel:")) {
            item.setTitle(c);
        } else if ((d.contains("URL") || d.contains("Link")) && tr.select("td a").size() > 0) {
            item.addDetail(new Detail(d, tr.select("td a").first().attr("href")));
        } else {
            item.addDetail(new Detail(d, c));
        }
    }
    if (doc.select(".ex table tr").size() > 0) {
        table = doc.select(".ex table tr");
        DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
        for (Element tr : table) {
            if (tr.hasClass("exueber") || tr.select(".exsig").size() == 0 || tr.select(".exso").size() == 0 || tr.select(".exstatus").size() == 0) {
                continue;
            }
            Copy copy = new Copy();
            copy.setShelfmark(tr.select(".exsig").first().text());
            copy.setBranch(tr.select(".exso").first().text());
            String status = tr.select(".exstatus").first().text();
            if (status.contains("entliehen bis")) {
                copy.setReturnDate(fmt.parseLocalDate(status.replaceAll("entliehen bis ([0-9.]+) .*", "$1")));
                copy.setReservations(status.replaceAll(".*\\(.*Vormerkungen: ([0-9]+)\\)", "$1"));
                copy.setStatus("entliehen");
            } else {
                copy.setStatus(status);
            }
            item.addCopy(copy);
        }
    }
    for (Element a : doc.select(".status1 a")) {
        if (a.attr("href").contains("bestellung.cgi")) {
            item.setReservable(true);
            item.setReservation_info(id);
            break;
        }
    }
    for (Element a : doc.select(".titelsatz a")) {
        if (a.text().trim().matches("B.+nde")) {
            Map<String, String> volumesearch = new HashMap<>();
            volumesearch.put("query", getQueryParamsFirst(a.attr("href")).get("query"));
            item.setVolumesearch(volumesearch);
        }
    }
    return item;
}
Also used : Copy(de.geeksfactory.opacclient.objects.Copy) HashMap(java.util.HashMap) Element(org.jsoup.nodes.Element) DetailedItem(de.geeksfactory.opacclient.objects.DetailedItem) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Detail(de.geeksfactory.opacclient.objects.Detail)

Example 22 with Copy

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

the class BiBer1992 method parse_result.

/*
     * Two-column table inside of a form 1st column is category, e.g.
     * "Verfasser" 2nd column is content, e.g. "Bach, Johann Sebastian" In some
     * rows, the 1st column is empty, then 2nd column is continued text from row
     * above.
     *
     * Some libraries have a second section for the copies in stock (Exemplare).
     * This 2nd section has reverse layout.
     *
     * |-------------------| | Subject | Content | |-------------------| |
     * Subject | Content | |-------------------| | | Content |
     * |-------------------| | Subject | Content |
     * |-------------------------------------------------| | | Site | Signatur|
     * ID | State | |-------------------------------------------------| | |
     * Content | Content | Content | Content |
     * |-------------------------------------------------|
     */
private DetailedItem parse_result(String html) {
    DetailedItem item = new DetailedItem();
    Document document = Jsoup.parse(html);
    Elements rows = document.select("html body form table tr");
    // Elements rows = document.select("html body div form table tr");
    // Element rowReverseSubject = null;
    Detail detail = null;
    // prepare copiestable
    Copy copy_last_content = null;
    int copy_row = 0;
    String[] copy_keys = new String[] { "barcode", "branch", "department", "location", "status", "returndate", "reservations" };
    int[] copy_map = new int[] { 3, 1, -1, 1, 4, -1, -1 };
    try {
        JSONObject map = data.getJSONObject("copiestable");
        for (int i = 0; i < copy_keys.length; i++) {
            if (map.has(copy_keys[i])) {
                copy_map[i] = map.getInt(copy_keys[i]);
            }
        }
    } catch (Exception e) {
    // "copiestable" is optional
    }
    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
    // go through all rows
    for (Element row : rows) {
        Elements columns = row.children();
        if (columns.size() == 2) {
            // HTML tag "&nbsp;" is encoded as 0xA0
            String firstColumn = columns.get(0).text().replace("\u00a0", " ").trim();
            String secondColumn = columns.get(1).text().replace("\u00a0", " ").trim();
            if (firstColumn.length() > 0) {
                // 1st column is category
                if (firstColumn.equalsIgnoreCase("titel")) {
                    detail = null;
                    item.setTitle(secondColumn);
                } else {
                    if (secondColumn.contains("hier klicken") && columns.get(1).select("a").size() > 0) {
                        secondColumn += " " + columns.get(1).select("a").first().attr("href");
                    }
                    detail = new Detail(firstColumn, secondColumn);
                    item.getDetails().add(detail);
                }
            } else {
                // category
                if (detail != null) {
                    String content = detail.getContent() + "\n" + secondColumn;
                    detail.setContent(content);
                } else {
                    // check if there is an amazon image
                    if (columns.get(0).select("a img[src]").size() > 0) {
                        item.setCover(columns.get(0).select("a img").first().attr("src"));
                    }
                }
            }
        } else if (columns.size() > 3) {
            // (copy_row > 0)
            if (copy_row > 0) {
                Copy copy = new Copy();
                for (int j = 0; j < copy_keys.length; j++) {
                    int col = copy_map[j];
                    if (col > -1) {
                        String text = "";
                        if (copy_keys[j].equals("branch")) {
                            // for "Standort" only use ownText() to suppress
                            // Link "Wegweiser"
                            text = columns.get(col).ownText().replace("\u00a0", " ").trim();
                        }
                        if (text.length() == 0) {
                            // text of children
                            text = columns.get(col).text().replace("\u00a0", " ").trim();
                        }
                        if (text.length() == 0) {
                            // this is sometimes the case for "Standort"
                            if (copy_keys[j].equals("status")) {
                                // but do it not for Status
                                text = " ";
                            } else {
                                if (copy_last_content != null) {
                                    text = copy_last_content.get(copy_keys[j]);
                                } else {
                                    text = "";
                                }
                            }
                        }
                        if (copy_keys[j].equals("reservations")) {
                            text = text.replace("Vorgemerkt: ", "").replace("Vorbestellt: ", "");
                        }
                        try {
                            copy.set(copy_keys[j], text, fmt);
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        }
                    }
                }
                if (copy.getBranch() != null && copy.getLocation() != null && copy.getLocation().equals(copy.getBranch())) {
                    copy.setLocation(null);
                }
                item.addCopy(copy);
                copy_last_content = copy;
            }
            // ignore 1st row
            copy_row++;
        }
    // if columns.size
    }
    // for rows
    // We cannot check if media is reservable
    item.setReservable(true);
    if (opacDir.contains("opax")) {
        if (document.select("input[type=checkbox]").size() > 0) {
            item.setReservation_info(document.select("input[type=checkbox]").first().attr("name"));
        } else if (document.select("a[href^=reserv" + opacSuffix + "]").size() > 0) {
            String href = document.select("a[href^=reserv" + opacSuffix + "]").first().attr("href");
            item.setReservation_info(href.substring(href.indexOf("resF_")));
        } else {
            item.setReservable(false);
        }
    } else {
        item.setReservation_info(document.select("input[name=ID]").attr("value"));
    }
    return item;
}
Also used : Element(org.jsoup.nodes.Element) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) JSONException(org.json.JSONException) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) IOException(java.io.IOException) JSONObject(org.json.JSONObject) Copy(de.geeksfactory.opacclient.objects.Copy) DetailedItem(de.geeksfactory.opacclient.objects.DetailedItem) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Detail(de.geeksfactory.opacclient.objects.Detail)

Aggregations

Copy (de.geeksfactory.opacclient.objects.Copy)22 DetailedItem (de.geeksfactory.opacclient.objects.DetailedItem)17 Detail (de.geeksfactory.opacclient.objects.Detail)16 Element (org.jsoup.nodes.Element)15 Document (org.jsoup.nodes.Document)13 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)10 Elements (org.jsoup.select.Elements)10 HashMap (java.util.HashMap)8 JSONException (org.json.JSONException)8 JSONObject (org.json.JSONObject)8 IOException (java.io.IOException)6 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 ArrayList (java.util.ArrayList)5 NameValuePair (org.apache.http.NameValuePair)5 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)5 Node (org.jsoup.nodes.Node)5 TextNode (org.jsoup.nodes.TextNode)5 Volume (de.geeksfactory.opacclient.objects.Volume)4 URI (java.net.URI)3