Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ACM fetcher #8338

Merged
merged 1 commit into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package org.jabref.logic.importer.fileformat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.jabref.logic.importer.FetcherException;
Expand All @@ -35,13 +30,15 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.http.client.utils.URIBuilder;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ACMPortalParser implements Parser {

private static final String HOST = "https://dl.acm.org";
private static final String DOI_URL = "https://dl.acm.org/action/exportCiteProcCitation";
private static final Pattern DOI_HTML_PATTERN = Pattern.compile("<input name=\"(.*?)\"");
private static final String ITEM_HTML = "<li class=\"search__item issue-item-container\">";
private static final int MAX_ITEM_CNT_PER_PAGE = 20;

/**
* Parse the DOI of the ACM Portal search result page and obtain the corresponding BibEntry
Expand All @@ -68,24 +65,20 @@ public List<BibEntry> parseEntries(InputStream stream) throws ParseException {
*/
public List<String> parseDoiSearchPage(InputStream stream) throws ParseException {
List<String> doiList = new ArrayList<>();
String htmlLine;
try (BufferedReader in = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
int cnt = 0;
while ((htmlLine = in.readLine()) != null) {
if (ITEM_HTML.equals(htmlLine)) {
Matcher matcher = DOI_HTML_PATTERN.matcher(in.readLine());
if (matcher.find()) {
doiList.add(matcher.group(1));
cnt++;
if (cnt >= MAX_ITEM_CNT_PER_PAGE) {
break;
}
}
}

try {
Document doc = Jsoup.parse(stream, null, HOST);
Elements doiHrefs = doc.select("div.issue-item__content-right > h5 > span > a");

for (Element elem : doiHrefs) {
String fullSegement = elem.attr("href");
String doi = fullSegement.substring(fullSegement.indexOf("10"));
doiList.add(doi);
}
} catch (IOException e) {
throw new ParseException(e);
} catch (IOException ex) {
throw new ParseException(ex);
}

return doiList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import java.util.Optional;

import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.fileformat.ACMPortalParser;
Expand All @@ -12,7 +13,6 @@
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.testutils.category.FetcherTest;

import com.google.common.base.Optional;
import org.apache.lucene.queryparser.flexible.core.QueryNodeParseException;
import org.apache.lucene.queryparser.flexible.core.parser.SyntaxParser;
import org.apache.lucene.queryparser.flexible.standard.parser.StandardSyntaxParser;
Expand Down