Listing books in index.php, saveing book informations to db.

This commit is contained in:
krzysiej
2018-04-13 14:53:01 +02:00
parent ea3c59ff86
commit 2899d8ab56
3 changed files with 61 additions and 37 deletions

View File

@@ -45,10 +45,15 @@ function c($url, $post = [])
}
function resolveBookUrl($bookUrl)
{
return 'https://www.packtpub.com/' . str_replace('https://www.packtpub.com/', '', $bookUrl);
}
function getBookInfo($bookUrl)
{
print_r($bookUrl . "\n");
$bookUrl = resolveBookUrl($bookUrl);
$bookData = [];
$bookPage = new Document($bookUrl, true);
$bookData['datepublished'] = $bookPage->first('.book-top-block-info-authors time[itemprop="datePublished"]::attr(datetime)');
@@ -84,24 +89,21 @@ function getBookInfo($bookUrl)
}
//$return = c('https://www.packtpub.com/', $loginData);
//$return = c('https://www.packtpub.com/account/my-ebooks');
//var_dump($return);
//var_dump($return);
//$document = new Document($return);
$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);
//$document = new Document('packt.html', true);
$booksData = [];
$books = $document->find('.product-line.unseen');
//$books = array_slice($books, 2, 1);
$bookData = json_decode('books.txt', 1);
$db = new SQLite3('data.db');
foreach ($books as $book) {
// print_r($book->html());
$bookData = [];
$bookData['title'] = str_replace(["\r\n"], '', trim($book->first('.title::text')));
$bookData['nid'] = (int)$book->attr('nid');
@@ -113,38 +115,33 @@ foreach ($books as $book) {
$bookData['isbn'] = $book->first('div[isbn]::attr(isbn)');
$bookData['img'] = $book->first('img[class*=imagecache]::attr(src)');
$bookData['url'] = $book->first('div[class*=product-thumbnail]')->first('a::attr(href)');
$bookData['info'] = getBookInfo($bookData['url']);
$db = new SQLite3('data.db');
$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();
if ($result->numColumns() > 0) {
$booksData[$bookData['nid']] = $bookData;
} else {
$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();
}
}
print_r($booksData);
file_put_contents('books.txt', json_encode($booksData));
//print_r($return);