Informations abut ToC, star and unstar book.

This commit is contained in:
krzysiej
2018-07-03 10:38:29 +02:00
parent 347451dd98
commit 42beb193e4
3 changed files with 361 additions and 100 deletions

265
packt.php
View File

@@ -46,7 +46,6 @@ function c($url, $post = [], $localFilePath = null)
return $server_output;
}
function resolveBookUrl($bookUrl)
{
return 'https://www.packtpub.com/' . trim(str_replace('https://www.packtpub.com/', '', $bookUrl), '/');
@@ -96,122 +95,210 @@ function getBookInfo($bookUrl)
}
function saveBookChapters($data, $bookId)
{
$db = new SQLite3('data.db');
print_r($data);
foreach ($data['toc'] as $chapterNumber => $chapter) {
$stmt = $db->prepare('REPLACE INTO chapter (book_id, chapter_number, parent_id, name)
VALUES (:book_id, :chapter_number, :parent_id, :name)');
$stmt->bindValue(':book_id', $bookId, SQLITE3_INTEGER);
$stmt->bindValue(':chapter_number', $chapterNumber + 1, SQLITE3_INTEGER);
$stmt->bindValue(':parent_id', null, SQLITE3_INTEGER);
$stmt->bindValue(':name', $chapter['title'], SQLITE3_TEXT);
$result = $stmt->execute();
$stmt = $db->prepare('select seq from sqlite_sequence where name="chapter"');
$result = $stmt->execute();
$lastInsertedId = $result->fetchArray(SQLITE3_ASSOC);
$lastInsertedId['seq'];
if (isset($chapter['subchapters'])) {
foreach ($chapter['subchapters'] as $subChapterNumber => $subChapterName) {
$stmt = $db->prepare('REPLACE INTO chapter (book_id, chapter_number, parent_id, name)
VALUES (:book_id, :chapter_number, :parent_id, :name)');
$stmt->bindValue(':book_id', $bookId, SQLITE3_INTEGER);
$stmt->bindValue(':chapter_number', $subChapterNumber + 1, SQLITE3_INTEGER);
$stmt->bindValue(':parent_id', $lastInsertedId['seq'], SQLITE3_INTEGER);
$stmt->bindValue(':name', $subChapterName, SQLITE3_TEXT);
$result = $stmt->execute();
}
}
}
}
echo '<pre>';
//$data = getBookInfo('big-data-and-business-intelligence/mastering-blockchain');
//file_put_contents('data.txt', json_encode($data));
//die();
$db = new SQLite3('data.db');
//$stmt = $db->prepare('SELECT * FROM book ORDER BY random() limit 1 ');
$stmt = $db->prepare('select * from book');
$booksResult = $stmt->execute();
while ($book = $booksResult->fetchArray(SQLITE3_ASSOC)) {
$data = getBookInfo($book['url']);
// saveBookChapters($data, $book['id']);
// $db = new SQLite3('data.db');
// print_r($data);
foreach ($data['toc'] as $chapterNumber => $chapter) {
$stmt = $db->prepare('REPLACE INTO chapter (book_id, chapter_number, parent_id, name)
VALUES (:book_id, :chapter_number, :parent_id, :name)');
$stmt->bindValue(':book_id', $book['id'], SQLITE3_INTEGER);
$stmt->bindValue(':chapter_number', $chapterNumber + 1, SQLITE3_INTEGER);
$stmt->bindValue(':parent_id', null, SQLITE3_INTEGER);
$stmt->bindValue(':name', $chapter['title'], SQLITE3_TEXT);
$result = $stmt->execute();
$stmt = $db->prepare('select seq from sqlite_sequence where name="chapter"');
$result = $stmt->execute();
$lastInsertedId = $result->fetchArray(SQLITE3_ASSOC);
$lastInsertedId['seq'];
if (isset($chapter['subchapters'])) {
foreach ($chapter['subchapters'] as $subChapterNumber => $subChapterName) {
$stmt = $db->prepare('REPLACE INTO chapter (book_id, chapter_number, parent_id, name)
VALUES (:book_id, :chapter_number, :parent_id, :name)');
$stmt->bindValue(':book_id', $book['id'], SQLITE3_INTEGER);
$stmt->bindValue(':chapter_number', $subChapterNumber + 1, SQLITE3_INTEGER);
$stmt->bindValue(':parent_id', $lastInsertedId['seq'], SQLITE3_INTEGER);
$stmt->bindValue(':name', $subChapterName, SQLITE3_TEXT);
$result = $stmt->execute();
}
}
}
}
//$data = json_decode(file_get_contents('data.txt'), 1);
//$bookId = 1;
//print_r($data);
function fetchAuthorsByBooks()
{
$db = new SQLite3('data.db');
//$stmt = $db->prepare('SELECT * FROM book order by random() limit 1 ');
$stmt = $db->prepare('select * from book');
$stmt = $db->prepare('select * from book left join author_book ON book.id = author_book.book_id where author_book.book_id is null and category is not null');
$booksResult = $stmt->execute();
$stmt = $db->prepare('select * from book');
$booksResult = $stmt->execute();
//$bookData = $result->fetchArray(SQLITE3_ASSOC);
echo '<pre>';
while ($book = $booksResult->fetchArray(SQLITE3_ASSOC)) {
$bookInfo = getBookInfo($book['url']);
foreach ($bookInfo['authors'] as $author) {
$stmt = $db->prepare('SELECT * FROM author WHERE name = :name and bio = :bio');
$stmt->bindValue(':name', trim($author['name']), SQLITE3_TEXT);
$stmt->bindValue(':bio', $author['bio'], SQLITE3_TEXT);
$result = $stmt->execute();
$authorData = $result->fetchArray(SQLITE3_ASSOC);
if (!$authorData) {
$stmt = $db->prepare('INSERT INTO author(name, bio) VALUES (:name, :bio)');
while ($book = $booksResult->fetchArray(SQLITE3_ASSOC)) {
$bookInfo = getBookInfo($book['url']);
foreach ($bookInfo['authors'] as $author) {
$stmt = $db->prepare('SELECT * FROM author WHERE name = :name and bio = :bio');
$stmt->bindValue(':name', trim($author['name']), SQLITE3_TEXT);
$stmt->bindValue(':bio', $author['bio'], SQLITE3_TEXT);
$result = $stmt->execute();
$authorData = $result->fetchArray(SQLITE3_ASSOC);
$stmt = $db->prepare('select seq from sqlite_sequence where name="author"');
$result = $stmt->execute();
$seqData = $result->fetchArray(SQLITE3_ASSOC);
if (!$authorData) {
$stmt = $db->prepare('INSERT INTO author(name, bio) VALUES (:name, :bio)');
$stmt->bindValue(':name', trim($author['name']), SQLITE3_TEXT);
$stmt->bindValue(':bio', $author['bio'], SQLITE3_TEXT);
$result = $stmt->execute();
$stmt = $db->prepare('INSERT INTO author_book(author_id, book_id) VALUES (:author_id, :book_id)');
$stmt->bindValue(':author_id', $seqData['seq'], SQLITE3_INTEGER);
$stmt->bindValue(':book_id', $book['id'], SQLITE3_INTEGER);
$result = $stmt->execute();
} else {
$stmt = $db->prepare('INSERT INTO author_book(author_id, book_id) VALUES (:author_id, :book_id)');
$stmt->bindValue(':author_id', $authorData['id'], SQLITE3_INTEGER);
$stmt->bindValue(':book_id', $book['id'], SQLITE3_INTEGER);
$result = $stmt->execute();
$stmt = $db->prepare('select seq from sqlite_sequence where name="author"');
$result = $stmt->execute();
$seqData = $result->fetchArray(SQLITE3_ASSOC);
$stmt = $db->prepare('INSERT INTO author_book(author_id, book_id) VALUES (:author_id, :book_id)');
$stmt->bindValue(':author_id', $seqData['seq'], SQLITE3_INTEGER);
$stmt->bindValue(':book_id', $book['id'], SQLITE3_INTEGER);
$result = $stmt->execute();
} else {
$stmt = $db->prepare('INSERT INTO author_book(author_id, book_id) VALUES (:author_id, :book_id)');
$stmt->bindValue(':author_id', $authorData['id'], SQLITE3_INTEGER);
$stmt->bindValue(':book_id', $book['id'], SQLITE3_INTEGER);
$result = $stmt->execute();
}
}
var_dump($authorData);
}
}
//print_r($bookData);
//print_r($bookInfo);
function downloadBooks()
{
$return = c('https://www.packtpub.com/', $loginData);
$return = c('https://www.packtpub.com/account/my-ebooks');
$document = new Document($return);
global $loginData;
$return = c('https://www.packtpub.com/', $loginData);
$return = c('https://www.packtpub.com/account/my-ebooks');
$document = new Document($return);
//
//$document = new Document('packt.html', true);
$booksData = [];
$books = $document->find('.product-line.unseen');
$booksData = [];
$books = $document->find('.product-line.unseen');
$dl = 0;
$db = new SQLite3('data.db');
$dl = 0;
$db = new SQLite3('data.db');
//shuffle($books);
foreach ($books as $book) {
foreach ($books as $book) {
$bookData = [];
$bookData['title'] = str_replace(["\r\n"], '', trim($book->first('.title::text')));
$bookData['nid'] = (int)$book->attr('nid');
$bookData = [];
$bookData['title'] = str_replace(["\r\n"], '', trim($book->first('.title::text')));
$bookData['nid'] = (int)$book->attr('nid');
$bookData['pdf'] = truncateBookUrl($book->first('a[href$=pdf]::attr(href)'));
$bookData['epub'] = truncateBookUrl($book->first('a[href$=epub]::attr(href)'));
$bookData['mobi'] = truncateBookUrl($book->first('a[href$=mobi]::attr(href)'));
$bookData['code'] = truncateBookUrl($book->first('a[href*=code_download]::attr(href)'));
$bookData['isbn'] = $book->first('div[isbn]::attr(isbn)');
$bookData['img'] = $book->first('img[class*=imagecache]::attr(src)');
$bookData['url'] = truncateBookUrl($book->first('div[class*=product-thumbnail]')->first('a::attr(href)'));
$bookData['pdf'] = truncateBookUrl($book->first('a[href$=pdf]::attr(href)'));
$bookData['epub'] = truncateBookUrl($book->first('a[href$=epub]::attr(href)'));
$bookData['mobi'] = truncateBookUrl($book->first('a[href$=mobi]::attr(href)'));
$bookData['code'] = truncateBookUrl($book->first('a[href*=code_download]::attr(href)'));
$bookData['isbn'] = $book->first('div[isbn]::attr(isbn)');
$bookData['img'] = $book->first('img[class*=imagecache]::attr(src)');
$bookData['url'] = truncateBookUrl($book->first('div[class*=product-thumbnail]')->first('a::attr(href)'));
$stmt = $db->prepare('SELECT id FROM book WHERE nid = :nid');
$stmt->bindValue(':nid', $bookData['nid'], SQLITE3_INTEGER);
$result = $stmt->execute();
$resultData = $result->fetchArray(SQLITE3_ASSOC);
$dl += downloadBook($bookData['pdf']);
$dl += downloadBook($bookData['epub']);
$dl += downloadBook($bookData['mobi']);
$dl += downloadBook($bookData['code']);
if (!$resultData) {
$bookData['info'] = getBookInfo($bookData['url']);
$stmt = $db->prepare('REPLACE INTO book (nid, title, isbn, img, url, datepublished, numberofpages, reviewCount, ratingValue, category, pdf, epub, mobi, code)
VALUES (:nid, :title, :isbn, :img, :url, :datepublished, :numberofpages, :reviewCount, :ratingValue, :category, :pdf, :epub, :mobi, :code)');
$stmt = $db->prepare('SELECT id FROM book WHERE nid = :nid');
$stmt->bindValue(':nid', $bookData['nid'], SQLITE3_INTEGER);
$stmt->bindValue(':title', $bookData['title'], SQLITE3_TEXT);
$stmt->bindValue(':isbn', $bookData['isbn'], SQLITE3_TEXT);
$stmt->bindValue(':img', $bookData['img'], SQLITE3_TEXT);
$stmt->bindValue(':url', $bookData['url'], SQLITE3_TEXT);
$stmt->bindValue(':datepublished', $bookData['info']['datepublished'], SQLITE3_TEXT);
$stmt->bindValue(':numberofpages', $bookData['info']['numberofpages'], SQLITE3_INTEGER);
$stmt->bindValue(':reviewCount', $bookData['info']['reviewCount'], SQLITE3_INTEGER);
$stmt->bindValue(':ratingValue', (float)$bookData['info']['ratingValue'], SQLITE3_FLOAT);
$stmt->bindValue(':category', $bookData['info']['category'], SQLITE3_TEXT);
$stmt->bindValue(':pdf', $bookData['pdf'], SQLITE3_TEXT);
$stmt->bindValue(':epub', $bookData['epub'], SQLITE3_TEXT);
$stmt->bindValue(':mobi', $bookData['mobi'], SQLITE3_TEXT);
$stmt->bindValue(':code', $bookData['code'], SQLITE3_TEXT);
$result = $stmt->execute();
}
echo $dl . ' - ';
if ($dl > 500) {
// var_dump($dl);
die();
}
$resultData = $result->fetchArray(SQLITE3_ASSOC);
$dl += downloadBook($bookData['pdf']);
$dl += downloadBook($bookData['epub']);
$dl += downloadBook($bookData['mobi']);
$dl += downloadBook($bookData['code']);
if (!$resultData) {
$bookData['info'] = getBookInfo($bookData['url']);
$stmt = $db->prepare('REPLACE INTO book (nid, title, isbn, img, url, datepublished, numberofpages, reviewCount, ratingValue, category, pdf, epub, mobi, code)
VALUES (:nid, :title, :isbn, :img, :url, :datepublished, :numberofpages, :reviewCount, :ratingValue, :category, :pdf, :epub, :mobi, :code)');
$stmt->bindValue(':nid', $bookData['nid'], SQLITE3_INTEGER);
$stmt->bindValue(':title', $bookData['title'], SQLITE3_TEXT);
$stmt->bindValue(':isbn', $bookData['isbn'], SQLITE3_TEXT);
$stmt->bindValue(':img', $bookData['img'], SQLITE3_TEXT);
$stmt->bindValue(':url', $bookData['url'], SQLITE3_TEXT);
$stmt->bindValue(':datepublished', $bookData['info']['datepublished'], SQLITE3_TEXT);
$stmt->bindValue(':numberofpages', $bookData['info']['numberofpages'], SQLITE3_INTEGER);
$stmt->bindValue(':reviewCount', $bookData['info']['reviewCount'], SQLITE3_INTEGER);
$stmt->bindValue(':ratingValue', (float)$bookData['info']['ratingValue'], SQLITE3_FLOAT);
$stmt->bindValue(':category', $bookData['info']['category'], SQLITE3_TEXT);
$stmt->bindValue(':pdf', $bookData['pdf'], SQLITE3_TEXT);
$stmt->bindValue(':epub', $bookData['epub'], SQLITE3_TEXT);
$stmt->bindValue(':mobi', $bookData['mobi'], SQLITE3_TEXT);
$stmt->bindValue(':code', $bookData['code'], SQLITE3_TEXT);
$result = $stmt->execute();
}
echo $dl . ' - ';
if ($dl > 500) {
// var_dump($dl);
die();
}
}
}
function downloadBook($url)