Added three sizes of book cover small medium and large to be returned as cover_url.

This commit is contained in:
krzysiej
2022-05-26 13:02:52 +02:00
parent 0ce3fe1b0a
commit 825348ce7a
2 changed files with 21 additions and 3 deletions

View File

@@ -10,7 +10,7 @@ abstract class AbstractBookInfo
public string $description;
public string $title;
public string $category;
public string $cover_url;
public array $cover_url;
public int $pages;
public string $cycle;
public int $volume;

View File

@@ -4,6 +4,7 @@ namespace Techtube\Bookinfo;
use DiDom\Document;
use DiDom\Exceptions\InvalidSelectorException;
use JetBrains\PhpStorm\ArrayShape;
use Techtube\Bookinfo\Api\AbstractBookInfo;
class DataParser
@@ -22,7 +23,9 @@ class DataParser
$info->description = $document->first('meta[property="og:description"]')->getAttribute('content');
$info->title = trim($document->first('h1.book__title')->text());
$info->category = trim($document->first('.book__category')->text());
$info->cover_url = $document->first('meta[property="og:image"]')->getAttribute('content');
$info->cover_url = $this->generateCoverUrls(
$document->first('meta[property="og:image"]')->getAttribute('content')
);
$info->pages = (int)$document->first('span.book__pages')?->text();
if (preg_match('#(.*) \(tom (\d*)\)#ism', trim($document->first('a[href*="/cykl/"]')?->text()), $series)) {
$info->cycle = $series[1];
@@ -49,9 +52,24 @@ class DataParser
$bookInfo->title = trim($book->first('.authorAllBooks__singleTextTitle')->text());
$bookInfo->author = trim($book->first('.authorAllBooks__singleTextAuthor')->text());
$bookInfo->url = $book->first('button[data-book-url]')->getAttribute('data-book-url');
$bookInfo->cover_url = $book->first('.img-fluid')->getAttribute('data-src');
$bookInfo->cover_url = $this->generateCoverUrls($book->first('.img-fluid')->getAttribute('data-src'));
$booksInfo[] = $bookInfo;
}
return $booksInfo;
}
/**
* @param string $coverUrl
* @return array
*/
#[ArrayShape(['small' => "string", 'medium' => "string", 'large' => "string"])]
private function generateCoverUrls(string $coverUrl): array
{
$coverUrlBase = preg_replace('(\d*?x\d*?\.jpg)', '', $coverUrl);
return [
'small' => $coverUrlBase . '70x100.jpg',
'medium' => $coverUrlBase . '170x243.jpg',
'large' => $coverUrlBase . '352x500.jpg'
];
}
}