Dodanie sposobu na pobieranie książek, dodanie wyświetlania linków do pobranych plików, dodanie katalogu books do ignore.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@ cookie.txt
|
|||||||
vendor/
|
vendor/
|
||||||
data.db
|
data.db
|
||||||
books.txt
|
books.txt
|
||||||
|
/books
|
||||||
|
|||||||
19
index.php
19
index.php
@@ -13,12 +13,25 @@ $result = $stmt->execute();
|
|||||||
echo '<pre>';
|
echo '<pre>';
|
||||||
|
|
||||||
echo '<table style="width: 100%">';
|
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>');
|
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> <th>Pdf</th> <th>Epub</th> <th>Mobi</th> <th>Code</th> </tr>');
|
||||||
while ($resultArray = $result->fetchArray(SQLITE3_ASSOC)) {
|
while ($resultArray = $result->fetchArray(SQLITE3_ASSOC)) {
|
||||||
// print_r($resultArray);
|
// 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>',
|
$pdfUrl = 'books/' . $resultArray['nid'] . '/' . $resultArray['nid'] . '.pdf';
|
||||||
$resultArray['id'], $resultArray['nid'], $resultArray['title'], $resultArray['category'], $resultArray['datepublished'], $resultArray['numberofpages'], $resultArray['reviewCount'], $resultArray['ratingValue']);
|
$epubUrl = 'books/' . $resultArray['nid'] . '/' . $resultArray['nid'] . '.epub';
|
||||||
|
$mobiUrl = 'books/' . $resultArray['nid'] . '/' . $resultArray['nid'] . '.mobi';
|
||||||
|
$codeUrl = '';
|
||||||
|
if (preg_match('#\d+#', $resultArray['code'], $found)) {
|
||||||
|
$codeId = $found[0];
|
||||||
|
$codeUrl = 'books/' . $codeId . '/' . $codeId . '.zip';
|
||||||
|
}
|
||||||
|
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> <td>%s</td><td>%s</td><td>%s</td><td>%s</td> </tr>',
|
||||||
|
$resultArray['id'], $resultArray['nid'], $resultArray['title'], $resultArray['category'], $resultArray['datepublished'], $resultArray['numberofpages'], $resultArray['reviewCount'], $resultArray['ratingValue'],
|
||||||
|
is_file($pdfUrl) ? "<a href='{$pdfUrl}'>pdf</a>" : (strlen($resultArray['pdf']) ? 'nie pobrano' : ''),
|
||||||
|
is_file($epubUrl) ? "<a href='{$epubUrl}'>epub</a>" : (strlen($resultArray['epub']) ? 'nie pobrano' : ''),
|
||||||
|
is_file($mobiUrl) ? "<a href='{$mobiUrl}'>mobi</a>" : (strlen($resultArray['mobi']) ? 'nie pobrano' : ''),
|
||||||
|
is_file($codeUrl) ? "<a href='{$codeUrl}'>zip</a>" : (strlen($resultArray['code']) ? 'nie pobrano' : '')
|
||||||
|
);
|
||||||
if ($resultArray) {
|
if ($resultArray) {
|
||||||
$x = $resultArray;
|
$x = $resultArray;
|
||||||
}
|
}
|
||||||
|
|||||||
87
packt.php
87
packt.php
@@ -15,37 +15,41 @@ $loginData = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
function c($url, $post = [])
|
function c($url, $post = [], $localFilePath = null)
|
||||||
{
|
{
|
||||||
$cookie = "cookie.txt";
|
$cookie = "cookie.txt";
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
curl_setopt($ch, CURLOPT_URL, $url);
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
curl_setopt($ch, CURLOPT_POST, 1);
|
|
||||||
|
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS,
|
if (!empty($post) && count($post)) {
|
||||||
http_build_query($post));
|
curl_setopt($ch, CURLOPT_POST, 1);
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* this means url is a path to a file and needs to be saved localy under $localFilePath location
|
||||||
|
*/
|
||||||
|
if (!is_null($localFilePath)) {
|
||||||
|
$fp = fopen($localFilePath, 'w+');
|
||||||
|
curl_setopt($ch, CURLOPT_FILE, $fp);
|
||||||
|
} else {
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||||
|
}
|
||||||
|
|
||||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
||||||
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
|
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
|
||||||
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
|
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
|
||||||
|
|
||||||
// receive server response ...
|
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
||||||
|
|
||||||
$server_output = curl_exec($ch);
|
$server_output = curl_exec($ch);
|
||||||
strlen($server_output);
|
strlen($server_output);
|
||||||
|
|
||||||
|
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
|
|
||||||
return $server_output;
|
return $server_output;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function resolveBookUrl($bookUrl)
|
function resolveBookUrl($bookUrl)
|
||||||
{
|
{
|
||||||
return 'https://www.packtpub.com/' . str_replace('https://www.packtpub.com/', '', $bookUrl);
|
return 'https://www.packtpub.com/' . trim(str_replace('https://www.packtpub.com/', '', $bookUrl), '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
function truncateBookUrl($bookUrl)
|
function truncateBookUrl($bookUrl)
|
||||||
@@ -95,14 +99,15 @@ 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');
|
||||||
$document = new Document($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');
|
||||||
|
|
||||||
|
$dl = 0;
|
||||||
$db = new SQLite3('data.db');
|
$db = new SQLite3('data.db');
|
||||||
|
shuffle($books);
|
||||||
foreach ($books as $book) {
|
foreach ($books as $book) {
|
||||||
|
|
||||||
$bookData = [];
|
$bookData = [];
|
||||||
@@ -123,9 +128,14 @@ foreach ($books as $book) {
|
|||||||
$result = $stmt->execute();
|
$result = $stmt->execute();
|
||||||
|
|
||||||
$resultData = $result->fetchArray(SQLITE3_ASSOC);
|
$resultData = $result->fetchArray(SQLITE3_ASSOC);
|
||||||
|
|
||||||
|
$dl += downloadBook($bookData['pdf']);
|
||||||
|
$dl += downloadBook($bookData['epub']);
|
||||||
|
$dl += downloadBook($bookData['mobi']);
|
||||||
|
$dl += downloadBook($bookData['code']);
|
||||||
|
|
||||||
|
|
||||||
if (!$resultData) {
|
if (!$resultData) {
|
||||||
|
|
||||||
|
|
||||||
$bookData['info'] = getBookInfo($bookData['url']);
|
$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)
|
$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)');
|
||||||
@@ -145,4 +155,47 @@ VALUES (:nid, :title, :isbn, :img, :url, :datepublished, :numberofpages, :revie
|
|||||||
$stmt->bindValue(':code', $bookData['code'], SQLITE3_TEXT);
|
$stmt->bindValue(':code', $bookData['code'], SQLITE3_TEXT);
|
||||||
$result = $stmt->execute();
|
$result = $stmt->execute();
|
||||||
}
|
}
|
||||||
|
echo $dl.' - ';
|
||||||
|
if ($dl > 500) {
|
||||||
|
// var_dump($dl);
|
||||||
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function downloadBook($url)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (strlen($url) && !fileExists(fileNameFromPath($url))) {
|
||||||
|
$localPath = fileNameFromPath($url);
|
||||||
|
$directory = directoryNameFromPath($url);
|
||||||
|
$fullUrl = resolveBookUrl($url);
|
||||||
|
|
||||||
|
echo ($fullUrl)."\n";
|
||||||
|
// var_dump($localPath);
|
||||||
|
if (!is_dir($directory)) {
|
||||||
|
mkdir($directory, 0777, true);
|
||||||
|
}
|
||||||
|
c($fullUrl, [], $localPath);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function fileNameFromPath($filePath)
|
||||||
|
{
|
||||||
|
$filePathParts = explode('/', trim($filePath, '/'));
|
||||||
|
return 'books' . DIRECTORY_SEPARATOR . $filePathParts[1] . DIRECTORY_SEPARATOR . $filePathParts[1] . '.' . (isset($filePathParts[2]) ? $filePathParts[2] : 'zip');
|
||||||
|
}
|
||||||
|
|
||||||
|
function directoryNameFromPath($filePath)
|
||||||
|
{
|
||||||
|
$filePathParts = explode('/', trim($filePath, '/'));
|
||||||
|
return 'books' . DIRECTORY_SEPARATOR . $filePathParts[1] . DIRECTORY_SEPARATOR;
|
||||||
|
}
|
||||||
|
|
||||||
|
function fileExists($filePath)
|
||||||
|
{
|
||||||
|
return file_exists($filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user