Support Hardcover as metadata provider
This commit is contained in:
committed by
Aditya Chandel
parent
d56d8181d0
commit
da6decb349
@@ -1,10 +1,7 @@
|
||||
package com.adityachandel.booklore.config;
|
||||
|
||||
import com.adityachandel.booklore.model.enums.MetadataProvider;
|
||||
import com.adityachandel.booklore.service.metadata.parser.AmazonBookParser;
|
||||
import com.adityachandel.booklore.service.metadata.parser.BookParser;
|
||||
import com.adityachandel.booklore.service.metadata.parser.GoodReadsParser;
|
||||
import com.adityachandel.booklore.service.metadata.parser.GoogleParser;
|
||||
import com.adityachandel.booklore.service.metadata.parser.*;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@@ -14,11 +11,13 @@ import java.util.Map;
|
||||
public class BookParserConfig {
|
||||
|
||||
@Bean
|
||||
public Map<MetadataProvider, BookParser> parserMap(GoogleParser googleParser, AmazonBookParser amazonBookParser, GoodReadsParser goodReadsParser) {
|
||||
public Map<MetadataProvider, BookParser> parserMap(GoogleParser googleParser, AmazonBookParser amazonBookParser,
|
||||
GoodReadsParser goodReadsParser, HardcoverParser hardcoverParser) {
|
||||
return Map.of(
|
||||
MetadataProvider.Amazon, amazonBookParser,
|
||||
MetadataProvider.GoodReads, goodReadsParser,
|
||||
MetadataProvider.Google, googleParser
|
||||
MetadataProvider.Google, googleParser,
|
||||
MetadataProvider.Hardcover, hardcoverParser
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,12 +30,16 @@ public class BookMetadata {
|
||||
private Integer pageCount;
|
||||
private String language;
|
||||
private Double rating;
|
||||
@Deprecated
|
||||
private Integer ratingCount;
|
||||
@Deprecated
|
||||
private Integer reviewCount;
|
||||
private Double amazonRating;
|
||||
private Integer amazonReviewCount;
|
||||
private Double goodreadsRating;
|
||||
private Integer goodreadsReviewCount;
|
||||
private Double hardcoverRating;
|
||||
private Integer hardcoverReviewCount;
|
||||
private Instant coverUpdatedOn;
|
||||
private List<String> authors;
|
||||
private List<String> categories;
|
||||
@@ -64,6 +68,8 @@ public class BookMetadata {
|
||||
private Boolean amazonReviewCountLocked;
|
||||
private Boolean goodreadsRatingLocked;
|
||||
private Boolean goodreadsReviewCountLocked;
|
||||
private Boolean hardcoverRatingLocked;
|
||||
private Boolean hardcoverReviewCountLocked;
|
||||
private Boolean coverLocked;
|
||||
private Boolean authorsLocked;
|
||||
private Boolean categoriesLocked;
|
||||
|
||||
+1
@@ -9,6 +9,7 @@ public enum AppSettingKey {
|
||||
OIDC_AUTO_PROVISION_DETAILS("oidc_auto_provision_details", true),
|
||||
SIDEBAR_LIBRARY_SORTING("sidebar_library_sorting", true),
|
||||
SIDEBAR_SHELF_SORTING("sidebar_shelf_sorting", true),
|
||||
METADATA_PROVIDER_SETTINGS("metadata_provider_settings", true),
|
||||
|
||||
AUTO_BOOK_SEARCH("auto_book_search", false),
|
||||
COVER_IMAGE_RESOLUTION("cover_image_resolution", false),
|
||||
|
||||
+1
@@ -24,4 +24,5 @@ public class AppSettings {
|
||||
private boolean oidcEnabled;
|
||||
private OidcProviderDetails oidcProviderDetails;
|
||||
private OidcAutoProvisionDetails oidcAutoProvisionDetails;
|
||||
private MetadataProviderSettings metadataProviderSettings;
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.adityachandel.booklore.model.dto.settings;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class MetadataProviderSettings {
|
||||
private Amazon amazon;
|
||||
private Google google;
|
||||
private Goodreads goodReads;
|
||||
private Hardcover hardcover;
|
||||
|
||||
@Data
|
||||
public static class Amazon {
|
||||
private boolean enabled;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Google {
|
||||
private boolean enabled;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Goodreads {
|
||||
private boolean enabled;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Hardcover {
|
||||
private boolean enabled;
|
||||
private String apiKey;
|
||||
}
|
||||
}
|
||||
+21
-2
@@ -84,6 +84,12 @@ public class BookMetadataEntity {
|
||||
@Column(name = "goodreads_review_count")
|
||||
private Integer goodreadsReviewCount;
|
||||
|
||||
@Column(name = "hardcover_rating")
|
||||
private Double hardcoverRating;
|
||||
|
||||
@Column(name = "hardcover_review_count")
|
||||
private Integer hardcoverReviewCount;
|
||||
|
||||
// Locking fields
|
||||
|
||||
@Column(name = "title_locked")
|
||||
@@ -137,6 +143,12 @@ public class BookMetadataEntity {
|
||||
@Column(name = "goodreads_review_count_locked")
|
||||
private Boolean goodreadsReviewCountLocked = Boolean.FALSE;
|
||||
|
||||
@Column(name = "hardcover_rating_locked")
|
||||
private Boolean hardcoverRatingLocked = Boolean.FALSE;
|
||||
|
||||
@Column(name = "hardcover_review_count_locked")
|
||||
private Boolean hardcoverReviewCountLocked = Boolean.FALSE;
|
||||
|
||||
@Column(name = "cover_locked")
|
||||
private Boolean coverLocked = Boolean.FALSE;
|
||||
|
||||
@@ -203,6 +215,8 @@ public class BookMetadataEntity {
|
||||
this.amazonReviewCountLocked = lock;
|
||||
this.goodreadsRatingLocked = lock;
|
||||
this.goodreadsReviewCountLocked = lock;
|
||||
this.hardcoverRatingLocked = lock;
|
||||
this.hardcoverReviewCountLocked = lock;
|
||||
}
|
||||
|
||||
public boolean isAnyFieldLocked() {
|
||||
@@ -228,7 +242,9 @@ public class BookMetadataEntity {
|
||||
|| Boolean.TRUE.equals(this.amazonRatingLocked)
|
||||
|| Boolean.TRUE.equals(this.amazonReviewCountLocked)
|
||||
|| Boolean.TRUE.equals(this.goodreadsRatingLocked)
|
||||
|| Boolean.TRUE.equals(this.goodreadsReviewCountLocked);
|
||||
|| Boolean.TRUE.equals(this.goodreadsReviewCountLocked)
|
||||
|| Boolean.TRUE.equals(this.hardcoverRatingLocked)
|
||||
|| Boolean.TRUE.equals(this.hardcoverReviewCountLocked);
|
||||
}
|
||||
|
||||
public boolean areAllFieldsLocked() {
|
||||
@@ -254,6 +270,9 @@ public class BookMetadataEntity {
|
||||
&& Boolean.TRUE.equals(this.amazonRatingLocked)
|
||||
&& Boolean.TRUE.equals(this.amazonReviewCountLocked)
|
||||
&& Boolean.TRUE.equals(this.goodreadsRatingLocked)
|
||||
&& Boolean.TRUE.equals(this.goodreadsReviewCountLocked);
|
||||
&& Boolean.TRUE.equals(this.goodreadsReviewCountLocked)
|
||||
&& Boolean.TRUE.equals(this.hardcoverRatingLocked)
|
||||
&& Boolean.TRUE.equals(this.hardcoverReviewCountLocked)
|
||||
;
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
package com.adityachandel.booklore.model.enums;
|
||||
|
||||
public enum MetadataProvider {
|
||||
Amazon, GoodReads, Google
|
||||
Amazon, GoodReads, Google, Hardcover
|
||||
}
|
||||
|
||||
@@ -70,6 +70,7 @@ public class AppSettingService {
|
||||
builder.metadataRefreshOptions(getJsonSetting(settingsMap, AppSettingKey.QUICK_BOOK_MATCH, MetadataRefreshOptions.class, null, false));
|
||||
builder.oidcProviderDetails(getJsonSetting(settingsMap, AppSettingKey.OIDC_PROVIDER_DETAILS, OidcProviderDetails.class, null, false));
|
||||
builder.oidcAutoProvisionDetails(getJsonSetting(settingsMap, AppSettingKey.OIDC_AUTO_PROVISION_DETAILS, OidcAutoProvisionDetails.class, null, false));
|
||||
builder.metadataProviderSettings(getJsonSetting(settingsMap, AppSettingKey.METADATA_PROVIDER_SETTINGS, MetadataProviderSettings.class, getMetadataProviderSettings(), true));
|
||||
|
||||
builder.coverResolution(getOrCreateSetting(AppSettingKey.COVER_IMAGE_RESOLUTION, "250x350"));
|
||||
builder.autoBookSearch(Boolean.parseBoolean(getOrCreateSetting(AppSettingKey.AUTO_BOOK_SEARCH, "true")));
|
||||
@@ -121,4 +122,24 @@ public class AppSettingService {
|
||||
private String serializeSettingValue(AppSettingKey key, Object val) throws JsonProcessingException {
|
||||
return key.isJson() ? objectMapper.writeValueAsString(val) : val.toString();
|
||||
}
|
||||
|
||||
private static MetadataProviderSettings getMetadataProviderSettings() {
|
||||
MetadataProviderSettings defaultMetadataProviderSettings = new MetadataProviderSettings();
|
||||
|
||||
MetadataProviderSettings.Amazon defaultAmazon = new MetadataProviderSettings.Amazon();
|
||||
defaultAmazon.setEnabled(true);
|
||||
MetadataProviderSettings.Google defaultGoogle = new MetadataProviderSettings.Google();
|
||||
defaultGoogle.setEnabled(true);
|
||||
MetadataProviderSettings.Goodreads defaultGoodreads = new MetadataProviderSettings.Goodreads();
|
||||
defaultGoodreads.setEnabled(true);
|
||||
MetadataProviderSettings.Hardcover defaultHardcover = new MetadataProviderSettings.Hardcover();
|
||||
defaultHardcover.setEnabled(false);
|
||||
defaultHardcover.setApiKey(null);
|
||||
|
||||
defaultMetadataProviderSettings.setAmazon(defaultAmazon);
|
||||
defaultMetadataProviderSettings.setGoogle(defaultGoogle);
|
||||
defaultMetadataProviderSettings.setGoodReads(defaultGoodreads);
|
||||
defaultMetadataProviderSettings.setHardcover(defaultHardcover);
|
||||
return defaultMetadataProviderSettings;
|
||||
}
|
||||
}
|
||||
+14
@@ -98,6 +98,14 @@ public class BookMetadataUpdater {
|
||||
metadata.setGoodreadsReviewCount(newMetadata.getGoodreadsReviewCount());
|
||||
}
|
||||
|
||||
if ((metadata.getHardcoverRatingLocked() == null || !metadata.getHardcoverRatingLocked()) && newMetadata.getHardcoverRating() != null) {
|
||||
metadata.setHardcoverRating(newMetadata.getHardcoverRating());
|
||||
}
|
||||
|
||||
if ((metadata.getHardcoverReviewCountLocked() == null || !metadata.getHardcoverReviewCountLocked()) && newMetadata.getHardcoverReviewCount() != null) {
|
||||
metadata.setHardcoverReviewCount(newMetadata.getHardcoverReviewCount());
|
||||
}
|
||||
|
||||
if ((metadata.getSeriesNameLocked() == null || !metadata.getSeriesNameLocked()) && newMetadata.getSeriesName() != null) {
|
||||
metadata.setSeriesName(newMetadata.getSeriesName());
|
||||
}
|
||||
@@ -239,6 +247,12 @@ public class BookMetadataUpdater {
|
||||
if (newMetadata.getGoodreadsReviewCountLocked() != null) {
|
||||
metadata.setGoodreadsReviewCountLocked(newMetadata.getGoodreadsReviewCountLocked());
|
||||
}
|
||||
if (newMetadata.getHardcoverRatingLocked() != null) {
|
||||
metadata.setHardcoverRatingLocked(newMetadata.getHardcoverRatingLocked());
|
||||
}
|
||||
if (newMetadata.getHardcoverReviewCountLocked() != null) {
|
||||
metadata.setHardcoverReviewCountLocked(newMetadata.getHardcoverReviewCountLocked());
|
||||
}
|
||||
if (newMetadata.getSeriesNameLocked() != null) {
|
||||
metadata.setSeriesNameLocked(newMetadata.getSeriesNameLocked());
|
||||
}
|
||||
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
package com.adityachandel.booklore.service.metadata.parser;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.Book;
|
||||
import com.adityachandel.booklore.model.dto.BookMetadata;
|
||||
import com.adityachandel.booklore.model.dto.request.FetchMetadataRequest;
|
||||
import com.adityachandel.booklore.model.enums.MetadataProvider;
|
||||
import com.adityachandel.booklore.service.metadata.parser.hardcover.GraphQLResponse;
|
||||
import com.adityachandel.booklore.service.metadata.parser.hardcover.HardcoverBookSearchService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class HardcoverParser implements BookParser {
|
||||
|
||||
private final HardcoverBookSearchService hardcoverBookSearchService;
|
||||
|
||||
@Override
|
||||
public List<BookMetadata> fetchMetadata(Book book, FetchMetadataRequest fetchMetadataRequest) {
|
||||
List<GraphQLResponse.Hit> hits = hardcoverBookSearchService.searchBooks(fetchMetadataRequest.getTitle());
|
||||
|
||||
return hits.stream().map(hit -> {
|
||||
GraphQLResponse.Document doc = hit.getDocument();
|
||||
BookMetadata metadata = new BookMetadata();
|
||||
metadata.setProviderBookId(doc.getSlug());
|
||||
metadata.setTitle(doc.getTitle());
|
||||
metadata.setDescription(doc.getDescription());
|
||||
metadata.setAuthors(doc.getAuthorNames());
|
||||
|
||||
if (doc.getFeaturedSeries() != null) {
|
||||
if (doc.getFeaturedSeries().getSeries() != null) {
|
||||
metadata.setSeriesName(doc.getFeaturedSeries().getSeries().getName());
|
||||
metadata.setSeriesTotal(doc.getFeaturedSeries().getSeries().getBooksCount());
|
||||
}
|
||||
if (doc.getFeaturedSeries().getPosition() != null) {
|
||||
try {
|
||||
metadata.setSeriesNumber(Float.parseFloat(String.valueOf(doc.getFeaturedSeries().getPosition())));
|
||||
} catch (NumberFormatException ignored) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (doc.getRating() != null) {
|
||||
BigDecimal roundedRating = BigDecimal.valueOf(doc.getRating()).setScale(2, RoundingMode.HALF_UP);
|
||||
metadata.setHardcoverRating(roundedRating.doubleValue());
|
||||
}
|
||||
metadata.setHardcoverReviewCount(doc.getRatingsCount());
|
||||
metadata.setPageCount(doc.getPages());
|
||||
metadata.setPublishedDate(doc.getReleaseDate() != null ? LocalDate.parse(doc.getReleaseDate()) : null);
|
||||
metadata.setCategories(doc.getGenres());
|
||||
|
||||
if (doc.getIsbns() != null) {
|
||||
metadata.setIsbn10(doc.getIsbns().stream()
|
||||
.filter(isbn -> isbn.length() == 10)
|
||||
.findFirst()
|
||||
.orElse(null));
|
||||
|
||||
metadata.setIsbn13(doc.getIsbns().stream()
|
||||
.filter(isbn -> isbn.length() == 13)
|
||||
.findFirst()
|
||||
.orElse(null));
|
||||
}
|
||||
|
||||
metadata.setThumbnailUrl(doc.getImage() != null ? doc.getImage().getUrl() : null);
|
||||
metadata.setProvider(MetadataProvider.Hardcover);
|
||||
return metadata;
|
||||
}).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public BookMetadata fetchTopMetadata(Book book, FetchMetadataRequest fetchMetadataRequest) {
|
||||
List<BookMetadata> bookMetadata = fetchMetadata(book, fetchMetadataRequest);
|
||||
return bookMetadata.getFirst();
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.adityachandel.booklore.service.metadata.parser.hardcover;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class GraphQLRequest {
|
||||
private String query;
|
||||
private String operationName;
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
package com.adityachandel.booklore.service.metadata.parser.hardcover;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class GraphQLResponse {
|
||||
private DataWrapper data;
|
||||
|
||||
@Data
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class DataWrapper {
|
||||
private SearchWrapper search;
|
||||
}
|
||||
|
||||
@Data
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class SearchWrapper {
|
||||
private ResultsWrapper results;
|
||||
}
|
||||
|
||||
@Data
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class ResultsWrapper {
|
||||
private List<Hit> hits;
|
||||
}
|
||||
|
||||
@Data
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class Hit {
|
||||
private Document document;
|
||||
}
|
||||
|
||||
@Data
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class Document {
|
||||
private String id;
|
||||
private String slug;
|
||||
private String title;
|
||||
|
||||
@JsonProperty("author_names")
|
||||
private List<String> authorNames;
|
||||
|
||||
private String description;
|
||||
private List<String> isbns;
|
||||
private Double rating;
|
||||
|
||||
@JsonProperty("ratings_count")
|
||||
private Integer ratingsCount;
|
||||
|
||||
private Integer pages;
|
||||
|
||||
@JsonProperty("release_date")
|
||||
private String releaseDate;
|
||||
|
||||
private List<String> genres;
|
||||
|
||||
@JsonProperty("featured_series")
|
||||
private FeaturedSeries featuredSeries;
|
||||
|
||||
private Image image;
|
||||
}
|
||||
|
||||
@Data
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class Image {
|
||||
private String url;
|
||||
}
|
||||
|
||||
@Data
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class FeaturedSeries {
|
||||
private Integer position;
|
||||
private Series series;
|
||||
}
|
||||
|
||||
@Data
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class Series {
|
||||
private String name;
|
||||
@JsonProperty("books_count")
|
||||
private Integer booksCount;
|
||||
}
|
||||
}
|
||||
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package com.adityachandel.booklore.service.metadata.parser.hardcover;
|
||||
|
||||
import com.adityachandel.booklore.service.AppSettingService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestClient;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class HardcoverBookSearchService {
|
||||
|
||||
private final RestClient restClient;
|
||||
private final AppSettingService appSettingService;
|
||||
|
||||
@Autowired
|
||||
public HardcoverBookSearchService(AppSettingService appSettingService) {
|
||||
this.appSettingService = appSettingService;
|
||||
String apiUrl = "https://api.hardcover.app/v1/graphql";
|
||||
this.restClient = RestClient.builder()
|
||||
.baseUrl(apiUrl)
|
||||
.build();
|
||||
}
|
||||
|
||||
public List<GraphQLResponse.Hit> searchBooks(String query) {
|
||||
String apiToken = appSettingService.getAppSettings().getMetadataProviderSettings().getHardcover().getApiKey();
|
||||
if (apiToken == null || apiToken.isEmpty()) {
|
||||
log.warn("Hardcover API token not set");
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
String graphqlQuery = """
|
||||
query SearchBooks {
|
||||
search(
|
||||
query: "%s",
|
||||
query_type: "Book",
|
||||
per_page: 5,
|
||||
page: 1
|
||||
) {
|
||||
results
|
||||
}
|
||||
}
|
||||
""".formatted(query);
|
||||
|
||||
GraphQLRequest body = new GraphQLRequest();
|
||||
body.setQuery(graphqlQuery);
|
||||
body.setOperationName("SearchBooks");
|
||||
|
||||
GraphQLResponse response = restClient.post()
|
||||
.uri("")
|
||||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
|
||||
.header(HttpHeaders.AUTHORIZATION, "Bearer " + apiToken)
|
||||
.body(body)
|
||||
.retrieve()
|
||||
.body(GraphQLResponse.class);
|
||||
|
||||
return response.getData().getSearch().getResults().getHits();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
ALTER TABLE book_metadata
|
||||
ADD COLUMN hardcover_rating FLOAT,
|
||||
ADD COLUMN hardcover_review_count INT,
|
||||
ADD COLUMN hardcover_rating_locked BOOLEAN DEFAULT FALSE,
|
||||
ADD COLUMN hardcover_review_count_locked BOOLEAN DEFAULT FALSE
|
||||
Reference in New Issue
Block a user