-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add oaDOI fulltext fetcher * Rename to OpenAccessDoi
- Loading branch information
1 parent
d381839
commit b246b9c
Showing
8 changed files
with
141 additions
and
27 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
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
58 changes: 58 additions & 0 deletions
58
src/main/java/org/jabref/logic/importer/fetcher/OpenAccessDoi.java
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.jabref.logic.importer.fetcher; | ||
|
||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import org.jabref.logic.importer.FulltextFetcher; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.FieldName; | ||
import org.jabref.model.entry.identifier.DOI; | ||
|
||
import com.mashape.unirest.http.HttpResponse; | ||
import com.mashape.unirest.http.JsonNode; | ||
import com.mashape.unirest.http.Unirest; | ||
import com.mashape.unirest.http.exceptions.UnirestException; | ||
import org.json.JSONObject; | ||
|
||
/** | ||
* A fulltext fetcher that uses <a href="https://oadoi.org/">oaDOI</a>. | ||
* | ||
* @implSpec API is documented at https://oadoi.org/api/v2 | ||
*/ | ||
public class OpenAccessDoi implements FulltextFetcher { | ||
private static 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(FieldName.DOI) | ||
.flatMap(DOI::parse); | ||
if (doi.isPresent()) { | ||
try { | ||
return findFullText(doi.get()); | ||
} catch (UnirestException e) { | ||
throw new IOException(e); | ||
} | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
public Optional<URL> findFullText(DOI doi) throws UnirestException, MalformedURLException { | ||
HttpResponse<JsonNode> jsonResponse = Unirest.get(API_URL + doi.getDOI() + "?email=developers@jabref.org") | ||
.header("accept", "application/json") | ||
.asJson(); | ||
JSONObject root = jsonResponse.getBody().getObject(); | ||
Optional<String> url = Optional.ofNullable(root.optJSONObject("best_oa_location")) | ||
.map(location -> location.optString("url")); | ||
if (url.isPresent()) { | ||
return Optional.of(new URL(url.get())); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
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
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
43 changes: 43 additions & 0 deletions
43
src/test/java/org/jabref/logic/importer/fetcher/OpenAccessDoiTest.java
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.jabref.logic.importer.fetcher; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.Optional; | ||
|
||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.testutils.category.FetcherTest; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@FetcherTest | ||
class OpenAccessDoiTest { | ||
|
||
private OpenAccessDoi finder; | ||
private BibEntry entry; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
finder = new OpenAccessDoi(); | ||
entry = new BibEntry(); | ||
} | ||
|
||
@Test | ||
void findByDOI() throws IOException { | ||
entry.setField("doi", "10.1038/nature12373"); | ||
|
||
assertEquals( | ||
Optional.of(new URL("https://dash.harvard.edu/bitstream/handle/1/12285462/Nanometer-Scale%20Thermometry.pdf?sequence=1")), | ||
finder.findFullText(entry) | ||
); | ||
} | ||
|
||
@Test | ||
void notFoundByDOI() throws IOException { | ||
entry.setField("doi", "10.1186/unknown-doi"); | ||
|
||
assertEquals(Optional.empty(), finder.findFullText(entry)); | ||
} | ||
} |