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:
krzysiej
2018-05-08 15:39:49 +02:00
parent b496f8a531
commit f4c4d029e6
3 changed files with 87 additions and 20 deletions

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@ cookie.txt
vendor/
data.db
books.txt
/books

View File

@@ -13,12 +13,25 @@ $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>');
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)) {
// 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']);
$pdfUrl = 'books/' . $resultArray['nid'] . '/' . $resultArray['nid'] . '.pdf';
$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) {
$x = $resultArray;
}

View File

@@ -15,37 +15,41 @@ $loginData = [
];
function c($url, $post = [])
function c($url, $post = [], $localFilePath = null)
{
$cookie = "cookie.txt";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
http_build_query($post));
if (!empty($post) && count($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_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$server_output = curl_exec($ch);
strlen($server_output);
curl_close($ch);
return $server_output;
}
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)
@@ -95,14 +99,15 @@ function getBookInfo($bookUrl)
$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);
$booksData = [];
$books = $document->find('.product-line.unseen');
$dl = 0;
$db = new SQLite3('data.db');
shuffle($books);
foreach ($books as $book) {
$bookData = [];
@@ -123,9 +128,14 @@ foreach ($books as $book) {
$result = $stmt->execute();
$resultData = $result->fetchArray(SQLITE3_ASSOC);
$dl += downloadBook($bookData['pdf']);
$dl += downloadBook($bookData['epub']);
$dl += downloadBook($bookData['mobi']);
$dl += downloadBook($bookData['code']);
if (!$resultData) {
$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)');
@@ -145,4 +155,47 @@ VALUES (:nid, :title, :isbn, :img, :url, :datepublished, :numberofpages, :revie
$stmt->bindValue(':code', $bookData['code'], SQLITE3_TEXT);
$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);
}