mirror of
https://github.com/Cybrarist/Discount-Bandit.git
synced 2026-11-24 19:49:54 +01:00
add option to save the get params for store add option to specify which parameters to keep for store add testing for this parsing case.
188 lines
5.6 KiB
PHP
188 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Stores;
|
|
|
|
use App\Classes\StoreTemplate;
|
|
use App\Helpers\GeneralHelper;
|
|
use App\Helpers\LinkHelper;
|
|
use App\Models\Link;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Noon extends StoreTemplate
|
|
{
|
|
const string MAIN_URL = "https://[domain]/[country]-en/[product_key]/p/";
|
|
|
|
protected array $extra_headers = [
|
|
'Accept' => 'application/json',
|
|
"Cache-Control" => "no-cache",
|
|
"Accept-Language" => "en-US,en;q=0.5",
|
|
'Connection' => 'keep-alive',
|
|
];
|
|
|
|
public function __construct(Link $link, array $extra_headers = [], ?string $user_agent = '')
|
|
{
|
|
$this->chromium_crawler = true;
|
|
parent::__construct($link);
|
|
}
|
|
|
|
public static function prepare_url(Link $link, $extra = []): string
|
|
{
|
|
[$link_base, $link_params] = LinkHelper::prepare_base_key_and_params($link);
|
|
|
|
|
|
$country = Str::of($link->store->name)
|
|
->lower()
|
|
->explode(" ")
|
|
->last();
|
|
|
|
return Str::replace(
|
|
["[domain]", "[country]", "[product_key]"],
|
|
[$link->store->domain,
|
|
$country,
|
|
Str::upper($link_base)],
|
|
self::MAIN_URL)."?{$link_params}";
|
|
|
|
}
|
|
|
|
public function get_name(): void
|
|
{
|
|
|
|
if (isset($this->schema['product']['name'])) {
|
|
$this->product_data['name'] = $this->schema['product']['name'];
|
|
|
|
return;
|
|
}
|
|
|
|
$ids_and_tag_selector = [
|
|
'title',
|
|
'h1 span[class^="ProductTitle_title"]',
|
|
];
|
|
|
|
$results_using_ids = $this->dom->querySelectorAll(implode(',', $ids_and_tag_selector));
|
|
$this->product_data['name'] = $results_using_ids[0]?->textContent;
|
|
|
|
}
|
|
|
|
public function get_image(): void
|
|
{
|
|
|
|
if (isset($this->schema['product']['image'])) {
|
|
$this->product_data['image'] = $this->schema['product']['image'][0];
|
|
|
|
return;
|
|
}
|
|
|
|
$ids_and_tag_selector = [
|
|
"img[alt='{$this->product_data['name']}']",
|
|
];
|
|
$results_using_ids = $this->dom->querySelectorAll(implode(',', $ids_and_tag_selector));
|
|
|
|
$attributes = [
|
|
'src',
|
|
];
|
|
|
|
foreach ($attributes as $attribute) {
|
|
$image_url = $results_using_ids[0]?->getAttribute($attribute);
|
|
if (! empty($image_url)) {
|
|
$this->product_data['image'] = $image_url;
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public function get_total_reviews(): void
|
|
{
|
|
|
|
if (isset($this->schema['product']['aggregateRating']['reviewCount'])) {
|
|
$this->product_data['total_reviews'] = $this->schema['product']['aggregateRating']['reviewCount'];
|
|
|
|
return;
|
|
}
|
|
|
|
$ids_and_tag_selector = [
|
|
'span[class^="RatingPreviewStarV2_countText"]',
|
|
];
|
|
$results_using_ids = $this->dom->querySelectorAll(implode(',', $ids_and_tag_selector));
|
|
|
|
$this->product_data['total_reviews'] = GeneralHelper::get_numbers_only($results_using_ids[0]?->textContent);
|
|
}
|
|
|
|
public function get_rating(): void
|
|
{
|
|
if (isset($this->schema['product']['aggregateRating']['reviewCount'])) {
|
|
$this->product_data['rating'] = $this->schema['product']['aggregateRating']['ratingValue'];
|
|
}
|
|
|
|
$general_selectors = [
|
|
'span[class^="RatingPreviewStarV2_text"]',
|
|
];
|
|
|
|
$results = $this->dom->querySelectorAll(implode(',', $general_selectors));
|
|
|
|
$this->product_data['rating'] = GeneralHelper::get_numbers_only_with_dot($results[0]?->textContent);
|
|
}
|
|
|
|
public function get_seller(): void
|
|
{
|
|
if (isset($this->schema['product']['offers'])) {
|
|
$this->product_data['seller'] = $this->schema['product']['offers'][0]['seller']['name'] ?? "";
|
|
$this->product_data['is_official'] = str_contains($this->product_data['seller'], 'Noon');
|
|
|
|
return;
|
|
}
|
|
|
|
$general_selectors = [
|
|
"strong[class^='PartnerRatingsV2_soldBy']",
|
|
];
|
|
|
|
$results = $this->dom->querySelectorAll(implode(',', $general_selectors));
|
|
|
|
$this->product_data['seller'] = Str::trim($results[0]?->textContent);
|
|
$this->product_data['is_official'] = str_contains($this->product_data['seller'], 'Noon');
|
|
}
|
|
|
|
public function get_price(): void
|
|
{
|
|
|
|
if (isset($this->schema['product']['offers'])) {
|
|
$this->product_data['price'] = (float) $this->schema['product']['offers'][0]['price'] ?? 0;
|
|
|
|
return;
|
|
}
|
|
|
|
$selectors = [
|
|
'span[class^="PriceOfferV2_priceNowText"]',
|
|
];
|
|
|
|
$results = $this->dom->querySelectorAll(implode(',', $selectors));
|
|
|
|
$this->product_data['price'] = (float) GeneralHelper::get_numbers_only_with_dot($results[0]?->textContent);
|
|
|
|
}
|
|
|
|
public function get_used_price(): void {}
|
|
|
|
public function get_stock(): void
|
|
{
|
|
if (isset($this->schema['product']['offers'])) {
|
|
$this->product_data['is_in_stock'] = Str::contains($this->schema['product']['offers'][0]['availability'], 'InStock', true);
|
|
|
|
return;
|
|
}
|
|
$this->product_data['is_in_stock'] = $this->product_data['price'] > 0 || $this->product_data['used_price'] > 0;
|
|
}
|
|
|
|
public function get_shipping_price(): void {}
|
|
|
|
public function get_condition(): void
|
|
{
|
|
|
|
if (isset($this->schema['product']['offers'])) {
|
|
$this->product_data['condition'] = Str::contains($this->schema['product']['offers'][0]['itemCondition'], 'NewCondition', true) ? 'New' : 'Used';
|
|
}
|
|
}
|
|
|
|
public function other_method_if_system_detected_as_bot(): void {}
|
|
}
|