WIP: New metadata engine
This commit is contained in:
@@ -1,13 +1,17 @@
|
||||
package com.adityachandel.booklore.controller;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.BookDTO;
|
||||
import com.adityachandel.booklore.model.dto.BookMetadataDTO;
|
||||
import com.adityachandel.booklore.model.dto.BookViewerSettingDTO;
|
||||
import com.adityachandel.booklore.model.dto.request.SetMetadataRequest;
|
||||
import com.adityachandel.booklore.model.dto.request.ShelvesAssignmentRequest;
|
||||
import com.adityachandel.booklore.model.dto.response.GoogleBooksMetadata;
|
||||
import com.adityachandel.booklore.service.BooksService;
|
||||
import com.adityachandel.booklore.service.metadata.parser.AmazonParser;
|
||||
import com.adityachandel.booklore.service.metadata.parser.model.QueryData;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.java.Log;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -21,6 +25,7 @@ import java.util.List;
|
||||
public class BookController {
|
||||
|
||||
private BooksService booksService;
|
||||
private AmazonParser amazonParser;
|
||||
|
||||
@GetMapping("/{bookId}")
|
||||
public ResponseEntity<BookDTO> getBook(@PathVariable long bookId) {
|
||||
@@ -69,6 +74,11 @@ public class BookController {
|
||||
return ResponseEntity.ok(booksService.fetchProspectiveMetadataListByBookId(bookId));
|
||||
}
|
||||
|
||||
@PostMapping("/{bookId}/query-for-books")
|
||||
public ResponseEntity<BookMetadataDTO> getBookMetadata(@RequestBody(required = false) QueryData queryData, @PathVariable Long bookId) {
|
||||
return ResponseEntity.ok(amazonParser.queryForBookMetadata(bookId, queryData));
|
||||
}
|
||||
|
||||
@GetMapping("/fetch-metadata")
|
||||
public ResponseEntity<List<GoogleBooksMetadata>> fetchMedataByTerm(@RequestParam String term) {
|
||||
return ResponseEntity.ok(booksService.fetchProspectiveMetadataListBySearchTerm(term));
|
||||
@@ -83,4 +93,10 @@ public class BookController {
|
||||
public ResponseEntity<List<BookDTO>> addBookToShelf(@RequestBody @Valid ShelvesAssignmentRequest request) {
|
||||
return ResponseEntity.ok(booksService.assignShelvesToBooks(request.getBookIds(), request.getShelvesToAssign(), request.getShelvesToUnassign()));
|
||||
}
|
||||
|
||||
@PutMapping("/{bookId}/metadata")
|
||||
public ResponseEntity<BookMetadataDTO> updateBookMetadata(@RequestBody BookMetadataDTO metadataDTO, @PathVariable long bookId) throws IOException {
|
||||
return ResponseEntity.ok(booksService.setMetadata(bookId, metadataDTO));
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -1,16 +1,20 @@
|
||||
package com.adityachandel.booklore.controller;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.BookDTO;
|
||||
import com.adityachandel.booklore.model.dto.BookMetadataDTO;
|
||||
import com.adityachandel.booklore.model.dto.BookWithNeighborsDTO;
|
||||
import com.adityachandel.booklore.model.dto.LibraryDTO;
|
||||
import com.adityachandel.booklore.model.dto.request.CreateLibraryRequest;
|
||||
import com.adityachandel.booklore.model.entity.Sort;
|
||||
import com.adityachandel.booklore.service.BooksService;
|
||||
import com.adityachandel.booklore.service.LibraryService;
|
||||
import com.adityachandel.booklore.service.metadata.parser.AmazonParser;
|
||||
import com.adityachandel.booklore.service.metadata.parser.model.QueryData;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -21,6 +25,7 @@ public class LibraryController {
|
||||
|
||||
private LibraryService libraryService;
|
||||
private BooksService booksService;
|
||||
private AmazonParser amazonParser;
|
||||
|
||||
@GetMapping("/{libraryId}")
|
||||
public ResponseEntity<LibraryDTO> getLibrary(@PathVariable long libraryId) {
|
||||
|
||||
+5
-3
@@ -1,17 +1,16 @@
|
||||
package com.adityachandel.booklore.model.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Builder
|
||||
@Data
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
@Builder
|
||||
public class BookMetadataDTO {
|
||||
private Long bookId;
|
||||
private String googleBookId;
|
||||
private String amazonBookId;
|
||||
private String title;
|
||||
private String subtitle;
|
||||
private String publisher;
|
||||
@@ -24,4 +23,7 @@ public class BookMetadataDTO {
|
||||
private String language;
|
||||
private List<AuthorDTO> authors;
|
||||
private List<CategoryDTO> categories;
|
||||
private String rating;
|
||||
private String reviewCount;
|
||||
private String printLength;
|
||||
}
|
||||
|
||||
@@ -19,9 +19,6 @@ public class BookMetadata {
|
||||
@Column(name = "book_id")
|
||||
private Long bookId;
|
||||
|
||||
@Column(name = "google_book_id", nullable = false, unique = true)
|
||||
private String googleBookId;
|
||||
|
||||
@Column(name = "title", nullable = false)
|
||||
private String title;
|
||||
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
package com.adityachandel.booklore.service;
|
||||
|
||||
import com.adityachandel.booklore.config.AppProperties;
|
||||
import com.adityachandel.booklore.model.dto.BookDTO;
|
||||
import com.adityachandel.booklore.model.dto.BookWithNeighborsDTO;
|
||||
import com.adityachandel.booklore.model.dto.response.GoogleBooksMetadata;
|
||||
import com.adityachandel.booklore.model.dto.BookViewerSettingDTO;
|
||||
import com.adityachandel.booklore.model.dto.request.SetMetadataRequest;
|
||||
import com.adityachandel.booklore.exception.ApiError;
|
||||
import com.adityachandel.booklore.model.dto.*;
|
||||
import com.adityachandel.booklore.model.dto.request.SetMetadataRequest;
|
||||
import com.adityachandel.booklore.model.dto.response.GoogleBooksMetadata;
|
||||
import com.adityachandel.booklore.model.entity.*;
|
||||
import com.adityachandel.booklore.repository.*;
|
||||
import com.adityachandel.booklore.transformer.BookMetadataTransformer;
|
||||
import com.adityachandel.booklore.transformer.BookSettingTransformer;
|
||||
import com.adityachandel.booklore.transformer.BookTransformer;
|
||||
import com.adityachandel.booklore.util.BookUtils;
|
||||
import com.adityachandel.booklore.util.DateUtils;
|
||||
import com.adityachandel.booklore.util.FileService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -23,13 +23,13 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@@ -37,7 +37,6 @@ import java.util.stream.Collectors;
|
||||
@Service
|
||||
public class BooksService {
|
||||
|
||||
private final AppProperties appProperties;
|
||||
private final BookRepository bookRepository;
|
||||
private final BookViewerSettingRepository bookViewerSettingRepository;
|
||||
private final GoogleBookMetadataService googleBookMetadataService;
|
||||
@@ -47,6 +46,7 @@ public class BooksService {
|
||||
private final LibraryRepository libraryRepository;
|
||||
private final NotificationService notificationService;
|
||||
private final ShelfRepository shelfRepository;
|
||||
private final FileService fileService;
|
||||
|
||||
|
||||
public BookDTO getBook(long bookId) {
|
||||
@@ -69,22 +69,6 @@ public class BooksService {
|
||||
bookViewerSettingRepository.save(bookViewerSetting);
|
||||
}
|
||||
|
||||
public Resource getBookCover(long bookId) {
|
||||
Book book = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
String thumbPath = appProperties.getPathConfig() + "/thumbs/" + getFileNameWithoutExtension(book.getFileName()) + ".jpg";
|
||||
Path filePath = Paths.get(thumbPath);
|
||||
try {
|
||||
Resource resource = new UrlResource(filePath.toUri());
|
||||
if (resource.exists() && resource.isReadable()) {
|
||||
return resource;
|
||||
} else {
|
||||
throw ApiError.IMAGE_NOT_FOUND.createException(thumbPath);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw ApiError.IMAGE_NOT_FOUND.createException(thumbPath);
|
||||
}
|
||||
}
|
||||
|
||||
public ResponseEntity<byte[]> getBookData(long bookId) throws IOException {
|
||||
Book book = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
byte[] pdfBytes = Files.readAllBytes(new File(book.getPath()).toPath());
|
||||
@@ -116,7 +100,7 @@ public class BooksService {
|
||||
searchString.append(book.getMetadata().getTitle());
|
||||
}
|
||||
if (searchString.isEmpty()) {
|
||||
searchString.append(cleanFileName(book.getFileName()));
|
||||
searchString.append(BookUtils.cleanFileName(book.getFileName()));
|
||||
}
|
||||
if (book.getMetadata().getAuthors() != null && !book.getMetadata().getAuthors().isEmpty()) {
|
||||
if (!searchString.isEmpty()) {
|
||||
@@ -129,23 +113,57 @@ public class BooksService {
|
||||
return googleBookMetadataService.queryByTerm(searchString.toString());
|
||||
}
|
||||
|
||||
private char[] cleanFileName(String fileName) {
|
||||
if (fileName == null) {
|
||||
return null;
|
||||
}
|
||||
String cleanedFileName = fileName.replace("(Z-Library)", "").trim();
|
||||
return cleanedFileName.toCharArray();
|
||||
}
|
||||
|
||||
public List<GoogleBooksMetadata> fetchProspectiveMetadataListBySearchTerm(String searchTerm) {
|
||||
return googleBookMetadataService.queryByTerm(searchTerm);
|
||||
}
|
||||
|
||||
public BookMetadataDTO setMetadata(long bookId, BookMetadataDTO newMetadata) throws IOException {
|
||||
Book book = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
BookMetadata metadata = book.getMetadata();
|
||||
metadata.setTitle(newMetadata.getTitle());
|
||||
metadata.setSubtitle(newMetadata.getSubtitle());
|
||||
metadata.setPublisher(newMetadata.getPublisher());
|
||||
metadata.setPublishedDate(DateUtils.parseDateToInstant(newMetadata.getPublishedDate()));
|
||||
metadata.setLanguage(newMetadata.getLanguage());
|
||||
metadata.setIsbn10(newMetadata.getIsbn10());
|
||||
metadata.setIsbn13(newMetadata.getIsbn13());
|
||||
metadata.setDescription(newMetadata.getDescription());
|
||||
metadata.setPageCount(newMetadata.getPageCount());
|
||||
if (newMetadata.getAuthors() != null && !newMetadata.getAuthors().isEmpty()) {
|
||||
List<Author> authors = newMetadata.getAuthors().stream()
|
||||
.map(authorDTO -> authorRepository.findByName(authorDTO.getName())
|
||||
.orElseGet(() -> authorRepository.save(Author.builder().name(authorDTO.getName()).build())))
|
||||
.collect(Collectors.toList());
|
||||
metadata.setAuthors(authors);
|
||||
}
|
||||
if (newMetadata.getCategories() != null && !newMetadata.getCategories().isEmpty()) {
|
||||
List<Category> categories = newMetadata
|
||||
.getCategories()
|
||||
.stream()
|
||||
.map(CategoryDTO::getName)
|
||||
.collect(Collectors.toSet())
|
||||
.stream()
|
||||
.map(categoryName -> categoryRepository.findByName(categoryName)
|
||||
.orElseGet(() -> categoryRepository.save(Category.builder().name(categoryName).build())))
|
||||
.collect(Collectors.toList());
|
||||
metadata.setCategories(categories);
|
||||
}
|
||||
|
||||
if(newMetadata.getThumbnail() != null && !newMetadata.getThumbnail().isEmpty()) {
|
||||
String thumbnailPath = fileService.createThumbnail(bookId, newMetadata.getThumbnail(), "amz");
|
||||
metadata.setThumbnail(thumbnailPath);
|
||||
}
|
||||
|
||||
authorRepository.saveAll(metadata.getAuthors());
|
||||
categoryRepository.saveAll(metadata.getCategories());
|
||||
metadataRepository.save(metadata);
|
||||
return BookMetadataTransformer.convertToBookDTO(metadata);
|
||||
}
|
||||
|
||||
public BookDTO setMetadata(SetMetadataRequest setMetadataRequest, long bookId) {
|
||||
Book book = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
GoogleBooksMetadata gMetadata = googleBookMetadataService.getByGoogleBookId(setMetadataRequest.getGoogleBookId());
|
||||
BookMetadata metadata = book.getMetadata();
|
||||
metadata.setGoogleBookId(gMetadata.getGoogleBookId());
|
||||
metadata.setDescription(gMetadata.getDescription());
|
||||
metadata.setTitle(gMetadata.getTitle());
|
||||
metadata.setLanguage(gMetadata.getLanguage());
|
||||
@@ -229,11 +247,9 @@ public class BooksService {
|
||||
List<Book> books = bookRepository.findAllById(bookIds);
|
||||
List<Shelf> shelvesToAssign = shelfRepository.findAllById(shelfIdsToAssign);
|
||||
List<Shelf> shelvesToUnassign = shelfRepository.findAllById(shelfIdsToUnassign);
|
||||
|
||||
for (Book book : books) {
|
||||
book.getShelves().removeIf(shelf -> shelfIdsToUnassign.contains(shelf.getId()));
|
||||
shelvesToUnassign.forEach(shelf -> shelf.getBooks().remove(book));
|
||||
|
||||
shelvesToAssign.forEach(shelf -> {
|
||||
if (!book.getShelves().contains(shelf)) {
|
||||
book.getShelves().add(shelf);
|
||||
@@ -242,12 +258,14 @@ public class BooksService {
|
||||
shelf.getBooks().add(book);
|
||||
}
|
||||
});
|
||||
|
||||
bookRepository.save(book);
|
||||
shelfRepository.saveAll(shelvesToAssign);
|
||||
}
|
||||
|
||||
return books.stream().map(BookTransformer::convertToBookDTO).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
public Resource getBookCover(long bookId) {
|
||||
Book book = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
return fileService.getBookCover(book.getMetadata().getThumbnail());
|
||||
}
|
||||
}
|
||||
+18
-17
@@ -7,6 +7,7 @@ import com.adityachandel.booklore.model.entity.Book;
|
||||
import com.adityachandel.booklore.model.entity.BookMetadata;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.transformer.BookTransformer;
|
||||
import com.adityachandel.booklore.util.FileService;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -69,13 +70,16 @@ public class PdfFileProcessor implements FileProcessor {
|
||||
bookCreatorService.addAuthorsToBook(authors, book);
|
||||
}
|
||||
}
|
||||
generateCoverImage(bookFile, new File(appProperties.getPathConfig() + "/thumbs"), document);
|
||||
bookCreatorService.saveConnections(book);
|
||||
Book saved = bookRepository.save(book);
|
||||
boolean success = generateCoverImage(saved.getId(), new File(appProperties.getPathConfig() + "/thumbs"), document);
|
||||
if (success) {
|
||||
bookMetadata.setThumbnail(appProperties.getPathConfig() + "/thumbs/" + book.getId() + "/f.jpg");
|
||||
}
|
||||
bookRepository.flush();
|
||||
} catch (Exception e) {
|
||||
log.error("Error while processing file {}, error: {}", libraryFile.getFilePath(), e.getMessage());
|
||||
}
|
||||
bookCreatorService.saveConnections(book);
|
||||
bookRepository.save(book);
|
||||
bookRepository.flush();
|
||||
return BookTransformer.convertToBookDTO(book);
|
||||
}
|
||||
|
||||
@@ -92,13 +96,19 @@ public class PdfFileProcessor implements FileProcessor {
|
||||
return authorNames.stream().map(String::trim).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private void generateCoverImage(File bookFile, File coverDirectory, PDDocument document) throws IOException {
|
||||
private boolean generateCoverImage(Long bookId, File coverDirectory, PDDocument document) throws IOException {
|
||||
PDFRenderer renderer = new PDFRenderer(document);
|
||||
BufferedImage coverImage = renderer.renderImageWithDPI(0, 300, ImageType.RGB);
|
||||
BufferedImage resizedImage = resizeImage(coverImage, 250, 350);
|
||||
String coverImageName = getFileNameWithoutExtension(bookFile.getName()) + ".jpg";
|
||||
File coverImageFile = new File(coverDirectory, coverImageName);
|
||||
ImageIO.write(resizedImage, "JPEG", coverImageFile);
|
||||
File bookDirectory = new File(coverDirectory, bookId.toString());
|
||||
if (!bookDirectory.exists()) {
|
||||
if (!bookDirectory.mkdirs()) {
|
||||
throw new IOException("Failed to create directory: " + bookDirectory.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
String coverImageName = "f.jpg";
|
||||
File coverImageFile = new File(bookDirectory, coverImageName);
|
||||
return ImageIO.write(resizedImage, "JPEG", coverImageFile);
|
||||
}
|
||||
|
||||
public static BufferedImage resizeImage(BufferedImage originalImage, int width, int height) {
|
||||
@@ -109,13 +119,4 @@ public class PdfFileProcessor implements FileProcessor {
|
||||
g2d.dispose();
|
||||
return resizedImage;
|
||||
}
|
||||
|
||||
public static String getFileNameWithoutExtension(String fileName) {
|
||||
int dotIndex = fileName.lastIndexOf('.');
|
||||
if (dotIndex == -1) {
|
||||
return fileName;
|
||||
} else {
|
||||
return fileName.substring(0, dotIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+358
@@ -0,0 +1,358 @@
|
||||
package com.adityachandel.booklore.service.metadata.parser;
|
||||
|
||||
import com.adityachandel.booklore.exception.ApiError;
|
||||
import com.adityachandel.booklore.model.dto.AuthorDTO;
|
||||
import com.adityachandel.booklore.model.dto.BookMetadataDTO;
|
||||
import com.adityachandel.booklore.model.dto.CategoryDTO;
|
||||
import com.adityachandel.booklore.model.entity.Book;
|
||||
import com.adityachandel.booklore.model.entity.Library;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.repository.LibraryRepository;
|
||||
import com.adityachandel.booklore.service.metadata.parser.model.QueryData;
|
||||
import com.adityachandel.booklore.util.BookUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jsoup.Connection;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.nodes.Element;
|
||||
import org.jsoup.select.Elements;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class AmazonParser {
|
||||
|
||||
private BookRepository bookRepository;
|
||||
|
||||
public BookMetadataDTO queryForBookMetadata(Long bookId, QueryData queryData) {
|
||||
Book book = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
if (queryData == null || (queryData.getBookTitle() == null && queryData.getAuthor() == null && queryData.getIsbn() == null)) {
|
||||
String title = book.getMetadata().getTitle();
|
||||
if (title == null || title.isEmpty()) {
|
||||
String cleanFileName = BookUtils.cleanFileName(book.getFileName());
|
||||
queryData = QueryData.builder().bookTitle(cleanFileName).build();
|
||||
} else {
|
||||
queryData = QueryData.builder().bookTitle(title).build();
|
||||
}
|
||||
}
|
||||
String amazonBookId = getAmazonBookId(queryData);
|
||||
if (amazonBookId == null) {
|
||||
return null;
|
||||
}
|
||||
return getBookMetadata(amazonBookId);
|
||||
}
|
||||
|
||||
public String getAmazonBookId(QueryData queryData) {
|
||||
String queryUrl = buildQueryUrl(queryData);
|
||||
if (queryUrl == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
Document doc = fetchDoc(queryUrl);
|
||||
Element searchResults = doc.select("span[data-component-type=s-search-results]").first();
|
||||
Element item = searchResults.select("div[role=listitem][data-index=2]").first();
|
||||
return item.attr("data-asin");
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to get asin: {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public BookMetadataDTO getBookMetadata(String amazonBookId) {
|
||||
|
||||
Document doc = fetchDoc("https://www.amazon.com/dp/" + amazonBookId);
|
||||
|
||||
return BookMetadataDTO.builder()
|
||||
.amazonBookId(amazonBookId)
|
||||
.title(getTitle(doc))
|
||||
.subtitle(getSubtitle(doc))
|
||||
.authors(getAuthors(doc).stream()
|
||||
.map(name -> AuthorDTO.builder().name(name).build())
|
||||
.collect(Collectors.toList()))
|
||||
.categories(getBestSellerCategories(doc).stream()
|
||||
.map(category -> CategoryDTO.builder().name(category).build())
|
||||
.collect(Collectors.toList()))
|
||||
.description(getDescription(doc))
|
||||
.isbn13(getIsbn13(doc))
|
||||
.isbn10(getIsbn10(doc))
|
||||
.publisher(getPublisher(doc))
|
||||
.publishedDate(getPublicationDate(doc))
|
||||
.language(getLanguage(doc))
|
||||
.pageCount(getPageCount(doc))
|
||||
.thumbnail(getThumbnail(doc))
|
||||
.rating(getRating(doc))
|
||||
.reviewCount(getReviewCount(doc))
|
||||
.printLength(getPrintLength(doc))
|
||||
.build();
|
||||
}
|
||||
|
||||
private String buildQueryUrl(QueryData queryData) {
|
||||
StringBuilder queryBuilder = new StringBuilder("https://www.amazon.com/s/?search-alias=stripbooks&unfiltered=1&sort=relevanceexprank");
|
||||
|
||||
if (queryData.getIsbn() != null && !queryData.getIsbn().isEmpty()) {
|
||||
queryBuilder.append("&field-isbn=").append(queryData.getIsbn());
|
||||
}
|
||||
|
||||
if (queryData.getBookTitle() != null && !queryData.getBookTitle().isEmpty()) {
|
||||
queryBuilder.append("&field-title=").append(queryData.getBookTitle().replace(" ", "%20"));
|
||||
}
|
||||
|
||||
if (queryData.getAuthor() != null && !queryData.getAuthor().isEmpty()) {
|
||||
queryBuilder.append("&field-author=").append(queryData.getAuthor().replace(" ", "%20"));
|
||||
}
|
||||
|
||||
if (queryData.getIsbn() == null && queryData.getBookTitle() == null && queryData.getAuthor() != null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return queryBuilder.toString();
|
||||
}
|
||||
|
||||
private String getTitle(Document doc) {
|
||||
Element titleElement = doc.getElementById("productTitle");
|
||||
if (titleElement != null) {
|
||||
return titleElement.text();
|
||||
}
|
||||
log.error("Error fetching title: Element not found.");
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getSubtitle(Document doc) {
|
||||
Element subtitleElement = doc.getElementById("productSubtitle");
|
||||
if (subtitleElement != null) {
|
||||
return subtitleElement.text();
|
||||
}
|
||||
log.error("Error fetching subtitle: Element not found.");
|
||||
return null;
|
||||
}
|
||||
|
||||
private Set<String> getAuthors(Document doc) {
|
||||
try {
|
||||
Element bylineDiv = doc.select("#bylineInfo_feature_div").first();
|
||||
if (bylineDiv != null) {
|
||||
return bylineDiv
|
||||
.select(".author a")
|
||||
.stream()
|
||||
.map(Element::text)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
log.error("Error fetching authors: Byline element not found.");
|
||||
} catch (Exception e) {
|
||||
log.error("Error fetching authors: {}", e.getMessage());
|
||||
}
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
private String getDescription(Document doc) {
|
||||
try {
|
||||
Elements descriptionElements = doc.select("[data-a-expander-name=book_description_expander] .a-expander-content");
|
||||
if (!descriptionElements.isEmpty()) {
|
||||
String html = descriptionElements.getFirst().html();
|
||||
html = html.replace("\n", "<br>");
|
||||
return html;
|
||||
}
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
log.error("Error extracting description from the document", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getIsbn10(Document doc) {
|
||||
try {
|
||||
Element isbn10Element = doc.select("#rpi-attribute-book_details-isbn10 .rpi-attribute-value span").first();
|
||||
if (isbn10Element != null) {
|
||||
return isbn10Element.text();
|
||||
}
|
||||
log.error("Error fetching ISBN-10: Element not found.");
|
||||
} catch (Exception e) {
|
||||
log.error("Error fetching ISBN-10: {}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getIsbn13(Document doc) {
|
||||
try {
|
||||
Element isbn13Element = doc.select("#rpi-attribute-book_details-isbn13 .rpi-attribute-value span").first();
|
||||
if (isbn13Element != null) {
|
||||
return isbn13Element.text();
|
||||
}
|
||||
log.error("Error fetching ISBN-13: Element not found.");
|
||||
} catch (Exception e) {
|
||||
log.error("Error fetching ISBN-13: {}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getPublisher(Document doc) {
|
||||
try {
|
||||
Element publisherElement = doc.select("#rpi-attribute-book_details-publisher .rpi-attribute-value span").first();
|
||||
if (publisherElement != null) {
|
||||
return publisherElement.text();
|
||||
}
|
||||
log.error("Error fetching publisher: Element not found.");
|
||||
} catch (Exception e) {
|
||||
log.error("Error fetching publisher: {}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getPublicationDate(Document doc) {
|
||||
try {
|
||||
Element publicationDateElement = doc.select("#rpi-attribute-book_details-publication_date .rpi-attribute-value span").first();
|
||||
if (publicationDateElement != null) {
|
||||
return publicationDateElement.text();
|
||||
}
|
||||
log.error("Error fetching publication date: Element not found.");
|
||||
} catch (Exception e) {
|
||||
log.error("Error fetching publication date: {}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getLanguage(Document doc) {
|
||||
try {
|
||||
Element languageElement = doc.select("#rpi-attribute-language .rpi-attribute-value span").first();
|
||||
if (languageElement != null) {
|
||||
return languageElement.text();
|
||||
}
|
||||
log.error("Error fetching language: Element not found.");
|
||||
} catch (Exception e) {
|
||||
log.error("Error fetching language: {}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getPrintLength(Document doc) {
|
||||
try {
|
||||
Element printLengthElement = doc.select("#rpi-attribute-book_details-fiona_pages .rpi-attribute-value span").first();
|
||||
if (printLengthElement != null) {
|
||||
return printLengthElement.text();
|
||||
}
|
||||
log.error("Error fetching print length: Element not found.");
|
||||
} catch (Exception e) {
|
||||
log.error("Error fetching print length: {}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Set<String> getBestSellerCategories(Document doc) {
|
||||
try {
|
||||
Element bestSellerCategoriesElement = doc.select("#detailBullets_feature_div").first();
|
||||
if (bestSellerCategoriesElement != null) {
|
||||
return bestSellerCategoriesElement
|
||||
.select(".zg_hrsr .a-list-item a")
|
||||
.stream()
|
||||
.map(Element::text)
|
||||
.map(c -> c.replace("(Books)", "").trim())
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
log.error("Error fetching best seller categories: Element not found.");
|
||||
} catch (Exception e) {
|
||||
log.error("Error fetching best seller categories: {}", e.getMessage());
|
||||
}
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
private String getRating(Document doc) {
|
||||
try {
|
||||
Element reviewDiv = doc.select("div#averageCustomerReviews_feature_div").first();
|
||||
if (reviewDiv != null) {
|
||||
Elements ratingElements = reviewDiv.select("span#acrPopover span.a-size-base.a-color-base");
|
||||
if (!ratingElements.isEmpty()) {
|
||||
return Objects.requireNonNull(ratingElements.first()).text();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Error fetching rating", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getReviewCount(Document doc) {
|
||||
try {
|
||||
Element reviewDiv = doc.select("div#averageCustomerReviews_feature_div").first();
|
||||
if (reviewDiv != null) {
|
||||
Element reviewCountElement = reviewDiv.getElementById("acrCustomerReviewText");
|
||||
if (reviewCountElement != null) {
|
||||
return Objects.requireNonNull(reviewCountElement).text().split(" ")[0];
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Error fetching review count", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getThumbnail(Document doc) {
|
||||
try {
|
||||
Element imageElement = doc.select("#landingImage").first();
|
||||
if (imageElement != null) {
|
||||
return imageElement.attr("src");
|
||||
}
|
||||
log.error("Error fetching image URL: Image element not found.");
|
||||
} catch (Exception e) {
|
||||
log.error("Error fetching image URL: {}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Integer getPageCount(Document doc) {
|
||||
Elements pageCountElements = doc.select("#rpi-attribute-book_details-fiona_pages .rpi-attribute-value span");
|
||||
if (!pageCountElements.isEmpty()) {
|
||||
String pageCountText = pageCountElements.first().text();
|
||||
if (!pageCountText.isEmpty()) {
|
||||
try {
|
||||
String cleanedPageCount = pageCountText.replaceAll("[^\\d]", "");
|
||||
return Integer.parseInt(cleanedPageCount);
|
||||
} catch (NumberFormatException e) {
|
||||
log.error("Error parsing page count: {}", pageCountText, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Document fetchDoc(String url) {
|
||||
try {
|
||||
Connection.Response response = Jsoup.connect(url)
|
||||
.header("accept", "text/html, application/json")
|
||||
.header("accept-language", "en-US,en;q=0.9")
|
||||
.header("content-type", "application/json")
|
||||
.header("device-memory", "8")
|
||||
.header("downlink", "10")
|
||||
.header("dpr", "2")
|
||||
.header("ect", "4g")
|
||||
.header("origin", "https://www.amazon.com")
|
||||
.header("priority", "u=1, i")
|
||||
.header("rtt", "50")
|
||||
.header("sec-ch-device-memory", "8")
|
||||
.header("sec-ch-dpr", "2")
|
||||
.header("sec-ch-ua", "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"")
|
||||
.header("sec-ch-ua-mobile", "?0")
|
||||
.header("sec-ch-ua-platform", "\"macOS\"")
|
||||
.header("sec-ch-viewport-width", "1170")
|
||||
.header("sec-fetch-dest", "empty")
|
||||
.header("sec-fetch-mode", "cors")
|
||||
.header("sec-fetch-site", "same-origin")
|
||||
.header("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36")
|
||||
.header("viewport-width", "1170")
|
||||
.header("x-amz-amabot-click-attributes", "disable")
|
||||
.header("x-requested-with", "XMLHttpRequest")
|
||||
.method(Connection.Method.GET)
|
||||
.execute();
|
||||
return response.parse();
|
||||
} catch (IOException e) {
|
||||
log.error("Error parsing url: {}", url, e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.adityachandel.booklore.service.metadata.parser.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Builder
|
||||
@Data
|
||||
public class QueryData {
|
||||
private String isbn;
|
||||
private String bookTitle;
|
||||
private String author;
|
||||
}
|
||||
+8
-2
@@ -1,8 +1,16 @@
|
||||
package com.adityachandel.booklore.transformer;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.BookMetadataDTO;
|
||||
import com.adityachandel.booklore.model.entity.Author;
|
||||
import com.adityachandel.booklore.model.entity.BookMetadata;
|
||||
import com.adityachandel.booklore.model.entity.Category;
|
||||
import com.adityachandel.booklore.util.FileService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.hibernate.annotations.Comment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class BookMetadataTransformer {
|
||||
@@ -10,7 +18,6 @@ public class BookMetadataTransformer {
|
||||
public static BookMetadataDTO convertToBookDTO(BookMetadata bookMetadata) {
|
||||
return BookMetadataDTO.builder()
|
||||
.bookId(bookMetadata.getBookId())
|
||||
.googleBookId(bookMetadata.getGoogleBookId())
|
||||
.title(bookMetadata.getTitle())
|
||||
.description(bookMetadata.getDescription())
|
||||
.isbn10(bookMetadata.getIsbn10())
|
||||
@@ -18,7 +25,6 @@ public class BookMetadataTransformer {
|
||||
.publisher(bookMetadata.getPublisher())
|
||||
.subtitle(bookMetadata.getSubtitle())
|
||||
.language(bookMetadata.getLanguage())
|
||||
.thumbnail(bookMetadata.getThumbnail())
|
||||
.pageCount(bookMetadata.getPageCount())
|
||||
.publishedDate(bookMetadata.getPublishedDate())
|
||||
.authors(bookMetadata.getAuthors() == null ? null : bookMetadata.getAuthors().stream().map(AuthorTransformer::toAuthorDTO).collect(Collectors.toList()))
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.adityachandel.booklore.util;
|
||||
|
||||
public class BookUtils {
|
||||
|
||||
public static String cleanFileName(String fileName) {
|
||||
if (fileName == null) {
|
||||
return null;
|
||||
}
|
||||
fileName = fileName.replace("(Z-Library)", "").trim();
|
||||
|
||||
// Remove the author name inside parentheses (e.g. (Jon Yablonski))
|
||||
fileName = fileName.replaceAll("\\s?\\(.*?\\)", "").trim();
|
||||
|
||||
// Remove the file extension (e.g., .pdf, .docx)
|
||||
int dotIndex = fileName.lastIndexOf('.');
|
||||
if (dotIndex > 0) {
|
||||
fileName = fileName.substring(0, dotIndex).trim();
|
||||
}
|
||||
|
||||
return fileName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.adityachandel.booklore.util;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.Locale;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class DateUtils {
|
||||
|
||||
private static final String DATE_FORMAT_REGEX = "^[A-Za-z]+\\s[0-9]{1,2},\\s[0-9]{4}$";
|
||||
|
||||
public static String parseDateToInstant(String dateString) {
|
||||
if (dateString == null || dateString.trim().isEmpty() || !Pattern.matches(DATE_FORMAT_REGEX, dateString)) {
|
||||
return dateString;
|
||||
}
|
||||
try {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH);
|
||||
LocalDate localDate = LocalDate.parse(dateString, formatter);
|
||||
Instant instant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant();
|
||||
LocalDate finalDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();
|
||||
return finalDate.toString();
|
||||
} catch (DateTimeParseException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package com.adityachandel.booklore.util;
|
||||
|
||||
import com.adityachandel.booklore.config.AppProperties;
|
||||
import com.adityachandel.booklore.exception.ApiError;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class FileService {
|
||||
|
||||
private final AppProperties appProperties;
|
||||
|
||||
public Resource getBookCover(String thumbnailPath) {
|
||||
Path filePath = Paths.get(thumbnailPath);
|
||||
try {
|
||||
Resource resource = new UrlResource(filePath.toUri());
|
||||
if (resource.exists() && resource.isReadable()) {
|
||||
return resource;
|
||||
} else {
|
||||
throw ApiError.IMAGE_NOT_FOUND.createException(filePath);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw ApiError.IMAGE_NOT_FOUND.createException(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
/*private String getCurrentThumbnail(String bookThumbFolder) {
|
||||
File directory = new File(bookThumbFolder);
|
||||
if (!directory.isDirectory()) {
|
||||
log.error("Invalid directory path: {}", bookThumbFolder);
|
||||
return null;
|
||||
}
|
||||
File[] files = directory.listFiles((dir, name) -> name.endsWith("-current.jpg"));
|
||||
if (files == null || files.length == 0) {
|
||||
log.info("No file ending with '-current.jpg' found in directory: {}", bookThumbFolder);
|
||||
return null;
|
||||
}
|
||||
return files[0].getPath();
|
||||
}*/
|
||||
|
||||
public String createThumbnail(long bookId, String thumbnailUrl, String suffix) throws IOException {
|
||||
String newFilename = suffix + ".jpg";
|
||||
resizeAndSaveImage(thumbnailUrl, new File(getThumbnailPath(bookId)), newFilename);
|
||||
return getThumbnailPath(bookId) + newFilename;
|
||||
//removeOldCurrent(bookId, prefix);
|
||||
}
|
||||
|
||||
/*public void removeOldCurrent(long bookId, String except) {
|
||||
String thumbnailPath = getThumbnailPath(bookId);
|
||||
File directory = new File(thumbnailPath);
|
||||
if (!directory.isDirectory()) {
|
||||
log.error("Invalid directory path: {}", thumbnailPath);
|
||||
return;
|
||||
}
|
||||
File[] files = directory.listFiles((dir, name) ->
|
||||
name.endsWith(".jpg") && !name.equals(except + "-current.jpg") && name.contains("-current"));
|
||||
if (files == null || files.length == 0) {
|
||||
log.info("No matching files to process in directory: {}", thumbnailPath);
|
||||
return;
|
||||
}
|
||||
for (File file : files) {
|
||||
File newFile = new File(file.getParent(), file.getName().replace("-current", ""));
|
||||
if (file.renameTo(newFile)) {
|
||||
log.info("Renamed: {} -> {}", file.getName(), newFile.getName());
|
||||
} else {
|
||||
log.error("Failed to rename: {}", file.getName());
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
private void resizeAndSaveImage(String imageUrl, File outputFolder, String outputFileName) throws IOException {
|
||||
BufferedImage originalImage;
|
||||
try (InputStream inputStream = new URL(imageUrl).openStream()) {
|
||||
originalImage = ImageIO.read(inputStream);
|
||||
}
|
||||
if (originalImage == null) {
|
||||
throw new IOException("Failed to read image from URL: " + imageUrl);
|
||||
}
|
||||
BufferedImage resizedImage = resizeImage(originalImage);
|
||||
if (!outputFolder.exists() && !outputFolder.mkdirs()) {
|
||||
throw new IOException("Failed to create output directory: " + outputFolder.getAbsolutePath());
|
||||
}
|
||||
File outputFile = new File(outputFolder, outputFileName);
|
||||
ImageIO.write(resizedImage, "JPEG", outputFile);
|
||||
log.info("Image saved to: {}", outputFile.getAbsolutePath());
|
||||
}
|
||||
|
||||
private BufferedImage resizeImage(BufferedImage originalImage) {
|
||||
BufferedImage resizedImage = new BufferedImage(250, 350, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D g2d = resizedImage.createGraphics();
|
||||
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||||
g2d.drawImage(originalImage, 0, 0, 250, 350, null);
|
||||
g2d.dispose();
|
||||
return resizedImage;
|
||||
}
|
||||
|
||||
public String getThumbnailPath(long bookId) {
|
||||
return appProperties.getPathConfig() + "/thumbs/" + bookId + "/";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,14 +24,13 @@ CREATE TABLE IF NOT EXISTS book
|
||||
CREATE TABLE IF NOT EXISTS book_metadata
|
||||
(
|
||||
book_id BIGINT NOT NULL PRIMARY KEY,
|
||||
google_book_id VARCHAR(255) UNIQUE,
|
||||
title VARCHAR(255),
|
||||
subtitle VARCHAR(255),
|
||||
publisher VARCHAR(255),
|
||||
published_date DATE,
|
||||
description TEXT,
|
||||
isbn_13 VARCHAR(13),
|
||||
isbn_10 VARCHAR(10),
|
||||
isbn_13 VARCHAR(20),
|
||||
isbn_10 VARCHAR(20),
|
||||
page_count INT,
|
||||
thumbnail VARCHAR(1000),
|
||||
language VARCHAR(10),
|
||||
|
||||
Reference in New Issue
Block a user