[OPDS] Add support for searching books in the catalog
Add new endpoint `/api/v1/opds/search.opds` to respond with the OpenSearchDescription Add new endpoint `/api/v1/opds/search?q=` endpoint to support search Match one of title, subtitle, seriesName, description or author name of the book metadata with the provided searchTerms References: OPDS Search specification: https://specs.opds.io/opds-1.2.html#3-search Sample OPDS Search description from gutenberg project: https://www.gutenberg.org/catalog/osd-books.xml
This commit is contained in:
committed by
Aditya Chandel
parent
1cf0a5e292
commit
c7d16454a0
+17
-1
@@ -28,6 +28,22 @@ public class OpdsController {
|
||||
.contentType(MediaType.parseMediaType("application/atom+xml;profile=opds-catalog"))
|
||||
.body(feed);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/search", produces = "application/atom+xml;profile=opds-catalog")
|
||||
public ResponseEntity<String> search(HttpServletRequest request) {
|
||||
String feed = opdsService.generateSearchResults(request, request.getParameter("q"));
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.parseMediaType("application/atom+xml;profile=opds-catalog"))
|
||||
.body(feed);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/search.opds", produces = "application/atom+xml;profile=opds-catalog")
|
||||
public ResponseEntity<String> searchDescription(HttpServletRequest request) {
|
||||
String feed = opdsService.generateSearchDescription(request);
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.parseMediaType("application/atom+xml;profile=opds-catalog"))
|
||||
.body(feed);
|
||||
}
|
||||
|
||||
@GetMapping("/{bookId}/download")
|
||||
public ResponseEntity<Resource> downloadBook(@PathVariable("bookId") Long bookId) {
|
||||
@@ -43,4 +59,4 @@ public class OpdsController {
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + coverImage.getFilename() + "\"")
|
||||
.body(coverImage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,5 +56,20 @@ public interface BookRepository extends JpaRepository<BookEntity, Long>, JpaSpec
|
||||
LEFT JOIN FETCH b.shelves
|
||||
""")
|
||||
List<BookEntity> findAllFullBooks();
|
||||
|
||||
@Query("""
|
||||
SELECT DISTINCT b FROM BookEntity b
|
||||
LEFT JOIN FETCH b.metadata m
|
||||
LEFT JOIN FETCH m.authors a
|
||||
LEFT JOIN FETCH m.categories
|
||||
LEFT JOIN FETCH b.shelves
|
||||
WHERE (b.metadata.title like %:text%)
|
||||
OR (b.metadata.subtitle like %:text%)
|
||||
OR (b.metadata.description like %:text%)
|
||||
OR (b.metadata.seriesName like %:text%)
|
||||
OR (a.name like %:text%)
|
||||
""")
|
||||
List<BookEntity> findBooksContainingMetadata(@Param("text") String text);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -51,6 +51,10 @@ public class BookQueryService {
|
||||
public List<BookEntity> getAllFullBookEntities() {
|
||||
return bookRepository.findAllFullBooks();
|
||||
}
|
||||
|
||||
public List<BookEntity> getBooksContainingMetadata(String text) {
|
||||
return bookRepository.findBooksContainingMetadata(text);
|
||||
}
|
||||
|
||||
public void saveAll(List<BookEntity> books) {
|
||||
bookRepository.saveAll(books);
|
||||
|
||||
+46
-1
@@ -27,11 +27,49 @@ public class OpdsService {
|
||||
};
|
||||
}
|
||||
|
||||
public String generateSearchResults(HttpServletRequest request, String queryParam) {
|
||||
List<BookEntity> books = List.of();
|
||||
if (queryParam != null) {
|
||||
books = bookQueryService.getBooksContainingMetadata(queryParam);
|
||||
} else {
|
||||
books = bookQueryService.getAllFullBookEntities();
|
||||
}
|
||||
var feedVersion = extractVersionFromAcceptHeader(request);
|
||||
|
||||
return switch (feedVersion) {
|
||||
case "2.0" -> generateOpdsV2Feed(books);
|
||||
default -> generateOpdsV1Feed(books);
|
||||
};
|
||||
}
|
||||
|
||||
public String generateSearchDescription(HttpServletRequest request) {
|
||||
var feedVersion = extractVersionFromAcceptHeader(request);
|
||||
|
||||
return switch (feedVersion) {
|
||||
case "2.0" -> generateOpdsV2SearchDescription();
|
||||
default -> generateOpdsV1SearchDescription();
|
||||
};
|
||||
}
|
||||
|
||||
private String extractVersionFromAcceptHeader(HttpServletRequest request) {
|
||||
var acceptHeader = request.getHeader("Accept");
|
||||
return (acceptHeader != null && acceptHeader.contains("version=2.0")) ? "2.0" : "1.2";
|
||||
}
|
||||
|
||||
private String generateOpdsV1SearchDescription() {
|
||||
return """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
|
||||
<LongName>Booklore catalog</LongName>
|
||||
<Description>Search the Booklore ebook catalog.</Description>
|
||||
<Url type="application/atom+xml;profile=opds-catalog" template="/api/v1/opds/search?q={searchTerms}"/>
|
||||
<Language>en-us</Language>
|
||||
<OutputEncoding>UTF-8</OutputEncoding>
|
||||
<InputEncoding>UTF-8</InputEncoding>
|
||||
</OpenSearchDescription>
|
||||
""";
|
||||
}
|
||||
|
||||
private String generateOpdsV1Feed(List<BookEntity> books) {
|
||||
var feed = new StringBuilder("""
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
@@ -39,6 +77,7 @@ public class OpdsService {
|
||||
<title>Booklore Catalog</title>
|
||||
<id>urn:booklore:catalog</id>
|
||||
<updated>%s</updated>
|
||||
<link rel="search" type="application/opensearchdescription+xml" title="Booklore Search" href="/api/v1/opds/search.opds"/>
|
||||
""".formatted(now()));
|
||||
|
||||
books.forEach(book -> appendBookEntryV1(feed, book));
|
||||
@@ -109,6 +148,12 @@ public class OpdsService {
|
||||
// Placeholder for OPDS v2.0 feed implementation (similar structure as v1)
|
||||
return "OPDS v2.0 Feed is under construction";
|
||||
}
|
||||
|
||||
private String generateOpdsV2SearchDescription() {
|
||||
// Placeholder for OPDS v2.0 feed implementation (similar structure as v1)
|
||||
return "OPDS v2.0 Feed is under construction";
|
||||
}
|
||||
|
||||
|
||||
private String now() {
|
||||
return DateTimeFormatter.ISO_INSTANT.format(java.time.Instant.now());
|
||||
@@ -130,4 +175,4 @@ public class OpdsService {
|
||||
.replace("\"", """)
|
||||
.replace("'", "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user