A big mess of packt class.
This commit is contained in:
246
packt.php
246
packt.php
@@ -4,15 +4,7 @@ include 'vendor/autoload.php';
|
|||||||
use DiDom\Document;
|
use DiDom\Document;
|
||||||
|
|
||||||
set_time_limit(-1);
|
set_time_limit(-1);
|
||||||
$settings = include 'settings.php';
|
$credentials = include 'settings.php';
|
||||||
|
|
||||||
$loginData = [
|
|
||||||
'email' => $settings['email'],
|
|
||||||
'password' => $settings['password'],
|
|
||||||
'op' => 'Login',
|
|
||||||
'form_build_id' => 'form-fba4b62ee04aafbf045b1d9ae019d90b',
|
|
||||||
'form_id' => 'packt_user_login_form'
|
|
||||||
];
|
|
||||||
|
|
||||||
|
|
||||||
class Packt
|
class Packt
|
||||||
@@ -21,7 +13,12 @@ class Packt
|
|||||||
|
|
||||||
private $credentials = [];
|
private $credentials = [];
|
||||||
const BASE_URL = 'https://www.packtpub.com/';
|
const BASE_URL = 'https://www.packtpub.com/';
|
||||||
|
const MY_BOOKS_PAGE = self::BASE_URL . 'account/my-ebooks';
|
||||||
|
const LOGIN_PAGE = 'https://www.packtpub.com/mapt-rest/users/tokens';
|
||||||
|
|
||||||
|
// private $accessToken = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJmNjUyODNlYy00OTE1LTQ3ZTktYjM1Mi1mYzkzM2FhOTUxYWIiLCJ1c2VybmFtZSI6ImtyenlzaWVqQGdtYWlsLmNvbSIsInBlcm1pc3Npb25zIjpbXSwiaWF0IjoxNTMyMDcxNjQ0LCJleHAiOjE1MzIwNzUyNDR9.Gp3no55EkmoXJJ4TLk2MwPsix7dzvudiWqJEA4To3W9xkubhFFAtkISJoMO9AHLuQJITns-zMAjg1VBnnQSp3G6U7lzi7PDzM_CThuZJHKy6jBA_HtBnHJ-GoAK6hVxqifooTECmXIAignMz8sEbThHAUqY75djXuDYnaAlJaBvzwlt-mRkbsvRjGHr379LsvoezPeYCgJtme1J_xPonjaJZJ9nP04QfiwbnUSEiGb71Fvixc1Vo3F8QO11fHgI_fXLZSidgB4WGkz_m9AvjmqOXc02fbxf-WgfPe8Oj-qndLoq6ZAWIy86XjAx8inFHWrPbvyoIdLRSty1a9aEDkQ';
|
||||||
|
private $accessToken = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJmNjUyODNlYy00OTE1LTQ3ZTktYjM1Mi1mYzkzM2FhOTUxYWIiLCJ1c2VybmFtZSI6ImtyenlzaWVqQGdtYWlsLmNvbSIsInBlcm1pc3Npb25zIjpbXSwiaWF0IjoxNTMyMzI5MDk4LCJleHAiOjE1MzIzMzI2OTh9.uUyYz2uMT3mZ77-Uc2yoVH4y5qqI_WfpXB9kKw81qhUKRuonD6jhLZKbCAQSObkKyEJz1bYB0cQVfwsKZiwAuY58VrCQ0ykQUPrQh3MfAEJ5zpE3CquAf5o892ToLbIwGieC05ElHXsMUCfgimA6ECtHzIT9dC4l93FYaoDpsnyZFA9ikxUJIAKI51rOS879J_B98UPdw-NOlsCl1-2CcTnjf5He4CQuYWYvnj0NpV0_GP3w5rnNy8989prqks_m7WKFMJXzhH9OlexKVgF3J4IS3216yEJrL8ViQ_gkAZBRRuKJYeYUiAJ4H7Hy_IohN90-SpaH4jvIt81SApVc2A';
|
||||||
|
// private $accessToken;
|
||||||
private $database = null;
|
private $database = null;
|
||||||
|
|
||||||
public function __construct($credentials)
|
public function __construct($credentials)
|
||||||
@@ -50,43 +47,43 @@ class Packt
|
|||||||
return str_replace('https://www.packtpub.com', '', $bookUrl);
|
return str_replace('https://www.packtpub.com', '', $bookUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getBookInfo($bookUrl)
|
// public function getBookInfo($bookUrl)
|
||||||
{
|
// {
|
||||||
|
//
|
||||||
$bookUrl = resolveBookUrl($bookUrl);
|
// $bookUrl = resolveBookUrl($bookUrl);
|
||||||
$bookData = [];
|
// $bookData = [];
|
||||||
$bookPage = new Document($bookUrl, true);
|
// $bookPage = new Document($bookUrl, true);
|
||||||
$bookData['datepublished'] = $bookPage->first('.book-top-block-info-authors time[itemprop="datePublished"]::attr(datetime)');
|
// $bookData['datepublished'] = $bookPage->first('.book-top-block-info-authors time[itemprop="datePublished"]::attr(datetime)');
|
||||||
$bookData['numberofpages'] = $bookPage->first('span[itemprop="numberOfPages"]::text');
|
// $bookData['numberofpages'] = $bookPage->first('span[itemprop="numberOfPages"]::text');
|
||||||
$bookData['isbn'] = $bookPage->first('span[itemprop="isbn"]::text');
|
// $bookData['isbn'] = $bookPage->first('span[itemprop="isbn"]::text');
|
||||||
$bookData['reviewCount'] = $bookPage->first('meta[itemprop="reviewCount"]::attr(content)');
|
// $bookData['reviewCount'] = $bookPage->first('meta[itemprop="reviewCount"]::attr(content)');
|
||||||
$bookData['ratingValue'] = $bookPage->first('meta[itemprop="ratingValue"]::attr(content)');
|
// $bookData['ratingValue'] = $bookPage->first('meta[itemprop="ratingValue"]::attr(content)');
|
||||||
|
//
|
||||||
$bookData['toc'] = [];
|
// $bookData['toc'] = [];
|
||||||
foreach ($bookPage->find('#book-info-toc.onlyDesktop .book-toc-chapter') as $chapter) {
|
// foreach ($bookPage->find('#book-info-toc.onlyDesktop .book-toc-chapter') as $chapter) {
|
||||||
if (!is_null($chapter->first('div[class*="book-toc-chapter-title"]'))) {
|
// if (!is_null($chapter->first('div[class*="book-toc-chapter-title"]'))) {
|
||||||
$tocSection = [];
|
// $tocSection = [];
|
||||||
$tocSection['title'] = trim($chapter->first('div[class*="book-toc-chapter-title"]')->text());
|
// $tocSection['title'] = trim($chapter->first('div[class*="book-toc-chapter-title"]')->text());
|
||||||
$tocSection['subchapters'] = $chapter->find('div[class*="book-toc-section-text"]::text');
|
// $tocSection['subchapters'] = $chapter->find('div[class*="book-toc-section-text"]::text');
|
||||||
$bookData['toc'][] = $tocSection;
|
// $bookData['toc'][] = $tocSection;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
$bookData['description'] = $bookPage->find('div.book-info-bottom-indetail-text[itemprop="description"] p::text');
|
// $bookData['description'] = implode(' ', $bookPage->find('div.book-info-bottom-indetail-text[itemprop="description"] p::text'));
|
||||||
$bookData['willLearn'] = $bookPage->find('div.book-info-will-learn-text li::text');
|
// $bookData['willLearn'] = $bookPage->find('div.book-info-will-learn-text li::text');
|
||||||
|
//
|
||||||
$bookData['category'] = $bookPage->first('div[data-product-id="' . $bookData['isbn'] . '"]::attr(data-product-category)');
|
// $bookData['category'] = $bookPage->first('div[data-product-id="' . $bookData['isbn'] . '"]::attr(data-product-category)');
|
||||||
|
//
|
||||||
$bookData['authors'] = [];
|
// $bookData['authors'] = [];
|
||||||
foreach ($bookPage->find('[itemprop="author"]') as $author) {
|
// foreach ($bookPage->find('[itemprop="author"]') as $author) {
|
||||||
|
//
|
||||||
$authorData = [];
|
// $authorData = [];
|
||||||
$authorData['name'] = trim($author->first('.book-info-bottom-author-title')->text());
|
// $authorData['name'] = trim($author->first('.book-info-bottom-author-title')->text());
|
||||||
$authorData['bio'] = preg_replace('#\s{2,}#', ' ', trim($author->first('.book-info-bottom-author-body')->text()));
|
// $authorData['bio'] = preg_replace('#\s{2,}#', ' ', trim($author->first('.book-info-bottom-author-body')->text()));
|
||||||
$bookData['authors'][] = $authorData;
|
// $bookData['authors'][] = $authorData;
|
||||||
}
|
// }
|
||||||
return $bookData;
|
// return $bookData;
|
||||||
}
|
// }
|
||||||
|
|
||||||
public function saveBookChapters($data, $bookId)
|
public function saveBookChapters($data, $bookId)
|
||||||
{
|
{
|
||||||
@@ -151,12 +148,36 @@ class Packt
|
|||||||
return 'books' . DIRECTORY_SEPARATOR . $filePathParts[1] . DIRECTORY_SEPARATOR . $filePathParts[1] . '.' . (isset($filePathParts[2]) ? $filePathParts[2] : 'zip');
|
return 'books' . DIRECTORY_SEPARATOR . $filePathParts[1] . DIRECTORY_SEPARATOR . $filePathParts[1] . '.' . (isset($filePathParts[2]) ? $filePathParts[2] : 'zip');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getBooks()
|
|
||||||
|
/**
|
||||||
|
* funkcja której zadaniem jest logowanie uzytkownika na stronie
|
||||||
|
*/
|
||||||
|
private function login()
|
||||||
|
{
|
||||||
|
if (is_null($this->accessToken)) {
|
||||||
|
$loginData = c(self::LOGIN_PAGE, json_encode($this->credentials));
|
||||||
|
$loginJson = json_decode($loginData);
|
||||||
|
print_r($loginJson);
|
||||||
|
if (isset($loginJson->httpStatus, $loginJson->status) && $loginJson->httpStatus == 200 && $loginJson->status == 'success') {
|
||||||
|
$this->accessToken = $loginJson->data->access;
|
||||||
|
} else {
|
||||||
|
throw new Exception($loginJson->message, $loginJson->httpStatus);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $page
|
||||||
|
* @return Generator
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function getBooks(int $page = 0)
|
||||||
{
|
{
|
||||||
//logowanie
|
//logowanie
|
||||||
c('https://www.packtpub.com/', $this->credentials);
|
$this->login();
|
||||||
//pobieranie listy ksiązek
|
//pobieranie listy ksiązek
|
||||||
$return = c('https://www.packtpub.com/account/my-ebooks');
|
$return = $this->c('https://www.packtpub.com/account/my-ebooks?page=' . $page);
|
||||||
$document = new Document($return);
|
$document = new Document($return);
|
||||||
|
|
||||||
$books = $document->find('.product-line.unseen');
|
$books = $document->find('.product-line.unseen');
|
||||||
@@ -175,6 +196,30 @@ class Packt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getAllBooks()
|
||||||
|
{
|
||||||
|
|
||||||
|
$this->login();
|
||||||
|
|
||||||
|
$data = $this->c(self::BASE_URL . 'mapt-rest/users/me/purchases?limit=100&offset=0&order=DESC&sort=purchase_date');
|
||||||
|
$jsonData = json_decode($data);
|
||||||
|
if (isset($jsonData->httpStatus, $loginJson->status) && $jsonData->httpStatus == 200 && $jsonData->status == 'success'){
|
||||||
|
return $jsonData['data'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* zwraca ilość stron książek
|
||||||
|
*/
|
||||||
|
public function getNumberOfPages()
|
||||||
|
{
|
||||||
|
$this->login();
|
||||||
|
$return = $this->c(self::MY_BOOKS_PAGE);
|
||||||
|
$document = new Document($return);
|
||||||
|
return (int)$document->first('.solr-pager-page-selector a:last-child')->text();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $nid
|
* @param int $nid
|
||||||
* @return bool
|
* @return bool
|
||||||
@@ -187,13 +232,60 @@ class Packt
|
|||||||
return $result->fetchArray(SQLITE3_ASSOC) === false ? false : true;
|
return $result->fetchArray(SQLITE3_ASSOC) === false ? false : true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $nid
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getBookByNid(int $nid)
|
||||||
|
{
|
||||||
|
$stmt = $this->getDatabase()->prepare('SELECT * FROM book WHERE nid = :nid');
|
||||||
|
$stmt->bindValue(':nid', $nid, SQLITE3_INTEGER);
|
||||||
|
$result = $stmt->execute();
|
||||||
|
return $result->fetchArray(SQLITE3_ASSOC);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function c($url, $post = [], $localFilePath = false)
|
||||||
|
{
|
||||||
|
$cookie = "cookie.txt";
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
|
||||||
|
|
||||||
|
if (!is_null($this->accessToken)) {
|
||||||
|
$authorization = "Authorization: Bearer " . $this->accessToken;
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (!empty($post) && count($post)) {
|
||||||
|
curl_setopt($ch, CURLOPT_POST, 1);
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($localFilePath) {
|
||||||
|
$fp = fopen($localFilePath, 'w+');
|
||||||
|
curl_setopt($ch, CURLOPT_FILE, $fp);
|
||||||
|
} else {
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
||||||
|
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
|
||||||
|
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
|
||||||
|
|
||||||
|
$server_output = curl_exec($ch);
|
||||||
|
curl_close($ch);
|
||||||
|
return $server_output;
|
||||||
|
}
|
||||||
|
|
||||||
public function saveBooks()
|
public function saveBooks()
|
||||||
{
|
{
|
||||||
foreach ($this->getBooks() as $basicBookInfo) {
|
foreach ($this->getBooks() as $basicBookInfo) {
|
||||||
if ($this->isBookByNid($basicBookInfo['nid'])) {
|
if ($this->isBookByNid($basicBookInfo['nid'])) {
|
||||||
$stmt = $this->getDatabase()->prepare('UPDATE book SET nid = :nid, title = :title, ebook_isbn = :ebook_isbn, img = :img, url = :url, pdf = :pdf, epub = :epub, mobi = :mobi, code = :code, upd_dt = (DATETIME(\'now\')) WHERE nid = :nid');
|
$stmt = $this->getDatabase()->prepare('UPDATE book SET nid = :nid, title = :title, ebook_isbn = :ebook_isbn, img = :img, url = :url, pdf = :pdf, epub = :epub, mobi = :mobi, code = :code, upd_dt = (DATETIME(\'now\')) WHERE nid = :nid');
|
||||||
} else {
|
} else {
|
||||||
$stmt = $this->getDatabase()->prepare('INSERT INTO book (nid, title, isbn, img, url, pdf, epub, mobi, code)
|
$stmt = $this->getDatabase()->prepare('INSERT INTO book (nid, title, ebook_isbn, img, url, pdf, epub, mobi, code)
|
||||||
VALUES (:nid, :title, :ebook_isbn, :img, :url, :pdf, :epub, :mobi, :code)');
|
VALUES (:nid, :title, :ebook_isbn, :img, :url, :pdf, :epub, :mobi, :code)');
|
||||||
}
|
}
|
||||||
$stmt->bindValue(':nid', $basicBookInfo['nid'], SQLITE3_INTEGER);
|
$stmt->bindValue(':nid', $basicBookInfo['nid'], SQLITE3_INTEGER);
|
||||||
@@ -213,7 +305,7 @@ VALUES (:nid, :title, :ebook_isbn, :img, :url, :pdf, :epub, :mobi, :code)');
|
|||||||
{
|
{
|
||||||
print_r($bookDetails['nid']);
|
print_r($bookDetails['nid']);
|
||||||
if ($this->isBookByNid($bookDetails['nid'])) {
|
if ($this->isBookByNid($bookDetails['nid'])) {
|
||||||
print_r($bookDetails);
|
print_r($bookDetails);
|
||||||
|
|
||||||
echo 'xxx';
|
echo 'xxx';
|
||||||
|
|
||||||
@@ -254,13 +346,10 @@ VALUES (:nid, :title, :ebook_isbn, :img, :url, :pdf, :epub, :mobi, :code)');
|
|||||||
}
|
}
|
||||||
|
|
||||||
$bookData['title'] = $bookPage->first('h1[itemprop="name"]::text');
|
$bookData['title'] = $bookPage->first('h1[itemprop="name"]::text');
|
||||||
$bookData['description'] = $bookPage->find('div.book-info-bottom-indetail-text[itemprop="description"] p::text');
|
$bookData['description'] = implode(' ', $bookPage->find('div.book-info-bottom-indetail-text[itemprop="description"] p::text'));
|
||||||
$bookData['willLearn'] = $bookPage->find('div.book-info-will-learn-text li::text');
|
$bookData['willLearn'] = $bookPage->find('div.book-info-will-learn-text li::text');
|
||||||
|
|
||||||
$bookData['category'] = $bookPage->first('div[data-product-id="' . $bookData['paper_isbn'] . '"]::attr(data-product-category)');
|
$bookData['category'] = $bookPage->first('div[data-product-id="' . $bookData['paper_isbn'] . '"]::attr(data-product-category)');
|
||||||
// $bookData['price'] = $bookPage->first('div[data-product-id="' . $bookData['isbn'] . '"]::attr(data-product-price)');
|
|
||||||
// $bookData['price'] = $bookPage->first('div[data-product-id="' . $bookData['paper_isbn'] . '"][data-product-type="ebooks"]');
|
|
||||||
// $bookData['nid'] = $bookPage->first('div[data-product-id="' . $bookData['paper_isbn'] . '"]::attr(data-product-nid)');
|
|
||||||
$bookData['nid'] = $bookPage->first('div.digital-product[data-product-type="ebooks"]::attr(data-product-nid)');
|
$bookData['nid'] = $bookPage->first('div.digital-product[data-product-type="ebooks"]::attr(data-product-nid)');
|
||||||
|
|
||||||
$bookData['authors'] = [];
|
$bookData['authors'] = [];
|
||||||
@@ -274,34 +363,63 @@ VALUES (:nid, :title, :ebook_isbn, :img, :url, :pdf, :epub, :mobi, :code)');
|
|||||||
return $bookData;
|
return $bookData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getRandomDatabaseBook()
|
||||||
|
{
|
||||||
|
$stmt = $this->getDatabase()->prepare('SELECT * FROM book ORDER BY RANDOM() LIMIT 1');
|
||||||
|
$result = $stmt->execute();
|
||||||
|
return $result->fetchArray(SQLITE3_ASSOC);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
echo '<pre>';
|
echo '<pre>';
|
||||||
|
|
||||||
$packt = new Packt($loginData);
|
$packt = new Packt($credentials);
|
||||||
|
|
||||||
|
|
||||||
//$books = $packt->saveBooks();
|
//var_dump($packt->getNumberOfPages());
|
||||||
//print_r($books);
|
|
||||||
|
|
||||||
|
$packt->getAllBooks();
|
||||||
|
die();
|
||||||
|
|
||||||
|
$books = $packt->getBooks();
|
||||||
|
print_r(iterator_to_array($books));
|
||||||
|
die();
|
||||||
|
|
||||||
echo '<pre>';
|
echo '<pre>';
|
||||||
$bookDetails = $packt->getBookDetails('/web-development/dependency-injection-angularjs');
|
|
||||||
|
//$getRandomDatabaseBook = $packt->getRandomDatabaseBook();
|
||||||
|
|
||||||
|
//print_r($getRandomDatabaseBook);
|
||||||
|
|
||||||
|
//if(!empty($getRandomDatabaseBook['url'])){
|
||||||
|
$bookDetails = $packt->getBookDetails("/networking-and-servers/windows-server-2016-automation-powershell-cookbook-second-edition");
|
||||||
print_r($bookDetails);
|
print_r($bookDetails);
|
||||||
//$packt->saveBookDetails($bookDetails);
|
$packt->saveBookDetails($bookDetails);
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
//print_r($packt->getBookByNid($bookDetails['nid']));
|
||||||
|
|
||||||
|
|
||||||
die();
|
die();
|
||||||
|
|
||||||
function c($url, $post = [], $localFilePath = null)
|
$bookDetails = $packt->getBookDetails('networking-and-servers/windows-server-2016-automation-powershell-cookbook-second-edition');
|
||||||
|
print_r($bookDetails);
|
||||||
|
$packt->saveBookDetails($bookDetails);
|
||||||
|
|
||||||
|
|
||||||
|
die();
|
||||||
|
|
||||||
|
function c($url, $post = '', $localFilePath = null)
|
||||||
{
|
{
|
||||||
$cookie = "cookie.txt";
|
$cookie = "cookie.txt";
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
curl_setopt($ch, CURLOPT_URL, $url);
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
|
||||||
if (!empty($post) && count($post)) {
|
if (!empty($post)) {
|
||||||
curl_setopt($ch, CURLOPT_POST, 1);
|
curl_setopt($ch, CURLOPT_POST, 1);
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -356,7 +474,7 @@ function getBookInfo($bookUrl)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$bookData['description'] = $bookPage->find('div.book-info-bottom-indetail-text[itemprop="description"] p::text');
|
$bookData['description'] = implode(' ', $bookPage->find('div.book-info-bottom-indetail-text[itemprop="description"] p::text'));
|
||||||
$bookData['willLearn'] = $bookPage->find('div.book-info-will-learn-text li::text');
|
$bookData['willLearn'] = $bookPage->find('div.book-info-will-learn-text li::text');
|
||||||
|
|
||||||
$bookData['category'] = $bookPage->first('div[data-product-id="' . $bookData['isbn'] . '"]::attr(data-product-category)');
|
$bookData['category'] = $bookPage->first('div[data-product-id="' . $bookData['isbn'] . '"]::attr(data-product-category)');
|
||||||
|
|||||||
Reference in New Issue
Block a user