-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Fetcher OttoBib #5125
Fetcher OttoBib #5125
Changes from 5 commits
56ca5c9
ce4ae96
46b1bfc
7c90f7a
477432a
10f1450
72776cc
77606fa
992f63f
236d622
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package org.jabref.logic.importer.fetcher; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.jabref.logic.importer.FetcherException; | ||
import org.jabref.logic.importer.ImportFormatPreferences; | ||
import org.jabref.logic.importer.ParseException; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.strings.StringUtil; | ||
|
||
import com.mashape.unirest.http.HttpResponse; | ||
import com.mashape.unirest.http.Unirest; | ||
import com.mashape.unirest.http.exceptions.UnirestException; | ||
|
||
/** | ||
* Fetcher for ISBN using https://www.ottobib.com | ||
*/ | ||
public class IsbnViaOttoBibFetcher extends AbstractIsbnFetcher { | ||
|
||
public IsbnViaOttoBibFetcher(ImportFormatPreferences importFormatPreferences) { | ||
super(importFormatPreferences); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "ISBN (OttoBib)"; | ||
} | ||
|
||
/** | ||
* @return null, because the identifier is passed using form data. This method is not used. | ||
*/ | ||
@Override | ||
public URL getURLForID(String identifier) throws URISyntaxException, MalformedURLException, FetcherException { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Optional<BibEntry> performSearchById(String identifier) throws FetcherException { | ||
if (StringUtil.isBlank(identifier)) { | ||
return Optional.empty(); | ||
} | ||
|
||
this.ensureThatIsbnIsValid(identifier); | ||
|
||
HttpResponse<String> postResponse; | ||
|
||
String BASE_URL = "https://www.ottobib.com/isbn/" + identifier + "/bibtex"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would propose to make Also, I would prefer a |
||
|
||
try { | ||
postResponse = Unirest.post(BASE_URL) | ||
.asString(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please put this into the line before |
||
} catch (UnirestException e) { | ||
throw new FetcherException("Could not retrieve data from ottobib.com", e); | ||
} | ||
if (postResponse.getStatus() != 200) { | ||
throw new FetcherException("Error while retrieving data from ottobib.com: " + postResponse.getBody()); | ||
} | ||
|
||
List<BibEntry> fetchedEntries; | ||
try { | ||
fetchedEntries = getParser().parseEntries(postResponse.getRawBody()); | ||
} catch (ParseException e) { | ||
throw new FetcherException("An internal parser error occurred", e); | ||
} | ||
if (fetchedEntries.isEmpty()) { | ||
return Optional.empty(); | ||
} else if (fetchedEntries.size() > 1) { | ||
LOGGER.info("Fetcher " + getName() + "found more than one result for identifier " + identifier | ||
+ ". We will use the first entry."); | ||
} | ||
|
||
BibEntry entry = fetchedEntries.get(0); | ||
|
||
// ottobib does not return an ISBN. | ||
entry.setField("isbn", identifier); | ||
|
||
doPostCleanup(entry); | ||
|
||
return Optional.of(entry); | ||
} | ||
|
||
@Override | ||
public void doPostCleanup(BibEntry entry) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can remove that method if it's not used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed the method I was not using. I'll do the unit test soon. |
||
|
||
} | ||
|
||
} |
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.
Please remove these exceptions. They can never be thrown.
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.
@LinusDietz You are right that those exceptions can never be thrown, but the Exception declaration is coming from the interface IdBasedParserFetcher
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.
Why is it not possible to implement the ottobib fetcher by properly overwriting
getURLForID
as e.g here:jabref/src/main/java/org/jabref/logic/importer/fetcher/IsbnViaEbookDeFetcher.java
Lines 32 to 38 in e89d603
I don't really see why the
performSearchById
has to be overwritten (which is more work as you have to manually post the request and parse the response).