1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
package net.sf.jabref.gui.importer.fetcher;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import net.sf.jabref.Globals;
import net.sf.jabref.gui.importer.ImportInspectionDialog;
import net.sf.jabref.logic.help.HelpFile;
import net.sf.jabref.logic.importer.ImportInspector;
import net.sf.jabref.logic.importer.OutputPrinter;
import net.sf.jabref.logic.importer.util.JSONEntryParser;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.entry.BibEntry;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class DOAJFetcher implements EntryFetcher {
private static final String SEARCH_URL = "https://doaj.org/api/v1/search/articles/";
private static final Log LOGGER = LogFactory.getLog(DOAJFetcher.class);
private static final int MAX_PER_PAGE = 100;
private boolean shouldContinue;
private final JSONEntryParser jsonConverter = new JSONEntryParser();
@Override
public void stopFetching() {
shouldContinue = false;
}
@Override
public boolean processQuery(String query, ImportInspector inspector, OutputPrinter status) {
shouldContinue = true;
try {
status.setStatus(Localization.lang("Searching..."));
HttpResponse<JsonNode> jsonResponse;
jsonResponse = Unirest.get(SEARCH_URL + query + "?pageSize=1").header("accept", "application/json").asJson();
JSONObject jo = jsonResponse.getBody().getObject();
int numberToFetch = jo.getInt("total");
if (numberToFetch > 0) {
if (numberToFetch > MAX_PER_PAGE) {
boolean numberEntered = false;
do {
String strCount = JOptionPane.showInputDialog(Localization.lang("%0 references found. Number of references to fetch?", String.valueOf(numberToFetch)));
if (strCount == null) {
status.setStatus(Localization.lang("%0 import canceled", getTitle()));
return false;
}
try {
numberToFetch = Integer.parseInt(strCount.trim());
numberEntered = true;
} catch (NumberFormatException ex) {
status.showMessage(Localization.lang("Please enter a valid number"));
}
} while (!numberEntered);
}
int fetched = 0; // Keep track of number of items fetched for the progress bar
for (int page = 1; ((page - 1) * MAX_PER_PAGE) <= numberToFetch; page++) {
if (!shouldContinue) {
break;
}
int noToFetch = Math.min(MAX_PER_PAGE, numberToFetch - ((page - 1) * MAX_PER_PAGE));
jsonResponse = Unirest.get(SEARCH_URL + query + "?page=" + page + "&pageSize=" + noToFetch)
.header("accept", "application/json").asJson();
jo = jsonResponse.getBody().getObject();
if (jo.has("results")) {
JSONArray results = jo.getJSONArray("results");
for (int i = 0; i < results.length(); i++) {
JSONObject bibJsonEntry = results.getJSONObject(i).getJSONObject("bibjson");
BibEntry entry = jsonConverter.parseBibJSONtoBibtex(bibJsonEntry,
Globals.prefs.getKeywordDelimiter());
inspector.addEntry(entry);
fetched++;
inspector.setProgress(fetched, numberToFetch);
}
}
}
return true;
} else {
status.showMessage(Localization.lang("No entries found for the search string '%0'", query),
Localization.lang("Search %0", getTitle()), JOptionPane.INFORMATION_MESSAGE);
return false;
}
} catch (UnirestException | JSONException e) {
LOGGER.error("Error while fetching from " + getTitle(), e);
((ImportInspectionDialog)inspector).showErrorMessage(this.getTitle(), e.getLocalizedMessage());
return false;
}
}
@Override
public String getTitle() {
return "DOAJ";
}
@Override
public HelpFile getHelpPage() {
return HelpFile.FETCHER_DOAJ;
}
@Override
public JPanel getOptionsPanel() {
// No additional options available
return null;
}
}
|