feat(hardcover): get Hardcover metadata from a specific edition when ISBN is provided (#2678)
* fix: clean ISBN should allow ISBNs to have an X at the end * hardcover mood filter overload for just list of tags * When ISBN is given, fetch the book directly by the edition with that ISBN, and provide metadata from that edition * chore: improved formatting * PR improvements * replaced CachedTag with GraphQL specific class * moved hardcover cached tag to shared model
This commit is contained in:
+127
-8
@@ -42,9 +42,9 @@ public class HardcoverParser implements BookParser {
|
||||
boolean searchByIsbn = isbnCleaned != null && !isbnCleaned.isBlank();
|
||||
|
||||
if (searchByIsbn) {
|
||||
log.info("Hardcover: Fetching metadata using ISBN {}", fetchMetadataRequest.getIsbn());
|
||||
List<GraphQLResponse.Hit> hits = hardcoverBookSearchService.searchBooks(fetchMetadataRequest.getIsbn());
|
||||
return processHits(hits, fetchMetadataRequest, true);
|
||||
log.info("Hardcover: Fetching metadata using ISBN {}", isbnCleaned);
|
||||
List<GraphQLResponse.BookWithEditions> hits = hardcoverBookSearchService.searchBookByIsbn(isbnCleaned);
|
||||
return processBooksWithEditions(hits);
|
||||
}
|
||||
|
||||
String title = fetchMetadataRequest.getTitle();
|
||||
@@ -79,6 +79,27 @@ public class HardcoverParser implements BookParser {
|
||||
return results;
|
||||
}
|
||||
|
||||
private List<BookMetadata> processBooksWithEditions(List<GraphQLResponse.BookWithEditions> books) {
|
||||
if (books == null || books.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<BookMetadata> results = new ArrayList<>();
|
||||
for (GraphQLResponse.BookWithEditions book : books) {
|
||||
if (book.getEditions() == null || book.getEditions().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
for (GraphQLResponse.Edition edition : book.getEditions()) {
|
||||
log.debug("Processing edition '{}' with id '{}' of book '{}'", edition.getTitle(), edition.getId(), book.getTitle());
|
||||
BookMetadata metadata = mapEditionToMetadata(edition, book);
|
||||
if (metadata != null) {
|
||||
results.add(metadata);
|
||||
}
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private List<BookMetadata> processHits(List<GraphQLResponse.Hit> hits, FetchMetadataRequest request, boolean searchByIsbn) {
|
||||
if (hits == null || hits.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
@@ -110,7 +131,7 @@ public class HardcoverParser implements BookParser {
|
||||
return results;
|
||||
}
|
||||
|
||||
private boolean filterByAuthor(GraphQLResponse.Document doc, String searchAuthor,
|
||||
private boolean filterByAuthor(GraphQLResponse.Document doc, String searchAuthor,
|
||||
boolean searchByIsbn, FuzzyScore fuzzyScore) {
|
||||
// Skip author filtering for ISBN searches or when no author provided
|
||||
if (searchByIsbn || searchAuthor.isBlank()) {
|
||||
@@ -131,7 +152,7 @@ public class HardcoverParser implements BookParser {
|
||||
for (String query : searchAuthorTokens) {
|
||||
int score = fuzzyScore.fuzzyScore(actual, query);
|
||||
int maxScore = Math.max(
|
||||
fuzzyScore.fuzzyScore(query, query),
|
||||
fuzzyScore.fuzzyScore(query, query),
|
||||
fuzzyScore.fuzzyScore(actual, actual)
|
||||
);
|
||||
double similarity = maxScore > 0 ? (double) score / maxScore : 0;
|
||||
@@ -183,6 +204,104 @@ public class HardcoverParser implements BookParser {
|
||||
|
||||
metadata.setThumbnailUrl(doc.getImage() != null ? doc.getImage().getUrl() : null);
|
||||
metadata.setProvider(MetadataProvider.Hardcover);
|
||||
return metadata;
|
||||
}
|
||||
|
||||
private BookMetadata mapEditionToMetadata(GraphQLResponse.Edition edition, GraphQLResponse.BookWithEditions book) {
|
||||
BookMetadata metadata = new BookMetadata();
|
||||
metadata.setHardcoverId(book.getSlug());
|
||||
|
||||
Integer bookId = book.getId();
|
||||
if (bookId != null) {
|
||||
metadata.setHardcoverBookId(bookId.toString());
|
||||
}
|
||||
|
||||
metadata.setTitle(edition.getTitle());
|
||||
metadata.setSubtitle(edition.getSubtitle());
|
||||
metadata.setDescription(book.getDescription());
|
||||
|
||||
if (edition.getCachedContributors() != null) {
|
||||
metadata.setAuthors(edition.getCachedContributors().stream()
|
||||
.map(GraphQLResponse.Contributor::getAuthor)
|
||||
.filter(Objects::nonNull)
|
||||
.map(GraphQLResponse.Author::getName)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
if (book.getFeaturedBookSeries() != null && book.getFeaturedBookSeries().getSeries() != null) {
|
||||
metadata.setSeriesName(book.getFeaturedBookSeries().getSeries().getName());
|
||||
metadata.setSeriesTotal(book.getFeaturedBookSeries().getSeries().getBooksCount());
|
||||
|
||||
if (book.getFeaturedBookSeries().getPosition() != null) {
|
||||
try {
|
||||
metadata.setSeriesNumber(Float.parseFloat(String.valueOf(book.getFeaturedBookSeries().getPosition())));
|
||||
} catch (NumberFormatException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (book.getRating() != null) {
|
||||
metadata.setHardcoverRating(
|
||||
BigDecimal.valueOf(book.getRating()).setScale(2, RoundingMode.HALF_UP).doubleValue()
|
||||
);
|
||||
}
|
||||
metadata.setHardcoverReviewCount(book.getRatingsCount());
|
||||
metadata.setPageCount(edition.getPages());
|
||||
|
||||
if (edition.getReleaseDate() != null) {
|
||||
try {
|
||||
metadata.setPublishedDate(LocalDate.parse(edition.getReleaseDate()));
|
||||
} catch (Exception e) {
|
||||
log.debug("Could not parse release date: {}", edition.getReleaseDate());
|
||||
}
|
||||
}
|
||||
|
||||
// Set the language from the edition
|
||||
if (edition.getLanguage() != null && edition.getLanguage().getCode2() != null) {
|
||||
metadata.setLanguage(edition.getLanguage().getCode2());
|
||||
}
|
||||
|
||||
// Set the Publisher from the edition
|
||||
if (edition.getPublisher() != null && edition.getPublisher().getName() != null) {
|
||||
metadata.setPublisher(edition.getPublisher().getName());
|
||||
}
|
||||
|
||||
GraphQLResponse.CachedTags cachedTags = book.getCachedTags();
|
||||
|
||||
if (cachedTags != null && cachedTags.getMood() != null && !cachedTags.getMood().isEmpty()) {
|
||||
Set<String> basicFilteredMoods = HardcoverMoodFilter.filterMoodsWithCounts(cachedTags.getMood());
|
||||
metadata.setMoods(basicFilteredMoods.stream()
|
||||
.map(WordUtils::capitalizeFully)
|
||||
.collect(Collectors.toCollection(LinkedHashSet::new)));
|
||||
}
|
||||
|
||||
if (cachedTags != null && cachedTags.getGenre() != null && !cachedTags.getGenre().isEmpty()) {
|
||||
Set<String> filteredGenres = HardcoverMoodFilter.filterGenresWithCounts(cachedTags.getGenre());
|
||||
metadata.setCategories(filteredGenres.stream()
|
||||
.map(WordUtils::capitalizeFully)
|
||||
.collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
if (cachedTags != null && cachedTags.getTag() != null && !cachedTags.getTag().isEmpty()) {
|
||||
Set<String> filteredTags = HardcoverMoodFilter.filterTagsWithCounts(cachedTags.getTag());
|
||||
metadata.setTags(filteredTags.stream()
|
||||
.map(WordUtils::capitalizeFully)
|
||||
.collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
metadata.setIsbn10(edition.getIsbn10());
|
||||
metadata.setIsbn13(edition.getIsbn13());
|
||||
|
||||
// If only one ISBN is provided, calculate the other
|
||||
if (metadata.getIsbn10() != null && metadata.getIsbn13() == null) {
|
||||
metadata.setIsbn13(BookUtils.isbn10To13(edition.getIsbn10()));
|
||||
} else if (metadata.getIsbn13() != null && metadata.getIsbn10() == null) {
|
||||
metadata.setIsbn10(BookUtils.isbn13to10(edition.getIsbn13()));
|
||||
}
|
||||
|
||||
metadata.setThumbnailUrl(edition.getImage() != null ? edition.getImage().getUrl() : null);
|
||||
metadata.setProvider(MetadataProvider.Hardcover);
|
||||
|
||||
return metadata;
|
||||
}
|
||||
@@ -282,9 +401,9 @@ public class HardcoverParser implements BookParser {
|
||||
if (StringUtils.isBlank(inputIsbn)) {
|
||||
// If we didn't search by ISBN, use first ISBN from results
|
||||
matchingIsbn = doc.getIsbns().stream()
|
||||
.filter(isbn -> isbn.length() == 10 || isbn.length() == 13)
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
.filter(isbn -> isbn.length() == 10 || isbn.length() == 13)
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
} else if (doc.getIsbns().contains(inputIsbn)) {
|
||||
// If we searched by ISBN and it matches a result perfectly, use that
|
||||
matchingIsbn = inputIsbn;
|
||||
|
||||
@@ -4,10 +4,15 @@ import java.util.regex.Pattern;
|
||||
|
||||
public class ParserUtils {
|
||||
|
||||
private static final Pattern NON_DIGIT_PATTERN = Pattern.compile("[^0-9]");
|
||||
private static final Pattern NON_ISBN_CHAR_PATTERN = Pattern.compile("[^0-9Xx]");
|
||||
|
||||
public static String cleanIsbn(String isbn) {
|
||||
if (isbn == null) return null;
|
||||
return NON_DIGIT_PATTERN.matcher(isbn).replaceAll("");
|
||||
String cleaned = NON_ISBN_CHAR_PATTERN.matcher(isbn).replaceAll("");
|
||||
// Normalize 'x' to 'X' for ISBN-10 check digit
|
||||
if (cleaned.length() == 10 && cleaned.endsWith("x")) {
|
||||
cleaned = cleaned.substring(0, 9) + "X";
|
||||
}
|
||||
return cleaned;
|
||||
}
|
||||
}
|
||||
|
||||
+133
@@ -23,6 +23,7 @@ public class GraphQLResponse {
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class Data {
|
||||
private Search search;
|
||||
private List<BookWithEditions> books;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@@ -160,6 +161,82 @@ public class GraphQLResponse {
|
||||
private Integer usersReadCount;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class BookWithEditions {
|
||||
private Integer id;
|
||||
private String slug;
|
||||
private String title;
|
||||
private String subtitle;
|
||||
private String description;
|
||||
|
||||
@JsonProperty("cached_contributors")
|
||||
private List<Contributor> cachedContributors;
|
||||
|
||||
@JsonProperty("featured_book_series")
|
||||
private FeaturedSeries featuredBookSeries;
|
||||
|
||||
private Double rating;
|
||||
|
||||
@JsonProperty("ratings_count")
|
||||
private Integer ratingsCount;
|
||||
|
||||
@JsonProperty("reviews_count")
|
||||
private Integer reviewsCount;
|
||||
|
||||
private Integer pages;
|
||||
|
||||
@JsonProperty("release_date")
|
||||
private String releaseDate;
|
||||
|
||||
@JsonProperty("release_year")
|
||||
private Integer releaseYear;
|
||||
|
||||
private Image image;
|
||||
|
||||
@JsonProperty("cached_tags")
|
||||
private CachedTags cachedTags;
|
||||
|
||||
private List<Edition> editions;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class Edition {
|
||||
private Integer id;
|
||||
private String title;
|
||||
private String subtitle;
|
||||
|
||||
@JsonProperty("cached_contributors")
|
||||
private List<Contributor> cachedContributors;
|
||||
|
||||
private Integer pages;
|
||||
|
||||
@JsonProperty("release_date")
|
||||
private String releaseDate;
|
||||
|
||||
@JsonProperty("release_year")
|
||||
private Integer releaseYear;
|
||||
|
||||
private Image image;
|
||||
|
||||
private Publisher publisher;
|
||||
|
||||
@JsonProperty("isbn_10")
|
||||
private String isbn10;
|
||||
|
||||
@JsonProperty("isbn_13")
|
||||
private String isbn13;
|
||||
|
||||
private Language language;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@@ -189,4 +266,60 @@ public class GraphQLResponse {
|
||||
@JsonProperty("books_count")
|
||||
private Integer booksCount;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class Contributor {
|
||||
private Author author;
|
||||
private String contribution;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class Author {
|
||||
private Integer id;
|
||||
private String slug;
|
||||
private String name;
|
||||
private Image image;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class CachedTags {
|
||||
@JsonProperty("Genre")
|
||||
private List<HardcoverCachedTag> genre;
|
||||
|
||||
@JsonProperty("Mood")
|
||||
private List<HardcoverCachedTag> mood;
|
||||
|
||||
@JsonProperty("Tag")
|
||||
private List<HardcoverCachedTag> tag;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class Publisher {
|
||||
private String name;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class Language {
|
||||
private String code2;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-23
@@ -18,27 +18,5 @@ public class HardcoverBookDetails {
|
||||
private String title;
|
||||
|
||||
@JsonProperty("cached_tags")
|
||||
private Map<String, List<CachedTag>> cachedTags;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class CachedTag {
|
||||
private String tag;
|
||||
|
||||
@JsonProperty("tagSlug")
|
||||
private String tagSlug;
|
||||
|
||||
private String category;
|
||||
|
||||
@JsonProperty("categorySlug")
|
||||
private String categorySlug;
|
||||
|
||||
@JsonProperty("spoilerRatio")
|
||||
private Double spoilerRatio;
|
||||
|
||||
private Integer count;
|
||||
}
|
||||
private Map<String, List<HardcoverCachedTag>> cachedTags;
|
||||
}
|
||||
|
||||
+70
-1
@@ -35,6 +35,75 @@ public class HardcoverBookSearchService {
|
||||
.build();
|
||||
}
|
||||
|
||||
public List<GraphQLResponse.BookWithEditions> searchBookByIsbn(String isbn) {
|
||||
String apiToken = getApiToken();
|
||||
if (apiToken == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
GraphQLRequest body = new GraphQLRequest();
|
||||
body.setQuery("""
|
||||
query BookSearchByIsbn($isbn: String!) {
|
||||
books(
|
||||
where: {editions: {_or: [{isbn_13: {_eq: $isbn}}, {isbn_10: {_eq: $isbn}}]}}
|
||||
) {
|
||||
id
|
||||
slug
|
||||
title
|
||||
subtitle
|
||||
description
|
||||
cached_contributors
|
||||
featured_book_series {
|
||||
series {
|
||||
name
|
||||
books_count
|
||||
}
|
||||
position
|
||||
}
|
||||
rating
|
||||
ratings_count
|
||||
reviews_count
|
||||
pages
|
||||
release_date
|
||||
release_year
|
||||
image {
|
||||
url
|
||||
}
|
||||
cached_tags
|
||||
editions(where: {_or: [{isbn_13: {_eq: $isbn}}, {isbn_10: {_eq: $isbn}}]}) {
|
||||
id
|
||||
title
|
||||
subtitle
|
||||
cached_contributors
|
||||
pages
|
||||
release_date
|
||||
release_year
|
||||
image {
|
||||
url
|
||||
}
|
||||
publisher {
|
||||
name
|
||||
}
|
||||
isbn_10
|
||||
isbn_13
|
||||
language {
|
||||
code2
|
||||
}
|
||||
}
|
||||
}
|
||||
}""");
|
||||
body.setVariables(java.util.Map.of("isbn", isbn));
|
||||
|
||||
GraphQLResponse response = executeRequest(body, GraphQLResponse.class, apiToken);
|
||||
if (response == null || response.getData() == null ||
|
||||
response.getData().getBooks() == null ||
|
||||
response.getData().getBooks().isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return response.getData().getBooks();
|
||||
}
|
||||
|
||||
public List<GraphQLResponse.Hit> searchBooks(String query) {
|
||||
return searchBooks(query, DEFAULT_PER_PAGE);
|
||||
}
|
||||
@@ -46,7 +115,7 @@ public class HardcoverBookSearchService {
|
||||
}
|
||||
|
||||
int sanitizedPerPage = Math.min(Math.max(perPage, 1), 100);
|
||||
|
||||
|
||||
GraphQLRequest body = new GraphQLRequest();
|
||||
body.setQuery("query BookSearch($q: String!, $limit: Int!) { search(query: $q, query_type: \"Book\", per_page: $limit, page: 1) { results } }");
|
||||
body.setVariables(java.util.Map.of("q", query, "limit", sanitizedPerPage));
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package org.booklore.service.metadata.parser.hardcover;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.*;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class HardcoverCachedTag {
|
||||
private String tag;
|
||||
|
||||
@JsonProperty("tagSlug")
|
||||
private String tagSlug;
|
||||
|
||||
private String category;
|
||||
|
||||
@JsonProperty("categorySlug")
|
||||
private String categorySlug;
|
||||
|
||||
@JsonProperty("spoilerRatio")
|
||||
private Double spoilerRatio;
|
||||
|
||||
private Integer count;
|
||||
}
|
||||
|
||||
+23
-14
@@ -38,12 +38,15 @@ public class HardcoverMoodFilter {
|
||||
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||
}
|
||||
|
||||
public static Set<String> filterMoodsWithCounts(Map<String, List<HardcoverBookDetails.CachedTag>> cachedTags) {
|
||||
public static Set<String> filterMoodsWithCounts(Map<String, List<HardcoverCachedTag>> cachedTags) {
|
||||
if (cachedTags == null || cachedTags.isEmpty()) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
List<HardcoverBookDetails.CachedTag> moodTags = cachedTags.get("Mood");
|
||||
return filterMoodsWithCounts(cachedTags.get("Mood"));
|
||||
}
|
||||
|
||||
public static Set<String> filterMoodsWithCounts(List<HardcoverCachedTag> moodTags) {
|
||||
if (moodTags == null || moodTags.isEmpty()) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
@@ -51,7 +54,7 @@ public class HardcoverMoodFilter {
|
||||
// Find the maximum vote count
|
||||
int maxCount = moodTags.stream()
|
||||
.filter(tag -> tag.getCount() != null)
|
||||
.mapToInt(HardcoverBookDetails.CachedTag::getCount)
|
||||
.mapToInt(HardcoverCachedTag::getCount)
|
||||
.max()
|
||||
.orElse(0);
|
||||
|
||||
@@ -62,10 +65,10 @@ public class HardcoverMoodFilter {
|
||||
|
||||
int minCountThreshold = Math.max(MIN_VOTE_COUNT, (int) (maxCount * MIN_VOTE_RATIO));
|
||||
|
||||
List<HardcoverBookDetails.CachedTag> filteredMoods = moodTags.stream()
|
||||
List<HardcoverCachedTag> filteredMoods = moodTags.stream()
|
||||
.filter(tag -> tag.getCount() != null && tag.getCount() >= minCountThreshold)
|
||||
.filter(tag -> tag.getTag() != null)
|
||||
.sorted(Comparator.comparingInt(HardcoverBookDetails.CachedTag::getCount).reversed())
|
||||
.sorted(Comparator.comparingInt(HardcoverCachedTag::getCount).reversed())
|
||||
.limit(MAX_MOODS)
|
||||
.toList();
|
||||
|
||||
@@ -73,16 +76,19 @@ public class HardcoverMoodFilter {
|
||||
moodTags.size(), filteredMoods.size(), maxCount, minCountThreshold);
|
||||
|
||||
return filteredMoods.stream()
|
||||
.map(HardcoverBookDetails.CachedTag::getTag)
|
||||
.map(HardcoverCachedTag::getTag)
|
||||
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||
}
|
||||
|
||||
public static Set<String> filterGenresWithCounts(Map<String, List<HardcoverBookDetails.CachedTag>> cachedTags) {
|
||||
public static Set<String> filterGenresWithCounts(Map<String, List<HardcoverCachedTag>> cachedTags) {
|
||||
if (cachedTags == null || cachedTags.isEmpty()) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
List<HardcoverBookDetails.CachedTag> genreTags = cachedTags.get("Genre");
|
||||
return filterGenresWithCounts(cachedTags.get("Genre"));
|
||||
}
|
||||
|
||||
public static Set<String> filterGenresWithCounts(List<HardcoverCachedTag> genreTags) {
|
||||
if (genreTags == null || genreTags.isEmpty()) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
@@ -91,18 +97,21 @@ public class HardcoverMoodFilter {
|
||||
return genreTags.stream()
|
||||
.filter(tag -> tag.getCount() != null && tag.getCount() >= 1)
|
||||
.filter(tag -> tag.getTag() != null)
|
||||
.sorted(Comparator.comparingInt(HardcoverBookDetails.CachedTag::getCount).reversed())
|
||||
.sorted(Comparator.comparingInt(HardcoverCachedTag::getCount).reversed())
|
||||
.limit(10) // Allow more genres
|
||||
.map(HardcoverBookDetails.CachedTag::getTag)
|
||||
.map(HardcoverCachedTag::getTag)
|
||||
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||
}
|
||||
|
||||
public static Set<String> filterTagsWithCounts(Map<String, List<HardcoverBookDetails.CachedTag>> cachedTags) {
|
||||
public static Set<String> filterTagsWithCounts(Map<String, List<HardcoverCachedTag>> cachedTags) {
|
||||
if (cachedTags == null || cachedTags.isEmpty()) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
List<HardcoverBookDetails.CachedTag> tagList = cachedTags.get("Tag");
|
||||
return filterTagsWithCounts(cachedTags.get("Tag"));
|
||||
}
|
||||
|
||||
public static Set<String> filterTagsWithCounts(List<HardcoverCachedTag> tagList) {
|
||||
if (tagList == null || tagList.isEmpty()) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
@@ -111,9 +120,9 @@ public class HardcoverMoodFilter {
|
||||
return tagList.stream()
|
||||
.filter(tag -> tag.getCount() != null && tag.getCount() >= 2)
|
||||
.filter(tag -> tag.getTag() != null)
|
||||
.sorted(Comparator.comparingInt(HardcoverBookDetails.CachedTag::getCount).reversed())
|
||||
.sorted(Comparator.comparingInt(HardcoverCachedTag::getCount).reversed())
|
||||
.limit(10)
|
||||
.map(HardcoverBookDetails.CachedTag::getTag)
|
||||
.map(HardcoverCachedTag::getTag)
|
||||
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user