Update book processing and matching

This commit is contained in:
DariusIII
2026-04-14 15:14:27 +02:00
parent 43773a0cda
commit 49ca479083
21 changed files with 1112 additions and 168 deletions
+1
View File
@@ -150,6 +150,7 @@ TVDB_APIKEY=${TVDB_APIKEY}
TVDB_PIN=${TVDB_PIN}
ANIDB_APIKEY=${ANIDB_APIKEY}
FANARTTV_APIKEY=${FANARTTV_APIKEY}
GOOGLE_BOOKS_API_KEY=${GOOGLE_BOOKS_API_KEY}
OMDB_APIKEY=${OMDB_APIKEY}
TRAKTTV_APIKEY=${TRAKTTV_APIKEY}
+1
View File
@@ -197,6 +197,7 @@ TVDB_APIKEY=ef6fb572-bcee-4d99-9b52-90549ad7553a
TVDB_PIN=
ANIDB_APIKEY=
FANARTTV_APIKEY=
GOOGLE_BOOKS_API_KEY=
ISBNDB_API_KEY=
OMDB_APIKEY=
TRAKTTV_APIKEY=
@@ -5,7 +5,6 @@ declare(strict_types=1);
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BasePageController;
use App\Models\Category;
use App\Models\GrabStat;
use App\Models\ReleaseStat;
use App\Models\RoleStat;
@@ -33,9 +32,6 @@ class AdminSiteController extends BasePageController
switch ($action) {
case 'submit':
if ($request->missing('book_reqids')) {
$request->merge(['book_reqids' => []]);
}
Settings::settingsUpdate($request->all());
return redirect()->to('admin/site-edit')->with('success', 'Settings updated successfully');
@@ -45,27 +41,6 @@ class AdminSiteController extends BasePageController
break;
}
// return a list of audiobooks, mags, ebooks, technical and foreign books
$result = Category::query()->whereIn('id', [Category::MUSIC_AUDIOBOOK, Category::BOOKS_MAGAZINES, Category::BOOKS_TECHNICAL, Category::BOOKS_FOREIGN])->get(['id', 'title']);
// setup the display lists for these categories
$book_reqids_ids = [];
$book_reqids_names = [];
foreach ($result as $bookcategory) {
$book_reqids_ids[] = $bookcategory['id'];
$book_reqids_names[] = $bookcategory['title'];
}
// convert from a string array to an int array as we want to use int
$book_reqids_ids = array_map(fn ($value) => (int) $value, $book_reqids_ids);
// convert from a list to an array as we need to use an array, but the Settings table only saves strings
$bookReqidsValue = Settings::settingValue('book_reqids') ?? '';
$books_selected = $bookReqidsValue !== '' ? explode(',', (string) $bookReqidsValue) : [];
// convert from a string array to an int array, filtering out empty values
$books_selected = array_map(fn ($value) => (int) trim($value), array_filter($books_selected));
$compress_headers_warning = ! str_contains(config('settings.nntp_server'), 'astra') ? 'compress_headers_warning' : '';
$this->viewData = array_merge($this->viewData, [
@@ -127,11 +102,6 @@ class AdminSiteController extends BasePageController
'ids' => [0, 1, 2],
'names' => ['Disabled', 'Lookup Request IDs', 'Lookup Request IDs Threaded'],
],
'book_reqids' => [
'ids' => $book_reqids_ids,
'names' => $book_reqids_names,
'selected' => $books_selected,
],
'compress_headers_warning' => $compress_headers_warning,
'title' => $title,
'meta_title' => $meta_title,
+233 -102
View File
@@ -11,6 +11,8 @@ use App\Models\Category;
use App\Models\Release;
use App\Models\Settings;
use App\Services\Releases\ReleaseBrowseService;
use App\Support\BookMatchScorer;
use App\Support\DTOs\BookParseResult;
use App\Support\MetadataSearchLookup;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
@@ -29,17 +31,12 @@ class BookService
public string $imgSavePath;
public ?string $bookreqids;
public string $renamed;
/**
* @var array<string, mixed>
*/
public array $failCache;
public ?string $parsedIsbn;
public ?BookParseResult $parsedBookResult;
/**
* @throws \Exception
*/
@@ -51,11 +48,10 @@ class BookService
$this->sleeptime = Settings::settingValue('amazonsleep') !== '' ? (int) Settings::settingValue('amazonsleep') : 1000;
$this->imgSavePath = storage_path('covers/book/');
$this->bookreqids = (string) Category::BOOKS_EBOOK;
$this->renamed = (int) Settings::settingValue('lookupbooks') === 2 ? 'AND isrenamed = 1' : '';
$this->failCache = [];
$this->parsedIsbn = null;
$this->parsedBookResult = null;
}
/**
@@ -73,8 +69,9 @@ class BookService
/**
* Get book info by name using full-text search.
*/
public function getBookInfoByName(string $title): ?Model
public function getBookInfoByName(string $title, ?BookParseResult $parsed = null): ?Model
{
$parsed ??= $this->parsedBookResult;
$searchWords = '';
$title = preg_replace(['/( - | -|\(.+\)|\(|\))/', '/[^\w ]+/'], [' ', ''], $title);
$title = trim(trim(preg_replace('/\s\s+/i', ' ', $title)));
@@ -97,14 +94,9 @@ class BookService
->whereIn('id', $bookIds)
->get()
->keyBy('id');
foreach ($bookIds as $bookId) {
if ($rowsById->has($bookId)) {
/** @var BookInfo $book */
$book = $rowsById->get($bookId);
return $book;
}
$best = $this->pickBestExistingBook($rowsById->values()->all(), $parsed);
if ($best !== null) {
return $best;
}
}
}
@@ -112,9 +104,12 @@ class BookService
return null;
}
return BookInfo::query()
$results = BookInfo::query()
->whereRaw('MATCH (author, title) AGAINST (? IN BOOLEAN MODE)', [$searchWords])
->first();
->limit(25)
->get();
return $this->pickBestExistingBook($results->all(), $parsed);
}
/**
@@ -366,35 +361,24 @@ class BookService
*/
public function processBookReleases(string $groupID = '', string $guidChar = ''): void
{
$bookids = [];
if (ctype_digit((string) $this->bookreqids)) {
$bookids[] = $this->bookreqids;
} else {
$bookids = explode(', ', $this->bookreqids);
$query = Release::query()
->whereNull('bookinfo_id')
->whereBetween('categories_id', [Category::BOOKS_ROOT, Category::BOOKS_UNKNOWN])
->orderByDesc('postdate')
->limit($this->bookqty);
if ($guidChar !== '') {
$query->where('leftguid', 'like', $guidChar.'%');
}
$total = \count($bookids);
if ($total > 0) {
foreach ($bookids as $i => $iValue) {
$query = Release::query()
->whereNull('bookinfo_id')
->whereIn('categories_id', [$iValue])
->orderByDesc('postdate')
->limit($this->bookqty);
if ($guidChar !== '') {
$query->where('leftguid', 'like', $guidChar.'%');
}
if ($groupID !== '') {
$query->where('groups_id', $groupID);
}
$this->processBookReleasesHelper(
$query->get(['searchname', 'id', 'categories_id']), $iValue
);
}
if ($groupID !== '') {
$query->where('groups_id', $groupID);
}
$this->processBookReleasesHelper(
$query->get(['searchname', 'id', 'categories_id']),
sprintf('%d-%d', Category::BOOKS_ROOT, Category::BOOKS_UNKNOWN)
);
}
/**
@@ -428,9 +412,10 @@ class BookService
}
// Do a local lookup first
$bookCheck = $this->getBookInfoByName($bookInfo);
$bookCheck = $this->getBookInfoByName($bookInfo, $this->parsedBookResult);
$failCacheKey = 'book_lookup_fail_'.md5(strtolower($bookInfo));
if ($bookCheck === null && \in_array($bookInfo, $this->failCache, false)) {
if ($bookCheck === null && Cache::has($failCacheKey)) {
// Lookup recently failed, no point trying again
if ($this->echooutput) {
cli()->info('Cached previous failure. Skipping.');
@@ -440,7 +425,9 @@ class BookService
$bookId = $this->updateBookInfo($bookInfo, $this->parsedIsbn);
$usedExternalApi = true;
if ($bookId === -2) {
$this->failCache[] = $bookInfo;
Cache::put($failCacheKey, true, now()->addDays(7));
} else {
Cache::forget($failCacheKey);
}
} else {
$bookId = $bookCheck['id'];
@@ -472,21 +459,14 @@ class BookService
*/
public function parseTitle(mixed $release_name, mixed $releaseID, mixed $releasetype)
{
$this->parsedIsbn = $this->extractIsbn((string) $release_name);
$a = preg_replace('/\d{1,2} \d{1,2} \d{2,4}|(19|20)\d\d|anybody got .+?[a-z]\? |[ ._-](Novel|TIA)([ ._-]|$)|([ \.])HQ([-\. ])|[\(\)\.\-_ ](AVI|AZW3?|DOC|EPUB|LIT|MOBI|NFO|RETAIL|(si)?PDF|RTF|TXT)[\)\]\.\-_ ](?![a-z0-9])|compleet|DAGSTiDNiNGEN|DiRFiX|\+ extra|r?e ?Books?([\.\-_ ]English|ers)?|azw3?|ePu([bp])s?|html|mobi|^NEW[\.\-_ ]|PDF([\.\-_ ]English)?|Please post more|Post description|Proper|Repack(fix)?|[\.\-_ ](Chinese|English|French|German|Italian|Retail|Scan|Swedish)|^R4 |Repost|Skytwohigh|TIA!+|TruePDF|V413HAV|(would someone )?please (re)?post.+? "|with the authors name right/i', '', $release_name);
$b = preg_replace('/^(As Req |conversion |eq |Das neue Abenteuer \d+|Fixed version( ignore previous post)?|Full |Per Req As Found|(\s+)?R4 |REQ |revised |version |\d+(\s+)?$)|(COMPLETE|INTERNAL|RELOADED| (AZW3|eB|docx|ENG?|exe|FR|Fix|gnv64|MU|NIV|R\d\s+\d{1,2} \d{1,2}|R\d|Req|TTL|UC|v(\s+)?\d))(\s+)?$/i', '', $a);
// Remove book series from title to improve metadata matching.
$c = preg_replace('/ - \[.+\]|\[.+\]/', '', $b);
// remove any brackets left behind
$d = preg_replace('/(\(\)|\[\])/', '', $c);
$releasename = trim(preg_replace('/\s\s+/i', ' ', $d));
$parsed = $this->parseReleaseName((string) $release_name, (string) $releasetype);
$this->parsedBookResult = $parsed;
$this->parsedIsbn = $parsed->isbn;
$releasename = $parsed->title;
// the default existing type was ebook, this handles that in the same manor as before
if ($releasetype === 'ebook') {
if (preg_match('/^([a-z0-9] )+$|ArtofUsenet|ekiosk|(ebook|mobi).+collection|erotica|Full Video|ImwithJamie|linkoff org|Mega.+pack|^[a-z0-9]+ (?!((January|February|March|April|May|June|July|August|September|O([ck])tober|November|De([cz])ember)))[a-z]+( (ebooks?|The))?$|NY Times|(Book|Massive) Dump|Sexual/i', $releasename)) {
if ($parsed->isJunk) {
if ($this->echooutput) {
cli()->headerOver('Changing category to misc books: ').cli()->primary($releasename);
}
@@ -496,7 +476,7 @@ class BookService
return false;
}
if (preg_match('/^([a-z0-9ü!]+ ){1,2}(N|Vol)?\d{1,4}([abc])?$|^([a-z0-9]+ ){1,2}(Jan( |unar|$)|Feb( |ruary|$)|Mar( |ch|$)|Apr( |il|$)|May(?![a-z0-9])|Jun([ e$])|Jul([ y$])|Aug( |ust|$)|Sep( |tember|$)|O([ck])t( |ober|$)|Nov( |ember|$)|De([cz])( |ember|$))/ui', $releasename) && ! preg_match('/Part \d+/i', $releasename)) {
if ($parsed->isMagazine) {
if ($this->echooutput) {
cli()->headerOver('Changing category to magazines: ').cli()->primary($releasename);
}
@@ -506,7 +486,7 @@ class BookService
return false;
}
if (! empty($releasename) && ! preg_match('/^[a-z0-9]+$|^([0-9]+ ){1,}$|Part \d+/i', $releasename)) {
return $releasename;
return $parsed->searchQuery();
}
return false;
@@ -515,7 +495,7 @@ class BookService
if (! empty($releasename) && ! preg_match('/^[a-z0-9]+$|^([0-9]+ ){1,}$|Part \d+/i', $releasename)) {
// We can skip category checks for audiobooks since the category is known.
// As long as the release name is valid, it should still be post-processed.
return $releasename;
return $parsed->searchQuery();
}
return false;
@@ -524,6 +504,70 @@ class BookService
return false;
}
public function parseReleaseName(string $releaseName, string $releaseType = 'ebook'): BookParseResult
{
$isbn = $this->extractIsbn($releaseName);
$a = preg_replace('/\d{1,2} \d{1,2} \d{2,4}|(19|20)\d\d|anybody got .+?[a-z]\? |[ ._-](Novel|TIA)([ ._-]|$)|([ \.])HQ([-\. ])|[\(\)\.\-_ ](AVI|AZW3?|DOC|EPUB|LIT|MOBI|NFO|RETAIL|(si)?PDF|RTF|TXT)[\)\]\.\-_ ](?![a-z0-9])|compleet|DAGSTiDNiNGEN|DiRFiX|\+ extra|r?e ?Books?([\.\-_ ]English|ers)?|azw3?|ePu([bp])s?|html|mobi|^NEW[\.\-_ ]|PDF([\.\-_ ]English)?|Please post more|Post description|Proper|Repack(fix)?|[\.\-_ ](Chinese|English|French|German|Italian|Retail|Scan|Swedish|Multilingual)|^R4 |Repost|Skytwohigh|TIA!+|TruePDF|V413HAV|(would someone )?please (re)?post.+? "|with the authors name right/i', '', $releaseName);
$b = preg_replace('/^(As Req |conversion |eq |Das neue Abenteuer \d+|Fixed version( ignore previous post)?|Full |Per Req As Found|(\s+)?R4 |REQ |revised |version |\d+(\s+)?$)|(COMPLETE|INTERNAL|RELOADED| (AZW3|eB|docx|ENG?|exe|FR|Fix|gnv64|MU|NIV|R\d\s+\d{1,2} \d{1,2}|R\d|Req|TTL|UC|v(\s+)?\d))(\s+)?$/i', '', (string) $a);
$c = preg_replace('/ - \[.+\]|\[.+\]/', '', (string) $b);
$d = preg_replace('/(\(\)|\[\])/', '', (string) $c);
$normalized = trim((string) preg_replace('/\s\s+/i', ' ', (string) $d));
$normalized = (string) preg_replace('/[._]+/', ' ', $normalized);
$normalized = (string) preg_replace('/\b97[89](?:[-\s]?\d){10}\b|\b\d(?:[-\s]?\d){8}[-\s]?[\dXx]\b/', '', $normalized);
$normalized = trim((string) preg_replace('/\s+/', ' ', $normalized));
$year = null;
if (preg_match('/\b(19|20)\d{2}\b/', $normalized, $yearMatch) === 1) {
$year = (int) $yearMatch[0];
}
$format = null;
if (preg_match('/\b(EPUB|MOBI|AZW3?|PDF|FB2|DJVU|LIT)\b/i', $releaseName, $formatMatch) === 1) {
$format = strtoupper($formatMatch[1]);
}
$author = null;
$title = $normalized;
if (preg_match('/^(?<author>[A-Za-z0-9&\'\.\-\s]{3,80})\s-\s(?<title>.+)$/', $normalized, $matches) === 1) {
$author = trim($matches['author']);
$title = trim($matches['title']);
} elseif (preg_match('/^(?<author>[A-Za-z0-9&\'\.\-\s]{3,80})\s+by\s+(?<title>.+)$/i', $normalized, $matches) === 1) {
$author = trim($matches['author']);
$title = trim($matches['title']);
} elseif (preg_match('/^(?<title>.+?)\s+by\s+(?<author>[A-Za-z0-9&\'\.\-\s]{3,80})$/i', $normalized, $matches) === 1) {
$author = trim($matches['author']);
$title = trim($matches['title']);
}
$title = trim((string) preg_replace('/\s+/', ' ', (string) preg_replace('/\((Book|Series)\s*\d+\)/i', '', $title)));
$isJunk = false;
$junkPattern = '/^([a-z0-9] )+$|ArtofUsenet|ekiosk|(ebook|mobi).+collection|erotica|Full Video|ImwithJamie|linkoff org|Mega.+pack|^[a-z0-9]+ (?!((January|February|March|April|May|June|July|August|September|O([ck])tober|November|De([cz])ember)))[a-z]+( (ebooks?|The))?$|NY Times|(Book|Massive) Dump|Sexual/i';
if (preg_match($junkPattern, $normalized) === 1) {
$isJunk = true;
}
if ($releaseType === 'ebook' && preg_match('/\b(v?\d+\.\d+\.\d+(?:\.\d+)?|x64|x86|portable|setup|crack(ed)?|patch)\b/i', $releaseName) === 1) {
$isJunk = true;
}
if ($releaseType === 'ebook' && preg_match('/\b(WEB[\.\-_ ]?FLAC|MP3|320kbps|FALCON|discography)\b/i', $releaseName) === 1) {
$isJunk = true;
}
$isMagazine = preg_match('/^([a-z0-9ü!]+ ){1,2}(N|Vol)?\d{1,4}([abc])?$|^([a-z0-9]+ ){1,2}(Jan( |unar|$)|Feb( |ruary|$)|Mar( |ch|$)|Apr( |il|$)|May(?![a-z0-9])|Jun([ e$])|Jul([ y$])|Aug( |ust|$)|Sep( |tember|$)|O([ck])t( |ober|$)|Nov( |ember|$)|De([cz])( |ember|$))/ui', $normalized) === 1
&& preg_match('/Part \d+/i', $normalized) !== 1;
return new BookParseResult(
rawName: $releaseName,
title: $title,
author: $author,
isbn: $isbn,
year: $year,
format: $format,
isJunk: $isJunk,
isMagazine: $isMagazine,
);
}
public function extractIsbn(string $releaseName): ?string
{
if (preg_match('/\b(97[89](?:[-\s]?\d){10})\b/', $releaseName, $matches) === 1) {
@@ -554,27 +598,63 @@ class BookService
{
$ri = new ReleaseImageService;
$isbnDb = new IsbnDbService;
$googleBooks = new GoogleBooksService;
$openLibrary = new OpenLibraryService;
$itunes = new ItunesService;
$scorer = new BookMatchScorer;
$bookId = -2;
$book = false;
$book = null;
if ($bookInfo !== '') {
$parsed = $this->parsedBookResult ?? $this->parseReleaseName($bookInfo);
$candidates = [];
if ($isbnDb->isConfigured()) {
if ($isbn !== null && $isbn !== '') {
cli()->info('Fetching data from ISBNdb by ISBN '.$isbn);
$book = $isbnDb->findByIsbn($isbn);
$candidate = $isbnDb->findByIsbn($isbn);
if (is_array($candidate)) {
$candidates[] = $candidate;
}
}
if ($book === false || $book === null) {
if ($candidates === []) {
cli()->info('Fetching data from ISBNdb for '.$bookInfo);
$book = $isbnDb->searchBook($bookInfo);
$candidates = array_merge($candidates, $isbnDb->searchBooks($bookInfo));
}
}
if ($book === false || $book === null) {
cli()->info('Fetching data from iTunes for '.$bookInfo);
$book = $this->fetchItunesBookProperties($bookInfo);
if ($candidates === []) {
cli()->info('Fetching data from Google Books for '.$bookInfo);
$candidates = array_merge(
$candidates,
$googleBooks->searchBooks(
$bookInfo,
$parsed->title,
$parsed->author,
$isbn
)
);
}
if ($candidates === []) {
cli()->info('Fetching data from iTunes for '.$bookInfo);
$candidates = array_merge($candidates, $this->fetchItunesBookProperties($bookInfo, $itunes));
}
if ($candidates === []) {
cli()->info('Fetching data from Open Library for '.$bookInfo);
if ($isbn !== null && $isbn !== '') {
$candidate = $openLibrary->findByIsbn($isbn);
if (is_array($candidate)) {
$candidates[] = $candidate;
}
}
if ($candidates === []) {
$candidates = array_merge($candidates, $openLibrary->searchBooks($bookInfo));
}
}
$book = $this->pickBestCandidate($candidates, $parsed, $scorer);
}
if (empty($book)) {
@@ -652,43 +732,94 @@ class BookService
/**
* Fetch book properties from iTunes.
*
* @return array<string, mixed>|bool
* @return array<int, array<string, mixed>>
*/
public function fetchItunesBookProperties(string $bookInfo)
public function fetchItunesBookProperties(string $bookInfo, ?ItunesService $itunesService = null): array
{
$itunes = new ItunesService;
$iTunesBook = $itunes->findEbook($bookInfo);
$itunes = $itunesService ?? new ItunesService;
$iTunesBooks = $itunes->findEbooks($bookInfo);
if ($iTunesBook === null) {
if ($iTunesBooks === []) {
cli()->notice('Could not find a match on iTunes!');
return false;
return [];
}
cli()->info('Found matching title: '.$iTunesBook['name']);
$book = [
'title' => $iTunesBook['name'],
'author' => $iTunesBook['author'],
'asin' => $iTunesBook['id'],
'isbn' => null,
'ean' => null,
'url' => $iTunesBook['store_url'],
'salesrank' => '',
'publisher' => '',
'pages' => '',
'coverurl' => ! empty($iTunesBook['cover']) ? $iTunesBook['cover'] : '',
'genre' => is_array($iTunesBook['genres']) ? implode(', ', $iTunesBook['genres']) : $iTunesBook['genre'],
'overview' => strip_tags($iTunesBook['description'] ?? ''),
'publishdate' => $iTunesBook['release_date'],
];
if (! empty($book['coverurl'])) {
$book['cover'] = 1;
} else {
$book['cover'] = 0;
$books = [];
foreach ($iTunesBooks as $iTunesBook) {
$books[] = [
'title' => $iTunesBook['name'],
'author' => $iTunesBook['author'],
'asin' => (string) $iTunesBook['id'],
'isbn' => null,
'ean' => null,
'url' => $iTunesBook['store_url'],
'salesrank' => '',
'publisher' => '',
'pages' => '',
'coverurl' => ! empty($iTunesBook['cover']) ? $iTunesBook['cover'] : '',
'genre' => is_array($iTunesBook['genres']) ? implode(', ', $iTunesBook['genres']) : $iTunesBook['genre'],
'overview' => strip_tags($iTunesBook['description'] ?? ''),
'publishdate' => $iTunesBook['release_date'],
'cover' => ! empty($iTunesBook['cover']) ? 1 : 0,
];
}
return $book;
cli()->info('Found '.\count($books).' matching title(s) on iTunes');
return $books;
}
/**
* @param array<int, BookInfo> $books
*/
private function pickBestExistingBook(array $books, ?BookParseResult $parsed): ?BookInfo
{
if ($books === []) {
return null;
}
if ($parsed === null) {
return $books[0];
}
$scorer = new BookMatchScorer;
$best = null;
$bestScore = 0.0;
foreach ($books as $book) {
$score = $scorer->scoreBookInfo($book, $parsed);
if ($score > $bestScore) {
$best = $book;
$bestScore = $score;
}
}
return $bestScore >= 0.4 ? $best : null;
}
/**
* @param array<int, array<string, mixed>> $candidates
* @return array<string, mixed>|null
*/
private function pickBestCandidate(array $candidates, BookParseResult $parsed, BookMatchScorer $scorer): ?array
{
if ($candidates === []) {
return null;
}
$best = null;
$bestScore = 0.0;
foreach ($candidates as $candidate) {
$score = $scorer->score($candidate, $parsed);
if ($score > $bestScore) {
$bestScore = $score;
$best = $candidate;
}
}
if ($best === null || $bestScore < 0.4) {
return null;
}
return $best;
}
}
@@ -28,6 +28,14 @@ class BookCategorizer extends AbstractCategorizer
if (preg_match('/\b(?:PS[1-5]|PlayStation|Xbox|Switch|Nintendo|Wii|3DS|GameCube)\b/i', $context->releaseName)) {
return true;
}
// Skip software and utility release naming patterns.
if (preg_match('/\bv?\d+\.\d+\.\d+(?:\.\d+)?\b.*\b(?:Multilingual|x64|x86|Portable|Setup|Patch)\b/i', $context->releaseName)) {
return true;
}
// Skip music-style release names that are often misfiled as books.
if (preg_match('/\b(?:WEB[\.\-_ ]?FLAC|MP3|320kbps|discography|FALCON)\b/i', $context->releaseName)) {
return true;
}
// Skip TV shows (season patterns)
if (preg_match('/[._ -]S\d{1,3}[._ -]?(E\d|Complete|Full|1080|720|480|2160|WEB|HDTV|BluRay)/i', $context->releaseName)) {
return true;
+224
View File
@@ -0,0 +1,224 @@
<?php
declare(strict_types=1);
namespace App\Services;
use Carbon\Carbon;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
class GoogleBooksService
{
protected const API_URL = 'https://www.googleapis.com/books/v1/volumes';
protected Client $client;
protected ?string $apiKey;
protected int $maxResults = 5;
public function __construct(?Client $client = null, ?string $apiKey = null)
{
$this->apiKey = trim($apiKey ?? (string) config('nntmux_api.google_books_api_key', '')) ?: null;
$this->client = $client ?? new Client([
'timeout' => 15,
'connect_timeout' => 10,
'http_errors' => false,
'headers' => [
'Accept' => 'application/json',
'User-Agent' => 'NNTmux/1.0',
],
]);
}
public function hasApiKey(): bool
{
return $this->apiKey !== null;
}
public function isConfigured(): bool
{
return true;
}
/**
* @return array<string, mixed>|null
*/
public function findByIsbn(string $isbn): ?array
{
$books = $this->searchBooks('', null, null, $isbn);
return $books[0] ?? null;
}
/**
* @return array<string, mixed>|null
*/
public function searchBook(string $query, ?string $title = null, ?string $author = null): ?array
{
$books = $this->searchBooks($query, $title, $author);
return $books[0] ?? null;
}
/**
* @return array<int, array<string, mixed>>
*/
public function searchBooks(string $query, ?string $title = null, ?string $author = null, ?string $isbn = null): array
{
$query = trim($query);
$title = $title !== null ? trim($title) : null;
$author = $author !== null ? trim($author) : null;
$isbn = $isbn !== null ? strtoupper((string) preg_replace('/[^0-9X]/i', '', $isbn)) : null;
$searchQuery = $this->buildSearchQuery($query, $title, $author, $isbn);
if ($searchQuery === '') {
return [];
}
$cacheKey = 'google_books_search_'.md5($searchQuery.'_'.$this->maxResults.'_'.$this->apiKey);
$cached = Cache::get($cacheKey);
if (is_array($cached)) {
return $cached;
}
$response = $this->request([
'q' => $searchQuery,
'maxResults' => $this->maxResults,
'printType' => 'books',
]);
if (! is_array($response['items'] ?? null)) {
return [];
}
$books = [];
foreach ($response['items'] as $item) {
if (is_array($item)) {
$books[] = $this->normalizeBookResult($item);
}
}
Cache::put($cacheKey, $books, now()->addHours(12));
return $books;
}
/**
* @param array<string, mixed> $query
* @return array<string, mixed>|null
*/
protected function request(array $query): ?array
{
try {
if ($this->apiKey !== null) {
$query['key'] = $this->apiKey;
}
$response = $this->client->get(self::API_URL, ['query' => $query]);
if ($response->getStatusCode() !== 200) {
Log::warning('Google Books API request failed', ['status' => $response->getStatusCode()]);
return null;
}
$decoded = json_decode($response->getBody()->getContents(), true);
return is_array($decoded) ? $decoded : null;
} catch (GuzzleException $e) {
Log::error('Google Books API request error: '.$e->getMessage());
return null;
}
}
private function buildSearchQuery(string $query, ?string $title, ?string $author, ?string $isbn): string
{
if ($isbn !== null && $isbn !== '') {
return 'isbn:'.$isbn;
}
$parts = [];
if ($title !== null && $title !== '') {
$parts[] = 'intitle:'.$title;
}
if ($author !== null && $author !== '') {
$parts[] = 'inauthor:'.$author;
}
if ($parts !== []) {
return implode(' ', $parts);
}
return $query;
}
/**
* @param array<string, mixed> $item
* @return array<string, mixed>
*/
private function normalizeBookResult(array $item): array
{
$volumeInfo = is_array($item['volumeInfo'] ?? null) ? $item['volumeInfo'] : [];
$industryIdentifiers = is_array($volumeInfo['industryIdentifiers'] ?? null) ? $volumeInfo['industryIdentifiers'] : [];
$isbn13 = '';
$isbn10 = '';
foreach ($industryIdentifiers as $identifier) {
if (! is_array($identifier)) {
continue;
}
$type = (string) ($identifier['type'] ?? '');
$value = strtoupper((string) preg_replace('/[^0-9X]/i', '', (string) ($identifier['identifier'] ?? '')));
if ($type === 'ISBN_13' && $value !== '') {
$isbn13 = $value;
}
if ($type === 'ISBN_10' && $value !== '') {
$isbn10 = $value;
}
}
$identifier = $isbn13 !== '' ? $isbn13 : ($isbn10 !== '' ? $isbn10 : (string) ($item['id'] ?? md5(json_encode($item) ?: '')));
$authors = is_array($volumeInfo['authors'] ?? null)
? array_values(array_filter(array_map('strval', $volumeInfo['authors'])))
: [];
$categories = is_array($volumeInfo['categories'] ?? null)
? array_values(array_filter(array_map('strval', $volumeInfo['categories'])))
: [];
$imageLinks = is_array($volumeInfo['imageLinks'] ?? null) ? $volumeInfo['imageLinks'] : [];
$coverUrl = (string) ($imageLinks['thumbnail'] ?? $imageLinks['smallThumbnail'] ?? '');
return [
'title' => (string) ($volumeInfo['title'] ?? ''),
'author' => implode(', ', $authors),
'asin' => 'googlebooks:'.$identifier,
'isbn' => $isbn13 !== '' ? $isbn13 : null,
'ean' => $isbn10 !== '' ? $isbn10 : null,
'url' => (string) ($volumeInfo['infoLink'] ?? ''),
'salesrank' => '',
'publisher' => (string) ($volumeInfo['publisher'] ?? ''),
'publishdate' => $this->normalizePublishDate($volumeInfo['publishedDate'] ?? null),
'pages' => isset($volumeInfo['pageCount']) && is_numeric($volumeInfo['pageCount']) ? (string) ((int) $volumeInfo['pageCount']) : '',
'overview' => strip_tags((string) ($volumeInfo['description'] ?? '')),
'genre' => implode(', ', $categories),
'coverurl' => $coverUrl,
'cover' => $coverUrl !== '' ? 1 : 0,
];
}
private function normalizePublishDate(mixed $date): ?string
{
if (! is_string($date) || trim($date) === '') {
return null;
}
try {
return Carbon::parse($date)->format('Y-m-d');
} catch (\Exception) {
return null;
}
}
}
+22 -6
View File
@@ -46,10 +46,20 @@ class IsbnDbService
* @return array<string, mixed>|null
*/
public function searchBook(string $query): ?array
{
$books = $this->searchBooks($query);
return $books[0] ?? null;
}
/**
* @return array<int, array<string, mixed>>
*/
public function searchBooks(string $query): array
{
$query = trim($query);
if ($query === '' || ! $this->isConfigured()) {
return null;
return [];
}
$cacheKey = 'isbndb_search_'.md5($query.'_'.$this->pageSize);
@@ -64,14 +74,20 @@ class IsbnDbService
]);
$books = $response['data'] ?? null;
if (! is_array($books) || $books === [] || ! isset($books[0]) || ! is_array($books[0])) {
return null;
if (! is_array($books) || $books === []) {
return [];
}
$book = $this->normalizeBookResult($books[0]);
Cache::put($cacheKey, $book, now()->addHours(24));
$normalized = [];
foreach ($books as $book) {
if (is_array($book)) {
$normalized[] = $this->normalizeBookResult($book);
}
}
return $book;
Cache::put($cacheKey, $normalized, now()->addHours(24));
return $normalized;
}
/**
+21 -3
View File
@@ -206,13 +206,31 @@ class ItunesService
*/
public function findEbook(string $term): ?array
{
$results = $this->searchEbooks($term);
$results = $this->findEbooks($term);
return $results[0] ?? null;
}
/**
* Get normalized ebook results for a search term.
*
* @return array<int, array<string, mixed>>
*/
public function findEbooks(string $term): array
{
$results = $this->searchEbooks($term);
if (empty($results)) {
return null;
return [];
}
return $this->normalizeEbookResult($results[0]); // @phpstan-ignore offsetAccess.notFound
$normalized = [];
foreach ($results as $result) {
if (is_array($result)) {
$normalized[] = $this->normalizeEbookResult($result);
}
}
return $normalized;
}
/**
+234
View File
@@ -0,0 +1,234 @@
<?php
declare(strict_types=1);
namespace App\Services;
use Carbon\Carbon;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
class OpenLibraryService
{
protected const SEARCH_URL = 'https://openlibrary.org/search.json';
protected const ISBN_URL = 'https://openlibrary.org/isbn/';
protected Client $client;
protected int $limit = 5;
public function __construct(?Client $client = null)
{
$this->client = $client ?? new Client([
'timeout' => 15,
'connect_timeout' => 10,
'http_errors' => false,
'headers' => [
'Accept' => 'application/json',
'User-Agent' => 'NNTmux/1.0',
],
]);
}
/**
* @return array<string, mixed>|null
*/
public function findByIsbn(string $isbn): ?array
{
$isbn = strtoupper((string) preg_replace('/[^0-9X]/i', '', $isbn));
if ($isbn === '') {
return null;
}
$cacheKey = 'openlibrary_isbn_'.$isbn;
$cached = Cache::get($cacheKey);
if (is_array($cached)) {
return $cached;
}
try {
$response = $this->client->get(self::ISBN_URL.rawurlencode($isbn).'.json');
if ($response->getStatusCode() !== 200) {
return null;
}
$data = json_decode($response->getBody()->getContents(), true);
if (! is_array($data)) {
return null;
}
$book = $this->normalizeIsbnResult($data, $isbn);
Cache::put($cacheKey, $book, now()->addHours(24));
return $book;
} catch (GuzzleException $e) {
Log::error('OpenLibrary ISBN lookup error: '.$e->getMessage());
return null;
}
}
/**
* @return array<string, mixed>|null
*/
public function searchBook(string $query): ?array
{
$results = $this->searchBooks($query);
return $results[0] ?? null;
}
/**
* @return array<int, array<string, mixed>>
*/
public function searchBooks(string $query): array
{
$query = trim($query);
if ($query === '') {
return [];
}
$cacheKey = 'openlibrary_search_'.md5($query.'_'.$this->limit);
$cached = Cache::get($cacheKey);
if (is_array($cached)) {
return $cached;
}
try {
$response = $this->client->get(self::SEARCH_URL, [
'query' => [
'q' => $query,
'limit' => $this->limit,
],
]);
if ($response->getStatusCode() !== 200) {
return [];
}
$data = json_decode($response->getBody()->getContents(), true);
if (! is_array($data['docs'] ?? null)) {
return [];
}
$results = [];
foreach ($data['docs'] as $doc) {
if (is_array($doc)) {
$results[] = $this->normalizeSearchResult($doc);
}
}
Cache::put($cacheKey, $results, now()->addHours(12));
return $results;
} catch (GuzzleException $e) {
Log::error('OpenLibrary search error: '.$e->getMessage());
return [];
}
}
/**
* @param array<string, mixed> $book
* @return array<string, mixed>
*/
private function normalizeIsbnResult(array $book, string $isbn): array
{
$title = (string) ($book['title'] ?? '');
$authors = [];
if (is_array($book['authors'] ?? null)) {
foreach ($book['authors'] as $author) {
if (is_array($author) && isset($author['name'])) {
$authors[] = (string) $author['name'];
}
}
}
$publishDate = $this->normalizePublishDate($book['publish_date'] ?? null);
$coverUrl = '';
if (is_array($book['covers'] ?? null) && isset($book['covers'][0])) {
$coverUrl = 'https://covers.openlibrary.org/b/id/'.(int) $book['covers'][0].'-L.jpg';
}
return [
'title' => $title,
'author' => implode(', ', $authors),
'asin' => 'openlibrary:'.$isbn,
'isbn' => strlen($isbn) === 13 ? $isbn : null,
'ean' => strlen($isbn) === 10 ? $isbn : null,
'url' => (string) ($book['url'] ?? ''),
'salesrank' => '',
'publisher' => is_array($book['publishers'] ?? null) ? (string) ($book['publishers'][0] ?? '') : '',
'publishdate' => $publishDate,
'pages' => isset($book['number_of_pages']) && is_numeric($book['number_of_pages']) ? (string) ((int) $book['number_of_pages']) : '',
'overview' => '',
'genre' => '',
'coverurl' => $coverUrl,
'cover' => $coverUrl !== '' ? 1 : 0,
];
}
/**
* @param array<string, mixed> $doc
* @return array<string, mixed>
*/
private function normalizeSearchResult(array $doc): array
{
$isbn13 = null;
$isbn10 = null;
if (is_array($doc['isbn'] ?? null)) {
foreach ($doc['isbn'] as $isbn) {
$normalized = strtoupper((string) preg_replace('/[^0-9X]/i', '', (string) $isbn));
if ($normalized === '') {
continue;
}
if (strlen($normalized) === 13 && $isbn13 === null) {
$isbn13 = $normalized;
} elseif (strlen($normalized) === 10 && $isbn10 === null) {
$isbn10 = $normalized;
}
}
}
$identifier = $isbn13 ?? $isbn10 ?? (string) ($doc['key'] ?? md5((string) json_encode($doc)));
$authorNames = is_array($doc['author_name'] ?? null)
? array_values(array_filter(array_map('strval', $doc['author_name'])))
: [];
$coverUrl = isset($doc['cover_i']) && is_numeric($doc['cover_i'])
? 'https://covers.openlibrary.org/b/id/'.(int) $doc['cover_i'].'-L.jpg'
: '';
return [
'title' => (string) ($doc['title'] ?? ''),
'author' => implode(', ', $authorNames),
'asin' => 'openlibrary:'.$identifier,
'isbn' => $isbn13,
'ean' => $isbn10,
'url' => isset($doc['key']) ? 'https://openlibrary.org'.$doc['key'] : '',
'salesrank' => '',
'publisher' => is_array($doc['publisher'] ?? null) ? (string) ($doc['publisher'][0] ?? '') : '',
'publishdate' => isset($doc['first_publish_year']) ? ((int) $doc['first_publish_year']).'-01-01' : null,
'pages' => '',
'overview' => '',
'genre' => '',
'coverurl' => $coverUrl,
'cover' => $coverUrl !== '' ? 1 : 0,
];
}
private function normalizePublishDate(mixed $date): ?string
{
if (! is_string($date) || trim($date) === '') {
return null;
}
try {
return Carbon::parse($date)->format('Y-m-d');
} catch (\Exception) {
return null;
}
}
}
+4 -3
View File
@@ -294,7 +294,7 @@ class Tmux
/**
* @throws \Exception
*/
public function proc_query(mixed $qry, mixed $bookreqids, string $db_name, ?string $ppmax = '', ?string $ppmin = ''): bool|string
public function proc_query(mixed $qry, string $db_name, ?string $ppmax = '', ?string $ppmin = ''): bool|string
{
// Convert null to empty string for backward compatibility
$ppmax = $ppmax ?? '';
@@ -319,7 +319,7 @@ class Tmux
SUM(IF(categories_id BETWEEN %d AND %d AND '.$movieLookupSql.',1,0)) AS processmovies,
SUM(IF(categories_id IN (%d, %d, %d) AND musicinfo_id IS NULL,1,0)) AS processmusic,
SUM(IF(categories_id BETWEEN %d AND %d AND consoleinfo_id IS NULL,1,0)) AS processconsole,
SUM(IF(categories_id IN (%s) AND bookinfo_id IS NULL,1,0)) AS processbooks,
SUM(IF(categories_id BETWEEN %d AND %d AND bookinfo_id IS NULL,1,0)) AS processbooks,
SUM(IF(categories_id = %d AND gamesinfo_id = 0,1,0)) AS processgames,
SUM(IF(1=1 %s,1,0)) AS processnfo,
SUM(IF(isrenamed = %d AND predb_id = 0 AND passwordstatus >= 0 AND nfostatus > %d
@@ -340,7 +340,8 @@ class Tmux
Category::MUSIC_OTHER,
Category::GAME_ROOT,
Category::GAME_OTHER,
$bookreqids,
Category::BOOKS_ROOT,
Category::BOOKS_UNKNOWN,
Category::PC_GAMES,
NfoService::NfoQueryString(),
NameFixingService::IS_RENAMED_NONE,
+4 -6
View File
@@ -215,10 +215,9 @@ class TmuxMonitorService
$timer = time();
try {
$bookReqIds = $this->runVar['settings']['book_reqids'] ?? Category::BOOKS_ROOT;
$dbName = config('nntmux.db_name');
$proc1Query = $this->tmux->proc_query(1, $bookReqIds, $dbName);
$proc1Query = $this->tmux->proc_query(1, $dbName, '');
$proc1Result = DB::selectOne($proc1Query);
if ($proc1Result) {
@@ -234,7 +233,7 @@ class TmuxMonitorService
$maxSize = $this->runVar['settings']['maxsize_pp'] ?? '';
$minSize = $this->runVar['settings']['minsize_pp'] ?? '';
$proc2Query = $this->tmux->proc_query(2, $bookReqIds, $dbName, (string) $maxSize, (string) $minSize);
$proc2Query = $this->tmux->proc_query(2, $dbName, (string) $maxSize, (string) $minSize);
$proc2Result = DB::selectOne($proc2Query);
if ($proc2Result) {
@@ -285,8 +284,7 @@ class TmuxMonitorService
// Get additional table counts (query 4)
$timer4 = time();
$bookReqIds = $this->runVar['settings']['book_reqids'] ?? Category::BOOKS_ROOT;
$proc4Query = $this->tmux->proc_query(4, $bookReqIds, $dbName);
$proc4Query = $this->tmux->proc_query(4, $dbName, '');
$proc4Result = DB::selectOne($proc4Query);
if ($proc4Result) {
@@ -297,7 +295,7 @@ class TmuxMonitorService
// Get newest/oldest data (query 6)
$timer6 = time();
$proc6Query = $this->tmux->proc_query(6, $bookReqIds, $dbName);
$proc6Query = $this->tmux->proc_query(6, $dbName, '');
$proc6Result = DB::selectOne($proc6Query);
if ($proc6Result) {
+99
View File
@@ -0,0 +1,99 @@
<?php
declare(strict_types=1);
namespace App\Support;
use App\Models\BookInfo;
use App\Support\DTOs\BookParseResult;
class BookMatchScorer
{
/**
* @param array<string, mixed> $candidate
*/
public function score(array $candidate, BookParseResult $parsed): float
{
$candidateIsbn = $this->normalizeIsbn((string) ($candidate['isbn'] ?? ''));
$parsedIsbn = $this->normalizeIsbn((string) ($parsed->isbn ?? ''));
if ($parsedIsbn !== '' && $candidateIsbn !== '' && $parsedIsbn === $candidateIsbn) {
return 1.0;
}
$titleScore = $this->similarity($parsed->title, (string) ($candidate['title'] ?? ''));
$authorScore = $parsed->hasAuthor()
? $this->similarity((string) $parsed->author, (string) ($candidate['author'] ?? ''))
: 0.5;
$yearScore = $this->yearScore($parsed->year, (string) ($candidate['publishdate'] ?? ''));
$publisherScore = ! empty($candidate['publisher']) ? 1.0 : 0.0;
$coverScore = (int) ($candidate['cover'] ?? 0) === 1 ? 1.0 : 0.0;
return (0.4 * $titleScore)
+ (0.3 * $authorScore)
+ (0.1 * $yearScore)
+ (0.1 * $publisherScore)
+ (0.1 * $coverScore);
}
public function scoreBookInfo(BookInfo $book, BookParseResult $parsed): float
{
return $this->score([
'title' => $book->title,
'author' => $book->author,
'isbn' => $book->isbn,
'publishdate' => $book->publishdate,
'publisher' => $book->publisher,
'cover' => $book->cover ? 1 : 0,
], $parsed);
}
private function similarity(string $left, string $right): float
{
$left = $this->normalizeText($left);
$right = $this->normalizeText($right);
if ($left === '' || $right === '') {
return 0.0;
}
similar_text($left, $right, $percent);
return max(0.0, min(1.0, $percent / 100));
}
private function yearScore(?int $parsedYear, string $candidatePublishDate): float
{
if ($parsedYear === null) {
return 0.5;
}
if (preg_match('/\b(19|20)\d{2}\b/', $candidatePublishDate, $matches) !== 1) {
return 0.0;
}
$candidateYear = (int) $matches[0];
if ($parsedYear === $candidateYear) {
return 1.0;
}
if (abs($parsedYear - $candidateYear) <= 1) {
return 0.6;
}
return 0.0;
}
private function normalizeText(string $value): string
{
$value = strtolower($value);
$value = (string) preg_replace('/[._-]+/', ' ', $value);
$value = (string) preg_replace('/[^\p{L}\p{N}\s]+/u', ' ', $value);
$value = (string) preg_replace('/\s+/', ' ', $value);
return trim($value);
}
private function normalizeIsbn(string $value): string
{
return strtoupper((string) preg_replace('/[^0-9X]/i', '', $value));
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace App\Support\DTOs;
final readonly class BookParseResult
{
public function __construct(
public string $rawName,
public string $title,
public ?string $author = null,
public ?string $isbn = null,
public ?int $year = null,
public ?string $format = null,
public bool $isJunk = false,
public bool $isMagazine = false,
) {}
public function hasAuthor(): bool
{
return $this->author !== null && $this->author !== '';
}
public function searchQuery(): string
{
return trim(($this->author ?? '').' '.$this->title);
}
}
+1
View File
@@ -3,6 +3,7 @@
return [
'anidb_api_key' => env('ANIDB_APIKEY', ''),
'fanarttv_api_key' => env('FANARTTV_APIKEY', ''),
'google_books_api_key' => env('GOOGLE_BOOKS_API_KEY', ''),
'isbndb_api_key' => env('ISBNDB_API_KEY', ''),
'imdbapi_dev_enabled' => env('IMDBAPI_DEV_ENABLED', true),
'imdbapi_dev_base_url' => env('IMDBAPI_DEV_BASE_URL', 'https://api.imdbapi.dev'),
-4
View File
@@ -31,10 +31,6 @@ class SettingsTableSeeder extends Seeder
'name' => 'binarythreads',
'value' => '1',
],
6 => [
'name' => 'book_reqids',
'value' => '7010',
],
7 => [
'name' => 'checkpasswordedrar',
'value' => '0',
-14
View File
@@ -326,20 +326,6 @@
<p class="mt-1 text-sm text-gray-500">Whether to attempt to lookup book information from ISBNdb, with iTunes fallback.</p>
</div>
<div>
<label for="book_reqids" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
<i class="fas fa-list mr-1"></i>Type of Books to Look Up
</label>
<select id="book_reqids" name="book_reqids[]" multiple class="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:ring-blue-500 focus:border-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" size="4">
@foreach($book_reqids['ids'] as $index => $bookReqId)
<option value="{{ $bookReqId }}" {{ in_array($bookReqId, $book_reqids['selected'] ?? []) ? 'selected' : '' }}>
{{ $book_reqids['names'][$index] }}
</option>
@endforeach
</select>
<p class="mt-1 text-sm text-gray-500">Categories of Books to lookup information for (only work if Lookup Books is set to yes).</p>
</div>
<div>
<label for="lookupimdb" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
<i class="fas fa-film mr-1"></i>Lookup Movies
+33
View File
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Services;
use App\Services\BookService;
use Tests\TestCase;
class BookServiceTest extends TestCase
{
public function test_parse_release_name_extracts_author_title_and_isbn(): void
{
/** @var BookService $service */
$service = (new \ReflectionClass(BookService::class))->newInstanceWithoutConstructor();
$parsed = $service->parseReleaseName('Eric.Evans - Domain.Driven.Design 978-0321125217 RETAIL EPUB');
$this->assertSame('Domain Driven Design', $parsed->title);
$this->assertSame('Eric Evans', $parsed->author);
$this->assertSame('9780321125217', $parsed->isbn);
}
public function test_parse_release_name_flags_software_as_junk(): void
{
/** @var BookService $service */
$service = (new \ReflectionClass(BookService::class))->newInstanceWithoutConstructor();
$parsed = $service->parseReleaseName('Foxit PDF Editor Pro 14.0.4.33508 Multilingual x64', 'ebook');
$this->assertTrue($parsed->isJunk);
}
}
@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Services;
use App\Services\GoogleBooksService;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Tests\TestCase;
class GoogleBooksServiceTest extends TestCase
{
public function test_search_books_maps_google_books_payload(): void
{
$mock = new MockHandler([
new Response(200, [], json_encode([
'items' => [[
'id' => 'gbook1',
'volumeInfo' => [
'title' => 'The Pragmatic Programmer',
'authors' => ['Andrew Hunt', 'David Thomas'],
'publisher' => 'Addison-Wesley',
'publishedDate' => '1999-10-20',
'pageCount' => 352,
'description' => '<p>Classic software engineering guidance.</p>',
'categories' => ['Computers'],
'industryIdentifiers' => [
['type' => 'ISBN_13', 'identifier' => '9780201616224'],
['type' => 'ISBN_10', 'identifier' => '020161622X'],
],
'imageLinks' => [
'thumbnail' => 'https://example.com/cover.jpg',
],
],
]],
])),
]);
$service = new GoogleBooksService(
new Client(['handler' => HandlerStack::create($mock)]),
null
);
$books = $service->searchBooks('pragmatic programmer');
$this->assertCount(1, $books);
$this->assertSame('The Pragmatic Programmer', $books[0]['title']);
$this->assertSame('Andrew Hunt, David Thomas', $books[0]['author']);
$this->assertSame('9780201616224', $books[0]['isbn']);
$this->assertSame('020161622X', $books[0]['ean']);
$this->assertSame('googlebooks:9780201616224', $books[0]['asin']);
}
public function test_has_api_key_is_false_when_missing(): void
{
$service = new GoogleBooksService(null, '');
$this->assertTrue($service->isConfigured());
$this->assertFalse($service->hasApiKey());
}
}
@@ -50,8 +50,10 @@ class IsbnDbServiceTest extends TestCase
);
$book = $service->searchBook('domain driven design');
$books = $service->searchBooks('domain driven design');
$this->assertNotNull($book);
$this->assertCount(1, $books);
$this->assertSame('Domain-Driven Design', $book['title']);
$this->assertSame('Eric Evans', $book['author']);
$this->assertSame('9780321125217', $book['isbn']);
@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Services;
use App\Services\OpenLibraryService;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Tests\TestCase;
class OpenLibraryServiceTest extends TestCase
{
public function test_search_books_normalizes_docs_payload(): void
{
$mock = new MockHandler([
new Response(200, [], json_encode([
'docs' => [[
'key' => '/works/OL123W',
'title' => 'Refactoring',
'author_name' => ['Martin Fowler'],
'isbn' => ['9780201485677', '0201485672'],
'publisher' => ['Addison-Wesley'],
'first_publish_year' => 1999,
'cover_i' => 54321,
]],
])),
]);
$service = new OpenLibraryService(
new Client(['handler' => HandlerStack::create($mock)])
);
$books = $service->searchBooks('refactoring');
$this->assertCount(1, $books);
$this->assertSame('Refactoring', $books[0]['title']);
$this->assertSame('Martin Fowler', $books[0]['author']);
$this->assertSame('9780201485677', $books[0]['isbn']);
$this->assertSame('0201485672', $books[0]['ean']);
$this->assertSame('openlibrary:9780201485677', $books[0]['asin']);
}
public function test_find_by_isbn_normalizes_payload(): void
{
$mock = new MockHandler([
new Response(200, [], json_encode([
'title' => 'Clean Architecture',
'authors' => [['name' => 'Robert C. Martin']],
'publish_date' => '2017-09-20',
'publishers' => ['Prentice Hall'],
'covers' => [12345],
])),
]);
$service = new OpenLibraryService(
new Client(['handler' => HandlerStack::create($mock)])
);
$book = $service->findByIsbn('9780134494166');
$this->assertNotNull($book);
$this->assertSame('Clean Architecture', $book['title']);
$this->assertSame('Robert C. Martin', $book['author']);
$this->assertSame('9780134494166', $book['isbn']);
}
}
@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Support;
use App\Support\BookMatchScorer;
use App\Support\DTOs\BookParseResult;
use Tests\TestCase;
class BookMatchScorerTest extends TestCase
{
public function test_returns_perfect_score_for_isbn_match(): void
{
$parsed = new BookParseResult(
rawName: 'Domain Driven Design',
title: 'Domain-Driven Design',
author: 'Eric Evans',
isbn: '9780321125217'
);
$score = (new BookMatchScorer)->score([
'title' => 'Some Other Title',
'author' => 'Unknown',
'isbn' => '9780321125217',
'publishdate' => '2020-01-01',
'publisher' => '',
'cover' => 0,
], $parsed);
$this->assertSame(1.0, $score);
}
public function test_higher_score_for_better_title_author_match(): void
{
$parsed = new BookParseResult(
rawName: 'Refactoring',
title: 'Refactoring',
author: 'Martin Fowler',
year: 1999
);
$scorer = new BookMatchScorer;
$good = $scorer->score([
'title' => 'Refactoring: Improving the Design of Existing Code',
'author' => 'Martin Fowler',
'publishdate' => '1999-07-08',
'publisher' => 'Addison-Wesley',
'cover' => 1,
], $parsed);
$bad = $scorer->score([
'title' => 'Cooking for Beginners',
'author' => 'Random Author',
'publishdate' => '2018-01-01',
'publisher' => '',
'cover' => 0,
], $parsed);
$this->assertGreaterThan($bad, $good);
}
}