Skip to content

Commit 3aabbbd

Browse files
committed
Add Amazon Product & Amazon Search Support
1 parent 728c3ae commit 3aabbbd

7 files changed

+380
-8
lines changed

config/scrapingbee.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@
1111

1212
'walmart_search_base_url' => env('SCRAPINGBEE_WALMART_SEARCH_BASE_URL', 'https://app.scrapingbee.com/api/v1/walmart/search'),
1313
'walmart_product_base_url' => env('SCRAPINGBEE_WALMART_PRODUCT_BASE_URL', 'https://app.scrapingbee.com/api/v1/walmart/product'),
14+
15+
'amazon_search_base_url' => env('SCRAPINGBEE_AMAZON_SEARCH_BASE_URL', 'https://app.scrapingbee.com/api/v1/amazon/search'),
16+
'amazon_product_base_url' => env('SCRAPINGBEE_AMAZON_PRODUCT_BASE_URL', 'https://app.scrapingbee.com/api/v1/amazon/product'),
1417
];
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
namespace Ziming\LaravelScrapingBee;
4+
5+
use Illuminate\Http\Client\ConnectionException;
6+
use Illuminate\Http\Client\Response;
7+
use Illuminate\Support\Facades\Http;
8+
use Illuminate\Support\Traits\Conditionable;
9+
10+
final class LaravelScrapingBeeAmazonProduct
11+
{
12+
use Conditionable;
13+
14+
private readonly string $baseUrl;
15+
private readonly string $apiKey;
16+
17+
private array $params = [];
18+
19+
public static function make(#[\SensitiveParameter] ?string $apiKey = null): self
20+
{
21+
return new self($apiKey);
22+
}
23+
24+
public function __construct(#[\SensitiveParameter] ?string $apiKey = null)
25+
{
26+
// If somebody pass '' into the constructor, we should use '' as the api key
27+
// even if it doesn't make sense.
28+
// If $apiKey is null, then we use the 1 in the config file.
29+
$this->apiKey = $apiKey ?? config('scrapingbee.api_key');
30+
31+
$this->baseUrl = config(
32+
'scrapingbee.amazon_product_base_url',
33+
'https://app.scrapingbee.com/api/v1/amazon/product'
34+
);
35+
}
36+
37+
/**
38+
* @throws ConnectionException
39+
*/
40+
public function get(): Response
41+
{
42+
$this->params['api_key'] = $this->apiKey;
43+
$response = Http::get($this->baseUrl, $this->params);
44+
$this->reset();
45+
46+
return $response;
47+
}
48+
49+
/**
50+
* https://www.scrapingbee.com/documentation/amazon/#light_request
51+
*/
52+
public function lightRequest(bool $lightRequest = true): self
53+
{
54+
$this->params['light_request'] = $lightRequest;
55+
56+
return $this;
57+
}
58+
59+
/**
60+
* https://www.scrapingbee.com/documentation/amazon/#query
61+
*/
62+
public function query(string $productAsin): self
63+
{
64+
$this->params['query'] = $productAsin;
65+
66+
return $this;
67+
}
68+
69+
/**
70+
* https://www.scrapingbee.com/documentation/amazon/#device_AmazonProduct
71+
*/
72+
public function device(string $device): self
73+
{
74+
$this->params['device'] = $device;
75+
76+
return $this;
77+
}
78+
79+
/**
80+
* https://www.scrapingbee.com/documentation/amazon/#domain
81+
*/
82+
public function domain(string $domain): self
83+
{
84+
$this->params['domain'] = $domain;
85+
86+
return $this;
87+
}
88+
89+
/**
90+
* https://www.scrapingbee.com/documentation/amazon/#country_AmazonProduct
91+
*/
92+
public function country(string $country): self
93+
{
94+
$this->params['country'] = $country;
95+
96+
return $this;
97+
}
98+
99+
/**
100+
* https://www.scrapingbee.com/documentation/amazon/#zip_code_AmazonProduct
101+
*/
102+
public function zipCode(string $zipCode): self
103+
{
104+
$this->params['zip_code'] = $zipCode;
105+
106+
return $this;
107+
}
108+
109+
/**
110+
* https://www.scrapingbee.com/documentation/amazon/#language
111+
*/
112+
public function language(string $language): self
113+
{
114+
$this->params['language'] = $language;
115+
116+
return $this;
117+
}
118+
119+
/**
120+
* https://www.scrapingbee.com/documentation/amazon/#currency
121+
*/
122+
public function currency(string $currency): self
123+
{
124+
$this->params['currency'] = $currency;
125+
126+
return $this;
127+
}
128+
129+
/**
130+
* https://www.scrapingbee.com/documentation/walmart/#add_html_WalmartAPIProduct
131+
*/
132+
public function addHtml(): self
133+
{
134+
$this->params['add_html'] = true;
135+
136+
return $this;
137+
}
138+
139+
/*
140+
* If the API hasn't caught up, and you need to support a new ScrapingBee parameter,
141+
* you can set it using this method.
142+
*/
143+
public function setParam(string $key, mixed $value): self
144+
{
145+
$this->params[$key] = $value;
146+
147+
return $this;
148+
}
149+
150+
private function reset(): self
151+
{
152+
$this->params = [];
153+
154+
return $this;
155+
}
156+
}
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ziming\LaravelScrapingBee;
6+
7+
use Illuminate\Http\Client\ConnectionException;
8+
use Illuminate\Http\Client\Response;
9+
use Illuminate\Support\Facades\Http;
10+
use Illuminate\Support\Traits\Conditionable;
11+
12+
final class LaravelScrapingBeeAmazonSearch
13+
{
14+
use Conditionable;
15+
16+
private readonly string $baseUrl;
17+
private readonly string $apiKey;
18+
19+
private array $params = [];
20+
21+
public static function make(#[\SensitiveParameter] ?string $apiKey = null): self
22+
{
23+
return new self($apiKey);
24+
}
25+
26+
public function __construct(#[\SensitiveParameter] ?string $apiKey = null)
27+
{
28+
// If somebody pass '' into the constructor, we should use '' as the api key
29+
// even if it doesn't make sense.
30+
// If $apiKey is null, then we use the 1 in the config file.
31+
$this->apiKey = $apiKey ?? config('scrapingbee.api_key');
32+
33+
$this->baseUrl = config(
34+
'scrapingbee.walmart_search_base_url',
35+
'https://app.scrapingbee.com/api/v1/amazon/search'
36+
);
37+
}
38+
39+
/**
40+
* @throws ConnectionException
41+
*/
42+
public function get(): Response
43+
{
44+
$this->params['api_key'] = $this->apiKey;
45+
$response = Http::get($this->baseUrl, $this->params);
46+
$this->reset();
47+
48+
return $response;
49+
}
50+
51+
/**
52+
* https://www.scrapingbee.com/documentation/amazon/#light_request_AmazonSearch
53+
*/
54+
public function lightRequest(bool $lightRequest = true): self
55+
{
56+
$this->params['light_request'] = $lightRequest;
57+
58+
return $this;
59+
}
60+
61+
/**
62+
* https://www.scrapingbee.com/documentation/amazon/#query_AmazonSearch
63+
*/
64+
public function query(string $query): self
65+
{
66+
$this->params['query'] = $query;
67+
68+
return $this;
69+
}
70+
71+
/**
72+
* https://www.scrapingbee.com/documentation/amazon/#start_page
73+
*/
74+
public function startPage(int $page): self
75+
{
76+
$this->params['start_page'] = $page;
77+
78+
return $this;
79+
}
80+
81+
/**
82+
* https://www.scrapingbee.com/documentation/amazon/#pages
83+
*/
84+
public function pages(int $pages): self
85+
{
86+
$this->params['pages'] = $pages;
87+
88+
return $this;
89+
}
90+
91+
/**
92+
* https://www.scrapingbee.com/documentation/amazon/#sort_by
93+
*/
94+
public function sortBy(string $sortBy): self
95+
{
96+
$this->params['sort_by'] = $sortBy;
97+
98+
return $this;
99+
}
100+
101+
/**
102+
* https://www.scrapingbee.com/documentation/amazon/#device_AmazonSearch
103+
*/
104+
public function device(string $device): self
105+
{
106+
$this->params['device'] = $device;
107+
108+
return $this;
109+
}
110+
111+
/**
112+
* https://www.scrapingbee.com/documentation/amazon/#domain_AmazonSearch
113+
*/
114+
public function domain(string $domain): self
115+
{
116+
$this->params['domain'] = $domain;
117+
118+
return $this;
119+
}
120+
121+
/*
122+
* https://www.scrapingbee.com/documentation/amazon/#country_AmazonSearch
123+
*/
124+
public function country(string $isoCountryCode): self
125+
{
126+
$this->params['country'] = $isoCountryCode;
127+
128+
return $this;
129+
}
130+
131+
/**
132+
* https://www.scrapingbee.com/documentation/amazon/#zip_code_AmazonSearch
133+
*/
134+
public function zipCode(string $zipCode): self
135+
{
136+
$this->params['zip_code'] = $zipCode;
137+
138+
return $this;
139+
}
140+
141+
/**
142+
* https://www.scrapingbee.com/documentation/amazon/#language_AmazonSearch
143+
*/
144+
public function language(string $language): self
145+
{
146+
$this->params['language'] = $language;
147+
148+
return $this;
149+
}
150+
151+
/**
152+
* https://www.scrapingbee.com/documentation/amazon/#currency_AmazonSearch
153+
*/
154+
public function currency(string $currency): self
155+
{
156+
$this->params['currency'] = $currency;
157+
158+
return $this;
159+
}
160+
161+
/**
162+
* https://www.scrapingbee.com/documentation/amazon/#category_id
163+
*/
164+
public function categoryId(string $categoryId): self
165+
{
166+
$this->params['category_id'] = $categoryId;
167+
168+
return $this;
169+
}
170+
171+
/**
172+
* https://www.scrapingbee.com/documentation/amazon/#merchant_id
173+
*/
174+
public function merchantId(string $merchantId): self
175+
{
176+
$this->params['merchant_id'] = $merchantId;
177+
178+
return $this;
179+
}
180+
181+
/**
182+
* https://www.scrapingbee.com/documentation/amazon/#autoselect_variant
183+
*/
184+
public function autoSelectVariant(bool $autoSelectVariant = true): self
185+
{
186+
$this->params['autoselect_variant'] = $autoSelectVariant;
187+
188+
return $this;
189+
}
190+
191+
/**
192+
* https://www.scrapingbee.com/documentation/amazon/#add_html_AmazonSearch
193+
*/
194+
public function addHtml(): self
195+
{
196+
$this->params['add_html'] = true;
197+
198+
return $this;
199+
}
200+
201+
/*
202+
* If the API hasn't caught up and you need to support a new ScrapingBee parameter,
203+
* you can set it using this method.
204+
*/
205+
public function setParam(string $key, mixed $value): self
206+
{
207+
$this->params[$key] = $value;
208+
209+
return $this;
210+
}
211+
212+
private function reset(): self
213+
{
214+
$this->params = [];
215+
216+
return $this;
217+
}
218+
}

src/LaravelScrapingBeeGoogleSearch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function addHtml(): self
155155
}
156156

157157
/*
158-
* If the API hasn't caught up and you need to support a new ScrapingBee parameter,
158+
* If the API hasn't caught up, and you need to support a new ScrapingBee parameter,
159159
* you can set it using this method.
160160
*/
161161
public function setParam(string $key, mixed $value): self

0 commit comments

Comments
 (0)