fix(metadata): Use existing Goodreads ID for batch refresh instead of searching (#1973)
When refreshing metadata for books that already have a goodreadsId stored,
the GoodReadsParser now fetches directly from the book page instead of
performing a title search first.
Problem:
- Books with goodreadsId populated but missing goodreadsRating
- Batch refresh would search by title, ignoring the stored ID
- If search failed (rate limits, transient errors), ratings never populated
Solution:
- Check for existing goodreadsId before searching
- If valid ID exists, fetch directly from /book/show/{id}
- Fall back to search only if no ID or direct fetch fails
This ensures previously matched books get their ratings updated reliably.
This commit is contained in:
+42
@@ -46,6 +46,24 @@ public class GoodReadsParser implements BookParser {
|
||||
|
||||
@Override
|
||||
public BookMetadata fetchTopMetadata(Book book, FetchMetadataRequest fetchMetadataRequest) {
|
||||
// If book already has a Goodreads ID, use it directly instead of searching
|
||||
// This ensures we fetch ratings for previously matched books
|
||||
String existingGoodreadsId = getExistingGoodreadsId(book);
|
||||
if (existingGoodreadsId != null) {
|
||||
log.info("GoodReads: Using existing Goodreads ID: {}", existingGoodreadsId);
|
||||
try {
|
||||
Document document = fetchDoc(BASE_BOOK_URL + existingGoodreadsId);
|
||||
BookMetadata metadata = parseBookDetails(document, existingGoodreadsId);
|
||||
if (metadata != null) {
|
||||
return metadata;
|
||||
}
|
||||
log.warn("GoodReads: Failed to parse details for existing ID: {}, falling back to search", existingGoodreadsId);
|
||||
} catch (Exception e) {
|
||||
log.warn("GoodReads: Error fetching existing ID {}: {}, falling back to search", existingGoodreadsId, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to search-based approach
|
||||
Optional<BookMetadata> preview = fetchMetadataPreviews(book, fetchMetadataRequest).stream().findFirst();
|
||||
if (preview.isEmpty()) {
|
||||
return null;
|
||||
@@ -54,6 +72,30 @@ public class GoodReadsParser implements BookParser {
|
||||
return fetchedMetadata.isEmpty() ? null : fetchedMetadata.getFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts existing Goodreads ID from book metadata if available.
|
||||
* Returns null if no valid ID exists.
|
||||
*/
|
||||
private String getExistingGoodreadsId(Book book) {
|
||||
if (book == null || book.getMetadata() == null) {
|
||||
return null;
|
||||
}
|
||||
String goodreadsId = book.getMetadata().getGoodreadsId();
|
||||
if (goodreadsId == null || goodreadsId.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
// Validate it looks like a Goodreads ID (numeric, possibly with suffix like "11590-salems-lot")
|
||||
// Strip any non-numeric suffix for the URL
|
||||
String numericId = goodreadsId.split("-")[0].split("\\.")[0];
|
||||
try {
|
||||
Long.parseLong(numericId);
|
||||
return goodreadsId; // Return original ID (Goodreads handles both formats)
|
||||
} catch (NumberFormatException e) {
|
||||
log.debug("GoodReads: Invalid Goodreads ID format: {}", goodreadsId);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BookMetadata> fetchMetadata(Book book, FetchMetadataRequest fetchMetadataRequest) {
|
||||
String isbn = ParserUtils.cleanIsbn(fetchMetadataRequest.getIsbn());
|
||||
|
||||
Reference in New Issue
Block a user