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

Commit de45387

Browse files
authored
Use specific event to update dataset in configuration process
1 parent 6cbda18 commit de45387

File tree

5 files changed

+41
-21
lines changed

5 files changed

+41
-21
lines changed

Diff for: README.md

+8
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@ $event->scrapeRequest->url // Url scraped
185185
$event->scrapeRequest->type // Request type
186186
```
187187

188+
## Advanced usage
189+
190+
There is another event named `ConfigurationScraped` that is triggered when a scrape is done automatically in the
191+
reconfiguration step. It is exactly the same than `Scraped` event. It is named differently because usually it is not
192+
interesting to use it apart from internally to update the dataset.
193+
194+
`ConfigurationScraped` can be used to do updates or to know internals about the configuration process.
195+
188196
### Queue workers
189197

190198
You need to workers, one for the default queue and another for the `configure` queue. The `configure` worker

Diff for: src/Scraper/Application/Configurator.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Goutte\Client;
66
use Illuminate\Support\Collection;
77
use Illuminate\Support\Facades\Log;
8+
use Softonic\LaravelIntelligentScraper\Scraper\Events\ConfigurationScraped;
9+
use Softonic\LaravelIntelligentScraper\Scraper\Events\ScrapeRequest;
810
use Softonic\LaravelIntelligentScraper\Scraper\Exceptions\ConfigurationException;
911
use Softonic\LaravelIntelligentScraper\Scraper\Models\Configuration;
1012
use Softonic\LaravelIntelligentScraper\Scraper\Models\ScrapedDataset;
@@ -114,7 +116,14 @@ private function findConfigByScrapedData($scrapedData, $crawler, $currentConfigu
114116
}
115117
}
116118

117-
$this->updateVariant($scrapedData);
119+
event(new ConfigurationScraped(
120+
new ScrapeRequest(
121+
$scrapedData['url'],
122+
$scrapedData['type']
123+
),
124+
$scrapedData['data'],
125+
$this->variantGenerator->getId($scrapedData['type'])
126+
));
118127

119128
return $result;
120129
}
@@ -175,10 +184,4 @@ private function checkConfiguration($data, Collection $finalConfig)
175184
throw new ConfigurationException("Field(s) \"{$fieldsMissing}\" not found.", 0);
176185
}
177186
}
178-
179-
private function updateVariant($scrapedData): void
180-
{
181-
$scrapedData['variant'] = $this->variantGenerator->getId($scrapedData['type']);
182-
$scrapedData->save();
183-
}
184187
}

Diff for: src/Scraper/Events/ConfigurationScraped.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Softonic\LaravelIntelligentScraper\Scraper\Events;
4+
5+
class ConfigurationScraped extends Scraped
6+
{
7+
}

Diff for: src/ScraperProvider.php

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Foundation\Support\Providers\EventServiceProvider;
66
use Softonic\LaravelIntelligentScraper\Scraper\Application\XpathBuilder;
7+
use Softonic\LaravelIntelligentScraper\Scraper\Events\ConfigurationScraped;
78
use Softonic\LaravelIntelligentScraper\Scraper\Events\InvalidConfiguration;
89
use Softonic\LaravelIntelligentScraper\Scraper\Events\Scraped;
910
use Softonic\LaravelIntelligentScraper\Scraper\Events\ScrapeRequest;
@@ -28,6 +29,9 @@ class ScraperProvider extends EventServiceProvider
2829
Scraped::class => [
2930
UpdateDataset::class,
3031
],
32+
ConfigurationScraped::class => [
33+
UpdateDataset::class,
34+
],
3135
];
3236

3337
/**

Diff for: tests/Unit/Scraper/Application/ConfiguratorTest.php

+12-14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Foundation\Testing\DatabaseMigrations;
77
use Illuminate\Support\Facades\Log;
88
use Mockery\Mock;
9+
use Softonic\LaravelIntelligentScraper\Scraper\Events\ConfigurationScraped;
910
use Softonic\LaravelIntelligentScraper\Scraper\Exceptions\ConfigurationException;
1011
use Softonic\LaravelIntelligentScraper\Scraper\Models\Configuration as ConfigurationModel;
1112
use Softonic\LaravelIntelligentScraper\Scraper\Models\ScrapedDataset;
@@ -150,18 +151,18 @@ public function whenTryToFindNewXpathButNotFoundItShouldLogItAndResetVariant()
150151
$this->variantGenerator->shouldReceive('fieldNotFound')
151152
->once();
152153
$this->variantGenerator->shouldReceive('getId')
153-
->andReturnNull();
154+
->andReturn('');
154155

155156
Log::shouldReceive('warning')
156157
->with("Field 'author' with value 'My author' not found for 'https://test.c/123456789012'.");
157158

159+
$this->expectsEvents(ConfigurationScraped::class);
160+
158161
try {
159162
$this->configurator->configureFromDataset($posts);
160163
} catch (ConfigurationException $e) {
161164
$this->assertEquals('Field(s) "author" not found.', $e->getMessage());
162165
}
163-
164-
$this->assertNull($posts[0]['variant']);
165166
}
166167

167168
/**
@@ -225,18 +226,18 @@ public function whenUseSomeOldXpathButNotFoundNewsItShouldLogItAndResetVariant()
225226
$this->variantGenerator->shouldReceive('fieldNotFound')
226227
->once();
227228
$this->variantGenerator->shouldReceive('getId')
228-
->andReturnNull();
229+
->andReturn('');
229230

230231
Log::shouldReceive('warning')
231232
->with("Field 'author' with value 'My author' not found for 'https://test.c/123456789012'.");
232233

234+
$this->expectsEvents(ConfigurationScraped::class);
235+
233236
try {
234237
$this->configurator->configureFromDataset($posts);
235238
} catch (ConfigurationException $e) {
236239
$this->assertEquals('Field(s) "author" not found.', $e->getMessage());
237240
}
238-
239-
$this->assertNull($posts[0]['variant']);
240241
}
241242

242243
/**
@@ -306,22 +307,21 @@ public function whenTryToFindXpathInMultiplepostsAndNotFoundInAnyItShouldThrowAn
306307
$this->variantGenerator->shouldReceive('fieldNotFound')
307308
->times(4);
308309
$this->variantGenerator->shouldReceive('getId')
309-
->andReturnNull();
310+
->andReturn('');
310311

311312
Log::shouldReceive('warning')
312313
->with("Field 'title' with value 'My Title' not found for 'https://test.c/123456789012'.");
313314

314315
Log::shouldReceive('warning')
315316
->with("Field 'author' with value 'My author' not found for 'https://test.c/123456789012'.");
316317

318+
$this->expectsEvents(ConfigurationScraped::class);
319+
317320
try {
318321
$this->configurator->configureFromDataset($posts);
319322
} catch (ConfigurationException $e) {
320323
$this->assertEquals('Field(s) "title,author" not found.', $e->getMessage());
321324
}
322-
323-
$this->assertNull($posts[0]['variant']);
324-
$this->assertNull($posts[1]['variant']);
325325
}
326326

327327
/**
@@ -413,6 +413,8 @@ public function whenDiscoverDifferentXpathItShouldGetAllOfThemAndUpdateTheVarian
413413
$this->variantGenerator->shouldReceive('getId')
414414
->andReturn(10, 20, 30);
415415

416+
$this->expectsEvents(ConfigurationScraped::class);
417+
416418
$configurations = $this->configurator->configureFromDataset($posts);
417419

418420
$this->assertInstanceOf(ConfigurationModel::class, $configurations[0]);
@@ -436,9 +438,5 @@ public function whenDiscoverDifferentXpathItShouldGetAllOfThemAndUpdateTheVarian
436438
],
437439
array_values($configurations[1]['xpaths'])
438440
);
439-
440-
$this->assertEquals($posts[0]['variant'], 10);
441-
$this->assertEquals($posts[1]['variant'], 20);
442-
$this->assertEquals($posts[2]['variant'], 30);
443441
}
444442
}

0 commit comments

Comments
 (0)