Skip to content

Commit

Permalink
Started parser implementation - JabRef#8539
Browse files Browse the repository at this point in the history
  • Loading branch information
SwampTG committed Apr 8, 2022
1 parent 152dc3e commit df7b477
Showing 1 changed file with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@
import java.util.List;
import org.apache.http.client.utils.URIBuilder;
import org.jabref.logic.importer.ImporterPreferences;
import org.jabref.logic.layout.format.EntryTypeFormatter;
import org.jabref.logic.net.URLDownload;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.types.EntryType;
import org.jabref.model.entry.types.StandardEntryType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import kong.unirest.json.JSONObject;

public class BiodiversityLibrary {

private final String BASE_LIBRARY_URL = "https://www.biodiversitylibrary.org/api3";
private final String API_KEY = "33b1c2f0-5320-4714-85c7-a7c202d1a813";
private final String RESPONSE_FORMAT = "json";
private final String AUTHOR_SEARCH = "AuthorSearch";
private final String ADVANCED_SEARCH = "PublicationSearchAdvanced";
private final String SEARCH_BY_AUTHOR = "title";
private final String SEARCH_BY_TITLE = "authorname";

private final ImporterPreferences importerPreferences;

Expand All @@ -46,6 +54,13 @@ public URL getAuthorSearchBaseURL() throws URISyntaxException, MalformedURLExcep
return authorSearchURI.build().toURL();
}

public URL getAdvancedSearchByAuthorBaseURL() throws URISyntaxException, MalformedURLException {
URIBuilder authorSearchURI = new URIBuilder(getBaseURL().toURI());
authorSearchURI.addParameter("op", ADVANCED_SEARCH);

return authorSearchURI.build().toURL();
}

public String searchAuthors(String authorName) {
String result = "";

Expand All @@ -62,6 +77,33 @@ public String searchAuthors(String authorName) {
}

public List<BibEntry> performAdvancedSearch(BibEntry searchParam) {
return new ArrayList<>(Collections.emptyList());
String results = "";
try {
URIBuilder searchBuilder = new URIBuilder(getAdvancedSearchByAuthorBaseURL().toURI());

if (searchParam.hasField(StandardField.TITLE)) {
searchBuilder.addParameter(SEARCH_BY_TITLE, searchParam.getTitle().orElse(""));
}
searchBuilder.addParameter(SEARCH_BY_AUTHOR,
searchParam.getField(StandardField.AUTHOR).orElse(""));

URLDownload download = new URLDownload(searchBuilder.build().toURL());
results = download.asString();

} catch (URISyntaxException | IOException exception) {
logger.error("Search URL didn't functioned: ".concat(exception.getMessage()),
exception);
}
return !results.isEmpty() ? parseEntries(new JSONObject(results)) : Collections.emptyList();
}

public List<BibEntry> parseEntries(JSONObject jObject) {
if (!jObject.isEmpty() && !jObject.isNull("Result")) {
JSONObject jObjectToParse = jObject.getJSONObject("Result");
BibEntry newBibEntry = new BibEntry();
newBibEntry.setType(jObject.get("Genre"));
}

return Collections.emptyList();
}
}

0 comments on commit df7b477

Please sign in to comment.