-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Show fetch exception at citation relation #13549
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,15 +8,23 @@ | |
import org.jabref.logic.importer.ImporterPreferences; | ||
import org.jabref.logic.importer.fetcher.CustomizableKeyFetcher; | ||
import org.jabref.logic.importer.fetcher.citation.CitationFetcher; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.logic.net.URLDownload; | ||
import org.jabref.logic.util.URLUtil; | ||
import org.jabref.model.entry.BibEntry; | ||
|
||
import com.google.gson.Gson; | ||
import org.hisp.dhis.jsontree.JsonMixed; | ||
import org.hisp.dhis.jsontree.JsonNode; | ||
import org.jspecify.annotations.NonNull; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class SemanticScholarCitationFetcher implements CitationFetcher, CustomizableKeyFetcher { | ||
public static final String FETCHER_NAME = "Semantic Scholar Citations Fetcher"; | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(SemanticScholarCitationFetcher.class); | ||
|
||
private static final String SEMANTIC_SCHOLAR_API = "https://api.semanticscholar.org/graph/v1/"; | ||
|
||
private static final Gson GSON = new Gson(); | ||
|
@@ -27,8 +35,8 @@ public SemanticScholarCitationFetcher(ImporterPreferences importerPreferences) { | |
this.importerPreferences = importerPreferences; | ||
} | ||
|
||
public String getAPIUrl(String entry_point, BibEntry entry) { | ||
return SEMANTIC_SCHOLAR_API + "paper/" + "DOI:" + entry.getDOI().orElseThrow().asString() + "/" + entry_point | ||
public String getAPIUrl(String entryPoint, BibEntry entry) { | ||
return SEMANTIC_SCHOLAR_API + "paper/" + "DOI:" + entry.getDOI().orElseThrow().asString() + "/" + entryPoint | ||
+ "?fields=" + "title,authors,year,citationCount,referenceCount,externalIds,publicationTypes,abstract,url" | ||
+ "&limit=1000"; | ||
} | ||
|
@@ -42,6 +50,7 @@ public List<BibEntry> searchCitedBy(BibEntry entry) throws FetcherException { | |
URL citationsUrl; | ||
try { | ||
citationsUrl = URLUtil.create(getAPIUrl("citations", entry)); | ||
LOGGER.debug("Cited URL {} ", citationsUrl); | ||
} catch (MalformedURLException e) { | ||
throw new FetcherException("Malformed URL", e); | ||
} | ||
|
@@ -66,15 +75,30 @@ public List<BibEntry> searchCitedBy(BibEntry entry) throws FetcherException { | |
URL referencesUrl; | ||
try { | ||
referencesUrl = URLUtil.create(getAPIUrl("references", entry)); | ||
LOGGER.debug("Citing URL {} ", referencesUrl); | ||
} catch (MalformedURLException e) { | ||
throw new FetcherException("Malformed URL", e); | ||
} | ||
|
||
URLDownload urlDownload = new URLDownload(referencesUrl); | ||
importerPreferences.getApiKey(getName()).ifPresent(apiKey -> urlDownload.addHeader("x-api-key", apiKey)); | ||
ReferencesResponse referencesResponse = GSON.fromJson(urlDownload.asString(), ReferencesResponse.class); | ||
String response = urlDownload.asString(); | ||
ReferencesResponse referencesResponse = GSON.fromJson(response, ReferencesResponse.class); | ||
|
||
if (referencesResponse.getData() == null) { | ||
JsonNode json = JsonNode.of(response); | ||
JsonNode disclaimerJson = json.getOrNull("citingPaperInfo.openAccessPdf.disclaimer"); | ||
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. Use kong's unirest json that allows you to use optionals
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. Chaining will be very ugly here?! Sure that we want to have 4 Optional.ofNullable calls? Or some chaining of null checks? |
||
if (disclaimerJson != null) { | ||
JsonMixed disclaimerNode = JsonMixed.of(disclaimerJson); | ||
if (disclaimerNode.isString()) { | ||
String disclaimer = disclaimerNode.string(); | ||
LOGGER.debug("Received a disclaimer from Semantic Scholar: {}", disclaimer); | ||
if (disclaimer.contains("'references'")) { | ||
throw new FetcherException(Localization.lang("Restricted access to references: %0", disclaimer)); | ||
} | ||
} | ||
} | ||
|
||
return List.of(); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why another json library?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For optionals use kongs unirest json
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because I know the author personally. And it is fast
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Show me. 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see: eg.g
JSONObject result = item.getJSONObject("resultList").getJSONArray("result").getJSONObject(0);
jabref/jablib/src/main/java/org/jabref/logic/importer/fetcher/EuropePmcFetcher.java
Line 117 in e29044c