@@ -199,23 +199,64 @@ After configure the scraper, you will be able to request an specific scrape usin
199
199
scrape('https://test.c/p/my-objective', 'Item-definition-1');
200
200
```
201
201
202
+ There is an optional parameter called ` context ` that allows you to set a context to the scrapeRequest so you will
203
+ be able to access that context in your listener. This is useful if you need some additional data (out of the scraped
204
+ data) to work in your listener.
205
+
206
+ ``` php
207
+ <?php
208
+
209
+ scrape('https://test.c/p/my-objective', 'Item-definition-1', ['id' => 'my-objective']);
210
+ ```
211
+
202
212
The scrape will produce a ` \Softonic\LaravelIntelligentScraper\Scraper\Events\Scraped ` event if all worked as expected.
203
213
So attach a listener to that event to receive the data.
204
214
205
215
``` php
206
- $event->scrapeRequest->url // Url scraped
207
- $event->scrapeRequest->type // Request type
208
- $event->data // Contains all the data in a [ 'fieldName' => 'value' ] format.
209
- $event->variant // Contains the page variation sha1 hash.
216
+ $event->scrapeRequest->url; // Url scraped
217
+ $event->scrapeRequest->type; // Request type
218
+ $event->scrapeRequest->context; // Context
219
+ $event->data; // Contains all the data in a [ 'fieldName' => 'value' ] format.
220
+ $event->variant; // Contains the page variation sha1 hash.
210
221
```
211
222
212
223
All the output fields are arrays that can contain one or more results.
213
224
214
225
If the scrape fails a ` \Softonic\LaravelIntelligentScraper\Scraper\Events\ScrapeFailed ` event is fired with the
215
226
scrape request information.
216
227
``` php
217
- $event->scrapeRequest->url // Url scraped
218
- $event->scrapeRequest->type // Request type
228
+ $event->scrapeRequest->url; // Url scraped
229
+ $event->scrapeRequest->type; // Request type
230
+ $event->scrapeRequest->context; // Context
231
+ ```
232
+
233
+ To attach the listener, you can use the Laravel listener configuration like:
234
+ ``` php
235
+ // providers/EventServiceProvider
236
+ protected $listen = [
237
+ Scraped::class => [
238
+ MyListener::class,
239
+ ],
240
+ ScrapeFailed::class => [
241
+ MyListenerFirFailedScrapes::class,
242
+ ],
243
+ ];
244
+ ```
245
+
246
+ But the scrapes from all types will go to that listeners. To simplify the listeners and just listen scrapes from a
247
+ single type, there is a ` listeners ` configuration available at scraper.php, so you can configure the listeners
248
+ with greater granularity.
249
+ ``` php
250
+ // config/scrapper.php
251
+ 'listeners' => [
252
+ 'scraped' => [
253
+ 'my-type-1' => ListenerForTypeOne::class,
254
+ 'my-type-2' => ListenerForTypeTwo::class,
255
+ ],
256
+ 'scrape-failed' => [
257
+ 'my-type-1' => ListenerFailedForTypeOne::class,
258
+ ],
259
+ ];
219
260
```
220
261
221
262
## Advanced usage
0 commit comments