Search in sources :

Example 6 with ReservedItem

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

the class Heidi method account.

@Override
public AccountData account(Account account) throws IOException, JSONException, OpacErrorException {
    login(account);
    String html;
    Document doc;
    AccountData adata = new AccountData(account.getId());
    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
    html = httpGet(opac_url + "/konto.cgi?sess=" + sessid, getDefaultEncoding());
    doc = Jsoup.parse(html);
    doc.setBaseUri(opac_url + "/");
    for (Element td : doc.select("table.konto td")) {
        if (td.text().contains("Offene")) {
            String text = td.text().trim().replaceAll("Offene[^0-9]+Geb.+hren:[^0-9]+([0-9.," + "]+)[^0-9€A-Z]*(€|EUR|CHF|Fr.)", "$1 $2");
            adata.setPendingFees(text);
        }
    }
    List<LentItem> lent = new ArrayList<>();
    for (Element tr : doc.select("table.kontopos tr")) {
        LentItem item = new LentItem();
        Element desc = tr.child(1).select("label").first();
        String dates = tr.child(2).text().trim();
        if (tr.child(1).select("a").size() > 0) {
            String kk = getQueryParamsFirst(tr.child(1).select("a").first().absUrl("href")).get("katkey");
            item.setId(kk);
        }
        int i = 0;
        for (Node node : desc.childNodes()) {
            if (node instanceof TextNode) {
                String text = ((TextNode) node).text().trim();
                if (i == 0) {
                    item.setAuthor(text);
                } else if (i == 1) {
                    item.setTitle(text);
                } else if (text.contains("Mediennummer")) {
                    item.setBarcode(text.replace("Mediennummer: ", ""));
                }
                i++;
            }
        }
        if (tr.child(0).select("input").size() == 1) {
            item.setProlongData(tr.child(0).select("input").first().val());
            item.setRenewable(true);
        } else {
            item.setProlongData("§" + tr.child(0).select("span").first().attr("class"));
            item.setRenewable(false);
        }
        String todate = dates;
        if (todate.contains("-")) {
            String[] datesplit = todate.split("-");
            todate = datesplit[1].trim();
        }
        try {
            item.setDeadline(fmt.parseLocalDate(todate.substring(0, 10)));
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        lent.add(item);
    }
    adata.setLent(lent);
    List<ReservedItem> reservations = new ArrayList<>();
    html = httpGet(opac_url + "/konto.cgi?konto=v&sess=" + sessid, getDefaultEncoding());
    reservations.addAll(parse_reservations(html));
    html = httpGet(opac_url + "/konto.cgi?konto=b&sess=" + sessid, getDefaultEncoding());
    reservations.addAll(parse_reservations(html));
    adata.setReservations(reservations);
    return adata;
}
Also used : Element(org.jsoup.nodes.Element) TextNode(org.jsoup.nodes.TextNode) Node(org.jsoup.nodes.Node) ArrayList(java.util.ArrayList) TextNode(org.jsoup.nodes.TextNode) Document(org.jsoup.nodes.Document) AccountData(de.geeksfactory.opacclient.objects.AccountData) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) LentItem(de.geeksfactory.opacclient.objects.LentItem) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 7 with ReservedItem

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

the class WinBiap method parseResList.

static List<ReservedItem> parseResList(Document doc, StringProvider stringProvider, JSONObject data) {
    List<ReservedItem> reservations = new ArrayList<>();
    // the account page differs between WinBiap versions 4.2 and 4.3
    boolean winBiap43;
    if (doc.select("tr[id*=GridViewReservation]").size() > 0) {
        winBiap43 = false;
    } else {
        winBiap43 = true;
    }
    // 4.4: id=...DetailItemMain_0_rowBorrow_0
    for (Element tr : doc.select(winBiap43 ? ".detailTable tr[id*=_rowBorrow]" : "tr[id*=GridViewReservation]")) {
        ReservedItem item = new ReservedItem();
        Element detailsTr = winBiap43 ? tr.nextElementSibling() : tr;
        // the second column contains an img tag with the cover
        if (detailsTr.select(".cover, img[id*=ImageCover]").size() > 0) {
            // find media ID using cover URL
            Element cover = detailsTr.select(".cover, img[id*=ImageCover]").first();
            String src = cover.attr("abs:data-src");
            if (src.equals(""))
                src = cover.attr("abs:src");
            Map<String, String> params = getQueryParamsFirst(src);
            if (params.containsKey("catid"))
                item.setId(params.get("catid"));
            // find media type
            SearchResult.MediaType mt = getMediaType(cover, data);
            if (mt != null) {
                item.setFormat(stringProvider.getMediaTypeName(mt));
                item.setMediaType(mt);
            }
            // set cover if it's not the media type image
            if (!src.equals(cover.attr("grp")))
                item.setCover(src);
        }
        item.setStatus(nullIfEmpty(winBiap43 ? detailsTr.select("[id$=labelStatus], [id*=labelStatus_]").text() : tr.select("[id$=ImageBorrow]").attr("title")));
        item.setAuthor(nullIfEmpty(tr.select("[id$=LabelAutor], .autor").text()));
        item.setTitle(nullIfEmpty(tr.select("[id$=LabelTitle], .title").text()));
        item.setBranch(nullIfEmpty(detailsTr.select("[id$=LabelBranch], [id$=labelBranch], [id*=labelBranch_]").text()));
        item.setFormat(nullIfEmpty(detailsTr.select("[id$=Label_Mediengruppe], [id$=labelMediagroup], [id*=labelMediagroup_]").text()));
        if (tr.select("a[id$=ImageReservationDelete]").size() > 0) {
            String javascript = tr.select("a[id$=ImageReservationDelete]").attr("onclick");
            /*
                    Javascript example:

                    javascript:DeleteReservation(
                    '#ctl00_ContentPlaceHolderMain_GridViewReservation_ctl02',
                    '#ctl00_ContentPlaceHolderMain_GridViewReservation_ctl02_ImageReservationDelete',
                    'cmVzZXJ2YXRpb25JZD00MDk1JmFtcDtyZWFkZXJJZD05MzIwJmFtcDttb2RlPTE=-f2yu2300+t4=',
                    '../service/UserService.ashx',
                    'Vorbestellung: \'Beck, Rufus - Harry Potter Folge 4. Harry Potter und der
                    Feuerkelch\' wirklich löschen?',
                    '#ctl00_ContentPlaceHolderMain_LabelAccountTableResult',
                    'Sie haben derzeit $ Medien vorbestellt!');
                    return false;

                    We need the 3rd parameter (Base64 string) and will find it
                    using the following massive RegEx.
                 */
            Pattern regex = Pattern.compile("javascript:DeleteReservation\\('" + "(?:\\\\[\\\\']|[^\\\\'])*'\\s*,\\s*'(?:\\\\[\\\\']|[^\\\\'])*'\\s*,\\s*'" + "((?:\\\\[\\\\']|[^\\\\'])*)'\\s*,\\s*'(?:\\\\[\\\\']|[^\\\\'])*'\\s*," + "\\s*'(?:\\\\[\\\\']|[^\\\\'])*'\\s*,\\s*'(?:\\\\[\\\\']|[^\\\\'])*'\\s*," + "\\s*'(?:\\\\[\\\\']|[^\\\\'])*'\\s*\\);");
            Matcher matcher = regex.matcher(javascript);
            if (matcher.find()) {
                String base64 = matcher.group(1);
                item.setCancelData(base64);
            }
        } else if (detailsTr.select("input[id*=_hiddenValueDetail][value]").size() > 0) {
            item.setCancelData(detailsTr.select("input[id*=_hiddenValueDetail][value]").attr("value"));
        }
        reservations.add(item);
    }
    return reservations;
}
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) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) SearchResult(de.geeksfactory.opacclient.objects.SearchResult)

Example 8 with ReservedItem

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

the class Zones method account.

@Override
public AccountData account(Account acc) throws IOException, JSONException, OpacErrorException {
    Document login = login(acc);
    if (login == null) {
        return null;
    }
    AccountData res = new AccountData(acc.getId());
    AccountLinks accountLinks = new AccountLinks(login, res);
    String lentLink = accountLinks.getLentLink();
    String resLink = accountLinks.getResLink();
    if (lentLink == null) {
        return null;
    }
    List<LentItem> lentItems = new ArrayList<>();
    String lentUrl = opac_url + "/" + lentLink.replace("utf-8?Method", "utf-8&Method");
    String lentHtml = httpGet(lentUrl, getDefaultEncoding());
    Document lentDoc = Jsoup.parse(lentHtml);
    lentDoc.setBaseUri(lentUrl);
    loadMediaList(lentDoc, lentItems);
    res.setLent(lentItems);
    // In Koeln, the reservations link only doesn't show on the overview page
    if (resLink == null) {
        for (Element a : lentDoc.select("a.AccountMenuLink")) {
            if (a.text().contains("Vormerkungen")) {
                resLink = a.attr("href");
            }
        }
    }
    List<ReservedItem> reservedItems = new ArrayList<>();
    String resUrl = opac_url + "/" + resLink;
    String resHtml = httpGet(resUrl, getDefaultEncoding());
    Document resDoc = Jsoup.parse(resHtml);
    resDoc.setBaseUri(resUrl);
    loadResList(resDoc, reservedItems);
    res.setReservations(reservedItems);
    return res;
}
Also used : AccountData(de.geeksfactory.opacclient.objects.AccountData) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) LentItem(de.geeksfactory.opacclient.objects.LentItem) Document(org.jsoup.nodes.Document)

Example 9 with ReservedItem

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

the class SISIS method account.

@Override
public AccountData account(Account acc) throws IOException, JSONException, OpacErrorException {
    // TODO: Is this necessary?
    start();
    int resultNum;
    if (!login(acc)) {
        return null;
    }
    // Geliehene Medien
    String html = httpGet(opac_url + "/userAccount.do?methodToCall=showAccount&typ=1", ENCODING);
    List<LentItem> medien = new ArrayList<>();
    Document doc = Jsoup.parse(html);
    doc.setBaseUri(opac_url);
    parse_medialist(medien, doc, 1, data);
    // additional pages
    Map<String, Integer> links = getAccountPageLinks(doc);
    for (Map.Entry<String, Integer> link : links.entrySet()) {
        html = httpGet(link.getKey(), ENCODING);
        parse_medialist(medien, Jsoup.parse(html), link.getValue(), data);
    }
    if (doc.select("#label1").size() > 0) {
        resultNum = 0;
        String rNum = doc.select("#label1").first().text().trim().replaceAll(".*\\(([0-9]*)\\).*", "$1");
        if (rNum.length() > 0) {
            resultNum = Integer.parseInt(rNum);
        }
        assert (resultNum == medien.size());
    }
    // Ordered media ("Bestellungen")
    html = httpGet(opac_url + "/userAccount.do?methodToCall=showAccount&typ=6", ENCODING);
    List<ReservedItem> reserved = new ArrayList<>();
    doc = Jsoup.parse(html);
    doc.setBaseUri(opac_url);
    parse_reslist("6", reserved, doc, 1);
    Elements label6 = doc.select("#label6");
    // additional pages
    links = getAccountPageLinks(doc);
    for (Map.Entry<String, Integer> link : links.entrySet()) {
        html = httpGet(link.getKey(), ENCODING);
        parse_reslist("6", reserved, Jsoup.parse(html), link.getValue());
    }
    // Prebooked media ("Vormerkungen")
    html = httpGet(opac_url + "/userAccount.do?methodToCall=showAccount&typ=7", ENCODING);
    doc = Jsoup.parse(html);
    doc.setBaseUri(opac_url);
    parse_reslist("7", reserved, doc, 1);
    // additional pages
    links = getAccountPageLinks(doc);
    for (Map.Entry<String, Integer> link : links.entrySet()) {
        html = httpGet(link.getKey(), ENCODING);
        parse_reslist("7", reserved, Jsoup.parse(html), link.getValue());
    }
    if (label6.size() > 0 && doc.select("#label7").size() > 0) {
        resultNum = 0;
        String rNum = label6.text().trim().replaceAll(".*\\(([0-9]*)\\).*", "$1");
        if (rNum.length() > 0) {
            resultNum = Integer.parseInt(rNum);
        }
        rNum = doc.select("#label7").text().trim().replaceAll(".*\\(([0-9]*)\\).*", "$1");
        if (rNum.length() > 0) {
            resultNum += Integer.parseInt(rNum);
        }
        assert (resultNum == reserved.size());
    }
    AccountData res = new AccountData(acc.getId());
    if (doc.select("#label8").size() > 0) {
        String text = doc.select("#label8").first().text().trim();
        if (text.matches("Geb.+hren[^\\(]+\\(([0-9.,]+)[^0-9€A-Z]*(€|EUR|CHF|Fr)\\)")) {
            text = text.replaceAll("Geb.+hren[^\\(]+\\(([0-9.,]+)[^0-9€A-Z]*(€|EUR|CHF|Fr)\\)", "$1 $2");
            res.setPendingFees(text);
        }
    }
    Pattern p = Pattern.compile("[^0-9.]*", Pattern.MULTILINE);
    if (doc.select(".box3").size() > 0) {
        for (Element box : doc.select(".box3")) {
            if (box.select("strong").size() == 1) {
                String text = box.select("strong").text();
                if (text.equals("Jahresgebühren")) {
                    text = box.text();
                    text = p.matcher(text).replaceAll("");
                    res.setValidUntil(text);
                }
            }
        }
    }
    res.setLent(medien);
    res.setReservations(reserved);
    return res;
}
Also used : Pattern(java.util.regex.Pattern) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) AccountData(de.geeksfactory.opacclient.objects.AccountData) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) LentItem(de.geeksfactory.opacclient.objects.LentItem) Map(java.util.Map) HashMap(java.util.HashMap)

Example 10 with ReservedItem

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

the class SISIS method parse_reslist.

protected void parse_reslist(String type, List<ReservedItem> reservations, Document doc, int offset) {
    Elements copytrs = doc.select(".data tr");
    doc.setBaseUri(opac_url);
    int trs = copytrs.size();
    if (trs == 1) {
        return;
    }
    assert (trs > 0);
    for (int i = 1; i < trs; i++) {
        Element tr = copytrs.get(i);
        ReservedItem item = new ReservedItem();
        if (tr.text().contains("keine Daten") || tr.children().size() == 1) {
            return;
        }
        item.setTitle(tr.child(1).select("strong").text().trim());
        try {
            String[] rowsplit1 = tr.child(1).html().split("<br[ /]*>");
            String[] rowsplit2 = tr.child(2).html().split("<br[ /]*>");
            if (rowsplit1.length > 1)
                item.setAuthor(rowsplit1[1].trim());
            if (rowsplit2.length > 2)
                item.setBranch(rowsplit2[2].trim());
            if (rowsplit2.length > 2)
                item.setStatus(rowsplit2[0].trim());
            if (tr.select("a").size() == 1) {
                item.setCancelData(type + "$" + offset + "$" + tr.select("a").attr("abs:href").split("\\?")[1]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        reservations.add(item);
    }
    assert (reservations.size() == trs - 1);
}
Also used : Element(org.jsoup.nodes.Element) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) Elements(org.jsoup.select.Elements) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException)

Aggregations

ReservedItem (de.geeksfactory.opacclient.objects.ReservedItem)34 ArrayList (java.util.ArrayList)20 Element (org.jsoup.nodes.Element)17 LentItem (de.geeksfactory.opacclient.objects.LentItem)16 AccountData (de.geeksfactory.opacclient.objects.AccountData)13 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)8 Document (org.jsoup.nodes.Document)8 Elements (org.jsoup.select.Elements)8 JSONObject (org.json.JSONObject)7 Test (org.junit.Test)7 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)6 DummyStringProvider (de.geeksfactory.opacclient.i18n.DummyStringProvider)4 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 Pattern (java.util.regex.Pattern)3 JSONException (org.json.JSONException)3 Intent (android.content.Intent)2 Account (de.geeksfactory.opacclient.objects.Account)2 Report (de.geeksfactory.opacclient.reporting.Report)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2