Search in sources :

Example 16 with AccountData

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

the class BibliothecaAccountTest method testParseMediaList.

@Test
public void testParseMediaList() throws OpacApi.OpacErrorException, JSONException, NotReachableException {
    String html = readResource("/bibliotheca/account/" + file);
    // we may not have all files for all libraries
    if (html == null)
        return;
    AccountData data = Bibliotheca.parse_account(new Account(), Jsoup.parse(html), new JSONObject(), reportHandler, new JSONObject(readResource("/bibliotheca/headers_lent.json")), new JSONObject(readResource("/bibliotheca/headers_reservations.json")));
    assertTrue(data.getLent().size() > 0);
    for (LentItem item : data.getLent()) {
        assertContainsData(item.getTitle());
        assertNullOrNotEmpty(item.getAuthor());
        assertNotNull(item.getProlongData());
        assertNotNull(item.getDeadline());
    }
}
Also used : Account(de.geeksfactory.opacclient.objects.Account) JSONObject(org.json.JSONObject) AccountData(de.geeksfactory.opacclient.objects.AccountData) LentItem(de.geeksfactory.opacclient.objects.LentItem) Test(org.junit.Test)

Example 17 with AccountData

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

the class PicaLBS method account.

@Override
public AccountData account(Account account) throws IOException, JSONException, OpacErrorException {
    if (!initialised) {
        start();
    }
    login(account);
    AccountData adata = new AccountData(account.getId());
    Document dataDoc = Jsoup.parse(httpGet(lbsUrl + "/LBS_WEB/borrower/borrower.htm", getDefaultLBSEncoding()));
    adata.setPendingFees(extractAccountInfo(dataDoc, "Total Costs", "Gesamtbetrag Kosten"));
    adata.setValidUntil(extractAccountInfo(dataDoc, "Expires at", "endet am"));
    Document lentDoc = Jsoup.parse(httpGet(lbsUrl + "/LBS_WEB/borrower/loans.htm", getDefaultLBSEncoding()));
    adata.setLent(parseMediaList(lentDoc, stringProvider));
    Document reservationsDoc = Jsoup.parse(httpGet(lbsUrl + "/LBS_WEB/borrower/reservations.htm", getDefaultLBSEncoding()));
    adata.setReservations(parseResList(reservationsDoc, stringProvider));
    return adata;
}
Also used : AccountData(de.geeksfactory.opacclient.objects.AccountData) Document(org.jsoup.nodes.Document)

Example 18 with AccountData

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

the class Bibliotheca method parse_account.

public static AccountData parse_account(Account acc, Document doc, JSONObject data, ReportHandler reportHandler, JSONObject headers_lent, JSONObject headers_reservations) throws JSONException, NotReachableException {
    if (doc.select(".kontozeile_center table").size() == 0) {
        throw new NotReachableException();
    }
    Map<String, Integer> copymap = new HashMap<>();
    Elements headerCells = doc.select(".kontozeile_center table").get(0).select("tr.exemplarmenubar").get(0).children();
    JSONArray headersList = new JSONArray();
    JSONArray unknownHeaders = new JSONArray();
    int i = 0;
    for (Element headerCell : headerCells) {
        String header = headerCell.text();
        headersList.put(header);
        if (headers_lent.has(header)) {
            if (!headers_lent.isNull(header))
                copymap.put(headers_lent.getString(header), i);
        } else {
            unknownHeaders.put(header);
        }
        i++;
    }
    if (unknownHeaders.length() > 0) {
        // send report
        JSONObject reportData = new JSONObject();
        reportData.put("headers", headersList);
        reportData.put("unknown_headers", unknownHeaders);
        Report report = new Report(acc.getLibrary(), "bibliotheca", "unknown header - lent", DateTime.now(), reportData);
        reportHandler.sendReport(report);
        // fallback to JSON
        JSONObject accounttable = data.getJSONObject("accounttable");
        copymap = jsonToMap(accounttable);
    }
    List<LentItem> media = new ArrayList<>();
    Elements exemplartrs = doc.select(".kontozeile_center table").get(0).select("tr.tabKonto");
    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
    DateTimeFormatter fmt2 = DateTimeFormat.forPattern("d/M/yyyy").withLocale(Locale.GERMAN);
    for (Element tr : exemplartrs) {
        LentItem item = new LentItem();
        for (Entry<String, Integer> entry : copymap.entrySet()) {
            String key = entry.getKey();
            int index = entry.getValue();
            if (key.equals("prolongurl")) {
                if (tr.child(index).children().size() > 0) {
                    item.setProlongData(tr.child(index).child(0).attr("href"));
                    item.setRenewable(!tr.child(index).child(0).attr("href").contains("vermsg"));
                }
            } else if (key.equals("returndate")) {
                try {
                    item.setDeadline(fmt.parseLocalDate(tr.child(index).text()));
                } catch (IllegalArgumentException e1) {
                    try {
                        item.setDeadline(fmt2.parseLocalDate(tr.child(index).text()));
                    } catch (IllegalArgumentException e2) {
                        e2.printStackTrace();
                    }
                }
            } else {
                item.set(key, tr.child(index).text());
            }
        }
        media.add(item);
    }
    copymap = new HashMap<>();
    headerCells = doc.select(".kontozeile_center table").get(1).select("tr.exemplarmenubar").get(0).children();
    headersList = new JSONArray();
    unknownHeaders = new JSONArray();
    i = 0;
    for (Element headerCell : headerCells) {
        String header = headerCell.text();
        headersList.put(header);
        if (headers_reservations.has(header)) {
            if (!headers_reservations.isNull(header)) {
                copymap.put(headers_reservations.getString(header), i);
            }
        } else {
            unknownHeaders.put(header);
        }
        i++;
    }
    if (unknownHeaders.length() > 0) {
        // send report
        JSONObject reportData = new JSONObject();
        reportData.put("headers", headersList);
        reportData.put("unknown_headers", unknownHeaders);
        Report report = new Report(acc.getLibrary(), "bibliotheca", "unknown header - reservations", DateTime.now(), reportData);
        reportHandler.sendReport(report);
        // fallback to JSON
        JSONObject reservationtable = data.getJSONObject("reservationtable");
        copymap = jsonToMap(reservationtable);
    }
    List<ReservedItem> reservations = new ArrayList<>();
    exemplartrs = doc.select(".kontozeile_center table").get(1).select("tr.tabKonto");
    for (Element tr : exemplartrs) {
        ReservedItem item = new ReservedItem();
        for (Entry<String, Integer> entry : copymap.entrySet()) {
            String key = entry.getKey();
            int index = entry.getValue();
            if (key.equals("cancelurl")) {
                if (tr.child(index).children().size() > 0) {
                    item.setCancelData(tr.child(index).child(0).attr("href"));
                }
            } else if (key.equals("availability")) {
                try {
                    item.setReadyDate(fmt.parseLocalDate(tr.child(index).text()));
                } catch (IllegalArgumentException e1) {
                    try {
                        item.setReadyDate(fmt2.parseLocalDate(tr.child(index).text()));
                    } catch (IllegalArgumentException e2) {
                        e2.printStackTrace();
                    }
                }
            } else if (key.equals("expirationdate")) {
                try {
                    item.setExpirationDate(fmt.parseLocalDate(tr.child(index).text()));
                } catch (IllegalArgumentException e1) {
                    try {
                        item.setExpirationDate(fmt2.parseLocalDate(tr.child(index).text()));
                    } catch (IllegalArgumentException e2) {
                        item.setStatus(tr.child(index).text());
                    }
                }
            } else {
                item.set(key, tr.child(index).text());
            }
        }
        reservations.add(item);
    }
    AccountData res = new AccountData(acc.getId());
    for (Element row : doc.select(".kontozeile_center, div[align=center]")) {
        String text = row.text().trim();
        if (text.matches(".*Ausstehende Geb.+hren:[^0-9]+([0-9.,]+)[^0-9€A-Z]*(€|EUR|CHF|Fr.).*")) {
            text = text.replaceAll(".*Ausstehende Geb.+hren:[^0-9]+([0-9.," + "]+)[^0-9€A-Z]*(€|EUR|CHF|Fr.).*", "$1 $2");
            res.setPendingFees(text);
        }
        if (text.matches("Ihr Ausweis ist g.ltig bis:.*")) {
            text = text.replaceAll("Ihr Ausweis ist g.ltig bis:[^A-Za-z0-9]+", "");
            res.setValidUntil(text);
        } else if (text.matches("Ausweis g.ltig bis:.*")) {
            text = text.replaceAll("Ausweis g.ltig bis:[^A-Za-z0-9]+", "");
            res.setValidUntil(text);
        }
    }
    res.setLent(media);
    res.setReservations(reservations);
    return res;
}
Also used : NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) HashMap(java.util.HashMap) Report(de.geeksfactory.opacclient.reporting.Report) Element(org.jsoup.nodes.Element) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) Elements(org.jsoup.select.Elements) JSONObject(org.json.JSONObject) 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 19 with AccountData

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

the class IOpac method account.

@Override
public AccountData account(Account account) throws IOException, JSONException, OpacErrorException {
    if (!initialised) {
        start();
    }
    Document doc = getAccountPage(account);
    AccountData res = new AccountData(account.getId());
    List<LentItem> media = new ArrayList<>();
    List<ReservedItem> reserved = new ArrayList<>();
    parseMediaList(media, doc, data);
    parseResList(reserved, doc, data);
    res.setLent(media);
    res.setReservations(reserved);
    if (doc.select("h4:contains(Kontostand)").size() > 0) {
        Element h4 = doc.select("h4:contains(Kontostand)").first();
        Pattern regex = Pattern.compile("Kontostand (-?\\d+\\.\\d\\d EUR)");
        Matcher matcher = regex.matcher(h4.text());
        if (matcher.find())
            res.setPendingFees(matcher.group(1));
    }
    if (doc.select("h4:contains(Ausweis g)").size() > 0) {
        Element h4 = doc.select("h4:contains(Ausweis g)").first();
        Pattern regex = Pattern.compile("Ausweis g.+ltig bis\\s*.\\s*(\\d\\d.\\d\\d.\\d\\d\\d\\d)");
        Matcher matcher = regex.matcher(h4.text());
        if (matcher.find())
            res.setValidUntil(matcher.group(1));
    }
    if (doc.select(".ReaderAccount_expiredID").size() > 0) {
        res.setWarning(doc.select(".ReaderAccount_expiredID").text());
    }
    if (media.isEmpty() && reserved.isEmpty()) {
        if (doc.select("h1").size() > 0) {
            // noinspection StatementWithEmptyBody
            if (doc.select("h4").text().trim().contains("keine ausgeliehenen Medien")) {
            // There is no lent media, but the server is working
            // correctly
            } else if (doc.select("h1").text().trim().contains("RUNTIME ERROR")) {
                // Server Error
                throw new NotReachableException("IOPAC RUNTIME ERROR");
            } else {
                throw new OpacErrorException(stringProvider.getFormattedString(StringProvider.UNKNOWN_ERROR_ACCOUNT_WITH_DESCRIPTION, doc.select("h1").text().trim()));
            }
        } else {
            throw new OpacErrorException(stringProvider.getString(StringProvider.UNKNOWN_ERROR_ACCOUNT));
        }
    }
    return res;
}
Also used : Pattern(java.util.regex.Pattern) AccountData(de.geeksfactory.opacclient.objects.AccountData) Matcher(java.util.regex.Matcher) NotReachableException(de.geeksfactory.opacclient.networking.NotReachableException) 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)

Aggregations

AccountData (de.geeksfactory.opacclient.objects.AccountData)19 LentItem (de.geeksfactory.opacclient.objects.LentItem)14 ReservedItem (de.geeksfactory.opacclient.objects.ReservedItem)13 ArrayList (java.util.ArrayList)10 Document (org.jsoup.nodes.Document)9 Element (org.jsoup.nodes.Element)7 JSONObject (org.json.JSONObject)5 Account (de.geeksfactory.opacclient.objects.Account)4 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)4 Test (org.junit.Test)4 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)3 Elements (org.jsoup.select.Elements)3 HashMap (java.util.HashMap)2 Pattern (java.util.regex.Pattern)2 NameValuePair (org.apache.http.NameValuePair)2 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)2 LocalDate (org.joda.time.LocalDate)2 Context (android.content.Context)1 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1