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>';
|
||||||
|
|
||||||
49
packt.php
49
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)
|
function getBookInfo($bookUrl)
|
||||||
{
|
{
|
||||||
|
|
||||||
print_r($bookUrl . "\n");
|
$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)');
|
||||||
@@ -84,24 +89,21 @@ function getBookInfo($bookUrl)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//$return = c('https://www.packtpub.com/', $loginData);
|
$return = c('https://www.packtpub.com/', $loginData);
|
||||||
//$return = c('https://www.packtpub.com/account/my-ebooks');
|
$return = c('https://www.packtpub.com/account/my-ebooks');
|
||||||
//var_dump($return);
|
$document = new Document($return);
|
||||||
//var_dump($return);
|
|
||||||
//$document = new Document($return);
|
|
||||||
|
|
||||||
$document = new Document('packt.html', true);
|
//$document = new Document('packt.html', true);
|
||||||
|
|
||||||
$booksData = [];
|
$booksData = [];
|
||||||
$books = $document->find('.product-line.unseen');
|
$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) {
|
foreach ($books as $book) {
|
||||||
|
|
||||||
|
|
||||||
// print_r($book->html());
|
|
||||||
$bookData = [];
|
$bookData = [];
|
||||||
$bookData['title'] = str_replace(["\r\n"], '', trim($book->first('.title::text')));
|
$bookData['title'] = str_replace(["\r\n"], '', trim($book->first('.title::text')));
|
||||||
$bookData['nid'] = (int)$book->attr('nid');
|
$bookData['nid'] = (int)$book->attr('nid');
|
||||||
@@ -113,12 +115,17 @@ foreach ($books as $book) {
|
|||||||
$bookData['isbn'] = $book->first('div[isbn]::attr(isbn)');
|
$bookData['isbn'] = $book->first('div[isbn]::attr(isbn)');
|
||||||
$bookData['img'] = $book->first('img[class*=imagecache]::attr(src)');
|
$bookData['img'] = $book->first('img[class*=imagecache]::attr(src)');
|
||||||
$bookData['url'] = $book->first('div[class*=product-thumbnail]')->first('a::attr(href)');
|
$bookData['url'] = $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();
|
||||||
|
|
||||||
|
if ($result->numColumns() > 0) {
|
||||||
|
|
||||||
|
|
||||||
|
} else {
|
||||||
$bookData['info'] = getBookInfo($bookData['url']);
|
$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)
|
$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)');
|
VALUES (:nid, :title, :isbn, :img, :url, :datepublished, :numberofpages, :reviewCount, :ratingValue, :category, :pdf, :epub, :mobi, :code)');
|
||||||
$stmt->bindValue(':nid', $bookData['nid'], SQLITE3_INTEGER);
|
$stmt->bindValue(':nid', $bookData['nid'], SQLITE3_INTEGER);
|
||||||
@@ -135,16 +142,6 @@ VALUES (:nid, :title, :isbn, :img, :url, :datepublished, :numberofpages, :revie
|
|||||||
$stmt->bindValue(':epub', $bookData['epub'], SQLITE3_TEXT);
|
$stmt->bindValue(':epub', $bookData['epub'], SQLITE3_TEXT);
|
||||||
$stmt->bindValue(':mobi', $bookData['mobi'], SQLITE3_TEXT);
|
$stmt->bindValue(':mobi', $bookData['mobi'], SQLITE3_TEXT);
|
||||||
$stmt->bindValue(':code', $bookData['code'], SQLITE3_TEXT);
|
$stmt->bindValue(':code', $bookData['code'], SQLITE3_TEXT);
|
||||||
|
|
||||||
$result = $stmt->execute();
|
$result = $stmt->execute();
|
||||||
|
}
|
||||||
|
|
||||||
$booksData[$bookData['nid']] = $bookData;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
print_r($booksData);
|
|
||||||
|
|
||||||
file_put_contents('books.txt', json_encode($booksData));
|
|
||||||
|
|
||||||
|
|
||||||
//print_r($return);
|
|
||||||
|
|||||||
Reference in New Issue
Block a user