Skip to content

Commit 13bb3c0

Browse files
roberteinsleclaude
andcommitted
Fix ISBN detection and language filter bugs
**ISBN Detection Fix:** - Fix regex pattern to correctly detect 10 and 13 digit ISBNs - Remove hyphens and spaces before checking ISBN format - Now correctly identifies: 3551354030, 978-3-551-35403-0, etc. - Support manual prefixes: isbn:, intitle:, inauthor: (Google) - Support manual prefixes: isbn:, author:, title: (OpenLibrary) **Language Filter Fix:** - Fix Google Books language filter (was concatenating to query string) - Now correctly passes langRestrict as separate parameter - German books (de) will no longer show Dutch (nl) results **Pattern Improvements:** - Simplified author name detection (2+ words, capitalized) - More robust ISBN cleaning (removes hyphens AND spaces) - Consistent behavior across both search providers Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 73c3da2 commit 13bb3c0

2 files changed

Lines changed: 31 additions & 17 deletions

File tree

app/Services/GoogleBooksService.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@ public function search(string $query, int $maxResults = 20, string $language = n
2323
// Detect query type and build appropriate search query
2424
$searchQuery = $this->buildSearchQuery($query);
2525

26+
$params = [
27+
'q' => $searchQuery,
28+
'key' => $this->apiKey,
29+
'maxResults' => $maxResults,
30+
'orderBy' => 'relevance',
31+
];
32+
2633
// Add language filter if specified
2734
if ($language) {
28-
$searchQuery .= '&langRestrict=' . $language;
35+
$params['langRestrict'] = $language;
2936
}
3037

3138
try {
32-
$response = Http::get("{$this->baseUrl}/volumes", [
33-
'q' => $searchQuery,
34-
'key' => $this->apiKey,
35-
'maxResults' => $maxResults,
36-
'orderBy' => 'relevance',
37-
]);
39+
$response = Http::get("{$this->baseUrl}/volumes", $params);
3840

3941
if ($response->successful()) {
4042
return $this->formatResults($response->json());
@@ -89,13 +91,21 @@ protected function buildSearchQuery(string $query): string
8991
{
9092
$query = trim($query);
9193

92-
// ISBN-10 or ISBN-13 (with or without hyphens)
93-
if (preg_match('/^(\d{10}|\d{13}|\d{1,5}-\d{1,7}-\d{1,7}-[\dX])$/i', str_replace('-', '', $query))) {
94-
return 'isbn:' . str_replace('-', '', $query);
94+
// Remove any existing prefix if user added it manually
95+
if (preg_match('/^(isbn|intitle|inauthor):/i', $query)) {
96+
return $query; // Already formatted, return as-is
97+
}
98+
99+
// Clean query for ISBN check (remove hyphens and spaces)
100+
$cleanQuery = str_replace(['-', ' '], '', $query);
101+
102+
// ISBN-10 (10 digits) or ISBN-13 (13 digits)
103+
if (preg_match('/^\d{10}$|^\d{13}$/', $cleanQuery)) {
104+
return 'isbn:' . $cleanQuery;
95105
}
96106

97-
// Check if query looks like an author name (2+ words with capitalization)
98-
if (preg_match('/^[A-Z][a-z]+\s+[A-Z][a-z]+/', $query)) {
107+
// Check if query looks like an author name (2+ words, first letters capitalized)
108+
if (preg_match('/^[A-Z][a-z]+\s+[A-Z]/', $query)) {
99109
return 'inauthor:' . $query;
100110
}
101111

app/Services/OpenLibraryService.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,18 +185,22 @@ protected function buildSearchQuery(string $query): string
185185
$query = trim($query);
186186

187187
// Remove 'isbn:', 'author:', 'title:' prefixes if user added them manually
188-
if (preg_match('/^(isbn|author|title):\s*(.+)$/i', $query, $matches)) {
188+
}
189+
// Remove 'isbn:', 'author:', 'title:' prefixes if user added them manually
190+
if (preg_match('/^(isbn|author|title):/i', $query)) {
189191
return $query; // Already formatted, return as-is
190192
}
191193

192-
// ISBN-10 or ISBN-13 (with or without hyphens)
193-
$cleanQuery = str_replace('-', '', $query);
194+
// Clean query for ISBN check (remove hyphens and spaces)
195+
$cleanQuery = str_replace(['-', ' '], '', $query);
196+
197+
// ISBN-10 (10 digits) or ISBN-13 (13 digits)
194198
if (preg_match('/^\d{10}$|^\d{13}$/', $cleanQuery)) {
195199
return 'isbn:' . $cleanQuery;
196200
}
197201

198-
// Check if query looks like an author name (2+ words with capitalization)
199-
if (preg_match('/^[A-Z][a-z]+\s+[A-Z][a-z]+/', $query)) {
202+
// Check if query looks like an author name (2+ words, first letters capitalized)
203+
if (preg_match('/^[A-Z][a-z]+\s+[A-Z]/', $query)) {
200204
return 'author:' . $query;
201205
}
202206

0 commit comments

Comments
 (0)