forked from JabRef/jabref
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0784b46
commit 086629d
Showing
3 changed files
with
32 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 | ||
|
@@ -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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters