Search in sources :

Example 6 with Copy

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

the class VuFind method parseCopies.

static void parseCopies(DetailedItem res, Document doc, JSONObject data) throws JSONException {
    if ("doublestacked".equals(data.optString("copystyle"))) {
        // e.g. http://vopac.nlg.gr/Record/393668/Holdings#tabnav
        // for Athens_GreekNationalLibrary
        Element container = doc.select(".tab-container").first();
        String branch = "";
        for (Element child : container.children()) {
            if (child.tagName().equals("h5")) {
                branch = child.text();
            } else if (child.tagName().equals("table")) {
                int i = 0;
                String callNumber = "";
                for (Element row : child.select("tr")) {
                    if (i == 0) {
                        callNumber = row.child(1).text();
                    } else {
                        Copy copy = new Copy();
                        copy.setBranch(branch);
                        copy.setShelfmark(callNumber);
                        copy.setBarcode(row.child(0).text());
                        copy.setStatus(row.child(1).text());
                        res.addCopy(copy);
                    }
                    i++;
                }
            }
        }
    } else if ("stackedtable".equals(data.optString("copystyle"))) {
        // e.g. http://search.lib.auth.gr/Record/376356
        // or https://katalog.ub.uni-leipzig.de/Record/0000196115
        // or https://www.stadt-muenster.de/opac2/Record/0367968
        Element container = doc.select(".recordsubcontent, .tab-container").first();
        // .tab-container is used in Muenster.
        String branch = "";
        JSONObject copytable = data.getJSONObject("copytable");
        for (Element child : container.children()) {
            if (child.tagName().equals("div")) {
                child = child.child(0);
            }
            if (child.tagName().equals("h3")) {
                branch = child.text();
            } else if (child.tagName().equals("table")) {
                if (child.select("caption").size() > 0) {
                    // Leipzig_Uni
                    branch = child.select("caption").first().ownText();
                }
                int i = 0;
                String callNumber = null;
                if ("headrow".equals(copytable.optString("signature"))) {
                    callNumber = child.select("tr").get(0).child(1).text();
                }
                for (Element row : child.select("tr")) {
                    if (i < copytable.optInt("_offset", 0)) {
                        i++;
                        continue;
                    }
                    Copy copy = new Copy();
                    if (callNumber != null) {
                        copy.setShelfmark(callNumber);
                    }
                    copy.setBranch(branch);
                    Iterator<?> keys = copytable.keys();
                    while (keys.hasNext()) {
                        String key = (String) keys.next();
                        if (key.startsWith("_"))
                            continue;
                        if (copytable.optString(key, "").contains("/")) {
                            // Leipzig_Uni
                            String[] splitted = copytable.getString(key).split("/");
                            int col = Integer.parseInt(splitted[0]);
                            int line = Integer.parseInt(splitted[1]);
                            int j = 0;
                            for (Node node : row.child(col).childNodes()) {
                                if (node instanceof Element) {
                                    if (((Element) node).tagName().equals("br")) {
                                        j++;
                                    } else if (j == line) {
                                        copy.set(key, ((Element) node).text());
                                    }
                                } else if (node instanceof TextNode && j == line && !((TextNode) node).text().trim().equals("")) {
                                    copy.set(key, ((TextNode) node).text());
                                }
                            }
                        } else {
                            // Thessaloniki_University
                            if (copytable.optInt(key, -1) == -1)
                                continue;
                            String value = row.child(copytable.getInt(key)).text();
                            copy.set(key, value);
                        }
                    }
                    res.addCopy(copy);
                    i++;
                }
            }
        }
    }
}
Also used : JSONObject(org.json.JSONObject) Copy(de.geeksfactory.opacclient.objects.Copy) Element(org.jsoup.nodes.Element) TextNode(org.jsoup.nodes.TextNode) Node(org.jsoup.nodes.Node) TextNode(org.jsoup.nodes.TextNode)

Example 7 with Copy

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

the class WebOpacNet method parse_detail.

private DetailedItem parse_detail(String text) throws OpacErrorException {
    try {
        DetailedItem result = new DetailedItem();
        JSONObject json = new JSONObject(text);
        result.setTitle(json.getString("titel"));
        result.setCover(json.getString("imageurl"));
        result.setId(json.getString("medid"));
        // Details
        JSONArray info = json.getJSONArray("medium");
        for (int i = 0; i < info.length(); i++) {
            JSONObject detailJson = info.getJSONObject(i);
            String name = detailJson.getString("bez");
            String value = "";
            JSONArray values = detailJson.getJSONArray("values");
            for (int j = 0; j < values.length(); j++) {
                JSONObject valJson = values.getJSONObject(j);
                if (j != 0) {
                    value += ", ";
                }
                String content = valJson.getString("dval");
                content = content.replaceAll("<span[^>]*>", "");
                content = content.replaceAll("</span>", "");
                value += content;
            }
            Detail detail = new Detail(name, value);
            result.addDetail(detail);
        }
        // Copies
        JSONArray copies = json.getJSONArray("exemplare");
        for (int i = 0; i < copies.length(); i++) {
            JSONObject copyJson = copies.getJSONObject(i);
            Copy copy = new Copy();
            JSONArray values = copyJson.getJSONArray("rows");
            for (int j = 0; j < values.length(); j++) {
                JSONObject valJson = values.getJSONObject(j);
                String name = valJson.getString("bez");
                String value = valJson.getJSONArray("values").getJSONObject(0).getString("dval");
                if (!value.equals("")) {
                    switch(name) {
                        case "Exemplarstatus":
                            copy.setStatus(value);
                            break;
                        case "Signatur":
                            copy.setShelfmark(value);
                            break;
                        case "Standort":
                            copy.setLocation(value);
                            break;
                        case "Themenabteilung":
                            if (copy.getDepartment() != null) {
                                value = copy.getDepartment() + value;
                            }
                            copy.setDepartment(value);
                            break;
                        case "Themenbereich":
                            if (copy.getDepartment() != null) {
                                value = copy.getDepartment() + value;
                            }
                            copy.setDepartment(value);
                            break;
                    }
                }
            }
            result.addCopy(copy);
        }
        return result;
    } catch (JSONException e) {
        e.printStackTrace();
        throw new OpacErrorException(stringProvider.getFormattedString(StringProvider.INTERNAL_ERROR_WITH_DESCRIPTION, e.getMessage()));
    }
}
Also used : JSONObject(org.json.JSONObject) Copy(de.geeksfactory.opacclient.objects.Copy) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) DetailedItem(de.geeksfactory.opacclient.objects.DetailedItem) Detail(de.geeksfactory.opacclient.objects.Detail)

Example 8 with Copy

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

the class WinBiap method reservation.

@Override
public ReservationResult reservation(DetailedItem item, Account account, int useraction, String selection) throws IOException {
    if (selection == null) {
        // Which copy?
        List<Map<String, String>> options = new ArrayList<>();
        for (Copy copy : item.getCopies()) {
            if (copy.getResInfo() == null)
                continue;
            Map<String, String> option = new HashMap<>();
            option.put("key", copy.getResInfo());
            option.put("value", copy.getBarcode() + " - " + copy.getBranch() + " - " + copy.getReturnDate());
            options.add(option);
        }
        if (options.size() == 0) {
            return new ReservationResult(MultiStepResult.Status.ERROR, stringProvider.getString(StringProvider.NO_COPY_RESERVABLE));
        } else if (options.size() == 1) {
            return reservation(item, account, useraction, options.get(0).get("key"));
        } else {
            ReservationResult res = new ReservationResult(MultiStepResult.Status.SELECTION_NEEDED);
            res.setSelection(options);
            return res;
        }
    } else {
        // Reservation
        // the URL stored in selection might be absolute (WinBiap 4.3) or relative (4.2)
        String reservationUrl = new URL(new URL(opac_url), selection).toString();
        // the URL stored in selection contains "=" and other things inside params
        // and will be messed up by our cleanUrl function, therefore we use a direct HttpGet
        Document doc = Jsoup.parse(httpGet(reservationUrl, getDefaultEncoding()));
        if (doc.select("[id$=LabelLoginMessage]").size() > 0) {
            doc.select("[id$=TextBoxLoginName]").val(account.getName());
            doc.select("[id$=TextBoxLoginPassword]").val(account.getPassword());
            FormElement form = (FormElement) doc.select("form").first();
            List<Connection.KeyVal> formData = form.formData();
            FormBody.Builder paramBuilder = new FormBody.Builder();
            for (Connection.KeyVal kv : formData) {
                if (!kv.key().contains("Button") || kv.key().endsWith("ButtonLogin")) {
                    paramBuilder.add(kv.key(), kv.value());
                }
            }
            doc = Jsoup.parse(httpPost(opac_url + "/user/" + form.attr("action"), paramBuilder.build(), getDefaultEncoding()));
        }
        FormElement confirmationForm = (FormElement) doc.select("form").first();
        List<Connection.KeyVal> formData = confirmationForm.formData();
        FormBody.Builder paramBuilder = new FormBody.Builder();
        for (Connection.KeyVal kv : formData) {
            if (!kv.key().contains("Button") || kv.key().endsWith("ButtonVorbestOk")) {
                paramBuilder.add(kv.key(), kv.value());
            }
        }
        httpPost(opac_url + "/user/" + confirmationForm.attr("action"), paramBuilder.build(), getDefaultEncoding());
        return new ReservationResult(MultiStepResult.Status.OK);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FormBody(okhttp3.FormBody) Connection(org.jsoup.Connection) Document(org.jsoup.nodes.Document) URL(java.net.URL) FormElement(org.jsoup.nodes.FormElement) Copy(de.geeksfactory.opacclient.objects.Copy) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with Copy

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

the class Littera method getResultById.

@Override
public DetailedItem getResultById(String id, String homebranch) throws IOException, OpacErrorException {
    if (!initialised) {
        start();
    }
    final String html = httpGet(getApiUrl() + "&view=detail&id=" + id, getDefaultEncoding());
    final Document doc = Jsoup.parse(html);
    final Element detailData = doc.select(".detailData").first();
    final Element detailTable = detailData.select("table.titel").first();
    final Element availabilityTable = doc.select(".bibliothek table").first();
    final DetailedItem result = new DetailedItem();
    final Copy copy = new Copy();
    result.addCopy(copy);
    result.setId(id);
    result.setCover(getCover(doc));
    result.setTitle(detailData.select("h3").first().text());
    result.setMediaType(MEDIA_TYPES.get(getCellContent(detailTable, "Medienart|Type of media")));
    copy.setStatus(getCellContent(availabilityTable, "Verfügbar|Available"));
    copy.setReturnDate(parseCopyReturn(getCellContent(availabilityTable, "Exemplare verliehen|Copies lent")));
    copy.setReservations(getCellContent(availabilityTable, "Reservierungen|Reservations"));
    for (final Element tr : detailTable.select("tr")) {
        final String desc = tr.child(0).text();
        final String content = tr.child(1).text();
        if (desc != null && !desc.trim().equals("")) {
            result.addDetail(new Detail(desc, content));
        } else if (!result.getDetails().isEmpty()) {
            final Detail lastDetail = result.getDetails().get(result.getDetails().size() - 1);
            lastDetail.setHtml(true);
            lastDetail.setContent(lastDetail.getContent() + "\n" + content);
        }
    }
    return result;
}
Also used : Copy(de.geeksfactory.opacclient.objects.Copy) Element(org.jsoup.nodes.Element) DetailedItem(de.geeksfactory.opacclient.objects.DetailedItem) Document(org.jsoup.nodes.Document) Detail(de.geeksfactory.opacclient.objects.Detail)

Example 10 with Copy

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

the class Pica method parse_result.

protected DetailedItem parse_result(String html) {
    Document doc = Jsoup.parse(html);
    doc.setBaseUri(opac_url);
    DetailedItem result = new DetailedItem();
    for (Element a : doc.select("a[href*=PPN")) {
        Map<String, String> hrefq = getQueryParamsFirst(a.absUrl("href"));
        String ppn = hrefq.get("PPN");
        result.setId(ppn);
        break;
    }
    // GET COVER
    if (doc.select("img[title=Titelbild]").size() > 0) {
        result.setCover(doc.select("img[title=Titelbild]").first().absUrl("src"));
    } else if (doc.select("td.preslabel:contains(ISBN) + td.presvalue").size() > 0) {
        Element isbnElement = doc.select("td.preslabel:contains(ISBN) + td.presvalue").first();
        String isbn = isbnElement.text().trim();
        if (!isbn.equals("")) {
            result.setCover(ISBNTools.getAmazonCoverURL(isbn, true));
        }
    }
    // GET TITLE AND SUBTITLE
    String titleAndSubtitle;
    Element titleAndSubtitleElem = null;
    String titleRegex = ".*(Titel|Aufsatz|Zeitschrift|Gesamttitel" + "|Title|Article|Periodical|Collective\\stitle" + "|Titre|Article|P.riodique|Titre\\sg.n.ral).*";
    String selector = "td.preslabel:matches(" + titleRegex + ") + td.presvalue";
    if (doc.select(selector).size() > 0) {
        titleAndSubtitleElem = doc.select(selector).first();
        titleAndSubtitle = titleAndSubtitleElem.text().trim();
        int slashPosition = Math.min(titleAndSubtitle.indexOf("/"), titleAndSubtitle.indexOf(":"));
        String title;
        if (slashPosition > 0) {
            title = titleAndSubtitle.substring(0, slashPosition).trim();
            String subtitle = titleAndSubtitle.substring(slashPosition + 1).trim();
            result.addDetail(new Detail(stringProvider.getString(StringProvider.SUBTITLE), subtitle));
        } else {
            title = titleAndSubtitle;
        }
        result.setTitle(title);
    } else {
        result.setTitle("");
    }
    // Details
    int line = 0;
    Elements lines = doc.select("td.preslabel + td.presvalue");
    if (titleAndSubtitleElem != null) {
        lines.remove(titleAndSubtitleElem);
    }
    for (Element element : lines) {
        Element titleElem = element.firstElementSibling();
        String detail = "";
        if (element.select("div").size() > 1 && element.select("div").text().equals(element.text())) {
            boolean first = true;
            for (Element div : element.select("div")) {
                if (!div.text().replace("\u00a0", " ").trim().equals("")) {
                    if (!first) {
                        detail += "\n" + div.text().replace("\u00a0", " ").trim();
                    } else {
                        detail += div.text().replace("\u00a0", " ").trim();
                        first = false;
                    }
                }
            }
        } else {
            detail = element.text().replace("\u00a0", " ").trim();
        }
        String title = titleElem.text().replace("\u00a0", " ").trim();
        if (element.select("hr").size() > 0 || element.text().trim().equals("")) // after the separator we get the copies
        {
            break;
        }
        if (detail.length() == 0 && title.length() == 0) {
            line++;
            continue;
        }
        if (title.contains(":")) {
            // remove colon
            title = title.substring(0, title.indexOf(":"));
        }
        result.addDetail(new Detail(title, detail));
        if (element.select("a").size() == 1 && !element.select("a").get(0).text().trim().equals("")) {
            String url = element.select("a").first().absUrl("href");
            if (!url.startsWith(opac_url)) {
                result.addDetail(new Detail(stringProvider.getString(StringProvider.LINK), url));
            }
        }
        line++;
    }
    // next line after separator
    line++;
    // Copies
    Copy copy = new Copy();
    String location = "";
    // reservation info will be stored as JSON
    JSONArray reservationInfo = new JSONArray();
    while (line < lines.size()) {
        Element element = lines.get(line);
        if (element.select("hr").size() == 0 && !element.text().trim().equals("")) {
            Element titleElem = element.firstElementSibling();
            String detail = element.text().trim();
            String title = titleElem.text().replace("\u00a0", " ").trim();
            if (detail.length() == 0 && title.length() == 0) {
                line++;
                continue;
            }
            if (title.contains("Standort") || title.contains("Vorhanden in") || title.contains("Location")) {
                location += detail;
            } else if (title.contains("Sonderstandort")) {
                location += " - " + detail;
            } else if (title.contains("Systemstelle") || title.contains("Sachgebiete") || title.contains("Subject")) {
                copy.setDepartment(detail);
            } else if (title.contains("Fachnummer") || title.contains("locationnumber") || title.contains("Schlagwörter")) {
                copy.setLocation(detail);
            } else if (title.contains("Signatur") || title.contains("Shelf mark")) {
                copy.setShelfmark(detail);
            } else if (title.contains("Anmerkung")) {
                location += " (" + detail + ")";
            } else if (title.contains("Link")) {
                result.addDetail(new Detail(title.replace(":", "").trim(), detail));
            } else if (title.contains("Status") || title.contains("Ausleihinfo") || title.contains("Ausleihstatus") || title.contains("Request info")) {
                // Find return date
                Pattern pattern = Pattern.compile("(till|bis) (\\d{2}-\\d{2}-\\d{4})");
                Matcher matcher = pattern.matcher(detail);
                if (matcher.find()) {
                    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd-MM-yyyy").withLocale(Locale.GERMAN);
                    try {
                        copy.setStatus(detail.substring(0, matcher.start() - 1).trim());
                        copy.setReturnDate(fmt.parseLocalDate(matcher.group(2)));
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                        copy.setStatus(detail);
                    }
                } else {
                    copy.setStatus(detail);
                }
                // Get reservation info
                if (element.select("a:has(img[src*=inline_arrow])").size() > 0) {
                    Element a = element.select("a:has(img[src*=inline_arrow])").first();
                    boolean multipleCopies = a.text().matches(".*(Exemplare|Volume list).*");
                    JSONObject reservation = new JSONObject();
                    try {
                        reservation.put("multi", multipleCopies);
                        reservation.put("link", _extract_url(a));
                        reservation.put("desc", location);
                        reservationInfo.put(reservation);
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }
                    result.setReservable(true);
                }
            }
        } else {
            copy.setBranch(location);
            if (copy.notEmpty()) {
                result.addCopy(copy);
            }
            location = "";
            copy = new Copy();
        }
        line++;
    }
    if (copy.notEmpty()) {
        copy.setBranch(location);
        result.addCopy(copy);
    }
    if (reservationInfo.length() == 0) {
        // as details, we still want to use it.
        if (doc.select("td a:has(img[src*=inline_arrow])").size() > 0) {
            Element a = doc.select("td a:has(img[src*=inline_arrow])").first();
            boolean multipleCopies = a.text().matches(".*(Exemplare|Volume list).*");
            JSONObject reservation = new JSONObject();
            try {
                reservation.put("multi", multipleCopies);
                reservation.put("link", _extract_url(a));
                reservation.put("desc", location);
                reservationInfo.put(reservation);
            } catch (JSONException e1) {
                e1.printStackTrace();
            }
            result.setReservable(true);
        }
    }
    result.setReservation_info(reservationInfo.toString());
    // Volumes
    if (doc.select("a[href^=FAM?PPN=]").size() > 0) {
        String href = doc.select("a[href^=FAM?PPN=]").attr("href");
        String ppn = getQueryParamsFirst(href).get("PPN");
        Map<String, String> data = new HashMap<>();
        data.put("ppn", ppn);
        result.setVolumesearch(data);
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) Element(org.jsoup.nodes.Element) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) 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