Skip to content
This repository has been archived by the owner. It is now read-only.

Commit 3e50ee2

Browse files
authored
Add context for scrape request. (#19)
Add context for scrape request.
1 parent b90f468 commit 3e50ee2

File tree

8 files changed

+66
-29
lines changed

8 files changed

+66
-29
lines changed

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ matrix:
1010
env: STATIC_ANALYSIS=true VALIDATE_CODING_STYLE=true
1111
- php: 7.3
1212
env: STATIC_ANALYSIS=true VALIDATE_CODING_STYLE=true
13+
- php: 7.4
14+
env: STATIC_ANALYSIS=true VALIDATE_CODING_STYLE=true
1315
- php: master
1416
env: STATIC_ANALYSIS=true VALIDATE_CODING_STYLE=false
1517
allow_failures:

README.md

+47-6
Original file line numberDiff line numberDiff line change
@@ -199,23 +199,64 @@ After configure the scraper, you will be able to request an specific scrape usin
199199
scrape('https://test.c/p/my-objective', 'Item-definition-1');
200200
```
201201

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+
202212
The scrape will produce a `\Softonic\LaravelIntelligentScraper\Scraper\Events\Scraped` event if all worked as expected.
203213
So attach a listener to that event to receive the data.
204214

205215
```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.
210221
```
211222

212223
All the output fields are arrays that can contain one or more results.
213224

214225
If the scrape fails a `\Softonic\LaravelIntelligentScraper\Scraper\Events\ScrapeFailed` event is fired with the
215226
scrape request information.
216227
```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+
];
219260
```
220261

221262
## Advanced usage

src/Scraper/Events/InvalidConfiguration.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ public function __construct(ScrapeRequest $scrapeRequest)
2525
*/
2626
public function tags()
2727
{
28-
$type = $this->scrapeRequest->type;
29-
3028
return [
31-
"reconfigure_type:$type",
29+
"reconfigure_type:{$this->scrapeRequest->type}",
3230
];
3331
}
3432
}

src/Scraper/Events/ScrapeFailed.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ public function __construct(ScrapeRequest $scrapeRequest)
3535
*/
3636
public function tags()
3737
{
38-
$type = $this->scrapeRequest->type;
39-
4038
return [
41-
"failed_type:$type",
39+
"failed_type:{$this->scrapeRequest->type}",
4240
];
4341
}
4442
}

src/Scraper/Events/ScrapeRequest.php

+11-8
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,23 @@ class ScrapeRequest
1919
*/
2020
public $type;
2121

22+
/**
23+
* @var array
24+
*/
25+
public $context;
26+
2227
/**
2328
* Create a new event instance.
2429
*
2530
* @param string $url
2631
* @param string $type
32+
* @param array $context
2733
*/
28-
public function __construct(string $url, string $type)
34+
public function __construct(string $url, string $type, array $context = [])
2935
{
30-
$this->url = $url;
31-
$this->type = $type;
36+
$this->url = $url;
37+
$this->type = $type;
38+
$this->context = $context;
3239
}
3340

3441
/**
@@ -42,10 +49,6 @@ public function __construct(string $url, string $type)
4249
*/
4350
public function tags()
4451
{
45-
$type = $this->type;
46-
47-
return [
48-
"request_type:$type",
49-
];
52+
return ["request_type:{$this->type}"];
5053
}
5154
}

src/Scraper/Events/Scraped.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,9 @@ public function __construct(ScrapeRequest $scrapeRequest, array $data, string $v
4949
*/
5050
public function tags()
5151
{
52-
$type = $this->scrapeRequest->type;
53-
$variant = $this->variant;
54-
5552
return [
56-
"scraped_type:$type",
57-
"scraped_variant:$variant",
53+
"scraped_type:{$this->scrapeRequest->type}",
54+
"scraped_variant:{$this->variant}",
5855
];
5956
}
6057
}

src/Scraper/helpers.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ function regexp($regexp)
88
}
99

1010
if (!function_exists('scrape')) {
11-
function scrape($url, $type)
11+
function scrape($url, $type, $context = [])
1212
{
13-
event(new \Softonic\LaravelIntelligentScraper\Scraper\Events\ScrapeRequest($url, $type));
13+
event(new \Softonic\LaravelIntelligentScraper\Scraper\Events\ScrapeRequest($url, $type, $context));
1414
}
1515
}

src/config/scraper.php

-2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@
3535
*/
3636
'listeners' => [
3737
'scraped' => [
38-
//
3938
],
4039
'scrape-failed' => [
41-
//
4240
],
4341
],
4442
];

0 commit comments

Comments
 (0)