|
| 1 | +package org.jabref.logic.importer.fetcher; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.net.MalformedURLException; |
| 6 | +import java.net.URISyntaxException; |
| 7 | +import java.net.URL; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Objects; |
| 11 | +import java.util.Optional; |
| 12 | +import java.util.stream.Collectors; |
| 13 | +import java.util.stream.IntStream; |
| 14 | + |
| 15 | +import org.jabref.logic.importer.FetcherException; |
| 16 | +import org.jabref.logic.importer.ImporterPreferences; |
| 17 | +import org.jabref.logic.importer.PagedSearchBasedParserFetcher; |
| 18 | +import org.jabref.logic.importer.Parser; |
| 19 | +import org.jabref.logic.importer.fetcher.transformers.LOBIDQueryTransformer; |
| 20 | +import org.jabref.logic.util.OS; |
| 21 | +import org.jabref.model.entry.BibEntry; |
| 22 | +import org.jabref.model.entry.field.Field; |
| 23 | +import org.jabref.model.entry.field.StandardField; |
| 24 | +import org.jabref.model.entry.types.EntryType; |
| 25 | +import org.jabref.model.entry.types.StandardEntryType; |
| 26 | + |
| 27 | +import kong.unirest.json.JSONArray; |
| 28 | +import kong.unirest.json.JSONObject; |
| 29 | +import org.apache.http.client.utils.URIBuilder; |
| 30 | +import org.apache.lucene.queryparser.flexible.core.nodes.QueryNode; |
| 31 | +import org.slf4j.Logger; |
| 32 | +import org.slf4j.LoggerFactory; |
| 33 | + |
| 34 | +/** |
| 35 | + * Fetches data from the LOBID API |
| 36 | + * |
| 37 | + * @see <a href="https://lobid.org/resources/api">API documentation</a> for more details |
| 38 | + */ |
| 39 | +public class LOBIDFetcher implements PagedSearchBasedParserFetcher { |
| 40 | + |
| 41 | + public static final String FETCHER_NAME = "LOBID"; |
| 42 | + |
| 43 | + private static final Logger LOGGER = LoggerFactory.getLogger(LOBIDFetcher.class); |
| 44 | + |
| 45 | + private static final String API_URL = "https://lobid.org/resources/search"; |
| 46 | + |
| 47 | + private final ImporterPreferences importerPreferences; |
| 48 | + |
| 49 | + public LOBIDFetcher(ImporterPreferences importerPreferences) { |
| 50 | + this.importerPreferences = importerPreferences; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Gets the query URL |
| 55 | + * |
| 56 | + * @param luceneQuery the search query |
| 57 | + * @param pageNumber the number of the page indexed from 0 |
| 58 | + * @return URL |
| 59 | + */ |
| 60 | + @Override |
| 61 | + public URL getURLForQuery(QueryNode luceneQuery, int pageNumber) throws URISyntaxException, MalformedURLException, FetcherException { |
| 62 | + URIBuilder uriBuilder = new URIBuilder(API_URL); |
| 63 | + uriBuilder.addParameter("q", new LOBIDQueryTransformer().transformLuceneQuery(luceneQuery).orElse("")); // search query |
| 64 | + uriBuilder.addParameter("from", String.valueOf(getPageSize() * pageNumber)); // from entry number, starts indexing at 0 |
| 65 | + uriBuilder.addParameter("size", String.valueOf(getPageSize())); // page size |
| 66 | + uriBuilder.addParameter("format", "json"); // response format |
| 67 | + return uriBuilder.build().toURL(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public Parser getParser() { |
| 72 | + return inputStream -> { |
| 73 | + String response = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining(OS.NEWLINE)); |
| 74 | + JSONObject jsonObject = new JSONObject(response); |
| 75 | + |
| 76 | + List<BibEntry> entries = new ArrayList<>(); |
| 77 | + if (jsonObject.has("member")) { |
| 78 | + JSONArray results = jsonObject.getJSONArray("member"); |
| 79 | + for (int i = 0; i < results.length(); i++) { |
| 80 | + JSONObject jsonEntry = results.getJSONObject(i); |
| 81 | + BibEntry entry = parseJSONtoBibtex(jsonEntry); |
| 82 | + entries.add(entry); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return entries; |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + private BibEntry parseJSONtoBibtex(JSONObject jsonEntry) { |
| 91 | + BibEntry entry = new BibEntry(); |
| 92 | + Field nametype = StandardField.JOURNAL; |
| 93 | + EntryType entryType = StandardEntryType.InCollection; |
| 94 | + |
| 95 | + // publication type |
| 96 | + JSONArray typeArray = jsonEntry.optJSONArray("type"); |
| 97 | + String types = ""; |
| 98 | + if (typeArray != null) { |
| 99 | + List<String> typeList = IntStream.range(0, typeArray.length()) |
| 100 | + .mapToObj(typeArray::optString) |
| 101 | + .filter(type -> !type.isEmpty()) |
| 102 | + .toList(); |
| 103 | + types = String.join(", ", typeList); |
| 104 | + entry.setField(StandardField.TYPE, types); |
| 105 | + } |
| 106 | + |
| 107 | + if (types.toLowerCase().contains("book")) { |
| 108 | + entryType = StandardEntryType.Book; |
| 109 | + nametype = StandardField.BOOKTITLE; |
| 110 | + } else if (types.toLowerCase().contains("article")) { |
| 111 | + entryType = StandardEntryType.Article; |
| 112 | + } |
| 113 | + entry.setType(entryType); |
| 114 | + |
| 115 | + // isbn |
| 116 | + String isbn = getFirstArrayElement(jsonEntry, "isbn"); |
| 117 | + entry.setField(StandardField.ISBN, isbn); |
| 118 | + |
| 119 | + // parent resource |
| 120 | + String bibliographicCitation = jsonEntry.optString("bibliographicCitation", ""); |
| 121 | + String[] bibSplit = bibliographicCitation.split("/"); |
| 122 | + String parentResource = ""; |
| 123 | + if (bibSplit.length > 0) { |
| 124 | + parentResource = bibSplit[0].trim(); |
| 125 | + entry.setField(nametype, parentResource); |
| 126 | + } |
| 127 | + |
| 128 | + entry.setField(StandardField.ISSN, getFirstArrayElement(jsonEntry, "issn")); |
| 129 | + entry.setField(StandardField.TITLE, jsonEntry.optString("title", "")); |
| 130 | + entry.setField(StandardField.ABSTRACT, getFirstArrayElement(jsonEntry, "note")); |
| 131 | + entry.setField(StandardField.TITLEADDON, getFirstArrayElement(jsonEntry, "otherTitleInformation")); |
| 132 | + entry.setField(StandardField.EDITION, getFirstArrayElement(jsonEntry, "edition")); |
| 133 | + |
| 134 | + // authors |
| 135 | + JSONArray authors = jsonEntry.optJSONArray("contribution"); |
| 136 | + if (authors != null) { |
| 137 | + List<String> authorNames = getAuthorNames(authors); |
| 138 | + if (!authors.isEmpty()) { |
| 139 | + entry.setField(StandardField.AUTHOR, String.join(" and ", authorNames)); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // publication |
| 144 | + Optional.ofNullable(jsonEntry.optJSONArray("publication")) |
| 145 | + .map(array -> array.getJSONObject(0)) |
| 146 | + .ifPresent(publication -> { |
| 147 | + entry.setField(StandardField.PUBLISHER, getFirstArrayElement(publication, "publishedBy")); |
| 148 | + entry.setField(StandardField.LOCATION, getFirstArrayElement(publication, "location")); |
| 149 | + String date = publication.optString("startDate"); |
| 150 | + entry.setField(StandardField.DATE, date); |
| 151 | + entry.setField(StandardField.YEAR, date); |
| 152 | + }); |
| 153 | + |
| 154 | + // url |
| 155 | + JSONObject describedBy = jsonEntry.optJSONObject("describedBy"); |
| 156 | + if (describedBy != null) { |
| 157 | + entry.setField(StandardField.URL, describedBy.optString("id")); |
| 158 | + } |
| 159 | + |
| 160 | + // language |
| 161 | + JSONArray languageArray = jsonEntry.optJSONArray("language"); |
| 162 | + if (languageArray != null) { |
| 163 | + List<String> languageList = IntStream.range(0, languageArray.length()) |
| 164 | + .mapToObj(languageArray::getJSONObject) |
| 165 | + .filter(Objects::nonNull) |
| 166 | + .map(language -> language.optString("label")) |
| 167 | + .toList(); |
| 168 | + entry.setField(StandardField.LANGUAGE, String.join(" and ", languageList)); |
| 169 | + } |
| 170 | + |
| 171 | + // keywords |
| 172 | + JSONArray keywordArray = jsonEntry.optJSONArray("subjectslabels"); |
| 173 | + if (keywordArray != null) { |
| 174 | + List<String> keywordList = IntStream.range(0, keywordArray.length()) |
| 175 | + .mapToObj(keywordArray::optString) |
| 176 | + .filter(keyword -> !keyword.isEmpty()) |
| 177 | + .toList(); |
| 178 | + entry.setField(StandardField.KEYWORDS, String.join(", ", keywordList)); |
| 179 | + } |
| 180 | + |
| 181 | + return entry; |
| 182 | + } |
| 183 | + |
| 184 | + private static List<String> getAuthorNames(JSONArray authors) { |
| 185 | + return IntStream.range(0, authors.length()) |
| 186 | + .mapToObj(authors::getJSONObject) |
| 187 | + .map(author -> author.optJSONObject("agent")) |
| 188 | + .filter(Objects::nonNull) |
| 189 | + .map(agent -> agent.optString("label")) |
| 190 | + .toList(); |
| 191 | + } |
| 192 | + |
| 193 | + private static String getFirstArrayElement(JSONObject jsonEntry, String key) { |
| 194 | + return Optional.ofNullable(jsonEntry.optJSONArray(key)) |
| 195 | + .map(array -> array.getString(0)) |
| 196 | + .orElse(""); |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public String getName() { |
| 201 | + return FETCHER_NAME; |
| 202 | + } |
| 203 | +} |
0 commit comments