Skip to content

Commit

Permalink
Improve OpenAccessDoi fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-kolb committed Mar 15, 2020
1 parent 0784b46 commit 086629d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
38 changes: 21 additions & 17 deletions src/main/java/org/jabref/logic/importer/fetcher/OpenAccessDoi.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.identifier.DOI;

import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;
import kong.unirest.UnirestException;
import org.slf4j.Logger;
Expand All @@ -22,26 +24,26 @@
* @implSpec API is documented at http://unpaywall.org/api/v2
*/
public class OpenAccessDoi implements FulltextFetcher {

private static final Logger LOGGER = LoggerFactory.getLogger(FulltextFetcher.class);

private static String API_URL = "https://api.oadoi.org/v2/";
private static final String API_URL = "https://api.oadoi.org/v2/";

@Override
public Optional<URL> findFullText(BibEntry entry) throws IOException {
Objects.requireNonNull(entry);

Optional<DOI> doi = entry.getField(StandardField.DOI)
.flatMap(DOI::parse);
if (doi.isPresent()) {
try {
return findFullText(doi.get());
} catch (UnirestException e) {
throw new IOException(e);
}
} else {

if (!doi.isPresent()) {
return Optional.empty();
}

try {
return findFullText(doi.get());
} catch (UnirestException e) {
throw new IOException(e);
}
}

@Override
Expand All @@ -50,15 +52,17 @@ public TrustLevel getTrustLevel() {
}

public Optional<URL> findFullText(DOI doi) throws UnirestException {
return Optional.of(Unirest.get(API_URL + doi.getDOI() + "[email protected]")
.header("accept", "application/json")
.asJson())
.map(response -> response.getBody())
.filter(body -> body != null)
.map(body -> body.getObject())
.filter(root -> root != null)
HttpResponse<JsonNode> request = Unirest.get(API_URL + doi.getDOI() + "[email protected]")
.header("accept", "application/json")
.asJson();

return Optional.of(request)
.map(HttpResponse::getBody)
.filter(Objects::nonNull)
.map(JsonNode::getObject)
.filter(Objects::nonNull)
.map(root -> root.optJSONObject("best_oa_location"))
.filter(object -> object != null)
.filter(Objects::nonNull)
.map(location -> location.optString("url"))
.flatMap(url -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void noTrustLevel() throws MalformedURLException {
}

@Test
public void higherTrustLevelWins() throws MalformedURLException, IOException, FetcherException {
public void higherTrustLevelWins() throws IOException, FetcherException {
final URL lowUrl = new URL("http://docs.oasis-open.org/opencsa/sca-bpel/sca-bpel-1.1-spec-cd-01.pdf");
final URL highUrl = new URL("http://docs.oasis-open.org/wsbpel/2.0/OS/wsbpel-v2.0-OS.pdf");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@ void notFoundByDOI() throws IOException {
entry.setField(StandardField.DOI, "10.1186/unknown-doi");
assertEquals(Optional.empty(), finder.findFullText(entry));
}

@Test
void entryWithoutDoi() throws IOException {
assertEquals(Optional.empty(), finder.findFullText(entry));
}

@Test
void trustLevel() {
assertEquals(TrustLevel.META_SEARCH, finder.getTrustLevel());
}
}

0 comments on commit 086629d

Please sign in to comment.