Informations about author
This commit is contained in:
29
packt.php
29
packt.php
@@ -86,12 +86,12 @@ function getBookInfo($bookUrl)
|
||||
|
||||
$bookData['authors'] = [];
|
||||
foreach ($bookPage->find('[itemprop="author"]') as $author) {
|
||||
|
||||
$authorData = [];
|
||||
$authorData['name'] = $author->first('h3::text');
|
||||
$authorData['bio'] = $author->find('p::text');
|
||||
$authorData['name'] = trim($author->first('.book-info-bottom-author-title')->text());
|
||||
$authorData['bio'] = preg_replace('#\s{2,}#', ' ', trim($author->first('.book-info-bottom-author-body')->text()));
|
||||
$bookData['authors'][] = $authorData;
|
||||
}
|
||||
|
||||
return $bookData;
|
||||
}
|
||||
|
||||
@@ -99,7 +99,8 @@ function getBookInfo($bookUrl)
|
||||
$db = new SQLite3('data.db');
|
||||
|
||||
//$stmt = $db->prepare('SELECT * FROM book order by random() limit 1 ');
|
||||
$stmt = $db->prepare('SELECT * FROM book');
|
||||
$stmt = $db->prepare('select * from book');
|
||||
$stmt = $db->prepare('select * from book left join author_book ON book.id = author_book.book_id where author_book.book_id is null and category is not null');
|
||||
$booksResult = $stmt->execute();
|
||||
|
||||
|
||||
@@ -108,32 +109,35 @@ echo '<pre>';
|
||||
while ($book = $booksResult->fetchArray(SQLITE3_ASSOC)) {
|
||||
$bookInfo = getBookInfo($book['url']);
|
||||
|
||||
// print_r($book);
|
||||
|
||||
foreach ($bookInfo['authors'] as $author) {
|
||||
|
||||
|
||||
$stmt = $db->prepare('SELECT * FROM author WHERE name = :name and bio = :bio');
|
||||
$stmt->bindValue(':name', trim($author['name']), SQLITE3_TEXT);
|
||||
$stmt->bindValue(':bio', trim(implode(' ', $author['bio'])), SQLITE3_TEXT);
|
||||
$stmt->bindValue(':bio', $author['bio'], SQLITE3_TEXT);
|
||||
$result = $stmt->execute();
|
||||
$authorData = $result->fetchArray(SQLITE3_ASSOC);
|
||||
|
||||
if (!$authorData) {
|
||||
$stmt = $db->prepare('INSERT INTO author(name, bio) VALUES (:name, :bio)');
|
||||
$stmt->bindValue(':name', trim($author['name']), SQLITE3_TEXT);
|
||||
$stmt->bindValue(':bio', trim(implode(' ', $author['bio'])), SQLITE3_TEXT);
|
||||
$stmt->bindValue(':bio', $author['bio'], SQLITE3_TEXT);
|
||||
$result = $stmt->execute();
|
||||
|
||||
$stmt = $db->prepare('select seq from sqlite_sequence where name="author"');
|
||||
$result = $stmt->execute();
|
||||
$seqData = $result->fetchArray(SQLITE3_ASSOC);
|
||||
|
||||
$stmt = $db->prepare('INSERT INTO book_author(book_id, author_id) VALUES (:book_id, :author_id)');
|
||||
$stmt->bindValue(':book_id', $book['id'], SQLITE3_INTEGER);
|
||||
$stmt = $db->prepare('INSERT INTO author_book(author_id, book_id) VALUES (:author_id, :book_id)');
|
||||
$stmt->bindValue(':author_id', $seqData['seq'], SQLITE3_INTEGER);
|
||||
$stmt->bindValue(':book_id', $book['id'], SQLITE3_INTEGER);
|
||||
$result = $stmt->execute();
|
||||
} else {
|
||||
$stmt = $db->prepare('INSERT INTO author_book(author_id, book_id) VALUES (:author_id, :book_id)');
|
||||
$stmt->bindValue(':author_id', $authorData['id'], SQLITE3_INTEGER);
|
||||
$stmt->bindValue(':book_id', $book['id'], SQLITE3_INTEGER);
|
||||
$result = $stmt->execute();
|
||||
}
|
||||
|
||||
var_dump($authorData);
|
||||
|
||||
}
|
||||
@@ -141,9 +145,6 @@ while ($book = $booksResult->fetchArray(SQLITE3_ASSOC)) {
|
||||
|
||||
//print_r($bookData);
|
||||
//print_r($bookInfo);
|
||||
echo '</pre>';
|
||||
|
||||
die();
|
||||
|
||||
|
||||
$return = c('https://www.packtpub.com/', $loginData);
|
||||
|
||||
Reference in New Issue
Block a user