Skip to content

Commit 32485e5

Browse files
authored
Merge pull request #55 from Cybrarist/v3.5
V3.5
2 parents f6ecd3e + 977f928 commit 32485e5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1960
-256
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/public/build
44
/public/hot
55
/public/storage
6-
/storage/*.key
76
/vendor
87
.env
98
.env.backup

app/Filament/Resources/ProductResource/Pages/CreateProduct.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,45 @@
1111

1212
class CreateProduct extends CreateRecord
1313
{
14-
1514
protected static string $resource = ProductResource::class;
1615

17-
1816
private URLHelper $product_url;
1917

20-
2118
protected function mutateFormDataBeforeCreate(array $data): array
2219
{
23-
$this->product_url=new URLHelper($data['url']);
20+
$this->product_url = new URLHelper($data['url']);
2421

25-
if(!StoreHelper::is_unique($this->product_url))
22+
if (! StoreHelper::is_unique($this->product_url)) {
2623
$this->halt();
24+
}
2725

2826
return $data;
2927
}
3028

31-
protected function afterCreate() : void
29+
protected function afterCreate(): void
3230
{
3331

34-
$product_store=ProductStore::updateOrCreate([
32+
$product_store = ProductStore::updateOrCreate([
3533
"store_id" => $this->product_url->store->id,
3634
"product_id" => $this->record->id,
37-
"key"=>$this->product_url->product_unique_key
38-
],[
35+
"key" => $this->product_url->product_unique_key,
36+
], [
3937
"notify_price" => $this->data['notify_price'] ?? 0,
4038
"notify_percentage" => $this->data['notify_percentage'] ?? 0,
4139
]);
4240

4341
$product_store->load([
44-
"store"
42+
"store",
4543
]);
4644

47-
if (!empty($this->data['variation_options'])){
48-
StoreTemplate::insert_variation( $this->data["variation_options"] , $product_store->store , $this->data);
45+
if (! empty($this->data['variation_options'])) {
46+
StoreTemplate::insert_variation($this->data["variation_options"], $product_store->store, $this->data);
4947
}
5048

5149
}
5250

53-
5451
protected function getRedirectUrl(): string
5552
{
5653
return $this->getResource()::getUrl('index');
57-
}
58-
59-
54+
}
6055
}

app/Filament/Resources/ProductResource/RelationManagers/StoresRelationManager.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -129,30 +129,30 @@ public function table(Table $table): Table
129129
}),
130130
])
131131
->actions([
132-
Tables\Actions\EditAction::make()->form(fn (Tables\Actions\EditAction $action): array => [
133-
Forms\Components\Checkbox::make('add_shipping')
134-
->label('Shipping'),
135-
136-
Forms\Components\TextInput::make('price')
137-
->disabled()
138-
->dehydrated(false)
139-
->label('Current Price')
140-
->suffix(CurrencyHelper::get_currencies(currency_id: $action->getRecord()->currency_id)),
141-
142-
Forms\Components\TextInput::make('notify_price')
143-
->numeric()
144-
->step(0.01)
145-
->label('Notify when cheaper than')
146-
->suffix(CurrencyHelper::get_currencies(currency_id: $action->getRecord()->currency_id)),
147-
148-
])->using(function ($record, array $data) {
149-
$record->products()->updateExistingPivot($this->ownerRecord->id, [
150-
'notify_price' => $data['notify_price'] * 100,
151-
'add_shipping' => $data['add_shipping'],
152-
]);
153-
154-
return $record;
155-
}),
132+
Tables\Actions\EditAction::make()
133+
->form(fn (Tables\Actions\EditAction $action): array => [
134+
Forms\Components\Checkbox::make('add_shipping')
135+
->label('Shipping'),
136+
137+
Forms\Components\TextInput::make('price')
138+
->disabled()
139+
->dehydrated(false)
140+
->label('Current Price')
141+
->suffix(CurrencyHelper::get_currencies(currency_id: $action->getRecord()->currency_id)),
142+
143+
Forms\Components\TextInput::make('notify_price')
144+
->numeric()
145+
->step(0.01)
146+
->label('Notify when cheaper than')
147+
->suffix(CurrencyHelper::get_currencies(currency_id: $action->getRecord()->currency_id)),
148+
])->using(function ($record, array $data) {
149+
$record->products()->updateExistingPivot($this->ownerRecord->id, [
150+
'notify_price' => $data['notify_price'] * 100,
151+
'add_shipping' => $data['add_shipping'],
152+
]);
153+
154+
return $record;
155+
}),
156156

157157
Tables\Actions\DetachAction::make()->label("Remove")->after(function ($record) {
158158
if (ProductStore::where([

app/Helpers/CurrencyHelper.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77

88
class CurrencyHelper
99
{
10-
public static function get_currencies($currency_id=null){
10+
public static function get_currencies($currency_id = null)
11+
{
1112

12-
$currencies= Cache::remember('currencies', 86400, function() {
13-
return Currency::pluck("code", "id")->toArray();
13+
$currencies = Cache::remember('currencies', 86400, function () {
14+
return Currency::pluck("code", "id")->toArray();
1415
});
1516

16-
if ($currency_id)
17+
if ($currency_id) {
1718
return $currencies[$currency_id];
19+
}
1820

1921
return $currencies;
2022
}
21-
2223
}

app/Helpers/GeneralHelper.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
namespace App\Helpers;
44

5-
use Illuminate\Support\Str;
6-
75
class GeneralHelper
86
{
9-
107
public static function get_numbers_only_with_dot($string): array|string|null
118
{
129
return preg_replace('/[^0-9.]/', '', $string);
@@ -17,14 +14,16 @@ public static function get_numbers_only($string): array|string|null
1714
return preg_replace('/[^0-9]/', '', $string);
1815
}
1916

20-
public static function get_value_from_meta_tag(array $meta_items, string $key , string $attribute): string
17+
public static function get_value_from_meta_tag(array $meta_items, string $key, string $attribute): string
2118
{
22-
foreach ($meta_items as $meta)
23-
foreach ($meta->attributes() as $value)
24-
if ($value == $key)
19+
foreach ($meta_items as $meta) {
20+
foreach ($meta->attributes() as $value) {
21+
if ($value == $key) {
2522
return $meta->attributes()[$attribute]->__toString();
23+
}
24+
}
25+
}
2626

2727
return "";
2828
}
29-
3029
}
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<?php
2+
3+
namespace App\Helpers\StoresAvailable;
4+
5+
use Illuminate\Support\Str;
6+
use Throwable;
7+
8+
class Eprice extends StoreTemplate
9+
{
10+
const string MAIN_URL = "https://store/product_id";
11+
12+
private $product_schema;
13+
14+
private $initial_data;
15+
16+
public function __construct(int $product_store_id)
17+
{
18+
parent::__construct($product_store_id);
19+
}
20+
21+
// define crawler
22+
public function crawler(): void
23+
{
24+
parent::crawl_url(extra_headers: [
25+
'Accept' => 'application/json',
26+
'DNT' => 1,
27+
"Cache-Control" => "no-cache",
28+
'Sec-Fetch-User' => '1',
29+
"Accept-Language" => "en-US,en;q=0.5",
30+
'Connection' => 'keep-alive',
31+
"Accept-Encoding" => "gzip, deflate",
32+
]);
33+
}
34+
35+
public function prepare_sections_to_crawl(): void
36+
{
37+
if ($this->is_system_detected_as_robot()) {
38+
parent::crawl_url_chrome();
39+
}
40+
41+
try {
42+
$this->product_schema = $this->get_product_schema(script_type: 'application/ld+json');
43+
} catch (Throwable $exception) {
44+
$this->log_error("Crawling Eprice Schema", $exception->getMessage());
45+
}
46+
47+
try {
48+
$this->initial_data = json_decode($this->document->getElementById('initial-data')->attributes->getNamedItem('data-json')->textContent, true);
49+
} catch (Throwable $exception) {
50+
$this->log_error("Crawling Eprice Schema", $exception->getMessage());
51+
}
52+
}
53+
54+
/**
55+
* Get the data from the store
56+
*/
57+
public function get_name(): void
58+
{
59+
try {
60+
$this->name = $this->product_schema['object']['name'];
61+
62+
return;
63+
} catch (Throwable $exception) {
64+
$this->log_error("Product Name First Method", $exception->getMessage());
65+
}
66+
67+
try {
68+
$this->name = $this->initial_data["page"]["metaInfo"]["title"];
69+
70+
return;
71+
} catch (Throwable $exception) {
72+
$this->log_error("Product Name Second Method", $exception->getMessage());
73+
}
74+
}
75+
76+
public function get_image(): void
77+
{
78+
try {
79+
$this->image = $this->product_schema['object']['image'];
80+
81+
return;
82+
} catch (Throwable $exception) {
83+
$this->log_error("Product Image First Method", $exception->getMessage());
84+
}
85+
86+
try {
87+
$this->image = $this->initial_data["page"]["product"]["media"][0]["uri"];
88+
89+
return;
90+
} catch (Throwable $exception) {
91+
$this->log_error("Product Name Second Method", $exception->getMessage());
92+
}
93+
94+
}
95+
96+
public function get_price(): void
97+
{
98+
try {
99+
$this->price = (float) $this->product_schema['object']['offers']['priceSpecification'][0]['price'];
100+
101+
return;
102+
} catch (Throwable $exception) {
103+
$this->log_error("Price First Method", $exception->getMessage());
104+
}
105+
106+
try {
107+
$this->price = (float) $this->initial_data["page"]["product"]["price"] / 100;
108+
109+
return;
110+
} catch (Throwable $exception) {
111+
$this->log_error("Price First Method", $exception->getMessage());
112+
}
113+
114+
}
115+
116+
public function get_used_price(): void {}
117+
118+
public function get_stock(): void
119+
{
120+
try {
121+
$this->in_stock = Str::contains($this->product_schema['object']['offers']['availability'], "InStock", true);
122+
123+
return;
124+
} catch (Throwable $exception) {
125+
$this->log_error("Stock Availability First Method", $exception->getMessage());
126+
}
127+
128+
}
129+
130+
public function get_no_of_rates(): void
131+
{
132+
try {
133+
$this->no_of_rates = (int) $this->product_schema['object']['aggregateRating']['reviewCount'];
134+
} catch (Throwable $exception) {
135+
$this->log_error("Number of Rates First Method", $exception->getMessage());
136+
}
137+
138+
try {
139+
$this->no_of_rates = (int) $this->initial_data["page"]["reviews"]["reviews"];
140+
} catch (Throwable $exception) {
141+
$this->log_error("Number of Rates Second Method", $exception->getMessage());
142+
}
143+
}
144+
145+
public function get_rate(): void
146+
{
147+
try {
148+
$this->rating = $this->product_schema['object']['aggregateRating']['ratingValue'];
149+
} catch (Throwable $exception) {
150+
$this->log_error("Number of Rates First Method", $exception->getMessage());
151+
}
152+
153+
try {
154+
$this->rating = $this->initial_data["page"]["reviews"]["averageRating"];
155+
} catch (Throwable $exception) {
156+
$this->log_error("Number of Rates Second Method", $exception->getMessage());
157+
}
158+
}
159+
160+
public function get_seller(): void
161+
{
162+
try {
163+
$this->seller = $this->initial_data['page']['product']['currentOffer']['ShopName'];
164+
} catch (Throwable $exception) {
165+
$this->log_error("The Seller First Method", $exception->getMessage());
166+
}
167+
}
168+
169+
public function get_shipping_price(): void {}
170+
171+
public function get_condition(): void {}
172+
173+
public static function get_variations($url): array
174+
{
175+
return [];
176+
}
177+
178+
public static function prepare_url($domain, $product, $store = null): string
179+
{
180+
return Str::replace(
181+
["store", "product_id"],
182+
[$domain, $product],
183+
self::MAIN_URL
184+
);
185+
186+
}
187+
188+
public function is_system_detected_as_robot(): bool
189+
{
190+
return Str::contains($this->document->textContent, "Access Denied", true);
191+
}
192+
}

0 commit comments

Comments
 (0)