diff --git a/data.sample.db b/data.sample.db
index 6daf9b4..74fddb0 100644
Binary files a/data.sample.db and b/data.sample.db differ
diff --git a/index.php b/index.php
new file mode 100644
index 0000000..0a8d87b
--- /dev/null
+++ b/index.php
@@ -0,0 +1,27 @@
+prepare('SELECT * FROM book ');
+//$stmt->bindValue(':nid', $bookData['nid'], SQLITE3_INTEGER);
+$result = $stmt->execute();
+echo '
';
+
+echo '
';
+print(' | id | nid | Title | Category | Publish date | Pages | Votes | Rating |
');
+while ($resultArray = $result->fetchArray(SQLITE3_ASSOC)) {
+// print_r($resultArray);
+
+ printf(' | %d | %d | %s | %s | %s | %s | %d | %.1f |
',
+ $resultArray['id'], $resultArray['nid'], $resultArray['title'], $resultArray['category'], $resultArray['datepublished'], $resultArray['numberofpages'], $resultArray['reviewCount'], $resultArray['ratingValue']);
+ if ($resultArray) {
+ $x = $resultArray;
+ }
+}
+echo '
';
+
diff --git a/packt.php b/packt.php
index 7053c97..b6649ae 100644
--- a/packt.php
+++ b/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);