Skip to content
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

Implement isbn fetcher (#9145) #9205

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We added a warning message next to the authors field in the merge dialog to warn users when the authors are the same but formatted differently. [#8745](https://github.com/JabRef/jabref/issues/8745)
- The properties of an existing systematic literature review can be edited. [koppor#604](https://github.com/koppor/jabref/issues/604)
- An SLR can now be started from the SLR itself. [#9131](https://github.com/JabRef/jabref/pull/9131), [koppor#601](https://github.com/koppor/jabref/issues/601)
- Implement a new ISBN Fetcher ([doi-to-bibtex-converter.herokuapp.com](http://doi-to-bibtex-converter.herokuapp.com) as source). [#9145](https://github.com/JabRef/jabref/pull/9145)
- We added support for the Ukrainian and Arabic languages. [#9236](https://github.com/JabRef/jabref/pull/9236), [#9243](https://github.com/JabRef/jabref/pull/9243)

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import org.jabref.logic.importer.fetcher.ArXiv;
import org.jabref.logic.importer.fetcher.DoiFetcher;
import org.jabref.logic.importer.fetcher.IsbnFetcher;
import org.jabref.logic.importer.fetcher.isbntobibtex.DoiToBibtexConverterComIsbnFetcher;
import org.jabref.logic.importer.fetcher.isbntobibtex.EbookDeIsbnFetcher;
import org.jabref.logic.importer.fetcher.isbntobibtex.IsbnFetcher;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.identifier.ArXivIdentifier;
import org.jabref.model.entry.identifier.DOI;
Expand All @@ -29,7 +31,10 @@ public Optional<BibEntry> performSearchById(String identifier) throws FetcherExc
}
Optional<ISBN> isbn = ISBN.parse(identifier);
if (isbn.isPresent()) {
return new IsbnFetcher(importFormatPreferences).performSearchById(isbn.get().getNormalized());
return new IsbnFetcher(importFormatPreferences)
.addRetryFetcher(new EbookDeIsbnFetcher(importFormatPreferences))
.addRetryFetcher(new DoiToBibtexConverterComIsbnFetcher(importFormatPreferences))
.performSearchById(isbn.get().getNormalized());
}
/* TODO: IACR is currently disabled, because it needs to be reworked: https://github.com/JabRef/jabref/issues/8876
Optional<IacrEprint> iacrEprint = IacrEprint.parse(identifier);
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/org/jabref/logic/importer/WebFetchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.jabref.logic.importer.fetcher.IEEE;
import org.jabref.logic.importer.fetcher.INSPIREFetcher;
import org.jabref.logic.importer.fetcher.IacrEprintFetcher;
import org.jabref.logic.importer.fetcher.IsbnFetcher;
import org.jabref.logic.importer.fetcher.LibraryOfCongress;
import org.jabref.logic.importer.fetcher.MathSciNet;
import org.jabref.logic.importer.fetcher.MedlineFetcher;
Expand All @@ -42,6 +41,9 @@
import org.jabref.logic.importer.fetcher.SpringerLink;
import org.jabref.logic.importer.fetcher.TitleFetcher;
import org.jabref.logic.importer.fetcher.ZbMATH;
import org.jabref.logic.importer.fetcher.isbntobibtex.DoiToBibtexConverterComIsbnFetcher;
import org.jabref.logic.importer.fetcher.isbntobibtex.EbookDeIsbnFetcher;
import org.jabref.logic.importer.fetcher.isbntobibtex.IsbnFetcher;
import org.jabref.logic.importer.fileformat.PdfMergeMetadataImporter;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.field.Field;
Expand All @@ -58,15 +60,17 @@ public class WebFetchers {
private WebFetchers() {
}

public static Optional<IdBasedFetcher> getIdBasedFetcherForField(Field field, ImportFormatPreferences preferences) {
public static Optional<IdBasedFetcher> getIdBasedFetcherForField(Field field, ImportFormatPreferences importFormatPreferences) {
IdBasedFetcher fetcher;

if (field == StandardField.DOI) {
fetcher = new DoiFetcher(preferences);
fetcher = new DoiFetcher(importFormatPreferences);
} else if (field == ISBN) {
fetcher = new IsbnFetcher(preferences);
fetcher = new IsbnFetcher(importFormatPreferences)
.addRetryFetcher(new EbookDeIsbnFetcher(importFormatPreferences))
.addRetryFetcher(new DoiToBibtexConverterComIsbnFetcher(importFormatPreferences));
} else if (field == EPRINT) {
fetcher = new ArXiv(preferences);
fetcher = new ArXiv(importFormatPreferences);
} else {
return Optional.empty();
}
Expand Down Expand Up @@ -116,7 +120,7 @@ public static SortedSet<SearchBasedFetcher> getSearchBasedFetchers(ImportFormatP
set.add(new SemanticScholar());
set.add(new ResearchGate(importFormatPreferences));
set.add(new BiodiversityLibrary(importerPreferences));
return set;
return set;
}

/**
Expand All @@ -127,7 +131,9 @@ public static SortedSet<IdBasedFetcher> getIdBasedFetchers(ImportFormatPreferenc
SortedSet<IdBasedFetcher> set = new TreeSet<>(Comparator.comparing(WebFetcher::getName));
set.add(new ArXiv(importFormatPreferences));
set.add(new AstrophysicsDataSystem(importFormatPreferences, importerPreferences));
set.add(new IsbnFetcher(importFormatPreferences));
set.add(new IsbnFetcher(importFormatPreferences)
.addRetryFetcher(new EbookDeIsbnFetcher(importFormatPreferences))
.addRetryFetcher(new DoiToBibtexConverterComIsbnFetcher(importFormatPreferences)));
set.add(new DiVA(importFormatPreferences));
set.add(new DoiFetcher(importFormatPreferences));
set.add(new MedlineFetcher());
Expand All @@ -153,7 +159,9 @@ public static SortedSet<EntryBasedFetcher> getEntryBasedFetchers(ImporterPrefere
SortedSet<EntryBasedFetcher> set = new TreeSet<>(Comparator.comparing(WebFetcher::getName));
set.add(new AstrophysicsDataSystem(importFormatPreferences, importerPreferences));
set.add(new DoiFetcher(importFormatPreferences));
set.add(new IsbnFetcher(importFormatPreferences));
set.add(new IsbnFetcher(importFormatPreferences)
.addRetryFetcher(new EbookDeIsbnFetcher(importFormatPreferences))
.addRetryFetcher(new DoiToBibtexConverterComIsbnFetcher(importFormatPreferences)));
set.add(new MathSciNet(importFormatPreferences));
set.add(new CrossRef());
set.add(new ZbMATH(importFormatPreferences));
Expand Down
80 changes: 0 additions & 80 deletions src/main/java/org/jabref/logic/importer/fetcher/IsbnFetcher.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package org.jabref.logic.importer.fetcher.isbntobibtex;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.ParseException;
import org.jabref.logic.importer.Parser;
import org.jabref.logic.importer.fetcher.AbstractIsbnFetcher;
import org.jabref.logic.importer.util.JsonReader;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.model.strings.StringUtil;

import kong.unirest.json.JSONArray;
import kong.unirest.json.JSONException;
import kong.unirest.json.JSONObject;
import org.apache.http.client.utils.URIBuilder;

/**
* Fetcher for ISBN using <a href="https://doi-to-bibtex-converter.herokuapp.com">doi-to-bibtex-converter.herokuapp</a>.
*/
public class DoiToBibtexConverterComIsbnFetcher extends AbstractIsbnFetcher {
private static final String BASE_URL = "https://doi-to-bibtex-converter.herokuapp.com";

public DoiToBibtexConverterComIsbnFetcher(ImportFormatPreferences importFormatPreferences) {
super(importFormatPreferences);
}

@Override
public String getName() {
return "ISBN (doi-to-bibtex-converter.herokuapp.com)";
}

@Override
public URL getUrlForIdentifier(String identifier) throws URISyntaxException, MalformedURLException, FetcherException {
this.ensureThatIsbnIsValid(identifier);
return new URIBuilder(BASE_URL)
.setPathSegments("getInfo.php")
.setParameter("query", identifier)
.setParameter("format", "json")
.build()
.toURL();
}

@Override
public Parser getParser() {
return inputStream -> {
JSONObject response = JsonReader.toJsonObject(inputStream);
if (response.isEmpty()) {
return Collections.emptyList();
}

String error = response.optString("error");
if (StringUtil.isNotBlank(error)) {
throw new ParseException(error);
}

BibEntry entry = jsonItemToBibEntry(response);
return List.of(entry);
};
}

@Override
public void doPostCleanup(BibEntry entry) {
}

private BibEntry jsonItemToBibEntry(JSONObject item) throws ParseException {
try {
JSONArray data = item.optJSONArray("data");
var type = getElementFromJSONArrayByKey(data, "type");

BibEntry entry = new BibEntry(evaluateBibEntryTypeFromString(type));
entry.setField(StandardField.AUTHOR, getElementFromJSONArrayByKey(data, "author"));
entry.setField(StandardField.PAGES, getElementFromJSONArrayByKey(data, "pagecount"));
entry.setField(StandardField.ISBN, getElementFromJSONArrayByKey(data, "isbn"));
entry.setField(StandardField.TITLE, getElementFromJSONArrayByKey(data, "title"));
entry.setField(StandardField.YEAR, getElementFromJSONArrayByKey(data, "year"));
entry.setField(StandardField.MONTH, getElementFromJSONArrayByKey(data, "month"));
entry.setField(StandardField.DAY, getElementFromJSONArrayByKey(data, "day"));
return entry;
} catch (JSONException exception) {
throw new ParseException("CrossRef API JSON format has changed", exception);
}
}

private String getElementFromJSONArrayByKey(JSONArray jsonArray, String key) {
return IntStream.range(0, jsonArray.length())
.mapToObj(jsonArray::getJSONObject)
.map(obj -> obj.getString(key))
.findFirst()
.orElse("");
}

private StandardEntryType evaluateBibEntryTypeFromString(String type) {
return Stream.of(StandardEntryType.values())
.filter(entryType -> entryType.name().equalsIgnoreCase(type))
.findAny()
.orElse(StandardEntryType.Book);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jabref.logic.importer.fetcher;
package org.jabref.logic.importer.fetcher.isbntobibtex;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
Expand All @@ -9,18 +9,19 @@
import org.jabref.logic.formatter.bibtexfields.NormalizePagesFormatter;
import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.fetcher.AbstractIsbnFetcher;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;

import org.apache.http.client.utils.URIBuilder;

/**
* Fetcher for ISBN using http://www.ebook.de.
* Fetcher for ISBN using <a href="https://www.ebook.de">https://www.ebook.de</a>.
*/
public class IsbnViaEbookDeFetcher extends AbstractIsbnFetcher {
private static final String BASE_URL = "http://www.ebook.de/de/tools/isbn2bibtex";
public class EbookDeIsbnFetcher extends AbstractIsbnFetcher {
private static final String BASE_URL = "https://www.ebook.de/de/tools/isbn2bibtex";

public IsbnViaEbookDeFetcher(ImportFormatPreferences importFormatPreferences) {
public EbookDeIsbnFetcher(ImportFormatPreferences importFormatPreferences) {
super(importFormatPreferences);
}

Expand All @@ -32,9 +33,10 @@ public String getName() {
@Override
public URL getUrlForIdentifier(String identifier) throws URISyntaxException, MalformedURLException, FetcherException {
this.ensureThatIsbnIsValid(identifier);
URIBuilder uriBuilder = new URIBuilder(BASE_URL);
uriBuilder.addParameter("isbn", identifier);
return uriBuilder.build().toURL();
return new URIBuilder(BASE_URL)
.addParameter("isbn", identifier)
.build()
.toURL();
}

@Override
Expand Down
Loading