forked from JabRef/jabref
-
Notifications
You must be signed in to change notification settings - Fork 15
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
Adding mass addition of bib information feature #664
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
src/main/java/org/jabref/logic/importer/AutoEntryAddition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package org.jabref.logic.importer; | ||
|
||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.jabref.logic.importer.fetcher.CrossRef; | ||
import org.jabref.logic.net.URLDownload; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.field.StandardField; | ||
import org.jabref.model.entry.identifier.DOI; | ||
|
||
import kong.unirest.json.JSONArray; | ||
import kong.unirest.json.JSONException; | ||
import kong.unirest.json.JSONObject; | ||
import org.apache.http.NameValuePair; | ||
import org.apache.http.client.utils.URLEncodedUtils; | ||
|
||
import static org.jabref.gui.edit.automaticfiededitor.AbstractAutomaticFieldEditorTabViewModel.LOGGER; | ||
|
||
public class AutoEntryAddition { | ||
private static final String CROSSREF_API = "https://api.crossref.org/works?query="; | ||
private static final String ARXIV_API = "http://export.arxiv.org/api/query?id_list="; | ||
|
||
private static final Pattern ARXIV_ID_PATTERN = Pattern.compile("(\\d{4}\\.\\d{4,5})(v\\d+)?"); | ||
|
||
private ImportFormatPreferences preferences; | ||
// Function to determine DOI from a given title - just a draft function to test some functionality | ||
// public Optional<DOI> findDOI(String title) { | ||
// try { | ||
// String encodedTitle = URLEncoder.encode(title, StandardCharsets.UTF_8); | ||
// URLDownload download = new URLDownload(CROSSREF_API + encodedTitle); | ||
// String response = download.asString(); | ||
// JSONObject jsonResponse = new JSONObject(response); | ||
// | ||
// if (jsonResponse.getJSONObject("message").getInt("total-results") > 0) { | ||
// JSONArray items = jsonResponse.getJSONObject("message").getJSONArray("items"); | ||
// String doi = items.getJSONObject(0).getString("DOI"); | ||
// return DOI.parse(doi); | ||
// } | ||
// } catch ( | ||
// IOException | | ||
// JSONException e) { | ||
// LOGGER.error("Error while fetching DOI from CrossRef", e); | ||
// } | ||
// return Optional.empty(); | ||
// } | ||
// public Optional<BibEntry> fetchEntryByTitle(String title) throws FetcherException { | ||
// Optional<DOI> optionalDOI = findDOI(title); | ||
// if (optionalDOI.isPresent()) { | ||
// DoiFetcher fetcher = new DoiFetcher(preferences); | ||
// return fetcher.performSearchById(optionalDOI.get().getDOI()); | ||
// } | ||
// | ||
// return Optional.empty(); | ||
// } | ||
|
||
// Function to extract an arxiv ID from a given URL through pattern matching | ||
public static String extractArXivIDFromURL(String url) { | ||
Matcher matcher = ARXIV_ID_PATTERN.matcher(url); | ||
if (matcher.find()) { | ||
return matcher.group(1); | ||
} | ||
return null; | ||
} | ||
|
||
// draft function to fetch entries through arxiv ID | ||
public Optional<BibEntry> fetchEntryByArxivID(String arxivID) throws FetcherException { | ||
try { | ||
URLDownload download = new URLDownload(ARXIV_API + arxivID); | ||
String response = download.asString(); | ||
JSONObject jsonResponse = new JSONObject(response); | ||
|
||
JSONArray entries = jsonResponse.getJSONArray("entry"); | ||
if (entries.length() > 0) { | ||
JSONObject entry = entries.getJSONObject(0); | ||
BibEntry bibEntry = new BibEntry(); | ||
// need to extract bibEntries here. | ||
return Optional.of(bibEntry); | ||
} | ||
} catch (IOException | JSONException e) { | ||
LOGGER.error("Error while fetching entry from ArXiv", e); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
|
||
|
||
public Optional<DOI> getDoiFromUrl(String url) throws URISyntaxException, MalformedURLException, FetcherException, ParseException { | ||
// Extract relevant information from the URL. This will depend on the structure of the URL. | ||
URI uri = new URI(url); | ||
List<NameValuePair> params = URLEncodedUtils.parse(uri, StandardCharsets.UTF_8); | ||
String title = null; | ||
String author = null; | ||
for (NameValuePair param : params) { | ||
if ("title".equals(param.getName())) { | ||
title = param.getValue(); | ||
} else if ("author".equals(param.getName())) { | ||
author = param.getValue(); | ||
} | ||
} | ||
|
||
// Create a BibEntry object and populate its fields. | ||
BibEntry entry = new BibEntry(); | ||
entry.setField(StandardField.TITLE, title); | ||
entry.setField(StandardField.AUTHOR, author); | ||
|
||
// Get the CrossRef API URL for the BibEntry. | ||
CrossRef crossRef = new CrossRef(); | ||
URL apiUrl = crossRef.getURLForEntry(entry); | ||
|
||
// Fetch data from the CrossRef API. | ||
List<BibEntry> fetchedEntries = fetchDataFromUrl(apiUrl); | ||
|
||
// Extract the DOI. | ||
return crossRef.extractIdentifier(entry, fetchedEntries); | ||
} | ||
|
||
// need logic confirmation here | ||
private List<BibEntry> fetchDataFromUrl(URL apiUrl) { | ||
return null; | ||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to re-implement fetching from arxiv. JabRef already has arxiv support. You "just" need to wire the existing fetchers together in a smart way.