Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0aaf337889 | |||
|
|
5f3cbc8b8c | ||
|
|
475c200b67 | ||
|
|
293a776d16 |
50
README.md
50
README.md
@@ -0,0 +1,50 @@
|
||||
## Installation
|
||||
|
||||
Place this in the composer.json.
|
||||
|
||||
```composer
|
||||
{
|
||||
"repositories": [{
|
||||
"type": "composer",
|
||||
"url": "https://satis.techtube.pl"
|
||||
}]
|
||||
}
|
||||
```
|
||||
Then execute to get latest version:
|
||||
|
||||
```bash
|
||||
composer require techtube/bookinfo
|
||||
```
|
||||
|
||||
or this to get specific version:
|
||||
|
||||
```bash
|
||||
composer require techtube/bookinfo:"1.0.4"
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
To get information about the book by the link:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
include_once 'vendor/autoload.php';
|
||||
|
||||
$searcher = new \Techtube\Bookinfo\BookFinder();
|
||||
$data = $searcher->byUrl('https://lubimyczytac.pl/ksiazka/5008411/oni');
|
||||
|
||||
print_r($data);
|
||||
```
|
||||
|
||||
To search the book by phrase:
|
||||
```php
|
||||
<?php
|
||||
|
||||
include_once 'vendor/autoload.php';
|
||||
|
||||
$searcher = new \Techtube\Bookinfo\BookFinder();
|
||||
$data = $searcher->search('jack reacher');
|
||||
|
||||
print_r($data);
|
||||
```
|
||||
@@ -13,8 +13,8 @@
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "kplaczek",
|
||||
"email": "kplaczek@wi.ps.pl"
|
||||
"name": "Krzysztof Płaczek",
|
||||
"email": "krzysztofplaczek@techtube.pl"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -9,11 +9,15 @@ abstract class AbstractBookInfo
|
||||
public string $isbn;
|
||||
public string $description;
|
||||
public string $title;
|
||||
public string $originalTitle;
|
||||
public string $category;
|
||||
public array $cover_url;
|
||||
public int $pages;
|
||||
public string $cycle;
|
||||
public int $volume;
|
||||
public string $language;
|
||||
public string $cycleUrl;
|
||||
public string $volume;
|
||||
public ?string $language;
|
||||
public string $datePublished;
|
||||
public ?string $publisher;
|
||||
public ?string $translator;
|
||||
}
|
||||
@@ -7,7 +7,7 @@ use Techtube\Bookinfo\Api\AbstractBookInfo;
|
||||
|
||||
class BookFinder
|
||||
{
|
||||
private static $searchUrl = 'https://lubimyczytac.pl/szukaj/ksiazki?phrase=';
|
||||
private static string $searchUrl = 'https://lubimyczytac.pl/szukaj/ksiazki?phrase=';
|
||||
|
||||
private DataParser $parser;
|
||||
|
||||
@@ -21,14 +21,14 @@ class BookFinder
|
||||
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));
|
||||
}
|
||||
|
||||
public function getSearchUrl(string $phrase): string
|
||||
private function getSearchUrl(string $phrase): string
|
||||
{
|
||||
return self::$searchUrl . $phrase;
|
||||
return sprintf('%s%s', self::$searchUrl, $phrase);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Techtube\Bookinfo;
|
||||
|
||||
use DiDom\Document;
|
||||
use DiDom\Exceptions\InvalidSelectorException;
|
||||
use DiDom\Query;
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
use Techtube\Bookinfo\Api\AbstractBookInfo;
|
||||
|
||||
@@ -18,21 +19,25 @@ class DataParser
|
||||
{
|
||||
$info = new Info();
|
||||
$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->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->originalTitle = trim($document->first("//*[contains(text(), 'Tytuł oryginału:')]", Query::TYPE_XPATH)?->nextSibling('dd')?->text() ?? '');
|
||||
$info->category = trim($document->first('.book__category')->text());
|
||||
$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)) {
|
||||
if (preg_match('#(.*) \(tom (\d*)\)#ism', trim($document->first('a[href*="/cykl/"]')?->text() ?? ''), $series)) {
|
||||
$info->cycle = $series[1];
|
||||
$info->volume = $series[2];
|
||||
}
|
||||
$info->language = $jsonInfo->inLanguage ?? null;
|
||||
$info->datePublished = $jsonInfo->datePublished ?? null;
|
||||
$info->language = $jsonInfo?->inLanguage ?? trim($document->xpath("//*[contains(text(), 'Język:')]")[0]->nextSibling('dd')->text());
|
||||
$info->datePublished = $jsonInfo?->datePublished ?? null;
|
||||
$info->translator = trim($document->first("//*[contains(text(), 'Tłumacz:')]", Query::TYPE_XPATH)?->nextSibling('dd')?->text() ?? '');
|
||||
|
||||
return $info;
|
||||
}
|
||||
@@ -44,16 +49,22 @@ class DataParser
|
||||
*/
|
||||
public function searchPage(Document $document): array
|
||||
{
|
||||
$books = $document->find('#search .authorAllBooks__single');
|
||||
|
||||
$books = $document->find('#ksiazki .authorAllBooks__single');
|
||||
$booksInfo = [];
|
||||
foreach ($books as $book) {
|
||||
$bookInfo = new Info();
|
||||
$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 = $this->generateCoverUrls($book->first('.img-fluid')->getAttribute('data-src'));
|
||||
$booksInfo[] = $bookInfo;
|
||||
if ($document->has('#searchksiazki')) {
|
||||
foreach ($books as $book) {
|
||||
$bookInfo = new Info();
|
||||
$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 = $this->generateCoverUrls($book->first('.img-fluid')->getAttribute('data-src'));
|
||||
if (preg_match('#(.*) \(tom (.*)\)#ism', trim($book->first('a[href*="/cykl/"]')?->text() ?? ''), $series)) {
|
||||
$bookInfo->cycle = $series[1];
|
||||
$bookInfo->volume = $series[2];
|
||||
$bookInfo->cycleUrl = $book->first('a[href*="/cykl/"]')?->href;
|
||||
}
|
||||
$booksInfo[] = $bookInfo;
|
||||
}
|
||||
}
|
||||
return $booksInfo;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user