Search in sources :

Example 6 with Volume

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

the class Open method parse_result.

protected DetailedItem parse_result(Document doc) {
    DetailedItem item = new DetailedItem();
    // Title and Subtitle
    item.setTitle(doc.select("span[id$=LblShortDescriptionValue], span[id$=LblTitleValue]").text());
    String subtitle = doc.select("span[id$=LblSubTitleValue]").text();
    if (subtitle.equals("") && doc.select("span[id$=LblShortDescriptionValue]").size() > 0) {
        // Subtitle detection for Bern
        Element next = doc.select("span[id$=LblShortDescriptionValue]").first().parent().nextElementSibling();
        if (next.select("span").size() == 0) {
            subtitle = next.text().trim();
        }
    }
    if (!subtitle.equals("")) {
        item.addDetail(new Detail(stringProvider.getString(StringProvider.SUBTITLE), subtitle));
    }
    // Cover
    if (doc.select("input[id$=mediumImage]").size() > 0) {
        item.setCover(doc.select("input[id$=mediumImage]").attr("src"));
    } else if (doc.select("img[id$=CoverView_Image]").size() > 0) {
        assignBestCover(item, getCoverUrlList(doc.select("img[id$=CoverView_Image]").first()));
    }
    // ID
    item.setId(doc.select("input[id$=regionmednr]").val());
    // Description
    if (doc.select("span[id$=ucCatalogueContent_LblAnnotation]").size() > 0) {
        String name = doc.select("span[id$=lblCatalogueContent]").text();
        String value = doc.select("span[id$=ucCatalogueContent_LblAnnotation]").text();
        item.addDetail(new Detail(name, value));
    }
    // Parent
    if (doc.select("a[id$=HyperLinkParent]").size() > 0) {
        item.setCollectionId(doc.select("a[id$=HyperLinkParent]").first().attr("href"));
    }
    // Details
    String DETAIL_SELECTOR = "div[id$=CatalogueDetailView] .spacingBottomSmall:has(span+span)," + "div[id$=CatalogueDetailView] .spacingBottomSmall:has(span+a), " + "div[id$=CatalogueDetailView] .oclc-searchmodule-detail-data div:has" + "(span+span), " + "div[id$=CatalogueDetailView] .oclc-searchmodule-detail-data div:has" + "(span+a)";
    for (Element detail : doc.select(DETAIL_SELECTOR)) {
        String name = detail.select("span").get(0).text().replace(": ", "");
        String value = "";
        if (detail.select("a").size() > 1) {
            int i = 0;
            for (Element a : detail.select("a")) {
                if (i != 0) {
                    value += ", ";
                }
                value += a.text().trim();
                i++;
            }
        } else {
            value = detail.select("span, a").get(1).text();
            if (value.contains("hier klicken") && detail.select("a").size() > 0) {
                value = value + " " + detail.select("a").first().attr("href");
            }
        }
        item.addDetail(new Detail(name, value));
    }
    // Description
    if (doc.select("div[id$=CatalogueContent]").size() > 0) {
        String name = doc.select("div[id$=CatalogueContent] .oclc-module-header").text();
        String value = doc.select("div[id$=CatalogueContent] .oclc-searchmodule-detail-annotation").text();
        item.addDetail(new Detail(name, value));
    }
    // Copies
    Element table = doc.select("table[id$=grdViewMediumCopies]").first();
    if (table != null) {
        Elements trs = table.select("tr");
        List<String> columnmap = new ArrayList<>();
        for (Element th : trs.first().select("th")) {
            columnmap.add(getCopyColumnKey(th.text()));
        }
        DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
        for (int i = 1; i < trs.size(); i++) {
            Elements tds = trs.get(i).select("td");
            Copy copy = new Copy();
            for (int j = 0; j < tds.size(); j++) {
                if (columnmap.get(j) == null)
                    continue;
                String text = tds.get(j).text().replace("\u00a0", "");
                if (tds.get(j).select(".oclc-module-label").size() > 0 && tds.get(j).select("span").size() == 2) {
                    text = tds.get(j).select("span").get(1).text();
                }
                if (text.equals(""))
                    continue;
                copy.set(columnmap.get(j), text, fmt);
            }
            item.addCopy(copy);
        }
    }
    // Dependent (e.g. Verden)
    if (doc.select("div[id$=DivDependentCatalogue]").size() > 0) {
        String url = opac_url + "/DesktopModules/OCLC.OPEN.PL.DNN.SearchModule/SearchService.asmx/GetDependantCatalogues";
        JSONObject postData = new JSONObject();
        // Determine portalID value
        int portalId = 1;
        for (Element scripttag : doc.select("script")) {
            String scr = scripttag.html();
            if (scr.contains("LoadCatalogueViewDependantCataloguesAsync")) {
                Pattern portalIdPattern = Pattern.compile(".*LoadCatalogueViewDependantCataloguesAsync\\([^,]*,[^,]*," + "[^,]*,[^,]*,[^,]*,[^0-9,]*([0-9]+)[^0-9,]*,.*\\).*");
                Matcher portalIdMatcher = portalIdPattern.matcher(scr);
                if (portalIdMatcher.find()) {
                    portalId = Integer.parseInt(portalIdMatcher.group(1));
                }
            }
        }
        try {
            postData.put("portalId", portalId).put("mednr", item.getId()).put("tabUrl", opac_url + "/" + data.getJSONObject("urls").getString("simple_search") + NO_MOBILE + "&id=").put("branchFilter", "");
            RequestBody entity = RequestBody.create(MEDIA_TYPE_JSON, postData.toString());
            String json = httpPost(url, entity, getDefaultEncoding());
            JSONObject volumeData = new JSONObject(json);
            JSONArray cat = volumeData.getJSONObject("d").getJSONArray("Catalogues");
            for (int i = 0; i < cat.length(); i++) {
                JSONObject obj = cat.getJSONObject(i);
                Map<String, String> params = getQueryParamsFirst(obj.getString("DependantUrl"));
                item.addVolume(new Volume(params.get("id"), obj.getString("DependantTitle")));
            }
        } catch (JSONException | IOException e) {
            e.printStackTrace();
        }
    }
    return item;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Element(org.jsoup.nodes.Element) FormElement(org.jsoup.nodes.FormElement) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) Elements(org.jsoup.select.Elements) JSONObject(org.json.JSONObject) Copy(de.geeksfactory.opacclient.objects.Copy) Volume(de.geeksfactory.opacclient.objects.Volume) DetailedItem(de.geeksfactory.opacclient.objects.DetailedItem) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Detail(de.geeksfactory.opacclient.objects.Detail) RequestBody(okhttp3.RequestBody)

Aggregations

Volume (de.geeksfactory.opacclient.objects.Volume)6 Copy (de.geeksfactory.opacclient.objects.Copy)4 DetailedItem (de.geeksfactory.opacclient.objects.DetailedItem)4 Detail (de.geeksfactory.opacclient.objects.Detail)3 JSONObject (org.json.JSONObject)3 Element (org.jsoup.nodes.Element)3 IOException (java.io.IOException)2 Matcher (java.util.regex.Matcher)2 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)2 JSONException (org.json.JSONException)2 Elements (org.jsoup.select.Elements)2 Test (org.junit.Test)2 Intent (android.content.Intent)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 TextView (android.widget.TextView)1 DummyStringProvider (de.geeksfactory.opacclient.i18n.DummyStringProvider)1 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1