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());
}
}
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;
}
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;
}
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;
}
Aggregations