Feature: Add Comicvine to metadata providers. (#804)
* added comcivine * updated comicvine schema * added comic vine support * changed SQL changes * fixed init provider bug * updated changes for Flyway migration. --------- Co-authored-by: Aditya Chandel <8075870+adityachandelgit@users.noreply.github.com>
This commit is contained in:
co-authored by
Aditya Chandel
parent
e027225775
commit
46f86deb24
@@ -12,12 +12,13 @@ public class BookParserConfig {
|
||||
|
||||
@Bean
|
||||
public Map<MetadataProvider, BookParser> parserMap(GoogleParser googleParser, AmazonBookParser amazonBookParser,
|
||||
GoodReadsParser goodReadsParser, HardcoverParser hardcoverParser) {
|
||||
GoodReadsParser goodReadsParser, HardcoverParser hardcoverParser, ComicvineBookParser comicvineBookParser) {
|
||||
return Map.of(
|
||||
MetadataProvider.Amazon, amazonBookParser,
|
||||
MetadataProvider.GoodReads, goodReadsParser,
|
||||
MetadataProvider.Google, googleParser,
|
||||
MetadataProvider.Hardcover, hardcoverParser
|
||||
MetadataProvider.Hardcover, hardcoverParser,
|
||||
MetadataProvider.Comicvine, comicvineBookParser
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+1
@@ -20,6 +20,7 @@ public interface MetadataClearFlagsMapper {
|
||||
@Mapping(target = "isbn10", source = "clearIsbn10")
|
||||
@Mapping(target = "asin", source = "clearAsin")
|
||||
@Mapping(target = "goodreadsId", source = "clearGoodreadsId")
|
||||
@Mapping(target = "comicvineId", source = "clearComicvineId")
|
||||
@Mapping(target = "hardcoverId", source = "clearHardcoverId")
|
||||
@Mapping(target = "googleId", source = "clearGoogleId")
|
||||
@Mapping(target = "pageCount", source = "clearPageCount")
|
||||
|
||||
@@ -17,6 +17,7 @@ public class MetadataClearFlags {
|
||||
private boolean isbn10;
|
||||
private boolean asin;
|
||||
private boolean goodreadsId;
|
||||
private boolean comicvineId;
|
||||
private boolean hardcoverId;
|
||||
private boolean googleId;
|
||||
private boolean pageCount;
|
||||
|
||||
@@ -34,6 +34,7 @@ public class BookMetadata {
|
||||
private Double amazonRating;
|
||||
private Integer amazonReviewCount;
|
||||
private String goodreadsId;
|
||||
private String comicvineId;
|
||||
private Double goodreadsRating;
|
||||
private Integer goodreadsReviewCount;
|
||||
private String hardcoverId;
|
||||
@@ -59,6 +60,7 @@ public class BookMetadata {
|
||||
private Boolean isbn10Locked;
|
||||
private Boolean asinLocked;
|
||||
private Boolean goodreadsIdLocked;
|
||||
private Boolean comicvineIdLocked;
|
||||
private Boolean hardcoverIdLocked;
|
||||
private Boolean googleIdLocked;
|
||||
private Boolean pageCountLocked;
|
||||
|
||||
@@ -35,6 +35,7 @@ public class EpubMetadata {
|
||||
private Double amazonRating;
|
||||
private Integer amazonReviewCount;
|
||||
private String goodreadsId;
|
||||
private String comicvineId;
|
||||
private Double goodreadsRating;
|
||||
private Integer goodreadsReviewCount;
|
||||
private String hardcoverId;
|
||||
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
package com.adityachandel.booklore.model.dto.response.comicvineapi;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
@JsonTypeInfo(
|
||||
use = JsonTypeInfo.Id.NAME,
|
||||
include = JsonTypeInfo.As.EXISTING_PROPERTY,
|
||||
|
||||
property = "resource_type",
|
||||
defaultImpl = Volume.class
|
||||
)
|
||||
|
||||
@JsonSubTypes({
|
||||
@JsonSubTypes.Type(value = Volume.class, name = "volume"),
|
||||
@JsonSubTypes.Type(value = Issue.class, name = "issue")
|
||||
})
|
||||
|
||||
public abstract class Comic {
|
||||
private int id;
|
||||
protected String name;
|
||||
protected ComicVineImage image;
|
||||
private String description;
|
||||
|
||||
@JsonProperty("concept_credits")
|
||||
private List<ComicvineItem> conceptCredits;
|
||||
|
||||
|
||||
@JsonProperty("person_credits")
|
||||
private List<ComicvineItem> personCredits;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@JsonProperty("resource_type")
|
||||
private String resourceType;
|
||||
|
||||
|
||||
public abstract String getDisplayName();
|
||||
|
||||
public abstract String getComicId();
|
||||
|
||||
public abstract LocalDate getDate();
|
||||
|
||||
public Set<String> getAuthors() {
|
||||
Set<String> authors = new HashSet<>();
|
||||
if (personCredits != null) {
|
||||
for (ComicvineItem person : personCredits) {
|
||||
authors.add(person.name);
|
||||
}
|
||||
}
|
||||
return authors;
|
||||
}
|
||||
|
||||
public String getImageUrl() {
|
||||
if (image == null) {
|
||||
return null;
|
||||
}
|
||||
return image.getOriginalUrl() != null ? image.getOriginalUrl() : image.getThumbUrl();
|
||||
}
|
||||
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class ComicVineImage {
|
||||
@JsonProperty("icon_url")
|
||||
private String iconUrl;
|
||||
|
||||
@JsonProperty("medium_url")
|
||||
private String mediumUrl;
|
||||
|
||||
@JsonProperty("screen_url")
|
||||
private String screenUrl;
|
||||
|
||||
@JsonProperty("screen_large_url")
|
||||
private String screenLargeUrl;
|
||||
|
||||
@JsonProperty("small_url")
|
||||
private String smallUrl;
|
||||
|
||||
@JsonProperty("super_url")
|
||||
private String superUrl;
|
||||
|
||||
@JsonProperty("thumb_url")
|
||||
private String thumbUrl;
|
||||
|
||||
@JsonProperty("tiny_url")
|
||||
private String tinyUrl;
|
||||
|
||||
@JsonProperty("original_url")
|
||||
private String originalUrl;
|
||||
|
||||
@JsonProperty("image_tags")
|
||||
private String imageTags;
|
||||
}
|
||||
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class ComicvineItem {
|
||||
@JsonProperty("api_detail_url")
|
||||
private String apiDetailUrl;
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.adityachandel.booklore.model.dto.response.comicvineapi;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class ComicvineApiResponse {
|
||||
private String error;
|
||||
private int limit;
|
||||
private int offset;
|
||||
|
||||
@JsonProperty("number_of_page_results")
|
||||
private int numberOfPageResults;
|
||||
|
||||
@JsonProperty("number_of_total_results")
|
||||
private int numberOfTotalResults;
|
||||
|
||||
@JsonProperty("status_code")
|
||||
private int statusCode;
|
||||
private List<Comic> results;
|
||||
private String version;
|
||||
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package com.adityachandel.booklore.model.dto.response.comicvineapi;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class Issue extends Comic {
|
||||
@JsonProperty("cover_date")
|
||||
private String coverDate;
|
||||
|
||||
@JsonProperty("volume")
|
||||
private ComicvineItem volume;
|
||||
|
||||
@JsonProperty("issue_number")
|
||||
private int issueNumber;
|
||||
|
||||
|
||||
@Override
|
||||
public String getComicId() {
|
||||
return "4000-" + String.valueOf(getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDisplayName() {
|
||||
if(name ==null){
|
||||
if(volume != null){
|
||||
return volume.getName() + " " + "Issue #" + String.valueOf(issueNumber);
|
||||
|
||||
}
|
||||
else{
|
||||
return "Unknown Comic";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDate getDate() {
|
||||
if (coverDate == null || coverDate.isEmpty()) return null;
|
||||
try {
|
||||
return LocalDate.parse(coverDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.adityachandel.booklore.model.dto.response.comicvineapi;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class Volume extends Comic{
|
||||
@JsonProperty("api_detail_url")
|
||||
private String apiDetailUrl;
|
||||
|
||||
private String description;
|
||||
|
||||
@JsonProperty("publisher")
|
||||
private ComicvineItem publisher;
|
||||
|
||||
@JsonProperty("site_detail_url")
|
||||
private String siteDetailUrl;
|
||||
|
||||
@JsonProperty("start_year")
|
||||
private String startYear;
|
||||
|
||||
public String getPublisherName() {
|
||||
return publisher != null ? publisher.getName() : null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getDisplayName(){
|
||||
if(name==null){
|
||||
return "Unknown Comic";
|
||||
}
|
||||
else{
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getComicId() {
|
||||
return "4500-" + String.valueOf(getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDate getDate() {
|
||||
if (startYear == null || startYear.isEmpty()) return null;
|
||||
try {
|
||||
return LocalDate.parse(startYear, DateTimeFormatter.ofPattern("yyyy"));
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+6
@@ -8,6 +8,7 @@ public class MetadataProviderSettings {
|
||||
private Google google;
|
||||
private Goodreads goodReads;
|
||||
private Hardcover hardcover;
|
||||
private Comicvine comicvine;
|
||||
|
||||
@Data
|
||||
public static class Amazon {
|
||||
@@ -31,4 +32,9 @@ public class MetadataProviderSettings {
|
||||
private boolean enabled;
|
||||
private String apiKey;
|
||||
}
|
||||
@Data
|
||||
public static class Comicvine {
|
||||
private boolean enabled;
|
||||
private String apiKey;
|
||||
}
|
||||
}
|
||||
|
||||
+8
@@ -105,6 +105,9 @@ public class BookMetadataEntity {
|
||||
@Column(name = "google_id", length = 100)
|
||||
private String googleId;
|
||||
|
||||
@Column(name = "comicvine_id", length = 100)
|
||||
private String comicvineId;
|
||||
|
||||
// Locking fields
|
||||
|
||||
@Column(name = "title_locked")
|
||||
@@ -185,6 +188,9 @@ public class BookMetadataEntity {
|
||||
@Column(name = "google_id_locked")
|
||||
private Boolean googleIdLocked = Boolean.FALSE;
|
||||
|
||||
@Column(name = "comicvine_id_locked")
|
||||
private Boolean comicvineIdLocked = Boolean.FALSE;
|
||||
|
||||
@OneToOne(fetch = FetchType.LAZY)
|
||||
@MapsId
|
||||
@JoinColumn(name = "book_id")
|
||||
@@ -231,6 +237,7 @@ public class BookMetadataEntity {
|
||||
this.goodreadsReviewCountLocked = lock;
|
||||
this.hardcoverRatingLocked = lock;
|
||||
this.hardcoverReviewCountLocked = lock;
|
||||
this.comicvineIdLocked = lock;
|
||||
this.personalRatingLocked = lock;
|
||||
this.goodreadsIdLocked = lock;
|
||||
this.hardcoverIdLocked = lock;
|
||||
@@ -262,6 +269,7 @@ public class BookMetadataEntity {
|
||||
&& Boolean.TRUE.equals(this.hardcoverReviewCountLocked)
|
||||
&& Boolean.TRUE.equals(this.personalRatingLocked)
|
||||
&& Boolean.TRUE.equals(this.goodreadsIdLocked)
|
||||
&& Boolean.TRUE.equals(this.comicvineIdLocked)
|
||||
&& Boolean.TRUE.equals(this.hardcoverIdLocked)
|
||||
&& Boolean.TRUE.equals(this.googleIdLocked)
|
||||
;
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
package com.adityachandel.booklore.model.enums;
|
||||
|
||||
public enum MetadataProvider {
|
||||
Amazon, GoodReads, Google, Hardcover
|
||||
Amazon, GoodReads, Google, Hardcover, Comicvine
|
||||
}
|
||||
|
||||
+5
@@ -78,10 +78,15 @@ public class SettingPersistenceHelper {
|
||||
defaultHardcover.setEnabled(false);
|
||||
defaultHardcover.setApiKey(null);
|
||||
|
||||
MetadataProviderSettings.Comicvine defaultComicvine = new MetadataProviderSettings.Comicvine();
|
||||
defaultComicvine.setEnabled(false);
|
||||
defaultComicvine.setApiKey(null);
|
||||
|
||||
defaultMetadataProviderSettings.setAmazon(defaultAmazon);
|
||||
defaultMetadataProviderSettings.setGoogle(defaultGoogle);
|
||||
defaultMetadataProviderSettings.setGoodReads(defaultGoodreads);
|
||||
defaultMetadataProviderSettings.setHardcover(defaultHardcover);
|
||||
defaultMetadataProviderSettings.setComicvine(defaultComicvine);
|
||||
|
||||
return defaultMetadataProviderSettings;
|
||||
}
|
||||
|
||||
+1
@@ -131,6 +131,7 @@ public class EpubProcessor extends AbstractFileProcessor implements BookFileProc
|
||||
metadata.setHardcoverRating(epubMetadata.getHardcoverRating());
|
||||
metadata.setHardcoverReviewCount(epubMetadata.getHardcoverReviewCount());
|
||||
metadata.setGoogleId(truncate(epubMetadata.getGoogleId(), 100));
|
||||
metadata.setComicvineId(truncate(epubMetadata.getComicvineId(), 100));
|
||||
|
||||
bookCreatorService.addAuthorsToBook(epubMetadata.getAuthors(), bookEntity);
|
||||
|
||||
|
||||
+3
@@ -114,6 +114,9 @@ public class PdfProcessor extends AbstractFileProcessor implements BookFileProce
|
||||
if (StringUtils.isNotBlank(extracted.getGoodreadsId())) {
|
||||
bookEntity.getMetadata().setGoodreadsId(extracted.getGoodreadsId());
|
||||
}
|
||||
if (StringUtils.isNotBlank(extracted.getComicvineId())){
|
||||
bookEntity.getMetadata().setComicvineId(extracted.getComicvineId());
|
||||
}
|
||||
if (StringUtils.isNotBlank(extracted.getIsbn10())) {
|
||||
bookEntity.getMetadata().setIsbn10(extracted.getIsbn10());
|
||||
}
|
||||
|
||||
+2
@@ -138,6 +138,7 @@ public class BookMetadataUpdater {
|
||||
handleFieldUpdate(e.getIsbn10Locked(), clear.isIsbn10(), m.getIsbn10(), v -> e.setIsbn10(nullIfBlank(v)));
|
||||
handleFieldUpdate(e.getAsinLocked(), clear.isAsin(), m.getAsin(), v -> e.setAsin(nullIfBlank(v)));
|
||||
handleFieldUpdate(e.getGoodreadsIdLocked(), clear.isGoodreadsId(), m.getGoodreadsId(), v -> e.setGoodreadsId(nullIfBlank(v)));
|
||||
handleFieldUpdate(e.getComicvineIdLocked(), clear.isComicvineId(), m.getComicvineId(), v -> e.setComicvineId(nullIfBlank(v)));
|
||||
handleFieldUpdate(e.getHardcoverIdLocked(), clear.isHardcoverId(), m.getHardcoverId(), v -> e.setHardcoverId(nullIfBlank(v)));
|
||||
handleFieldUpdate(e.getGoogleIdLocked(), clear.isGoogleId(), m.getGoogleId(), v -> e.setGoogleId(nullIfBlank(v)));
|
||||
handleFieldUpdate(e.getPageCountLocked(), clear.isPageCount(), m.getPageCount(), e::setPageCount);
|
||||
@@ -227,6 +228,7 @@ public class BookMetadataUpdater {
|
||||
Pair.of(m.getIsbn10Locked(), e::setIsbn10Locked),
|
||||
Pair.of(m.getAsinLocked(), e::setAsinLocked),
|
||||
Pair.of(m.getGoodreadsIdLocked(), e::setGoodreadsIdLocked),
|
||||
Pair.of(m.getComicvineIdLocked(), e::setComicvineIdLocked),
|
||||
Pair.of(m.getHardcoverIdLocked(), e::setHardcoverIdLocked),
|
||||
Pair.of(m.getGoogleIdLocked(), e::setGoogleIdLocked),
|
||||
Pair.of(m.getPageCountLocked(), e::setPageCountLocked),
|
||||
|
||||
+5
@@ -309,6 +309,11 @@ public class MetadataRefreshService {
|
||||
if (metadataMap.containsKey(Google)) {
|
||||
metadata.setGoogleId(metadataMap.get(Google).getGoogleId());
|
||||
}
|
||||
if (metadataMap.containsKey(Comicvine)){
|
||||
metadata.setComicvineId(metadataMap.get(Comicvine).getComicvineId());
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (request.getRefreshOptions().isMergeCategories()) {
|
||||
metadata.setCategories(getAllCategories(metadataMap, fieldOptions.getCategories(), BookMetadata::getCategories));
|
||||
|
||||
+1
@@ -56,6 +56,7 @@ public class BookMetadataRestorer {
|
||||
if (!isLocked(metadata.getIsbn10Locked())) metadata.setIsbn10(backup.getIsbn10());
|
||||
if (!isLocked(metadata.getAsinLocked())) metadata.setAsin(backup.getAsin());
|
||||
if (!isLocked(metadata.getGoodreadsIdLocked())) metadata.setGoodreadsId(backup.getGoodreadsId());
|
||||
if (!isLocked(metadata.getComicvineIdLocked())) metadata.setComicvineId(backup.getComicvineId());
|
||||
if (!isLocked(metadata.getHardcoverIdLocked())) metadata.setHardcoverId(backup.getHardcoverId());
|
||||
if (!isLocked(metadata.getGoogleIdLocked())) metadata.setGoogleId(backup.getGoogleId());
|
||||
|
||||
|
||||
+2
@@ -109,6 +109,7 @@ public class EpubMetadataExtractor implements FileMetadataExtractor {
|
||||
else if (value.length() == 10) builderMeta.isbn10(value);
|
||||
}
|
||||
case "GOODREADS" -> builderMeta.goodreadsId(value);
|
||||
case "COMICVINE" -> builderMeta.comicvineId(value);
|
||||
case "GOOGLE" -> builderMeta.googleId(value);
|
||||
case "AMAZON" -> builderMeta.asin(value);
|
||||
case "HARDCOVER" -> builderMeta.hardcoverId(value);
|
||||
@@ -148,6 +149,7 @@ public class EpubMetadataExtractor implements FileMetadataExtractor {
|
||||
switch (prop) {
|
||||
case "booklore:asin" -> builderMeta.asin(content);
|
||||
case "booklore:goodreads_id" -> builderMeta.goodreadsId(content);
|
||||
case "booklore:comicvine_id" -> builderMeta.comicvineId(content);
|
||||
case "booklore:hardcover_id" -> builderMeta.hardcoverId(content);
|
||||
case "booklore:google_books_id" -> builderMeta.googleId(content);
|
||||
case "booklore:page_count" -> safeParseInt(content, builderMeta::pageCount);
|
||||
|
||||
+5
@@ -165,6 +165,11 @@ public class PdfMetadataExtractor implements FileMetadataExtractor {
|
||||
metadataBuilder.goodreadsId(goodreads);
|
||||
}
|
||||
|
||||
String comicvine = identifiers.get("comicvine");
|
||||
if (StringUtils.isNotBlank(comicvine)) {
|
||||
metadataBuilder.comicvineId(comicvine);
|
||||
}
|
||||
|
||||
String hardcover = identifiers.get("hardcover");
|
||||
if (StringUtils.isNotBlank(hardcover)) {
|
||||
metadataBuilder.hardcoverId(hardcover);
|
||||
|
||||
+156
@@ -0,0 +1,156 @@
|
||||
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.dto.response.comicvineapi.ComicvineApiResponse;
|
||||
import com.adityachandel.booklore.model.dto.response.comicvineapi.Issue;
|
||||
import com.adityachandel.booklore.model.dto.response.comicvineapi.Volume;
|
||||
import com.adityachandel.booklore.model.dto.response.comicvineapi.Comic;
|
||||
import com.adityachandel.booklore.model.enums.MetadataProvider;
|
||||
import com.adityachandel.booklore.service.appsettings.AppSettingService;
|
||||
import com.adityachandel.booklore.util.BookUtils;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collector;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ComicvineBookParser implements BookParser {
|
||||
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
private static final String COMICVINE_URL = "https://comicvine.gamespot.com/api/";
|
||||
private final AppSettingService appSettingService;
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public List<BookMetadata> fetchMetadata(Book book, FetchMetadataRequest fetchMetadataRequest) {
|
||||
String searchTerm = getSearchTerm(book, fetchMetadataRequest);
|
||||
return searchTerm != null ? getMetadataListByTerm(searchTerm) : List.of();
|
||||
}
|
||||
|
||||
public List<BookMetadata> getMetadataListByTerm(String term) {
|
||||
String apiToken = appSettingService.getAppSettings().getMetadataProviderSettings().getComicvine().getApiKey();
|
||||
if (apiToken == null || apiToken.isEmpty()) {
|
||||
log.warn("Comicvine API token not set");
|
||||
return Collections.emptyList();
|
||||
}
|
||||
log.info("Comicvine: Fetching metadata for: {}", term);
|
||||
try {
|
||||
|
||||
String fieldsList = "cover_date,id,issue_number,name,person_credits,volume,api_detail_url,concept_credits,start_year,publisher,description,image";
|
||||
String resources = "volume,issue";
|
||||
|
||||
URI uri = UriComponentsBuilder.fromUriString(COMICVINE_URL) // Base URL
|
||||
|
||||
.path("/search/")
|
||||
.queryParam("api_key", apiToken)
|
||||
.queryParam("format", "json")
|
||||
.queryParam("resources", resources)
|
||||
.queryParam("resource_type", resources)
|
||||
.queryParam("query", term)
|
||||
.queryParam("filter", "name:" + term)
|
||||
.queryParam("limit", "10") // Limit results to reduce response size
|
||||
.queryParam("field_list", fieldsList)
|
||||
.build()
|
||||
.toUri();
|
||||
|
||||
log.debug("Comicvine API request URI: {}", uri);
|
||||
HttpClient httpClient = HttpClient.newHttpClient();
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(uri)
|
||||
.header("User-Agent", "MyComicApp/1.0")
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
if (response.statusCode() == 200) {
|
||||
return parseComicvineApiResponse(response.body());
|
||||
} else {
|
||||
log.error("Failed to fetch data from Comicvine API. Status code: {}", response.statusCode());
|
||||
return List.of();
|
||||
}
|
||||
} catch (IOException | InterruptedException e) {
|
||||
log.error("Error fetching metadata from Comicvine API", e);
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public BookMetadata fetchTopMetadata(Book book, FetchMetadataRequest fetchMetadataRequest) {
|
||||
List<BookMetadata> fetchedBookMetadata = fetchMetadata(book, fetchMetadataRequest);
|
||||
return fetchedBookMetadata.isEmpty() ? null : fetchedBookMetadata.get(0);
|
||||
}
|
||||
|
||||
private String getSearchTerm(Book book, FetchMetadataRequest request) {
|
||||
return (request.getTitle() != null && !request.getTitle().isEmpty())
|
||||
? request.getTitle()
|
||||
: (book.getFileName() != null && !book.getFileName().isEmpty()
|
||||
? BookUtils.cleanFileName(book.getFileName())
|
||||
: null);
|
||||
}
|
||||
|
||||
private List<BookMetadata> parseComicvineApiResponse(String responseBody) throws IOException {
|
||||
ComicvineApiResponse apiResponse = objectMapper.readValue(responseBody, ComicvineApiResponse.class);
|
||||
return apiResponse.getResults().stream()
|
||||
.map(this::convertToBookMetadata)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private BookMetadata convertToBookMetadata(Comic comic) {
|
||||
BookMetadata.BookMetadataBuilder builder = BookMetadata.builder()
|
||||
.title(comic.getDisplayName())
|
||||
.comicvineId(String.valueOf(comic.getId()))
|
||||
.authors(comic.getAuthors())
|
||||
.thumbnailUrl(comic.getImageUrl())
|
||||
.description(comic.getDescription())
|
||||
.provider(MetadataProvider.Comicvine);
|
||||
|
||||
// Handle publishedDate based on the comic type
|
||||
if (comic instanceof Volume) {
|
||||
Volume volume = (Volume) comic;
|
||||
builder.publisher(volume.getPublisherName());
|
||||
builder.publishedDate(volume.getDate());
|
||||
} else if (comic instanceof Issue) {
|
||||
Issue issue = (Issue) comic;
|
||||
builder.seriesName(issue.getVolume().getName());
|
||||
builder.seriesNumber((float) issue.getIssueNumber());
|
||||
builder.publishedDate(issue.getDate()); // Already parsed
|
||||
}
|
||||
|
||||
// Return the final built object
|
||||
return builder.build();
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
+4
@@ -119,6 +119,7 @@ public class EpubMetadataWriter implements MetadataWriter {
|
||||
boolean clearFlag = clear != null && switch (scheme) {
|
||||
case "AMAZON" -> clear.isAsin();
|
||||
case "GOOGLE" -> clear.isGoogleId();
|
||||
case "COMICVINE" -> clear.isComicvineId();
|
||||
case "GOODREADS" -> clear.isGoodreadsId();
|
||||
case "HARDCOVER" -> clear.isHardcoverId();
|
||||
case "ISBN" -> clear.isIsbn10();
|
||||
@@ -135,6 +136,9 @@ public class EpubMetadataWriter implements MetadataWriter {
|
||||
case "GOODREADS" -> helper.copyGoodreadsId(restoreMode, clearFlag, idValue -> {
|
||||
updateIdentifier(metadataElement, opfDoc, scheme, idValue, hasChanges);
|
||||
});
|
||||
case "COMICVINE" -> helper.copyComicvineId(restoreMode, clearFlag, idValue -> {
|
||||
updateIdentifier(metadataElement, opfDoc, scheme, idValue, hasChanges);
|
||||
});
|
||||
case "HARDCOVER" -> helper.copyHardcoverId(restoreMode, clearFlag, idValue -> {
|
||||
updateIdentifier(metadataElement, opfDoc, scheme, idValue, hasChanges);
|
||||
});
|
||||
|
||||
+7
@@ -126,6 +126,13 @@ public class MetadataCopyHelper {
|
||||
}
|
||||
}
|
||||
|
||||
public void copyComicvineId(boolean restore, boolean clear, Consumer<String> consumer) {
|
||||
if (!isLocked(metadata.getComicvineIdLocked())) {
|
||||
if (clear) consumer.accept(null);
|
||||
else if (metadata.getComicvineId() != null || restore) consumer.accept(metadata.getComicvineId());
|
||||
}
|
||||
}
|
||||
|
||||
public void copyHardcoverId(boolean restore, boolean clear, Consumer<String> consumer) {
|
||||
if (!isLocked(metadata.getHardcoverIdLocked())) {
|
||||
if (clear) consumer.accept(null);
|
||||
|
||||
+1
@@ -151,6 +151,7 @@ public class PdfMetadataWriter implements MetadataWriter {
|
||||
|
||||
helper.copyGoogleId(restoreMode, clear != null && clear.isGoogleId(), id -> appendIdentifier(doc, rdfBag, "google", id != null ? id : ""));
|
||||
helper.copyGoodreadsId(restoreMode, clear != null && clear.isGoodreadsId(), id -> appendIdentifier(doc, rdfBag, "goodreads", id != null ? id : ""));
|
||||
helper.copyComicvineId(restoreMode, clear != null && clear.isComicvineId(), id -> appendIdentifier(doc, rdfBag, "comicvine", id != null ? id : ""));
|
||||
helper.copyHardcoverId(restoreMode, clear != null && clear.isHardcoverId(), id -> appendIdentifier(doc, rdfBag, "hardcover", id != null ? id : ""));
|
||||
helper.copyAsin(restoreMode, clear != null && clear.isAsin(), id -> appendIdentifier(doc, rdfBag, "amazon", id != null ? id : ""));
|
||||
helper.copyIsbn13(restoreMode, clear != null && clear.isIsbn13(), id -> appendIdentifier(doc, rdfBag, "isbn", id != null ? id : ""));
|
||||
|
||||
@@ -31,6 +31,7 @@ public class MetadataChangeDetector {
|
||||
compare(changes, "isbn10", clear.isIsbn10(), newMeta.getIsbn10(), existingMeta.getIsbn10(), () -> !isTrue(existingMeta.getIsbn10Locked()), newMeta.getIsbn10Locked(), existingMeta.getIsbn10Locked());
|
||||
compare(changes, "asin", clear.isAsin(), newMeta.getAsin(), existingMeta.getAsin(), () -> !isTrue(existingMeta.getAsinLocked()), newMeta.getAsinLocked(), existingMeta.getAsinLocked());
|
||||
compare(changes, "goodreadsId", clear.isGoodreadsId(), newMeta.getGoodreadsId(), existingMeta.getGoodreadsId(), () -> !isTrue(existingMeta.getGoodreadsIdLocked()), newMeta.getGoodreadsIdLocked(), existingMeta.getGoodreadsIdLocked());
|
||||
compare(changes, "comicvineId", clear.isComicvineId(), newMeta.getComicvineId(), existingMeta.getComicvineId(), () -> !isTrue(existingMeta.getComicvineIdLocked()), newMeta.getComicvineIdLocked(), existingMeta.getComicvineIdLocked());
|
||||
compare(changes, "hardcoverId", clear.isHardcoverId(), newMeta.getHardcoverId(), existingMeta.getHardcoverId(), () -> !isTrue(existingMeta.getHardcoverIdLocked()), newMeta.getHardcoverIdLocked(), existingMeta.getHardcoverIdLocked());
|
||||
compare(changes, "googleId", clear.isGoogleId(), newMeta.getGoogleId(), existingMeta.getGoogleId(), () -> !isTrue(existingMeta.getGoogleIdLocked()), newMeta.getGoogleIdLocked(), existingMeta.getGoogleIdLocked());
|
||||
compare(changes, "pageCount", clear.isPageCount(), newMeta.getPageCount(), existingMeta.getPageCount(), () -> !isTrue(existingMeta.getPageCountLocked()), newMeta.getPageCountLocked(), existingMeta.getPageCountLocked());
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
ALTER TABLE book_metadata
|
||||
ADD COLUMN comicvine_id VARCHAR(100),
|
||||
ADD COLUMN comicvine_id_locked BOOLEAN DEFAULT FALSE;
|
||||
Reference in New Issue
Block a user