SISIS: Fix regex for covers loaded via AJAX
to work with relative and root-relative urls.
Refactor code to avoid duplicate code.
Provide tests with real-world examples for both cases.
@@ -472,11 +472,8 @@ public class SISIS extends OkHttpBaseApi implements OpacApi {
// covers loaded with AJAX (seen in Wuppertal)
if (tr.children().size() > 3 && tr.child(3).html().contains("jsp/result/cover.jsp")) {
- Pattern pattern = Pattern.compile(
- "\\$\\.ajax\\(\\{\\s*url:\\s*'(jsp/result/cover.jsp\\?[^']+)',");
- Matcher matcher = pattern.matcher(tr.child(3).html());
- if (matcher.find()) {
- String url = opac_url + "/" + matcher.group(1);
+ String url = getAjaxCoverUrl(tr.child(3).html());
+ if (url != null ) {
futures.add(CompletableFuture.runAsync(() -> {
try {
String result = httpGet(url, getDefaultEncoding());
@@ -694,6 +691,21 @@ public class SISIS extends OkHttpBaseApi implements OpacApi {
return new SearchRequestResult(results, results_total, page);
}
+ String getAjaxCoverUrl(String html) {
+ final Pattern coverPattern = Pattern.compile(
+ "\\$\\.ajax\\(\\{\\s*url:\\s*'(.*jsp/result/cover.jsp\\?[^']+)'");
+ Matcher matcher = coverPattern.matcher(html);
+ String url = null;
+ if (matcher.find()) {
+ try {
+ url = new URI(opac_url + "/").resolve(matcher.group(1)).toString();
+ } catch (URISyntaxException | IllegalArgumentException ignoreBadUrl) {
+ // ignore bad url and return null
+ }
+ }
+ return url;
+ }
+
@Override
public DetailedItem getResultById(String id, String homebranch)
throws IOException {
@@ -744,11 +756,9 @@ public class SISIS extends OkHttpBaseApi implements OpacApi {
ENCODING);
String coverJs = null;
- Pattern coverPattern = Pattern.compile("\\$\\.ajax\\(\\{[\\n\\s]*url: '(jsp/result/cover" +
- ".jsp\\?[^']+')");
- Matcher coverMatcher = coverPattern.matcher(html);
- if (coverMatcher.find()) {
- coverJs = httpGet(opac_url + "/" + coverMatcher.group(1), ENCODING);
+ String url = getAjaxCoverUrl(html);
+ if (url != null) {
+ coverJs = httpGet(url, ENCODING);
}
DetailedItem result = parseDetail(html, html2, html3, coverJs, data, stringProvider);
