From 0f93e2244d6500f1a6154c8433f2e2ad605b7939 Mon Sep 17 00:00:00 2001 From: Stefan Kolb Date: Sun, 15 Mar 2020 18:15:44 +0100 Subject: [PATCH] Minor improvements for Google scholar fetcher Detect Google captcha div. Follow up PRs might show or give more info to the user. --- .../logic/importer/fetcher/GoogleScholar.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/jabref/logic/importer/fetcher/GoogleScholar.java b/src/main/java/org/jabref/logic/importer/fetcher/GoogleScholar.java index 4e28444ff28..a8850bd9822 100644 --- a/src/main/java/org/jabref/logic/importer/fetcher/GoogleScholar.java +++ b/src/main/java/org/jabref/logic/importer/fetcher/GoogleScholar.java @@ -35,7 +35,7 @@ /** * FulltextFetcher implementation that attempts to find a PDF URL at GoogleScholar. - * + *

* Search String infos: https://scholar.google.com/intl/en/scholar/help.html#searching */ public class GoogleScholar implements FulltextFetcher, SearchBasedFetcher { @@ -58,11 +58,10 @@ public GoogleScholar(ImportFormatPreferences importFormatPreferences) { @Override public Optional findFullText(BibEntry entry) throws IOException, FetcherException { Objects.requireNonNull(entry); - Optional pdfLink = Optional.empty(); // Search in title if (!entry.hasField(StandardField.TITLE)) { - return pdfLink; + return Optional.empty(); } try { @@ -74,12 +73,10 @@ public Optional findFullText(BibEntry entry) throws IOException, FetcherExc // as_occt field to search in uriBuilder.addParameter("as_occt", "title"); - pdfLink = search(uriBuilder.toString()); + return search(uriBuilder.toString()); } catch (URISyntaxException e) { throw new FetcherException("Building URI failed.", e); } - - return pdfLink; } @Override @@ -91,6 +88,11 @@ private Optional search(String url) throws IOException { Optional pdfLink = Optional.empty(); Document doc = Jsoup.connect(url).userAgent(URLDownload.USER_AGENT).get(); + + if (needsCaptcha(doc.body().html())) { + LOGGER.warn("Hit Google traffic limitation. Captcha prevents automatic fetching."); + return Optional.empty(); + } // Check results for PDF link // TODO: link always on first result or none? for (int i = 0; i < NUM_RESULTS; i++) { @@ -111,6 +113,10 @@ private Optional search(String url) throws IOException { return pdfLink; } + private boolean needsCaptcha(String body) { + return body.contains("id=\"gs_captcha_ccl\""); + } + @Override public String getName() { return "Google Scholar"; @@ -158,6 +164,11 @@ public List performSearch(String query) throws FetcherException { private void addHitsFromQuery(List entryList, String queryURL) throws IOException, FetcherException { String content = new URLDownload(queryURL).asString(); + if (needsCaptcha(content)) { + throw new FetcherException("Fetching from Google Scholar failed.", + Localization.lang("This might be caused by reaching the traffic limitation of Google Scholar (see 'Help' for details)."), null); + } + Matcher matcher = LINK_TO_BIB_PATTERN.matcher(content); while (matcher.find()) { String citationsPageURL = matcher.group().replace("&", "&");