Search in sources :

Example 11 with Detail

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

the class TouchPoint method parse_result.

protected DetailedItem parse_result(String html) throws IOException {
    Document doc = Jsoup.parse(html);
    doc.setBaseUri(opac_url);
    DetailedItem result = new DetailedItem();
    result.setCover(findCoverUrl(doc, false));
    if (doc.select("#permalink-link").size() > 0) {
        String href = doc.select("#permalink-link").first().attr("href");
        JSONObject id = new JSONObject();
        try {
            id.put("url", href);
            result.setId(id.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    for (Element tr : doc.select(".titleinfo tr")) {
        // Sometimes there is one th and one td, sometimes two tds
        String detailName = tr.select("th, td").first().text().trim();
        if (detailName.endsWith(":")) {
            detailName = detailName.substring(0, detailName.length() - 1);
        }
        String detailValue = tr.select("td").last().text().trim();
        result.addDetail(new Detail(detailName, detailValue));
        if (detailName.contains("ID in diesem Katalog") && result.getId() == null) {
            result.setId(detailValue);
        }
        if (detailName.equals("Titel")) {
            result.setTitle(detailValue);
        }
    }
    if (result.getDetails().size() == 0 && doc.select("#details").size() > 0) {
        // e.g. Bayreuth_Uni
        String dname = "";
        String dval = "";
        boolean in_value = true;
        for (Node n : doc.select("#details").first().childNodes()) {
            if (n instanceof Element && ((Element) n).tagName().equals("strong")) {
                if (in_value) {
                    if (dname.length() > 0 && dval.length() > 0) {
                        result.addDetail(new Detail(dname, dval));
                        if (dname.equals("Titel")) {
                            result.setTitle(dval);
                        }
                    }
                    dname = ((Element) n).text();
                    in_value = false;
                } else {
                    dname += ((Element) n).text();
                }
            } else {
                String t = null;
                if (n instanceof TextNode) {
                    t = ((TextNode) n).text();
                } else if (n instanceof Element) {
                    t = ((Element) n).text();
                }
                if (t != null) {
                    if (in_value) {
                        dval += t;
                    } else {
                        in_value = true;
                        dval = t;
                    }
                }
            }
        }
    }
    if (result.getTitle() == null) {
        result.setTitle(doc.select("h1").first().text());
    }
    // Copies
    String copiesParameter = doc.select("div[id^=ajax_holdings_url").attr("ajaxParameter").replace("&", "");
    if (!"".equals(copiesParameter)) {
        String copiesHtml = httpGet(opac_url + "/" + copiesParameter, ENCODING);
        Document copiesDoc = Jsoup.parse(copiesHtml);
        List<String> table_keys = new ArrayList<>();
        for (Element th : copiesDoc.select(".data tr th")) {
            if (th.text().contains("Zweigstelle")) {
                table_keys.add("branch");
            } else if (th.text().contains("Status")) {
                table_keys.add("status");
            } else if (th.text().contains("Signatur")) {
                table_keys.add("signature");
            } else {
                table_keys.add(null);
            }
        }
        for (Element tr : copiesDoc.select(".data tr:has(td)")) {
            Copy copy = new Copy();
            int i = 0;
            for (Element td : tr.select("td")) {
                if (table_keys.get(i) != null) {
                    copy.set(table_keys.get(i), td.text().trim());
                }
                i++;
            }
            result.addCopy(copy);
        }
    }
    // Reservation Info, only works if the code above could find a URL
    if (!"".equals(copiesParameter)) {
        String reservationParameter = copiesParameter.replace("showHoldings", "showDocument");
        try {
            String reservationHtml = httpGet(opac_url + "/" + reservationParameter, ENCODING);
            Document reservationDoc = Jsoup.parse(reservationHtml);
            reservationDoc.setBaseUri(opac_url);
            if (reservationDoc.select("a[href*=requestItem.do]").size() == 1) {
                result.setReservable(true);
                result.setReservation_info(reservationDoc.select("a").first().attr("abs:href"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        // fail silently
        }
    }
    try {
        Element isvolume = null;
        Map<String, String> volume = new HashMap<>();
        Elements links = doc.select(".data td a");
        int elcount = links.size();
        for (int eli = 0; eli < elcount; eli++) {
            List<NameValuePair> anyurl = URLEncodedUtils.parse(new URI(links.get(eli).attr("href")), "UTF-8");
            for (NameValuePair nv : anyurl) {
                if (nv.getName().equals("methodToCall") && nv.getValue().equals("volumeSearch")) {
                    isvolume = links.get(eli);
                } else if (nv.getName().equals("catKey")) {
                    volume.put("catKey", nv.getValue());
                } else if (nv.getName().equals("dbIdentifier")) {
                    volume.put("dbIdentifier", nv.getValue());
                }
            }
            if (isvolume != null) {
                volume.put("volume", "true");
                result.setVolumesearch(volume);
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
Also used : NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HashMap(java.util.HashMap) Element(org.jsoup.nodes.Element) Node(org.jsoup.nodes.Node) TextNode(org.jsoup.nodes.TextNode) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) TextNode(org.jsoup.nodes.TextNode) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) JSONObject(org.json.JSONObject) Copy(de.geeksfactory.opacclient.objects.Copy) DetailedItem(de.geeksfactory.opacclient.objects.DetailedItem) Detail(de.geeksfactory.opacclient.objects.Detail)

Example 12 with Detail

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

the class VuFind method parseDetail.

static DetailedItem parseDetail(String id, Document doc, JSONObject data) throws OpacErrorException, JSONException {
    if (doc.select("p.error, p.errorMsg, .alert-error").size() > 0) {
        throw new OpacErrorException(doc.select("p.error, p.errorMsg, .alert-error").text());
    }
    DetailedItem res = new DetailedItem();
    res.setId(id);
    Elements title = doc.select(".record h1, .record [itemprop=name], .record [property=name]");
    if (title.size() > 0) {
        res.setTitle(title.first().text());
    }
    for (Element img : doc.select(".record img, #cover img")) {
        String src = img.absUrl("src");
        if (src.contains("over")) {
            if (!src.contains("Unavailable")) {
                res.setCover(src);
            }
            break;
        }
    }
    String head = null;
    StringBuilder value = new StringBuilder();
    for (Element tr : doc.select(".record table").first().select("tr")) {
        if (tr.children().size() == 1) {
            if (tr.child(0).tagName().equals("th")) {
                if (head != null) {
                    res.addDetail(new Detail(head, value.toString()));
                    value = new StringBuilder();
                }
                head = tr.child(0).text();
            } else {
                if (!value.toString().equals(""))
                    value.append("\n");
                value.append(tr.child(0).text());
            }
        } else {
            String text = tr.child(1).text();
            if (tr.child(1).select("a").size() > 0) {
                String href = tr.child(1).select("a").attr("href");
                if (!href.startsWith("/") && !text.contains(data.getString("baseurl"))) {
                    text += " " + href;
                }
            }
            res.addDetail(new Detail(tr.child(0).text(), text));
        }
    }
    if (head != null)
        res.addDetail(new Detail(head, value.toString()));
    try {
        if (doc.select("#Volumes").size() > 0) {
            parseVolumes(res, doc, data);
        } else {
            parseCopies(res, doc, data);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return res;
}
Also used : Element(org.jsoup.nodes.Element) JSONException(org.json.JSONException) DetailedItem(de.geeksfactory.opacclient.objects.DetailedItem) Elements(org.jsoup.select.Elements) Detail(de.geeksfactory.opacclient.objects.Detail)

Example 13 with Detail

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

the class WinBiap method parse_result.

private DetailedItem parse_result(String html) {
    Document doc = Jsoup.parse(html);
    DetailedItem item = new DetailedItem();
    if (doc.select(".cover").size() > 0) {
        Element cover = doc.select(".cover").first();
        if (cover.hasAttr("data-src")) {
            item.setCover(cover.attr("data-src"));
        } else if (cover.hasAttr("src") && !cover.attr("src").equals("images/empty.gif")) {
            item.setCover(cover.attr("src"));
        }
        item.setMediaType(getMediaType(cover, data));
    }
    String permalink = doc.select(".PermalinkTextarea").text();
    item.setId(getQueryParamsFirst(permalink).get("Id"));
    Elements trs = doc.select(".DetailInformation").first().select("tr");
    for (Element tr : trs) {
        String name = tr.select(".DetailInformationEntryName").text().replace(":", "");
        String value = tr.select(".DetailInformationEntryContent").text();
        switch(name) {
            case "Titel":
                item.setTitle(value);
                break;
            case "Stücktitel":
                item.setTitle(item.getTitle() + " " + value);
                break;
            default:
                item.addDetail(new Detail(name, value));
                break;
        }
    }
    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
    trs = doc.select(".detailCopies .tableCopies > tbody > tr:not(.headerCopies)");
    for (Element tr : trs) {
        Copy copy = new Copy();
        copy.setBarcode(tr.select(".mediaBarcode").text().replace("#", ""));
        copy.setStatus(tr.select(".mediaStatus").text());
        if (tr.select(".DateofReturn .borrowUntil").size() > 0) {
            String returntime = tr.select(".DateofReturn .borrowUntil").text();
            try {
                copy.setReturnDate(fmt.parseLocalDate(returntime));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
        }
        if (tr.select(".mediaBranch").size() > 0) {
            copy.setBranch(tr.select(".mediaBranch").text());
        }
        copy.setLocation(tr.select(".cellMediaItemLocation span").text());
        if (tr.select("#HyperLinkReservation").size() > 0) {
            copy.setResInfo(tr.select("#HyperLinkReservation").attr("href"));
            item.setReservable(true);
            item.setReservation_info("reservable");
        }
        item.addCopy(copy);
    }
    return item;
}
Also used : Copy(de.geeksfactory.opacclient.objects.Copy) Element(org.jsoup.nodes.Element) FormElement(org.jsoup.nodes.FormElement) DetailedItem(de.geeksfactory.opacclient.objects.DetailedItem) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Detail(de.geeksfactory.opacclient.objects.Detail)

Example 14 with Detail

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

the class Zones method parse_result.

private DetailedItem parse_result(String id, String html) {
    Document doc = Jsoup.parse(html);
    DetailedItem result = new DetailedItem();
    result.setTitle("");
    boolean title_is_set = false;
    result.setId(id);
    String detailTrsQuery = version18 ? ".inRoundBox1 table table tr" : ".DetailDataCell table table:not(.inRecordHeader) tr";
    Elements detailtrs1 = doc.select(detailTrsQuery);
    for (int i = 0; i < detailtrs1.size(); i++) {
        Element tr = detailtrs1.get(i);
        int s = tr.children().size();
        if (tr.child(0).text().trim().equals("Titel") && !title_is_set) {
            result.setTitle(tr.child(s - 1).text().trim());
            title_is_set = true;
        } else if (s > 1) {
            Element valchild = tr.child(s - 1);
            if (valchild.select("table").isEmpty()) {
                String val = valchild.text().trim();
                if (val.length() > 0) {
                    result.addDetail(new Detail(tr.child(0).text().trim(), val));
                }
            }
        }
    }
    for (Element a : doc.select("a.SummaryActionLink")) {
        if (a.text().contains("Vormerken")) {
            result.setReservable(true);
            result.setReservation_info(a.attr("href"));
        }
    }
    Elements detaildiv = doc.select("div.record-item-new");
    if (!detaildiv.isEmpty()) {
        for (int i = 0; i < detaildiv.size(); i++) {
            Element dd = detaildiv.get(i);
            String text = "";
            for (Node node : dd.childNodes()) {
                if (node instanceof TextNode) {
                    String snip = ((TextNode) node).text();
                    if (snip.length() > 0) {
                        text += snip;
                    }
                } else if (node instanceof Element) {
                    if (((Element) node).tagName().equals("br")) {
                        text += "\n";
                    } else {
                        String snip = ((Element) node).text().trim();
                        if (snip.length() > 0) {
                            text += snip;
                        }
                    }
                }
            }
            result.addDetail(new Detail("", text));
        }
    }
    if (doc.select("span.z3988").size() > 0) {
        // Sometimes there is a <span class="Z3988"> item which provides
        // data in a standardized format.
        String z3988data = doc.select("span.z3988").first().attr("title").trim();
        for (String pair : z3988data.split("&")) {
            String[] nv = pair.split("=", 2);
            if (nv.length == 2) {
                if (!nv[1].trim().equals("")) {
                    if (nv[0].equals("rft.btitle") && result.getTitle().length() == 0) {
                        result.setTitle(nv[1]);
                    } else if (nv[0].equals("rft.atitle") && result.getTitle().length() == 0) {
                        result.setTitle(nv[1]);
                    } else if (nv[0].equals("rft.au")) {
                        result.addDetail(new Detail("Author", nv[1]));
                    }
                }
            }
        }
    }
    // Cover
    if (doc.select(".BookCover, .LargeBookCover").size() > 0) {
        result.setCover(doc.select(".BookCover, .LargeBookCover").first().attr("src"));
    }
    Elements copydivs = doc.select("div[id^=stock_]");
    String pop = "";
    for (int i = 0; i < copydivs.size(); i++) {
        Element div = copydivs.get(i);
        if (div.attr("id").startsWith("stock_head")) {
            pop = div.text().trim();
            continue;
        }
        Copy copy = new Copy();
        DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
        // This is getting very ugly - check if it is valid for libraries which are not Hamburg.
        // Seems to also work in Kiel (Zones 1.8, checked 10.10.2015)
        int j = 0;
        for (Node node : div.childNodes()) {
            try {
                if (node instanceof Element) {
                    if (((Element) node).tag().getName().equals("br")) {
                        copy.setBranch(pop);
                        result.addCopy(copy);
                        j = -1;
                    } else if (((Element) node).tag().getName().equals("b") && j == 1) {
                        copy.setLocation(((Element) node).text());
                    } else if (((Element) node).tag().getName().equals("b") && j > 1) {
                        copy.setStatus(((Element) node).text());
                    }
                    j++;
                } else if (node instanceof TextNode) {
                    if (j == 0) {
                        copy.setDepartment(((TextNode) node).text());
                    }
                    if (j == 2) {
                        copy.setBarcode(((TextNode) node).getWholeText().trim().split("\n")[0].trim());
                    }
                    if (j == 6) {
                        String text = ((TextNode) node).text().trim();
                        String date = text.substring(text.length() - 10);
                        try {
                            copy.setReturnDate(fmt.parseLocalDate(date));
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        }
                    }
                    j++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}
Also used : Element(org.jsoup.nodes.Element) TextNode(org.jsoup.nodes.TextNode) Node(org.jsoup.nodes.Node) TextNode(org.jsoup.nodes.TextNode) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) JSONException(org.json.JSONException) IOException(java.io.IOException) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Copy(de.geeksfactory.opacclient.objects.Copy) DetailedItem(de.geeksfactory.opacclient.objects.DetailedItem) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Detail(de.geeksfactory.opacclient.objects.Detail)

Example 15 with Detail

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

the class DetailsAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Detail detail = details.get(position);
    holder.desc.setText(detail.getDesc());
    holder.content.setText(detail.getContent());
    Linkify.addLinks(holder.content, Linkify.WEB_URLS);
}
Also used : Detail(de.geeksfactory.opacclient.objects.Detail)

Aggregations

Detail (de.geeksfactory.opacclient.objects.Detail)20 DetailedItem (de.geeksfactory.opacclient.objects.DetailedItem)18 Copy (de.geeksfactory.opacclient.objects.Copy)16 Element (org.jsoup.nodes.Element)14 Document (org.jsoup.nodes.Document)11 Elements (org.jsoup.select.Elements)11 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)10 JSONException (org.json.JSONException)9 JSONObject (org.json.JSONObject)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 NameValuePair (org.apache.http.NameValuePair)4 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)4 Node (org.jsoup.nodes.Node)4 TextNode (org.jsoup.nodes.TextNode)4 Volume (de.geeksfactory.opacclient.objects.Volume)3 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3