Listing books in index.php, saveing book informations to db.
This commit is contained in:
BIN
data.sample.db
BIN
data.sample.db
Binary file not shown.
27
index.php
Normal file
27
index.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
include 'vendor/autoload.php';
|
||||
|
||||
|
||||
set_time_limit(-1);
|
||||
|
||||
|
||||
$db = new SQLite3('data.db');
|
||||
|
||||
$stmt = $db->prepare('SELECT * FROM book ');
|
||||
//$stmt->bindValue(':nid', $bookData['nid'], SQLITE3_INTEGER);
|
||||
$result = $stmt->execute();
|
||||
echo '<pre>';
|
||||
|
||||
echo '<table style="width: 100%">';
|
||||
print('<tr> <th>id</th><th>nid</th><th>Title</th><th>Category</th><th>Publish date</th><th>Pages</th><th>Votes</th><th>Rating</th> </tr>');
|
||||
while ($resultArray = $result->fetchArray(SQLITE3_ASSOC)) {
|
||||
// print_r($resultArray);
|
||||
|
||||
printf('<tr> <td>%d</td><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%d</td><td>%.1f</td> </tr>',
|
||||
$resultArray['id'], $resultArray['nid'], $resultArray['title'], $resultArray['category'], $resultArray['datepublished'], $resultArray['numberofpages'], $resultArray['reviewCount'], $resultArray['ratingValue']);
|
||||
if ($resultArray) {
|
||||
$x = $resultArray;
|
||||
}
|
||||
}
|
||||
echo '</table>';
|
||||
|
||||
71
packt.php
71
packt.php
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user