-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add APS Fetcher (refactored) #6143
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5ba23c7
Add APS fetcher
augustjanse 33d9e7e
Fix case sensitivity bug
augustjanse b88570f
Refactor ApsFetcher
augustjanse d4df8aa
Add note about APS fetcher
augustjanse 827434c
Refactor findFulltext()
augustjanse 5ff30be
Refactor getId()
augustjanse 5f11a5c
Parameterize ApsFetcherTest
augustjanse 7a855dc
Add link to APS changelog entry
augustjanse e1b7a4f
Merge branch 'aps-fetcher' of https://github.com/augustjanse/jabref i…
Siedlerchr f6bd3b0
Refactor APS Fetcher
Siedlerchr 3006336
Merge remote-tracking branch 'upstream/master' into adsfetcher
Siedlerchr c06781e
Merge remote-tracking branch 'upstream/master' into adsfetcher
Siedlerchr d6299b0
make separate tests
Siedlerchr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
93 changes: 93 additions & 0 deletions
93
src/main/java/org/jabref/logic/importer/fetcher/ApsFetcher.java
This file contains hidden or 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,93 @@ | ||
package org.jabref.logic.importer.fetcher; | ||
|
||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
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.field.StandardField; | ||
import org.jabref.model.entry.identifier.DOI; | ||
|
||
import kong.unirest.Unirest; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* FulltextFetcher implementation that attempts to find a PDF URL at APS. Also see the <a | ||
* href="https://harvest.aps.org/docs/harvest-api">API</a>, although it isn't currently used. | ||
*/ | ||
public class ApsFetcher implements FulltextFetcher { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ApsFetcher.class); | ||
|
||
// The actual API needs either an API key or a header. This is a workaround. | ||
private static final String DOI_URL = "https://www.doi.org/"; | ||
private static final String PDF_URL = "https://journals.aps.org/prl/pdf/"; | ||
|
||
@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()) { | ||
return Optional.empty(); | ||
} | ||
|
||
Optional<String> id = getId(doi.get().getDOI()); | ||
|
||
if (id.isPresent()) { | ||
|
||
String pdfRequestUrl = PDF_URL + id.get(); | ||
int code = Unirest.head(pdfRequestUrl).asJson().getStatus(); | ||
|
||
if (code == 200) { | ||
LOGGER.info("Fulltext PDF found @ APS."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess debug level suffices There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's info at the other fetchers as well |
||
try { | ||
return Optional.of(new URL(pdfRequestUrl)); | ||
} catch (MalformedURLException e) { | ||
LOGGER.warn("APS returned malformed URL, cannot find PDF."); | ||
} | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public TrustLevel getTrustLevel() { | ||
return TrustLevel.PUBLISHER; | ||
} | ||
|
||
/** | ||
* Convert a DOI into an appropriate APS id. | ||
* | ||
* @param doi A case insensitive DOI | ||
* @return A DOI cased as APS likes it | ||
*/ | ||
private Optional<String> getId(String doi) { | ||
// DOI is not case sensitive, but the id for the PDF URL is, | ||
// so we follow DOI.org redirects to get the proper id. | ||
// https://stackoverflow.com/a/5270162/1729441 | ||
|
||
String doiRequest = DOI_URL + doi; | ||
|
||
URLConnection con; | ||
try { | ||
con = new URL(doiRequest).openConnection(); | ||
con.connect(); | ||
con.getInputStream(); | ||
String[] urlParts = con.getURL().toString().split("abstract/"); | ||
if (urlParts.length == 2) { | ||
return Optional.of(urlParts[1]); | ||
} | ||
|
||
} catch (IOException e) { | ||
LOGGER.warn("Error connecting to APS", e); | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/org/jabref/logic/importer/fetcher/ApsFetcherTest.java
This file contains hidden or 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,48 @@ | ||
package org.jabref.logic.importer.fetcher; | ||
|
||
import java.net.URL; | ||
import java.util.Optional; | ||
|
||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.field.StandardField; | ||
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 ApsFetcherTest { | ||
|
||
private ApsFetcher finder; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
finder = new ApsFetcher(); | ||
} | ||
|
||
@Test | ||
void findFullTextFromDoi() throws Exception { | ||
BibEntry entry = new BibEntry().withField(StandardField.DOI, "10.1103/PhysRevLett.116.061102"); | ||
assertEquals(Optional.of(new URL("https://journals.aps.org/prl/pdf/10.1103/PhysRevLett.116.061102")), finder.findFullText(entry)); | ||
} | ||
|
||
@Test | ||
void findFullTextFromLowercaseDoi() throws Exception { | ||
BibEntry entry = new BibEntry().withField(StandardField.DOI, "10.1103/physrevlett.124.029002"); | ||
assertEquals(Optional.of(new URL("https://journals.aps.org/prl/pdf/10.1103/PhysRevLett.124.029002")), finder.findFullText(entry)); | ||
} | ||
|
||
@Test | ||
void notFindFullTextForUnauthorized() throws Exception { | ||
BibEntry entry = new BibEntry().withField(StandardField.DOI, "10.1103/PhysRevLett.89.127401"); | ||
assertEquals(Optional.empty(), finder.findFullText(entry)); | ||
} | ||
|
||
@Test | ||
void notFindFullTextForUnknownEntry() throws Exception { | ||
BibEntry entry = new BibEntry().withField(StandardField.DOI, "10.1016/j.aasri.2014.0559.002"); | ||
assertEquals(Optional.empty(), finder.findFullText(entry)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.