326 lines
11 KiB
PHP
326 lines
11 KiB
PHP
<?php
|
|
// Copyright 2026 Umbra Tools (umbratools.com)
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
/**
|
|
* Goodreads Scraper class
|
|
*
|
|
* Fetches author and book data from Goodreads
|
|
*
|
|
* @package BD_Bookshelf
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class BDM_Goodreads_Scraper {
|
|
|
|
private $user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
|
|
|
|
/**
|
|
* Get author info from Goodreads
|
|
*/
|
|
public function get_author_info($author_id) {
|
|
$url = "https://www.goodreads.com/author/show/{$author_id}";
|
|
|
|
$response = wp_remote_get($url, [
|
|
'timeout' => 30,
|
|
'headers' => [
|
|
'User-Agent' => $this->user_agent,
|
|
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
'Accept-Language' => 'en-US,en;q=0.5',
|
|
],
|
|
]);
|
|
|
|
if (is_wp_error($response)) {
|
|
return ['error' => $response->get_error_message()];
|
|
}
|
|
|
|
$body = wp_remote_retrieve_body($response);
|
|
|
|
return $this->parse_author_page($body, $author_id);
|
|
}
|
|
|
|
/**
|
|
* Get books from Goodreads author page
|
|
*/
|
|
public function get_author_books($author_id) {
|
|
$url = "https://www.goodreads.com/author/list/{$author_id}";
|
|
|
|
$response = wp_remote_get($url, [
|
|
'timeout' => 30,
|
|
'headers' => [
|
|
'User-Agent' => $this->user_agent,
|
|
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
'Accept-Language' => 'en-US,en;q=0.5',
|
|
],
|
|
]);
|
|
|
|
if (is_wp_error($response)) {
|
|
return ['error' => $response->get_error_message()];
|
|
}
|
|
|
|
$body = wp_remote_retrieve_body($response);
|
|
|
|
return $this->parse_books_list($body);
|
|
}
|
|
|
|
/**
|
|
* Get full book details from individual book page
|
|
*/
|
|
public function get_book_details($goodreads_id) {
|
|
$url = "https://www.goodreads.com/book/show/{$goodreads_id}";
|
|
|
|
$response = wp_remote_get($url, [
|
|
'timeout' => 30,
|
|
'headers' => [
|
|
'User-Agent' => $this->user_agent,
|
|
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
'Accept-Language' => 'en-US,en;q=0.5',
|
|
],
|
|
]);
|
|
|
|
if (is_wp_error($response)) {
|
|
return ['error' => $response->get_error_message()];
|
|
}
|
|
|
|
$body = wp_remote_retrieve_body($response);
|
|
|
|
return $this->parse_book_details($body, $goodreads_id);
|
|
}
|
|
|
|
/**
|
|
* Parse author page HTML
|
|
*/
|
|
private function parse_author_page($html, $author_id) {
|
|
$author = [
|
|
'id' => $author_id,
|
|
'name' => '',
|
|
'photo' => '',
|
|
'bio' => '',
|
|
'website' => '',
|
|
'genre' => '',
|
|
'books_count' => 0,
|
|
'average_rating' => 0,
|
|
];
|
|
|
|
// Extract author name
|
|
if (preg_match('/<h1[^>]*class="[^"]*authorName[^"]*"[^>]*>(.*?)<\/h1>/si', $html, $matches)) {
|
|
$author['name'] = trim(strip_tags($matches[1]));
|
|
}
|
|
|
|
// Extract photo
|
|
if (preg_match('/<img[^>]*class="[^"]*authorImage[^"]*"[^>]*src="([^"]+)"/si', $html, $matches)) {
|
|
$author['photo'] = $matches[1];
|
|
}
|
|
|
|
// Extract bio
|
|
if (preg_match('/<div[^>]*class="[^"]*authorAbout[^"]*"[^>]*>.*?<div[^>]*class="[^"]*aboutRight[^"]*"[^>]*>(.*?)<\/div>/si', $html, $matches)) {
|
|
$author['bio'] = trim(strip_tags($matches[1]));
|
|
}
|
|
|
|
// Extract website
|
|
if (preg_match('/<a[^>]*class="[^"]*authorLink[^"]*"[^>]*href="([^"]+)"/si', $html, $matches)) {
|
|
$author['website'] = $matches[1];
|
|
}
|
|
|
|
// Extract books count
|
|
if (preg_match('/(\d+)\s*books?/si', $html, $matches)) {
|
|
$author['books_count'] = absint($matches[1]);
|
|
}
|
|
|
|
return $author;
|
|
}
|
|
|
|
/**
|
|
* Parse books list HTML
|
|
*/
|
|
private function parse_books_list($html) {
|
|
$books = [];
|
|
|
|
// Match book rows
|
|
if (preg_match_all('/<tr[^>]*itemtype="http:\/\/schema.org\/Book"[^>]*>(.*?)<\/tr>/si', $html, $matches)) {
|
|
foreach ($matches[1] as $row) {
|
|
$book = $this->parse_book_row($row);
|
|
if ($book) {
|
|
$books[] = $book;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $books;
|
|
}
|
|
|
|
/**
|
|
* Parse individual book row
|
|
*/
|
|
private function parse_book_row($html) {
|
|
$book = [
|
|
'title' => '',
|
|
'author' => '',
|
|
'isbn' => '',
|
|
'isbn13' => '',
|
|
'rating' => 0,
|
|
'ratings_count' => 0,
|
|
'reviews_count' => 0,
|
|
'cover_image' => '',
|
|
'description' => '',
|
|
'published' => '',
|
|
'goodreads_id' => '',
|
|
'goodreads_url' => '',
|
|
];
|
|
|
|
// Extract title from bookTitle span (role=heading)
|
|
if (preg_match('/<span[^>]*itemprop=["\']name["\'][^>]*role=["\']heading["\'][^>]*>(.*?)<\/span>/si', $html, $matches)) {
|
|
$book['title'] = trim(strip_tags($matches[1]));
|
|
} elseif (preg_match('/<a[^>]*class="[^"]*bookTitle[^"]*"[^>]*>.*?<span[^>]*>(.*?)<\/span>/si', $html, $matches)) {
|
|
$book['title'] = trim(strip_tags($matches[1]));
|
|
} elseif (preg_match('/<a[^>]*class="[^"]*bookTitle[^"]*"[^>]*>(.*?)<\/a>/si', $html, $matches)) {
|
|
$book['title'] = trim(strip_tags($matches[1]));
|
|
}
|
|
|
|
// Extract Goodreads URL and ID
|
|
if (preg_match('/<a[^>]*class="[^"]*bookTitle[^"]*"[^>]*href="([^"]+)"/si', $html, $matches)) {
|
|
$book['goodreads_url'] = trim($matches[1]);
|
|
if (preg_match('/\/book\/show\/(\d+)/', $matches[1], $id_matches)) {
|
|
$book['goodreads_id'] = $id_matches[1];
|
|
}
|
|
}
|
|
|
|
// Extract author
|
|
if (preg_match('/<a[^>]*class="[^"]*authorName[^"]*"[^>]*>.*?<span[^>]*itemprop=["\']name["\'][^>]*>(.*?)<\/span>/si', $html, $matches)) {
|
|
$book['author'] = trim(strip_tags($matches[1]));
|
|
} elseif (preg_match('/<a[^>]*class="[^"]*authorName[^"]*"[^>]*>(.*?)<\/a>/si', $html, $matches)) {
|
|
$book['author'] = trim(strip_tags($matches[1]));
|
|
}
|
|
|
|
// Extract cover image (class="bookCover" in list, class="bookImage" elsewhere)
|
|
if (preg_match('/<img[^>]*class="[^"]*bookCover[^"]*"[^>]*src="([^"]+)"/si', $html, $matches)) {
|
|
$book['cover_image'] = $matches[1];
|
|
} elseif (preg_match('/<img[^>]*class="[^"]*bookImage[^"]*"[^>]*src="([^"]+)"/si', $html, $matches)) {
|
|
$book['cover_image'] = $matches[1];
|
|
}
|
|
|
|
// Extract rating from minirating span
|
|
if (preg_match('/([\d.]+)\s*avg\s*rating\s*(?:—|—|--)\s*(\d[\d,]*)\s*rating/i', $html, $matches)) {
|
|
$book['rating'] = (float) $matches[1];
|
|
$book['ratings_count'] = absint(str_replace(',', '', $matches[2]));
|
|
} elseif (preg_match('/<span[^>]*class="[^"]*rating[^"]*"[^>]*>\s*(\d+\.?\d*)/si', $html, $matches)) {
|
|
$book['rating'] = (float) $matches[1];
|
|
}
|
|
|
|
// Extract ratings count (fallback)
|
|
if ($book['ratings_count'] === 0 && preg_match('/(\d+[\d,]*)\s*ratings?/si', $html, $matches)) {
|
|
$book['ratings_count'] = absint(str_replace(',', '', $matches[1]));
|
|
}
|
|
|
|
// Extract reviews count
|
|
if (preg_match('/(\d+[\d,]*)\s*reviews?/si', $html, $matches)) {
|
|
$book['reviews_count'] = absint(str_replace(',', '', $matches[1]));
|
|
}
|
|
|
|
// Extract ISBN
|
|
if (preg_match('/isbn(?:13)?[:\s]*(\d{10,13})/si', $html, $matches)) {
|
|
$book['isbn'] = $matches[1];
|
|
}
|
|
|
|
// Extract published date
|
|
if (preg_match('/published[^:]*:\s*([A-Za-z]+\s+\d{1,2},?\s*\d{4})/si', $html, $matches)) {
|
|
$book['published'] = trim($matches[1]);
|
|
}
|
|
|
|
// Upgrade cover image to larger version
|
|
if (!empty($book['cover_image'])) {
|
|
$book['cover_image'] = $this->upgrade_cover_url($book['cover_image']);
|
|
}
|
|
|
|
// Only return if we have a title
|
|
if (empty($book['title'])) {
|
|
return null;
|
|
}
|
|
|
|
return $book;
|
|
}
|
|
|
|
/**
|
|
* Parse book details page
|
|
*/
|
|
private function parse_book_details($html, $goodreads_id) {
|
|
$book = [
|
|
'goodreads_id' => $goodreads_id,
|
|
'title' => '',
|
|
'author' => '',
|
|
'isbn' => '',
|
|
'isbn13' => '',
|
|
'rating' => 0,
|
|
'ratings_count' => 0,
|
|
'reviews_count' => 0,
|
|
'cover_image' => '',
|
|
'description' => '',
|
|
'published' => '',
|
|
'pages' => 0,
|
|
'genre' => '',
|
|
];
|
|
|
|
// Extract title
|
|
if (preg_match('/<h1[^>]*id="bookTitle"[^>]*>(.*?)<\/h1>/si', $html, $matches)) {
|
|
$book['title'] = trim(strip_tags($matches[1]));
|
|
}
|
|
|
|
// Extract author
|
|
if (preg_match('/<a[^>]*class="[^"]*authorName[^"]*"[^>]*>(.*?)<\/a>/si', $html, $matches)) {
|
|
$book['author'] = trim(strip_tags($matches[1]));
|
|
}
|
|
|
|
// Extract cover image
|
|
if (preg_match('/<img[^>]*id="coverImage"[^>]*src="([^"]+)"/si', $html, $matches)) {
|
|
$book['cover_image'] = $matches[1];
|
|
}
|
|
|
|
// Extract description
|
|
if (preg_match('/<div[^>]*id="description"[^>]*>.*?<span[^>]*>(.*?)<\/span>/si', $html, $matches)) {
|
|
$book['description'] = trim(strip_tags($matches[1]));
|
|
}
|
|
|
|
// Extract ISBN13
|
|
if (preg_match('/isbn13[:\s]*(\d{13})/si', $html, $matches)) {
|
|
$book['isbn13'] = $matches[1];
|
|
}
|
|
|
|
// Extract ISBN
|
|
if (preg_match('/isbn[:\s]*(\d{10})/si', $html, $matches)) {
|
|
$book['isbn'] = $matches[1];
|
|
}
|
|
|
|
// Extract rating
|
|
if (preg_match('/<span[^>]*class="[^"]*RatingStatistics__rating[^"]*"[^>]*>(\d+\.?\d*)/si', $html, $matches)) {
|
|
$book['rating'] = (float) $matches[1];
|
|
}
|
|
|
|
// Extract pages
|
|
if (preg_match('/(\d+)\s*pages?/si', $html, $matches)) {
|
|
$book['pages'] = absint($matches[1]);
|
|
}
|
|
|
|
// Extract published date
|
|
if (preg_match('/published[^:]*:?\s*([A-Za-z]+\s+\d{1,2},?\s*\d{4})/si', $html, $matches)) {
|
|
$book['published'] = trim($matches[1]);
|
|
}
|
|
|
|
return $book;
|
|
}
|
|
|
|
/**
|
|
* Upgrade a Goodreads cover URL to a larger version
|
|
*
|
|
* Goodreads uses _SY75_ for thumbnails. We can change to _SX300_ for larger.
|
|
*/
|
|
private function upgrade_cover_url($url) {
|
|
// Replace thumbnail size with larger version
|
|
$url = preg_replace('/\._SY\d+_/', '._SX300_', $url);
|
|
$url = preg_replace('/\._SX\d+_/', '._SX300_', $url);
|
|
return $url;
|
|
}
|
|
}
|