|
| 1 | +package org.jabref.logic.importer.fetcher; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.MalformedURLException; |
| 5 | +import java.net.URL; |
| 6 | +import java.net.URLConnection; |
| 7 | +import java.util.Objects; |
| 8 | +import java.util.Optional; |
| 9 | + |
| 10 | +import org.jabref.logic.importer.FulltextFetcher; |
| 11 | +import org.jabref.model.entry.BibEntry; |
| 12 | +import org.jabref.model.entry.field.StandardField; |
| 13 | +import org.jabref.model.entry.identifier.DOI; |
| 14 | + |
| 15 | +import kong.unirest.Unirest; |
| 16 | +import org.slf4j.Logger; |
| 17 | +import org.slf4j.LoggerFactory; |
| 18 | + |
| 19 | +/** |
| 20 | + * FulltextFetcher implementation that attempts to find a PDF URL at APS. Also see the <a |
| 21 | + * href="https://harvest.aps.org/docs/harvest-api">API</a>, although it isn't currently used. |
| 22 | + */ |
| 23 | +public class ApsFetcher implements FulltextFetcher { |
| 24 | + |
| 25 | + private static final Logger LOGGER = LoggerFactory.getLogger(ApsFetcher.class); |
| 26 | + |
| 27 | + // The actual API needs either an API key or a header. This is a workaround. |
| 28 | + private static final String DOI_URL = "https://www.doi.org/"; |
| 29 | + private static final String PDF_URL = "https://journals.aps.org/prl/pdf/"; |
| 30 | + |
| 31 | + @Override |
| 32 | + public Optional<URL> findFullText(BibEntry entry) throws IOException { |
| 33 | + Objects.requireNonNull(entry); |
| 34 | + |
| 35 | + Optional<DOI> doi = entry.getField(StandardField.DOI).flatMap(DOI::parse); |
| 36 | + |
| 37 | + if (!doi.isPresent()) { |
| 38 | + return Optional.empty(); |
| 39 | + } |
| 40 | + |
| 41 | + Optional<String> id = getId(doi.get().getDOI()); |
| 42 | + |
| 43 | + if (id.isPresent()) { |
| 44 | + |
| 45 | + String pdfRequestUrl = PDF_URL + id.get(); |
| 46 | + int code = Unirest.head(pdfRequestUrl).asJson().getStatus(); |
| 47 | + |
| 48 | + if (code == 200) { |
| 49 | + LOGGER.info("Fulltext PDF found @ APS."); |
| 50 | + try { |
| 51 | + return Optional.of(new URL(pdfRequestUrl)); |
| 52 | + } catch (MalformedURLException e) { |
| 53 | + LOGGER.warn("APS returned malformed URL, cannot find PDF."); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + return Optional.empty(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public TrustLevel getTrustLevel() { |
| 62 | + return TrustLevel.PUBLISHER; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Convert a DOI into an appropriate APS id. |
| 67 | + * |
| 68 | + * @param doi A case insensitive DOI |
| 69 | + * @return A DOI cased as APS likes it |
| 70 | + */ |
| 71 | + private Optional<String> getId(String doi) { |
| 72 | + // DOI is not case sensitive, but the id for the PDF URL is, |
| 73 | + // so we follow DOI.org redirects to get the proper id. |
| 74 | + // https://stackoverflow.com/a/5270162/1729441 |
| 75 | + |
| 76 | + String doiRequest = DOI_URL + doi; |
| 77 | + |
| 78 | + URLConnection con; |
| 79 | + try { |
| 80 | + con = new URL(doiRequest).openConnection(); |
| 81 | + con.connect(); |
| 82 | + con.getInputStream(); |
| 83 | + String[] urlParts = con.getURL().toString().split("abstract/"); |
| 84 | + if (urlParts.length == 2) { |
| 85 | + return Optional.of(urlParts[1]); |
| 86 | + } |
| 87 | + |
| 88 | + } catch (IOException e) { |
| 89 | + LOGGER.warn("Error connecting to APS", e); |
| 90 | + } |
| 91 | + return Optional.empty(); |
| 92 | + } |
| 93 | +} |
0 commit comments