6 Commits

Author SHA1 Message Date
Krzysztof Płaczek
171c050368 Add original title 2025-10-03 15:08:45 +02:00
Krzysztof Płaczek
e8081cd898 Update composer.json 2025-10-02 13:01:28 +02:00
Krzysztof Płaczek
8c5ae22081 Add translator 2025-10-02 12:44:04 +02:00
Krzysztof Płaczek
406d6a8f8d Update search to website changes 2025-10-02 12:15:17 +02:00
krzysiej
5f3cbc8b8c Before running parse operation on search results page it checks now if there are any books to parse. 2022-05-30 12:00:42 +02:00
krzysiej
475c200b67 Before running parse operation on search results page it checks now if there are any books to parse. 2022-05-30 11:54:32 +02:00
5 changed files with 75 additions and 13 deletions

View File

@@ -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);
```

View File

@@ -13,8 +13,8 @@
}, },
"authors": [ "authors": [
{ {
"name": "kplaczek", "name": "Krzysztof Płaczek",
"email": "kplaczek@wi.ps.pl" "email": "krzysztofplaczek@techtube.pl"
} }
] ]
} }

View File

@@ -9,12 +9,15 @@ abstract class AbstractBookInfo
public string $isbn; public string $isbn;
public string $description; public string $description;
public string $title; public string $title;
public string $originalTitle;
public string $category; public string $category;
public array $cover_url; public array $cover_url;
public int $pages; public int $pages;
public string $cycle; public string $cycle;
public int $volume; public string $cycleUrl;
public string $volume;
public ?string $language; public ?string $language;
public string $datePublished; public string $datePublished;
public ?string $publisher; public ?string $publisher;
public ?string $translator;
} }

View File

@@ -28,7 +28,7 @@ class BookFinder
private function getSearchUrl(string $phrase): string private function getSearchUrl(string $phrase): string
{ {
return self::$searchUrl . $phrase; return sprintf('%s%s', self::$searchUrl, $phrase);
} }
} }

View File

@@ -4,6 +4,7 @@ namespace Techtube\Bookinfo;
use DiDom\Document; use DiDom\Document;
use DiDom\Exceptions\InvalidSelectorException; use DiDom\Exceptions\InvalidSelectorException;
use DiDom\Query;
use JetBrains\PhpStorm\ArrayShape; use JetBrains\PhpStorm\ArrayShape;
use Techtube\Bookinfo\Api\AbstractBookInfo; use Techtube\Bookinfo\Api\AbstractBookInfo;
@@ -24,6 +25,7 @@ class DataParser
$info->isbn = $document->first('meta[property="books:isbn"]')->getAttribute('content'); $info->isbn = $document->first('meta[property="books:isbn"]')->getAttribute('content');
$info->description = trim($document->first('#book-description p')->text()); $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->originalTitle = trim($document->first("//*[contains(text(), 'Tytuł oryginału:')]", Query::TYPE_XPATH)?->nextSibling('dd')?->text() ?? '');
$info->category = trim($document->first('.book__category')->text()); $info->category = trim($document->first('.book__category')->text());
$info->cover_url = $this->generateCoverUrls( $info->cover_url = $this->generateCoverUrls(
$document->first('meta[property="og:image"]')->getAttribute('content') $document->first('meta[property="og:image"]')->getAttribute('content')
@@ -35,6 +37,7 @@ class DataParser
} }
$info->language = $jsonInfo?->inLanguage ?? trim($document->xpath("//*[contains(text(), 'Język:')]")[0]->nextSibling('dd')->text()); $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;
$info->translator = trim($document->first("//*[contains(text(), 'Tłumacz:')]", Query::TYPE_XPATH)?->nextSibling('dd')?->text() ?? '');
return $info; return $info;
} }
@@ -46,16 +49,22 @@ class DataParser
*/ */
public function searchPage(Document $document): array public function searchPage(Document $document): array
{ {
$books = $document->find('#search .authorAllBooks__single'); $books = $document->find('#ksiazki .authorAllBooks__single');
$booksInfo = []; $booksInfo = [];
foreach ($books as $book) { if ($document->has('#searchksiazki')) {
$bookInfo = new Info(); foreach ($books as $book) {
$bookInfo->title = trim($book->first('.authorAllBooks__singleTextTitle')->text()); $bookInfo = new Info();
$bookInfo->author = trim($book->first('.authorAllBooks__singleTextAuthor')->text()); $bookInfo->title = trim($book->first('.authorAllBooks__singleTextTitle')->text());
$bookInfo->url = $book->first('button[data-book-url]')->getAttribute('data-book-url'); $bookInfo->author = trim($book->first('.authorAllBooks__singleTextAuthor')->text());
$bookInfo->cover_url = $this->generateCoverUrls($book->first('.img-fluid')->getAttribute('data-src')); $bookInfo->url = $book->first('button[data-book-url]')->getAttribute('data-book-url');
$booksInfo[] = $bookInfo; $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; return $booksInfo;
} }