Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
293a776d16 | ||
|
|
825348ce7a |
@@ -10,10 +10,11 @@ abstract class AbstractBookInfo
|
|||||||
public string $description;
|
public string $description;
|
||||||
public string $title;
|
public string $title;
|
||||||
public string $category;
|
public string $category;
|
||||||
public string $cover_url;
|
public array $cover_url;
|
||||||
public int $pages;
|
public int $pages;
|
||||||
public string $cycle;
|
public string $cycle;
|
||||||
public int $volume;
|
public int $volume;
|
||||||
public string $language;
|
public ?string $language;
|
||||||
public string $datePublished;
|
public string $datePublished;
|
||||||
|
public ?string $publisher;
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,7 @@ use Techtube\Bookinfo\Api\AbstractBookInfo;
|
|||||||
|
|
||||||
class BookFinder
|
class BookFinder
|
||||||
{
|
{
|
||||||
private static $searchUrl = 'https://lubimyczytac.pl/szukaj/ksiazki?phrase=';
|
private static string $searchUrl = 'https://lubimyczytac.pl/szukaj/ksiazki?phrase=';
|
||||||
|
|
||||||
private DataParser $parser;
|
private DataParser $parser;
|
||||||
|
|
||||||
@@ -21,12 +21,12 @@ class BookFinder
|
|||||||
return $this->parser->searchPage(new Document($this->getSearchUrl($phrase), true));
|
return $this->parser->searchPage(new Document($this->getSearchUrl($phrase), true));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function byUrl($url): AbstractBookInfo
|
public function byUrl(string $url): AbstractBookInfo
|
||||||
{
|
{
|
||||||
return $this->parser->singlePage(new Document($url, true));
|
return $this->parser->singlePage(new Document($url, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSearchUrl(string $phrase): string
|
private function getSearchUrl(string $phrase): string
|
||||||
{
|
{
|
||||||
return self::$searchUrl . $phrase;
|
return self::$searchUrl . $phrase;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace Techtube\Bookinfo;
|
|||||||
|
|
||||||
use DiDom\Document;
|
use DiDom\Document;
|
||||||
use DiDom\Exceptions\InvalidSelectorException;
|
use DiDom\Exceptions\InvalidSelectorException;
|
||||||
|
use JetBrains\PhpStorm\ArrayShape;
|
||||||
use Techtube\Bookinfo\Api\AbstractBookInfo;
|
use Techtube\Bookinfo\Api\AbstractBookInfo;
|
||||||
|
|
||||||
class DataParser
|
class DataParser
|
||||||
@@ -17,19 +18,23 @@ class DataParser
|
|||||||
{
|
{
|
||||||
$info = new Info();
|
$info = new Info();
|
||||||
$jsonInfo = json_decode($document->first('script[type="application/ld+json"]')->text());
|
$jsonInfo = json_decode($document->first('script[type="application/ld+json"]')->text());
|
||||||
|
$info->url = $document->getDocument()->baseURI;
|
||||||
|
$info->publisher = $document->first('a[href*="wydawnictwo"]')?->text();
|
||||||
$info->author = $document->first('meta[property="books:author"]')->getAttribute('content');
|
$info->author = $document->first('meta[property="books:author"]')->getAttribute('content');
|
||||||
$info->isbn = $document->first('meta[property="books:isbn"]')->getAttribute('content');
|
$info->isbn = $document->first('meta[property="books:isbn"]')->getAttribute('content');
|
||||||
$info->description = $document->first('meta[property="og:description"]')->getAttribute('content');
|
$info->description = trim($document->first('#book-description p')->text());
|
||||||
$info->title = trim($document->first('h1.book__title')->text());
|
$info->title = trim($document->first('h1.book__title')->text());
|
||||||
$info->category = trim($document->first('.book__category')->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();
|
$info->pages = (int)$document->first('span.book__pages')?->text();
|
||||||
if (preg_match('#(.*) \(tom (\d*)\)#ism', trim($document->first('a[href*="/cykl/"]')?->text()), $series)) {
|
if (preg_match('#(.*) \(tom (\d*)\)#ism', trim($document->first('a[href*="/cykl/"]')?->text() ?? ''), $series)) {
|
||||||
$info->cycle = $series[1];
|
$info->cycle = $series[1];
|
||||||
$info->volume = $series[2];
|
$info->volume = $series[2];
|
||||||
}
|
}
|
||||||
$info->language = $jsonInfo->inLanguage ?? null;
|
$info->language = $jsonInfo?->inLanguage ?? trim($document->xpath("//*[contains(text(), 'Język:')]")[0]->nextSibling('dd')->text());
|
||||||
$info->datePublished = $jsonInfo->datePublished ?? null;
|
$info->datePublished = $jsonInfo?->datePublished ?? null;
|
||||||
|
|
||||||
return $info;
|
return $info;
|
||||||
}
|
}
|
||||||
@@ -49,9 +54,24 @@ class DataParser
|
|||||||
$bookInfo->title = trim($book->first('.authorAllBooks__singleTextTitle')->text());
|
$bookInfo->title = trim($book->first('.authorAllBooks__singleTextTitle')->text());
|
||||||
$bookInfo->author = trim($book->first('.authorAllBooks__singleTextAuthor')->text());
|
$bookInfo->author = trim($book->first('.authorAllBooks__singleTextAuthor')->text());
|
||||||
$bookInfo->url = $book->first('button[data-book-url]')->getAttribute('data-book-url');
|
$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;
|
$booksInfo[] = $bookInfo;
|
||||||
}
|
}
|
||||||
return $booksInfo;
|
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'
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user