mirror of
https://github.com/jez500/pricebuddy.git
synced 2026-11-24 19:58:13 +01:00
354 lines
10 KiB
PHP
Executable File
354 lines
10 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Dto\Scraping\FieldExtractionDto;
|
|
use App\Enums\ScraperService;
|
|
use App\Enums\ScraperStrategyType;
|
|
use App\Enums\StockStatus;
|
|
use App\Models\Store;
|
|
use App\Services\Helpers\SettingsHelper;
|
|
use Exception;
|
|
use Filament\Notifications\Notification;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Uri;
|
|
use Jez500\WebScraperForLaravel\Exceptions\DomSelectorException;
|
|
use Jez500\WebScraperForLaravel\Facades\WebScraper;
|
|
use Jez500\WebScraperForLaravel\WebScraperApi;
|
|
use Jez500\WebScraperForLaravel\WebScraperInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class ScrapeUrl
|
|
{
|
|
public const string SELECTOR_ATTR_DELIMITER = '|';
|
|
|
|
public const string SELECTOR_HTML_PREFIX = '!';
|
|
|
|
/**
|
|
* For the title and image, limit the length.
|
|
*/
|
|
public const int MAX_STR_LENGTH = 1000;
|
|
|
|
protected WebScraperInterface $webScraper;
|
|
|
|
protected LoggerInterface $logger;
|
|
|
|
protected ScrapeSchemaCompiler $schemaCompiler;
|
|
|
|
protected int $scraperRequestTimeout = 30;
|
|
|
|
protected int $scraperConnectTimeout = 30;
|
|
|
|
protected string $scraperService = 'api';
|
|
|
|
protected int $maxAttempts;
|
|
|
|
protected array $keys = [
|
|
'title',
|
|
'description',
|
|
'price',
|
|
'image',
|
|
'availability',
|
|
];
|
|
|
|
public bool $sendUiNotifications = true;
|
|
|
|
public bool $logErrors = true;
|
|
|
|
public function __construct(protected string $url)
|
|
{
|
|
// @phpstan-ignore-next-line - withContext is valid.
|
|
$this->logger = Log::channel('db')->withContext(['url' => $url]);
|
|
$this->maxAttempts = SettingsHelper::getSetting('max_attempts_to_scrape', 3);
|
|
$this->schemaCompiler = new ScrapeSchemaCompiler;
|
|
}
|
|
|
|
public static function new(string $url): self
|
|
{
|
|
return resolve(static::class, ['url' => $url]);
|
|
}
|
|
|
|
public function setMaxAttempts(int $maxAttempts): self
|
|
{
|
|
$this->maxAttempts = $maxAttempts;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function setLogErrors(bool $logErrors): self
|
|
{
|
|
$this->logErrors = $logErrors;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function setSendUiNotifications(bool $sendUiNotifications): self
|
|
{
|
|
$this->sendUiNotifications = $sendUiNotifications;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function setScraper(string $scraper): self
|
|
{
|
|
$this->scraperService = $scraper;
|
|
$scraper = WebScraper::make($this->scraperService)
|
|
->setConnectTimeout($this->getConnectTimeout())
|
|
->setRequestTimeout($this->getRequestTimeout());
|
|
|
|
if ($this->scraperService === ScraperService::Api->value) {
|
|
/** @var WebScraperApi $scraper */
|
|
$scraper->setScraperApiBaseUrl(
|
|
config('price_buddy.scraper_api_url', 'http://scraper:3000')
|
|
);
|
|
}
|
|
|
|
$this->webScraper = $scraper;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function scrape(array $options = []): array
|
|
{
|
|
$attempt = 0;
|
|
$output = [];
|
|
|
|
while ($attempt < $this->maxAttempts) {
|
|
$attempt++;
|
|
|
|
// Don't use cache if previous attempt failed.
|
|
if ($attempt > 1) {
|
|
$options['use_cache'] = false;
|
|
}
|
|
|
|
$output = $this->scrapeUrl($options);
|
|
|
|
if ($output === false) {
|
|
$attempt = $this->maxAttempts;
|
|
$output = [];
|
|
}
|
|
|
|
if (! empty($output['title'])) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
$matchConfig = data_get($output, 'store.scrape_strategy.availability.match');
|
|
$isUnavailable = StockStatus::matchFromScrapedValue($output['availability'] ?? null, $matchConfig)->isUnavailable();
|
|
|
|
foreach (['price', 'title'] as $required) {
|
|
// Skip price requirement when product is unavailable.
|
|
if ($required === 'price' && $isUnavailable) {
|
|
continue;
|
|
}
|
|
|
|
if (empty($output[$required])) {
|
|
$this->errorLog('Error scraping URL '.$attempt.' times', [
|
|
'attempts' => $attempt,
|
|
'error' => __('Missing :field when scraping', ['field' => $required]),
|
|
'scrape_errors' => $output['errors'] ?? [],
|
|
'scraped_html' => $output['body'] ?? '',
|
|
]);
|
|
$this->errorNotification('Missing required field: '.$required);
|
|
|
|
return $output;
|
|
}
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
|
|
protected function scrapeUrl(array $options = []): array|false
|
|
{
|
|
$store = data_get($options, 'store') ?? $this->getStore();
|
|
$useCache = data_get($options, 'use_cache', true);
|
|
|
|
if (! $store) {
|
|
$this->errorLog('No store found for URL');
|
|
$this->errorNotification('No store found for URL');
|
|
|
|
return false;
|
|
}
|
|
|
|
$output = [
|
|
'store' => $store,
|
|
];
|
|
|
|
foreach ($this->keys as $key) {
|
|
$output[$key] = null;
|
|
}
|
|
|
|
try {
|
|
$this->setScraper($store->scraper_service);
|
|
|
|
$scraper = $this->webScraper->from($this->url)
|
|
->setCacheMinsTtl(SettingsHelper::getSetting('scrape_cache_ttl', 720))
|
|
->setUseCache($useCache)
|
|
->setOptions($store->scraper_options);
|
|
|
|
if ($store->cookies) {
|
|
$scraper->setCookies($store->cookies);
|
|
}
|
|
|
|
$page = $scraper->get();
|
|
|
|
if ($errors = $scraper->getErrors()) {
|
|
$this->errorLog('Error scraping URL', [
|
|
'store_id' => $store->getKey(),
|
|
'errors' => $errors,
|
|
]);
|
|
$this->errorNotification('Error scraping URL check logs');
|
|
|
|
return $output;
|
|
}
|
|
|
|
$strategy = data_get($store, 'scrape_strategy', []);
|
|
$compiledStrategy = $this->schemaCompiler->fromDto($strategy, $page);
|
|
|
|
foreach ($this->keys as $key) {
|
|
if (! isset($compiledStrategy[$key])) {
|
|
$output[$key] = null;
|
|
} else {
|
|
$output[$key] = data_get($compiledStrategy, $key.'.value');
|
|
}
|
|
}
|
|
|
|
$output['body'] = $page->getBody();
|
|
$output['errors'] = $scraper->getErrors();
|
|
} catch (Exception $e) {
|
|
$this->errorLog('Error scraping URL', [
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
|
|
public function getStore(): ?Store
|
|
{
|
|
$host = Uri::of($this->url)->host();
|
|
|
|
return Store::findByDomain($host);
|
|
}
|
|
|
|
protected function scrapeOption(WebScraperInterface $scraper, array $options, string $field): ?string
|
|
{
|
|
try {
|
|
$compiled = $this->schemaCompiler->compileField(
|
|
$field,
|
|
FieldExtractionDto::fromArray($options),
|
|
$scraper
|
|
);
|
|
|
|
return data_get($compiled, 'value');
|
|
} catch (DomSelectorException $e) {
|
|
$this->errorLog('Error scraping URL', [
|
|
'url' => $this->url,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
$this->errorNotification($e->getMessage());
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static function getMethodFromType(string $type): string
|
|
{
|
|
return match ($type) {
|
|
'css', ScraperStrategyType::Selector->value => 'getSelector',
|
|
ScraperStrategyType::Regex->value => 'getRegex',
|
|
ScraperStrategyType::Json->value => 'getJson',
|
|
ScraperStrategyType::xPath->value => 'getXpath',
|
|
ScraperStrategyType::SchemaOrg->value => 'getSchemaOrg',
|
|
default => 'getSelector'
|
|
};
|
|
}
|
|
|
|
public static function parseSelector(string $selector): array
|
|
{
|
|
// If starts with exclamation !, return unsanitized HTML.
|
|
if (str_starts_with($selector, self::SELECTOR_HTML_PREFIX)) {
|
|
$selector = substr($selector, 1) ?: '';
|
|
|
|
return [$selector, 'html'];
|
|
}
|
|
|
|
// If contains a pipe | extract attribute.
|
|
if (! str_contains($selector, self::SELECTOR_ATTR_DELIMITER)) {
|
|
return [$selector, 'text'];
|
|
}
|
|
|
|
// We get the attribute value from the selector assuming format is
|
|
// .selector|attribute
|
|
$parts = explode(self::SELECTOR_ATTR_DELIMITER, $selector);
|
|
$attr = array_pop($parts);
|
|
|
|
return [implode(self::SELECTOR_ATTR_DELIMITER, $parts), 'attr', [$attr]];
|
|
}
|
|
|
|
protected function errorNotification(string $message): void
|
|
{
|
|
if (! $this->sendUiNotifications) {
|
|
return;
|
|
}
|
|
|
|
Notification::make()
|
|
->title('Scrape error')
|
|
->body($message)
|
|
->danger()
|
|
->send();
|
|
}
|
|
|
|
protected function errorLog(string $message, array $data = []): void
|
|
{
|
|
if (! $this->logErrors) {
|
|
return;
|
|
}
|
|
|
|
$this->logger->error($message, $data);
|
|
}
|
|
|
|
public function getRequestTimeout(): int
|
|
{
|
|
return $this->scraperRequestTimeout;
|
|
}
|
|
|
|
public function setRequestTimeout(int $scraperRequestTimeout): self
|
|
{
|
|
$this->scraperRequestTimeout = $scraperRequestTimeout;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getConnectTimeout(): int
|
|
{
|
|
return $this->scraperConnectTimeout;
|
|
}
|
|
|
|
public function setConnectTimeout(int $scraperConnectTimeout): self
|
|
{
|
|
$this->scraperConnectTimeout = $scraperConnectTimeout;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* If a scraped field is greater than the max length, return null. This protects the db
|
|
* against incorrect and long strings for url or image, both can't be cropped.
|
|
*/
|
|
public static function preSaveMaxLength(?string $value): ?string
|
|
{
|
|
return $value && strlen($value) < self::MAX_STR_LENGTH ? $value : null;
|
|
}
|
|
|
|
/**
|
|
* For fields that can be truncated, truncate them, eg title attribute. Like preSaveMaxLength,
|
|
* protect the db from long strings.
|
|
*/
|
|
public static function preSaveTruncate(?string $value): ?string
|
|
{
|
|
return Str::limit($value, self::MAX_STR_LENGTH);
|
|
}
|
|
}
|