use of kong.unirest.json.JSONException in project jabref by JabRef.
the class OpenLibraryFetcher method jsonItemToBibEntry.
private BibEntry jsonItemToBibEntry(JSONObject item) throws ParseException {
try {
BibEntry entry = new BibEntry(StandardEntryType.Book);
String authors = toAuthors(item.optJSONArray("authors"));
if (authors.isEmpty()) {
JSONArray works = item.optJSONArray("works");
authors = fromWorksToAuthors(works);
}
entry.setField(StandardField.AUTHOR, authors);
entry.setField(StandardField.PAGES, item.optString("number_of_pages"));
entry.setField(StandardField.ISBN, Optional.ofNullable(item.optJSONArray("isbn_13")).map(array -> array.getString(0)).or(() -> Optional.ofNullable(item.optJSONArray("isbn_10")).map(array -> array.getString(0))).orElse(""));
entry.setField(StandardField.TITLE, Optional.ofNullable(item.optString("full_title", null)).or(() -> Optional.ofNullable(item.optString("title", null))).orElse(""));
entry.setField(StandardField.SUBTITLE, item.optString("subtitle"));
Optional<String> yearOpt = Date.parse(item.optString("publish_date")).flatMap(Date::getYear).map(year -> year.toString());
yearOpt.ifPresent(year -> {
entry.setField(StandardField.YEAR, year);
});
entry.setField(StandardField.PUBLISHER, Optional.ofNullable(item.optJSONArray("publishers")).map(array -> array.getString(0)).orElse(""));
return entry;
} catch (JSONException exception) {
throw new ParseException("CrossRef API JSON format has changed", exception);
}
}
use of kong.unirest.json.JSONException in project jabref by JabRef.
the class SemanticScholar method jsonItemToBibEntry.
/**
* This is copy-paste from CrossRef, need to be checked.
*
* @param item an entry received, needs to be parsed into a BibEntry
* @return The BibEntry that corresponds to the received object
* @throws ParseException if the JSONObject could not be parsed
*/
private BibEntry jsonItemToBibEntry(JSONObject item) throws ParseException {
try {
BibEntry entry = new BibEntry(StandardEntryType.Article);
entry.setField(StandardField.URL, item.optString("url"));
entry.setField(StandardField.TITLE, item.optString("title"));
entry.setField(StandardField.ABSTRACT, item.optString("abstract"));
entry.setField(StandardField.VENUE, item.optString("venue"));
entry.setField(StandardField.YEAR, item.optString("year"));
entry.setField(StandardField.AUTHOR, IntStream.range(0, item.optJSONArray("authors").length()).mapToObj(item.optJSONArray("authors")::getJSONObject).map((author) -> author.has("name") ? author.getString("name") : "").collect(Collectors.joining(" and ")));
JSONObject externalIds = item.optJSONObject("externalIds");
entry.setField(StandardField.DOI, externalIds.optString("DOI"));
if (externalIds.has("ArXiv")) {
entry.setField(StandardField.EPRINT, externalIds.getString("ArXiv"));
entry.setField(StandardField.EPRINTTYPE, "arXiv");
}
entry.setField(StandardField.PMID, externalIds.optString("PubMed"));
return entry;
} catch (JSONException exception) {
throw new ParseException("SemanticScholar API JSON format has changed", exception);
}
}
use of kong.unirest.json.JSONException in project jabref by JabRef.
the class DoiFetcher method getAgency.
/**
* Returns registration agency. Optional.empty() if no agency is found.
*
* @param doi the DOI to be searched
*/
public Optional<String> getAgency(DOI doi) throws IOException {
Optional<String> agency = Optional.empty();
try {
URLDownload download = getUrlDownload(new URL(DOI.AGENCY_RESOLVER + "/" + doi.getDOI()));
JSONObject response = new JSONArray(download.asString()).getJSONObject(0);
if (response != null) {
agency = Optional.ofNullable(response.optString("RA"));
}
} catch (JSONException e) {
LOGGER.error("Cannot parse agency fetcher repsonse to JSON");
return Optional.empty();
}
return agency;
}
use of kong.unirest.json.JSONException in project jabref by JabRef.
the class ShortDOIService method makeRequest.
private JSONObject makeRequest(DOI doi) throws ShortDOIServiceException {
URIBuilder uriBuilder = null;
URL url = null;
try {
uriBuilder = new URIBuilder(BASIC_URL);
uriBuilder.setPath(uriBuilder.getPath() + doi.getDOI());
uriBuilder.addParameter("format", "json");
URI uri = uriBuilder.build();
url = uri.toURL();
} catch (URISyntaxException | MalformedURLException e) {
throw new ShortDOIServiceException("Cannot get short DOI", e);
}
URLDownload urlDownload = new URLDownload(url);
try {
JSONObject resultAsJSON = JsonReader.toJsonObject(urlDownload.asInputStream());
if (resultAsJSON.isEmpty()) {
throw new ShortDOIServiceException("Cannot get short DOI");
}
return resultAsJSON;
} catch (ParseException | IOException | JSONException e) {
throw new ShortDOIServiceException("Cannot get short DOI", e);
}
}
use of kong.unirest.json.JSONException in project seleniumRobot by bhecquet.
the class SeleniumRobotServerConnector method createVersion.
/**
* create version
* If version name already exists on server, it's id will be returned. Else, a new one will be created
*/
public void createVersion() {
if (!active) {
return;
}
if (applicationId == null) {
createApplication();
}
try {
JSONObject versionJson = getJSonResponse(buildPostRequest(url + VERSION_API_URL).field(FIELD_NAME, SeleniumTestsContextManager.getApplicationVersion()).field("application", applicationId.toString()));
versionId = versionJson.getInt("id");
} catch (UnirestException | JSONException | SeleniumRobotServerException e) {
throw new SeleniumRobotServerException("cannot create version", e);
}
}
Aggregations