Skip to content

Commit a4fa59e

Browse files
committed
enable chromium crawler for best buy canada and see
add reset link functionality if name and image were fetched as error don't replace link name and image if crawled already fix route cache add fetch action to single link fix issue where price history doesn't show all the time by parsing date first fix product delete when category exists fix currency error if user selects a currency without an api key
1 parent eb58e99 commit a4fa59e

File tree

10 files changed

+71
-24
lines changed

10 files changed

+71
-24
lines changed

app/Classes/Crawler/ChromiumCrawler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(
3636

3737
$options = [
3838
//
39-
'connectionDelay' => 1,
39+
'connectionDelay' => 1.32,
4040
'headless' => false,
4141
'noSandbox' => true,
4242
"headers" => $extra_headers,

app/Classes/StoreTemplate.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ public function crawl_product(): void
9191
if ($this->chromium_crawler) {
9292
$this->dom = new ChromiumCrawler(
9393
url: $this->current_product_url,
94+
extra_headers: $this->extra_headers,
9495
timeout_ms: $this->chromium_options['timeout_ms'],
9596
page_event: $this->chromium_options['page'],
96-
extra_headers: $this->extra_headers,
9797
)->dom;
9898
} else {
9999
$this->dom = new SimpleCrawler(
@@ -190,6 +190,12 @@ public function update_product_details(): void
190190
->increment('notifications_sent');
191191
}
192192

193+
if (filled($this->link->name))
194+
unset($this->product_data['name']);
195+
196+
if (filled($this->link->image))
197+
unset($this->product_data['image']);
198+
193199
$this->link->update($this->product_data + [
194200
'highest_price' => ($this->product_data['price'] > $this->link->highest_price) ? $this->product_data['price'] : $this->link->highest_price,
195201
'lowest_price' => (($this->product_data['price'] &&

app/Classes/Stores/BestBuy.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ class BestBuy extends StoreTemplate
3434
public function __construct(Link $link, array $extra_headers = [], ?string $user_agent = '')
3535
{
3636

37-
// if ($this->link->store->domain === "bestbuy.ca") {
38-
// $this->chromium_crawler = true;
39-
// $this->extra_headers = $extra_headers + ['X-CLIENT-ID' => 'lib-price-browser'] + $this->extra_headers;
40-
// } else {
41-
$this->extra_headers = $extra_headers + $this->extra_headers;
42-
// }
43-
44-
$this->user_agent = ($user_agent) ?: UserAgentHelper::get_random_user_agent();
37+
if ($link->store->domain === "bestbuy.ca") {
38+
$this->chromium_crawler = true;
39+
$this->extra_headers = $extra_headers + ['X-CLIENT-ID' => 'lib-price-browser'] + $this->extra_headers;
40+
} else {
41+
$this->extra_headers = $extra_headers + $this->extra_headers;
42+
$this->user_agent = ($user_agent) ?: UserAgentHelper::get_random_user_agent();
43+
}
4544

4645
parent::__construct($link);
4746
}
@@ -62,7 +61,7 @@ public static function prepare_url(Link $link, array $extra = []): string
6261
return Str::replace(
6362
["[domain]", "[product_key]"],
6463
[$link->store->domain, $link_base],
65-
($link->store->domain == "bestbuy.com") ? self::MAIN_URL : self::CANADA_URL) ."?{$link_params}";
64+
($link->store->domain == "bestbuy.com") ? self::MAIN_URL : self::CANADA_URL)."?{$link_params}";
6665
}
6766

6867
public function get_name(): void
@@ -78,6 +77,7 @@ public function get_image(): void
7877
{
7978
$ids_and_tag_selector = [
8079
'.pdp-media-gallery img:first-child',
80+
'[class^="mediaGalleryGridArea"] img:first-child',
8181
];
8282
$results = $this->dom->querySelectorAll(implode(',', $ids_and_tag_selector));
8383

@@ -87,7 +87,7 @@ public function get_image(): void
8787

8888
foreach ($attributes as $attribute) {
8989
$image_url = $results[0]?->getAttribute($attribute);
90-
if (! empty($image_url)) {
90+
if (filled($image_url)) {
9191
$this->product_data['image'] = explode(';', $image_url)[0];
9292
break;
9393
}
@@ -143,12 +143,20 @@ public function get_price(): void
143143
'#corePrice_feature_div span.a-price-fraction',
144144
];
145145

146+
$full_price_selectors = [
147+
'[class^="style-module_price_"]',
148+
];
149+
146150
$results_whole = $this->dom->querySelectorAll(implode(',', $whole_price_selectors));
147151
$results_fraction = $this->dom->querySelectorAll(implode(',', $fractional_price_selectors));
152+
$full_price_results = $this->dom->querySelectorAll(implode(',', $full_price_selectors));
148153

149154
$only_whole = GeneralHelper::get_numbers_only($results_whole[0]?->textContent);
150155
$this->product_data['price'] = (float) "{$only_whole}.{$results_fraction[0]?->textContent}";
151156

157+
if (! $this->product_data['price']) {
158+
$this->product_data['price'] = (float) GeneralHelper::get_numbers_only_with_dot($full_price_results[0]?->textContent);
159+
}
152160
}
153161

154162
public function get_used_price(): void

app/Classes/Stores/Newegg.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function get_price(): void
139139
$currency_detected = $this->schema['offers']['priceCurrency'];
140140
$currency = Currency::firstWhere('code', $currency_detected);
141141

142-
if ($currency && $currency->code != $this->link->store->currency->code) {
142+
if ($currency && $currency->code != $this->link->store->currency->code && $currency->rate) {
143143
$this->product_data['price'] = ($this->product_data['price'] / $currency->rate) * $this->link->store->currency->rate;
144144
}
145145
}

app/Filament/Resources/Links/Pages/EditLink.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use App\Filament\Resources\Links\LinkResource;
66
use App\Helpers\LinkHelper;
7+
use App\Http\Controllers\Actions\FetchSingleLinkAction;
78
use Filament\Actions\Action;
9+
use Filament\Notifications\Notification;
810
use Filament\Resources\Pages\EditRecord;
911
use Filament\Support\Icons\Heroicon;
1012

@@ -19,6 +21,30 @@ protected function getHeaderActions(): array
1921
->outlined()
2022
->icon(Heroicon::Link)
2123
->url(fn ($record) => LinkHelper::get_url($record), true),
24+
25+
Action::make('reset_link')
26+
->icon(Heroicon::Backspace)
27+
->tooltip('Reset name and image, and fill them again on next fetch')
28+
->action(function ($record) {
29+
$record->update([
30+
'name' => null,
31+
'image' => null,
32+
]);
33+
34+
Notification::make()
35+
->title('name and image reseted successfully')
36+
->success()
37+
->send();
38+
39+
$this->fillForm();
40+
}),
41+
42+
43+
Action::make('Fetch')
44+
->icon(Heroicon::ArrowPath)
45+
->color('success')
46+
->action(fn ($record) => new FetchSingleLinkAction()->__invoke($record)),
47+
2248
];
2349
}
2450
}

app/Filament/Resources/Links/Tables/LinksTable.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public static function configure(Table $table, ?Model $product = null): Table
123123
->formatStateUsing(function ($record) {
124124
$price = $record->highest_price;
125125
$code = $record->store->currency->code;
126-
if (Auth::user()->currency_id) {
126+
if (Auth::user()->currency_id && Auth::user()->currency->rate) {
127127
$price = $price * Auth::user()->currency->rate / $record->store->currency->rate;
128128
$code = Auth::user()->currency->code;
129129
}
@@ -138,7 +138,7 @@ public static function configure(Table $table, ?Model $product = null): Table
138138
->formatStateUsing(function ($record) {
139139
$price = $record->price;
140140
$code = $record->store->currency->code;
141-
if (Auth::user()->currency_id) {
141+
if (Auth::user()->currency_id && Auth::user()->currency->rate) {
142142
$price = $price * Auth::user()->currency->rate / $record->store->currency->rate;
143143
$code = Auth::user()->currency->code;
144144
}
@@ -153,7 +153,7 @@ public static function configure(Table $table, ?Model $product = null): Table
153153
->formatStateUsing(function ($record) {
154154
$price = $record->used_price;
155155
$code = $record->store->currency->code;
156-
if (Auth::user()->currency_id) {
156+
if (Auth::user()->currency_id && Auth::user()->currency->rate) {
157157
$price = $price * Auth::user()->currency->rate / $record->store->currency->rate;
158158
$code = Auth::user()->currency->code;
159159
}
@@ -165,10 +165,10 @@ public static function configure(Table $table, ?Model $product = null): Table
165165

166166
TextColumn::make('lowest_price')
167167
->label('Lowest')
168-
->formatStateUsing(function ($record) {
168+
->formatStateUsing(callback: function ($record) {
169169
$price = $record->lowest_price;
170170
$code = $record->store->currency->code;
171-
if (Auth::user()->currency_id) {
171+
if (Auth::user()->currency_id && Auth::user()->currency->rate) {
172172
$price = $price * Auth::user()->currency->rate / $record->store->currency->rate;
173173
$code = Auth::user()->currency->code;
174174
}

app/Filament/Resources/ProductResource/Widgets/PriceHistoryChart.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Filament\Resources\ProductResource\Widgets;
44

55
use App\Models\Link;
6+
use Carbon\Carbon;
67
use Filament\Forms\Components\DatePicker;
78
use Filament\Schemas\Schema;
89
use Filament\Support\RawJs;
@@ -34,8 +35,8 @@ class PriceHistoryChart extends ApexChartWidget
3435
*/
3536
protected function getOptions(): array
3637
{
37-
$start_date = $this->filters['start_date'];
38-
$end_date = $this->filters['end_date'];
38+
$start_date = Carbon::parse($this->filters['start_date']);
39+
$end_date = Carbon::parse($this->filters['end_date']);
3940
$time_series = [];
4041

4142
Link::whereHas('products', function ($query) {

app/Filament/Resources/Products/Pages/EditProduct.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ protected function getHeaderActions(): array
1818
{
1919
return [
2020
DeleteAction::make()
21-
->icon(Heroicon::Trash),
21+
->icon(Heroicon::Trash)
22+
->action(function ($record) {
23+
$record->categories()->detach();
24+
$record->links()->detach();
25+
$record->delete();
26+
}),
2227

2328
Action::make('Fetch')
2429
->color('primary')

app/Filament/Resources/Products/Tables/ProductsTable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public static function configure(Table $table): Table
120120
TextColumn::make('links')
121121
->formatStateUsing(function ($state) {
122122
$price = $state->highest_price;
123-
if (Auth::user()->currency_id) {
123+
if (Auth::user()->currency_id && Auth::user()->currency->rate) {
124124
$price = $price * Auth::user()->currency->rate / $state->store->currency->rate;
125125
}
126126

@@ -137,7 +137,7 @@ public static function configure(Table $table): Table
137137
->formatStateUsing(function ($state) {
138138
$price = $state->price;
139139
$code = $state->store->currency->code;
140-
if (Auth::user()->currency_id) {
140+
if (Auth::user()->currency_id && Auth::user()->currency->rate) {
141141
$price = $price * Auth::user()->currency->rate / $state->store->currency->rate;
142142
$code = Auth::user()->currency->code;
143143
}
@@ -159,7 +159,7 @@ public static function configure(Table $table): Table
159159
->color('success')
160160
->formatStateUsing(function ($state) {
161161
$price = $state->lowest_price;
162-
if (Auth::user()->currency_id) {
162+
if (Auth::user()->currency_id && Auth::user()->currency->rate) {
163163
$price = $price * Auth::user()->currency->rate / $state->store->currency->rate;
164164
}
165165
return Number::format($price);

routes/api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// })->middleware('auth:sanctum');
1717
//
1818
Route::middleware('auth:sanctum')
19+
->name('api.')
1920
->group(function () {
2021
Route::post('/links/add', AddLinkFromBrowserPluginAction::class);
2122

0 commit comments

Comments
 (0)