Search in sources :

Example 21 with SearchRequestResult

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

the class Heidi method parse_search.

private SearchRequestResult parse_search(String html, int page) {
    Document doc = Jsoup.parse(html);
    doc.setBaseUri(opac_url);
    int results_total = 0;
    if (doc.select("#heiditreffer").size() > 0) {
        String resstr = doc.select("#heiditreffer").text();
        String resnum = resstr.replaceAll("\\(([0-9.]+)([^0-9]*)\\)", "$1").replace(".", "");
        results_total = Integer.parseInt(resnum);
    }
    Elements table = doc.select("table.treffer tr");
    List<SearchResult> results = new ArrayList<>();
    for (int i = 0; i < table.size(); i++) {
        Element tr = table.get(i);
        SearchResult sr = new SearchResult();
        StringBuilder description = null;
        String author = "";
        for (Element link : tr.select("a")) {
            String kk = getQueryParamsFirst(link.absUrl("href")).get("katkey");
            if (kk != null) {
                sr.setId(kk);
                break;
            }
        }
        if (tr.select("span.Z3988").size() == 1) {
            // Luckily there is a <span class="Z3988"> item which provides
            // data in a standardized format.
            String zdata = tr.select("span.Z3988").attr("title").replace(";", "%3B").replace(":", "%3A").replace("/", "%2F");
            boolean hastitle = false;
            description = new StringBuilder();
            List<NameValuePair> z3988data = parse_z3988data(zdata);
            for (NameValuePair nv : z3988data) {
                if (nv.getValue() != null) {
                    if (!nv.getValue().trim().equals("")) {
                        if (nv.getName().equals("rft.btitle") && !hastitle) {
                            description.append("<b>").append(nv.getValue()).append("</b>");
                            hastitle = true;
                        } else if (nv.getName().equals("rft.atitle") && !hastitle) {
                            description.append("<b>").append(nv.getValue()).append("</b>");
                            hastitle = true;
                        } else if (nv.getName().equals("rft.au")) {
                            author = nv.getValue();
                        } else if (nv.getName().equals("rft.aufirst")) {
                            author = author + ", " + nv.getValue();
                        } else if (nv.getName().equals("rft.aulast")) {
                            author = nv.getValue();
                        } else if (nv.getName().equals("rft.date")) {
                            description.append("<br />").append(nv.getValue());
                        }
                    }
                }
            }
        }
        if (!"".equals(author)) {
            author = author + "<br />";
        }
        sr.setInnerhtml(author + description.toString());
        if (tr.select(".kurzstat").size() > 0) {
            String stattext = tr.select(".kurzstat").first().text();
            if (stattext.contains("ausleihbar")) {
                sr.setStatus(Status.GREEN);
            } else if (stattext.contains("online")) {
                sr.setStatus(Status.GREEN);
            } else if (stattext.contains("entliehen")) {
                sr.setStatus(Status.RED);
            } else if (stattext.contains("Präsenznutzung")) {
                sr.setStatus(Status.YELLOW);
            } else if (stattext.contains("bestellen")) {
                sr.setStatus(Status.YELLOW);
            }
        }
        if (tr.select(".typbild").size() > 0) {
            String typtext = tr.select(".typbild").first().text();
            if (typtext.contains("Buch")) {
                sr.setType(MediaType.BOOK);
            } else if (typtext.contains("DVD-ROM")) {
                sr.setType(MediaType.CD_SOFTWARE);
            } else if (typtext.contains("Online-Ressource")) {
                sr.setType(MediaType.EDOC);
            } else if (typtext.contains("DVD")) {
                sr.setType(MediaType.DVD);
            } else if (typtext.contains("Film")) {
                sr.setType(MediaType.MOVIE);
            } else if (typtext.contains("Zeitschrift")) {
                sr.setType(MediaType.MAGAZINE);
            } else if (typtext.contains("Musiknoten")) {
                sr.setType(MediaType.SCORE_MUSIC);
            } else if (typtext.contains("Bildliche Darstellung")) {
                sr.setType(MediaType.ART);
            } else if (typtext.contains("Zeitung")) {
                sr.setType(MediaType.NEWSPAPER);
            } else if (typtext.contains("Karte")) {
                sr.setType(MediaType.MAP);
            } else if (typtext.contains("Mehrteilig")) {
                sr.setType(MediaType.PACKAGE_BOOKS);
            }
        }
        results.add(sr);
    }
    // TODO
    return new SearchRequestResult(results, results_total, page);
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) SearchResult(de.geeksfactory.opacclient.objects.SearchResult) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) SearchRequestResult(de.geeksfactory.opacclient.objects.SearchRequestResult)

Example 22 with SearchRequestResult

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

the class Adis method searchGetPage.

@Override
public SearchRequestResult searchGetPage(int page) throws IOException, OpacErrorException {
    SearchRequestResult res = null;
    while (page != s_lastpage) {
        List<NameValuePair> nvpairs = s_pageform;
        int i = 0;
        List<Integer> indexes = new ArrayList<>();
        for (NameValuePair np : nvpairs) {
            if (np.getName().contains("$Toolbar_")) {
                indexes.add(i);
            }
            i++;
        }
        for (int j = indexes.size() - 1; j >= 0; j--) {
            nvpairs.remove((int) indexes.get(j));
        }
        int p;
        if (page > s_lastpage) {
            nvpairs.add(new BasicNameValuePair(s_nextbutton + ".x", "1"));
            nvpairs.add(new BasicNameValuePair(s_nextbutton + ".y", "1"));
            p = s_lastpage + 1;
        } else {
            nvpairs.add(new BasicNameValuePair(s_previousbutton + ".x", "1"));
            nvpairs.add(new BasicNameValuePair(s_previousbutton + ".y", "1"));
            p = s_lastpage - 1;
        }
        Document docresults = htmlPost(opac_url + ";jsessionid=" + s_sid, nvpairs);
        res = parse_search_wrapped(docresults, p);
    }
    return res;
}
Also used : NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) SearchRequestResult(de.geeksfactory.opacclient.objects.SearchRequestResult) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) Document(org.jsoup.nodes.Document)

Example 23 with SearchRequestResult

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

the class Adis method parse_search.

private SearchRequestResult parse_search(Document doc, int page) throws OpacErrorException, SingleResultFound {
    if (doc.select(".message h1").size() > 0 && doc.select("#right #R06").size() == 0) {
        throw new OpacErrorException(doc.select(".message h1").text());
    }
    if (doc.select("#OPACLI").text().contains("nicht gefunden")) {
        throw new OpacErrorException(stringProvider.getString(StringProvider.NO_RESULTS));
    }
    int total_result_count = -1;
    List<SearchResult> results = new ArrayList<>();
    if (doc.select("#R06").size() > 0) {
        Pattern patNum = Pattern.compile(".*Treffer: .* von ([0-9]+)[^0-9]*");
        Matcher matcher = patNum.matcher(doc.select("#R06").text().trim());
        if (matcher.matches()) {
            total_result_count = Integer.parseInt(matcher.group(1));
        } else if (doc.select("#R06").text().trim().endsWith("Treffer: 1")) {
            total_result_count = 1;
        }
    }
    if (doc.select("#R03").size() == 1 && doc.select("#R03").text().trim().endsWith("Treffer: 1")) {
        throw new SingleResultFound();
    }
    Pattern patId = Pattern.compile("javascript:.*htmlOnLink\\('([0-9A-Za-z]+)'\\)");
    int nr = 1;
    String selector_row, selector_link, selector_img, selector_num, selector_text;
    if (doc.select("table.rTable_table tbody").size() > 0) {
        selector_row = "table.rTable_table tbody tr";
        selector_link = ".rTable_td_text a";
        selector_text = ".rList_name";
        selector_img = ".rTable_td_img img, .rTable_td_text img";
        selector_num = "tr td:first-child";
    } else {
        // New version, e.g. Berlin
        selector_row = ".rList li.rList_li_even, .rList li.rList_li_odd";
        selector_link = ".rList_titel a";
        selector_text = ".rList_name";
        selector_img = ".rlist_icon img, .rList_titel img, .rList_medium .icon, .rList_availability .icon, .rList_img img";
        selector_num = ".rList_num";
    }
    for (Element tr : doc.select(selector_row)) {
        SearchResult res = new SearchResult();
        Element innerele = tr.select(selector_link).first();
        innerele.select("img").remove();
        String descr = innerele.html();
        for (Element n : tr.select(selector_text)) {
            String t = n.text().replace("\u00a0", " ").trim();
            if (t.length() > 0) {
                descr += "<br />" + t.trim();
            }
        }
        res.setInnerhtml(descr);
        try {
            res.setNr(Integer.parseInt(tr.select(selector_num).text().trim()));
        } catch (NumberFormatException e) {
            res.setNr(nr);
        }
        Matcher matcher = patId.matcher(tr.select(selector_link).first().attr("href"));
        if (matcher.matches()) {
            res.setId(matcher.group(1));
        }
        for (Element img : tr.select(selector_img)) {
            String ttext = img.attr("title");
            String src = img.attr("abs:src");
            if (types.containsKey(ttext)) {
                res.setType(types.get(ttext));
            } else if (ttext.contains("+") && types.containsKey(ttext.split("\\+")[0].trim())) {
                res.setType(types.get(ttext.split("\\+")[0].trim()));
            } else if (ttext.matches(".*ist verf.+gbar") || ttext.contains("is available") || img.attr("href").contains("verfu_ja")) {
                res.setStatus(SearchResult.Status.GREEN);
            } else if (ttext.matches(".*nicht verf.+gbar") || ttext.contains("not available") || img.attr("href").contains("verfu_nein")) {
                res.setStatus(SearchResult.Status.RED);
            }
        }
        results.add(res);
        nr++;
    }
    updatePageform(doc);
    s_lastpage = page;
    String nextButton = doc.select("input[title=nächster], input[title=Vorwärts blättern]").attr("name");
    String previousButton = doc.select("input[title=nächster], input[title=Rückwärts blättern]").attr("name");
    if (!nextButton.equals(""))
        s_nextbutton = nextButton;
    if (!previousButton.equals(""))
        s_previousbutton = previousButton;
    return new SearchRequestResult(results, total_result_count, page);
}
Also used : Pattern(java.util.regex.Pattern) SearchRequestResult(de.geeksfactory.opacclient.objects.SearchRequestResult) Matcher(java.util.regex.Matcher) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) SearchResult(de.geeksfactory.opacclient.objects.SearchResult)

Aggregations

SearchRequestResult (de.geeksfactory.opacclient.objects.SearchRequestResult)23 SearchResult (de.geeksfactory.opacclient.objects.SearchResult)21 ArrayList (java.util.ArrayList)19 Element (org.jsoup.nodes.Element)16 Document (org.jsoup.nodes.Document)13 Elements (org.jsoup.select.Elements)12 JSONException (org.json.JSONException)11 Matcher (java.util.regex.Matcher)9 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)8 IOException (java.io.IOException)8 Pattern (java.util.regex.Pattern)8 URISyntaxException (java.net.URISyntaxException)5 NameValuePair (org.apache.http.NameValuePair)5 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)5 SearchQuery (de.geeksfactory.opacclient.searchfields.SearchQuery)4 TextSearchField (de.geeksfactory.opacclient.searchfields.TextSearchField)4 MalformedURLException (java.net.MalformedURLException)4 URI (java.net.URI)4 HashMap (java.util.HashMap)4 OpacErrorException (de.geeksfactory.opacclient.apis.OpacApi.OpacErrorException)3