Skip to content

Commit

Permalink
App semantic scholar api key to properties (JabRef#10656)
Browse files Browse the repository at this point in the history
* Update fetchers.md

* Add API key handling for semantic scholar

* checkstyle

* add env var

* fix template expansion

* Fix some fetcher tests

* checkstyle

* revert deletion

---------

Co-authored-by: Oliver Kopp <kopp.dev@gmail.com>
  • Loading branch information
Siedlerchr and koppor authored Nov 22, 2023
1 parent 0b0fcc9 commit 43364b8
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 39 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ processResources {
"astrophysicsDataSystemAPIKey": System.getenv('AstrophysicsDataSystemAPIKey') ? System.getenv('AstrophysicsDataSystemAPIKey') : '',
"ieeeAPIKey": System.getenv('IEEEAPIKey') ? System.getenv('IEEEAPIKey') : '',
"scienceDirectApiKey": System.getenv('SCIENCEDIRECTAPIKEY') ? System.getenv('SCIENCEDIRECTAPIKEY') : '',
"biodiversityHeritageApiKey": System.getenv('BiodiversityHeritageApiKey') ? System.getenv('BiodiversityHeritageApiKey') : ''
"biodiversityHeritageApiKey": System.getenv('BiodiversityHeritageApiKey') ? System.getenv('BiodiversityHeritageApiKey') : '',
"semanticScholarApiKey": System.getenv('SemanticScholarApiKey') ? System.getenv("SemanticScholarApiKey") : ''
)
filteringCharset = 'UTF-8'
}
Expand Down
21 changes: 11 additions & 10 deletions docs/code-howtos/fetchers.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ parent: Code Howtos

Fetchers are the implementation of the [search using online services](https://docs.jabref.org/collect/import-using-online-bibliographic-database). Some fetchers require API keys to get them working. To get the fetchers running in a JabRef development setup, the keys need to be placed in the respective environment variable. The following table lists the respective fetchers, where to get the key from and the environment variable where the key has to be placed.

| Service | Key Source | Environment Variable | Rate Limit |
|:-----------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|--------------------------------|----------------------------------|
| [IEEEXplore](https://docs.jabref.org/collect/import-using-online-bibliographic-database#ieeexplore) | [IEEE Xplore API portal](https://developer.ieee.org) | `IEEEAPIKey` | 200 calls/day |
| [MathSciNet](http://www.ams.org/mathscinet) | (none) | (none) | Depending on the current network |
| [SAO/NASA Astrophysics Data System](https://docs.jabref.org/collect/import-using-online-bibliographic-database#sao-nasa-astrophysics-data-system) | [ADS UI](https://ui.adsabs.harvard.edu/user/settings/token) | `AstrophysicsDataSystemAPIKey` | 5000 calls/day |
| [ScienceDirect](https://www.sciencedirect.com) | | `ScienceDirectApiKey` | |
| [Springer Nature](https://docs.jabref.org/collect/import-using-online-bibliographic-database#springer) | [Springer Nature API Portal](https://dev.springernature.com) | `SpringerNatureAPIKey` | 5000 calls/day |
| [Zentralblatt Math](https://www.zbmath.org) | (none) | (none) | Depending on the current network |
| [Biodiversity Heritage Library](https://www.biodiversitylibrary.org/) | [Biodiversitylibrary](https://about.biodiversitylibrary.org/tools-and-services/developer-and-data-tools/#APIs) | `BiodiversityHeritageApiKey` | - |
| Service | Key Source | Environment Variable | Rate Limit |
|:--------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|--------------------------------|----------------------------------|
| [IEEEXplore](https://docs.jabref.org/collect/import-using-online-bibliographic-database#ieeexplore) | [IEEE Xplore API portal](https://developer.ieee.org) | `IEEEAPIKey` | 200 calls/day |
| [MathSciNet](http://www.ams.org/mathscinet) | (none) | (none) | Depending on the current network |
| [SAO/NASA Astrophysics Data System](https://docs.jabref.org/collect/import-using-online-bibliographic-database#sao-nasa-astrophysics-data-system) | [ADS UI](https://ui.adsabs.harvard.edu/user/settings/token) | `AstrophysicsDataSystemAPIKey` | 5000 calls/day |
| [ScienceDirect](https://www.sciencedirect.com) | | `ScienceDirectApiKey` | |
| [SemanticScholar](https://www.semanticscholar.org/) | <https://www.semanticscholar.org/product/api#api-key-form> | `SemanticScholarApiKey` | |
| [Springer Nature](https://docs.jabref.org/collect/import-using-online-bibliographic-database#springer) | [Springer Nature API Portal](https://dev.springernature.com) | `SpringerNatureAPIKey` | 5000 calls/day |
| [Zentralblatt Math](https://www.zbmath.org) | (none) | (none) | Depending on the current network |
| [Biodiversity Heritage Library](https://www.biodiversitylibrary.org/) | [Biodiversitylibrary](https://about.biodiversitylibrary.org/tools-and-services/developer-and-data-tools/#APIs) | `BiodiversityHeritageApiKey` | - |

"Depending on the current network" means that it depends on whether your request is routed through a network having paid access. For instance, some universities have subscriptions to MathSciNet.

Expand Down Expand Up @@ -72,7 +73,7 @@ springerNatureAPIKey=${springerNatureAPIKey}
In `build.gradle`, these variables are filled:

```groovy
"springerNatureAPIKey": System.getenv('SpringerNatureAPIKey')
"springerNatureAPIKey" : System.getenv('SpringerNatureAPIKey')
```

The `BuildInfo` class reads from that file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public CitationRelationsTab(EntryEditorPreferences preferences, DialogService di
setText(Localization.lang("Citation relations"));
setTooltip(new Tooltip(Localization.lang("Show articles related by citation")));

this.bibEntryRelationsRepository = new BibEntryRelationsRepository(new SemanticScholarFetcher(),
this.bibEntryRelationsRepository = new BibEntryRelationsRepository(new SemanticScholarFetcher(preferencesService.getImporterPreferences()),
new BibEntryRelationsCache());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@
import java.util.List;

import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.ImporterPreferences;
import org.jabref.logic.importer.fetcher.CustomizableKeyFetcher;
import org.jabref.logic.net.URLDownload;
import org.jabref.logic.util.BuildInfo;
import org.jabref.model.entry.BibEntry;

import com.google.gson.Gson;

public class SemanticScholarFetcher implements CitationFetcher {
public class SemanticScholarFetcher implements CitationFetcher, CustomizableKeyFetcher {
private static final String SEMANTIC_SCHOLAR_API = "https://api.semanticscholar.org/graph/v1/";

private static final String API_KEY = new BuildInfo().semanticScholarApiKey;

private final ImporterPreferences importerPreferences;

public SemanticScholarFetcher(ImporterPreferences importerPreferences) {
this.importerPreferences = importerPreferences;
}

@Override
public List<BibEntry> searchCitedBy(BibEntry entry) throws FetcherException {
if (entry.getDOI().isPresent()) {
Expand All @@ -24,17 +35,20 @@ public List<BibEntry> searchCitedBy(BibEntry entry) throws FetcherException {
.append("/citations")
.append("?fields=").append("title,authors,year,citationCount,referenceCount")
.append("&limit=1000");

try {
URL citationsUrl = URI.create(urlBuilder.toString()).toURL();

URLDownload urlDownload = new URLDownload(citationsUrl);

String apiKey = getApiKey();
if (!apiKey.isEmpty()) {
urlDownload.addHeader("x-api-key", apiKey);
}
CitationsResponse citationsResponse = new Gson()
.fromJson(urlDownload.asString(), CitationsResponse.class);

return citationsResponse.getData()
.stream().filter(citationDataItem -> citationDataItem.getCitingPaper() != null)
.map(citationDataItem -> citationDataItem.getCitingPaper().toBibEntry()).toList();
.stream().filter(citationDataItem -> citationDataItem.getCitingPaper() != null)
.map(citationDataItem -> citationDataItem.getCitingPaper().toBibEntry()).toList();
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -50,19 +64,23 @@ public List<BibEntry> searchCiting(BibEntry entry) {
.append("paper/")
.append("DOI:").append(entry.getDOI().get().getDOI())
.append("/references")
.append("?fields=").append("title,authors,year,citationCount,referenceCount")
.append("?fields=")
.append("title,authors,year,citationCount,referenceCount")
.append("&limit=1000");
try {
URL referencesUrl = URI.create(urlBuilder.toString()).toURL();

URLDownload urlDownload = new URLDownload(referencesUrl);
String apiKey = getApiKey();
if (!apiKey.isEmpty()) {
urlDownload.addHeader("x-api-key", apiKey);
}
ReferencesResponse referencesResponse = new Gson()
.fromJson(urlDownload.asString(), ReferencesResponse.class);

return referencesResponse.getData()
.stream()
.filter(citationDataItem -> citationDataItem.getCitedPaper() != null)
.map(referenceDataItem -> referenceDataItem.getCitedPaper().toBibEntry()).toList();
.stream()
.filter(citationDataItem -> citationDataItem.getCitedPaper() != null)
.map(referenceDataItem -> referenceDataItem.getCitedPaper().toBibEntry()).toList();
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -75,4 +93,8 @@ public List<BibEntry> searchCiting(BibEntry entry) {
public String getName() {
return "Semantic Scholar Citations Fetcher";
}

private String getApiKey() {
return importerPreferences.getApiKey(getName()).orElse(API_KEY);
}
}
4 changes: 3 additions & 1 deletion src/main/java/org/jabref/logic/util/BuildInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public final class BuildInfo {
public final String minRequiredJavaVersion;
public final boolean allowJava9;
public final String biodiversityHeritageApiKey;
public final String semanticScholarApiKey;

public BuildInfo() {
this("/build.properties");
Expand Down Expand Up @@ -57,14 +58,15 @@ public BuildInfo(String path) {
minRequiredJavaVersion = properties.getProperty("minRequiredJavaVersion", "1.8");
allowJava9 = "true".equals(properties.getProperty("allowJava9", "true"));
biodiversityHeritageApiKey = BuildInfo.getValue(properties, "biodiversityHeritageApiKey", "36b910b6-2eb3-46f2-b64c-9abc149925ba");
semanticScholarApiKey = BuildInfo.getValue(properties, "semanticScholarApiKey", "");
}

private static String getValue(Properties properties, String key, String defaultValue) {
String result = Optional.ofNullable(properties.getProperty(key))
// workaround unprocessed build.properties file --> just remove the reference to some variable used in build.gradle
.map(value -> value.replaceAll("\\$\\{.*\\}", ""))
.orElse("");
if (!"".equals(result)) {
if (!result.isEmpty()) {
return result;
}
return defaultValue;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ springerNatureAPIKey=${springerNatureAPIKey}
astrophysicsDataSystemAPIKey=${astrophysicsDataSystemAPIKey}
ieeeAPIKey=${ieeeAPIKey}
biodiversityHeritageApiKey=${biodiversityHeritageApiKey}

semanticScholarApiKey=${semanticScholarApiKey}
Loading

0 comments on commit 43364b8

Please sign in to comment.