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

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);
}