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

Commit b90f468

Browse files
authored
Add support to store xpaths as string (#18)
There was a bug that avoid users to set xpaths as a simple string like '//div'. Now you don't need to use ['//div'] for single xpaths.
1 parent 2fd8449 commit b90f468

File tree

6 files changed

+20
-16
lines changed

6 files changed

+20
-16
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use GuzzleHttp\Exception\RequestException;
88
use Illuminate\Support\Facades\Log;
99
use Softonic\LaravelIntelligentScraper\Scraper\Exceptions\MissingXpathValueException;
10+
use Softonic\LaravelIntelligentScraper\Scraper\Models\Configuration;
1011

1112
class XpathFinder
1213
{
@@ -26,6 +27,12 @@ public function __construct(GoutteClient $client, VariantGenerator $variantGener
2627
$this->variantGenerator = $variantGenerator;
2728
}
2829

30+
/**
31+
* @param string $url
32+
* @param Configuration[] $configs
33+
*
34+
* @return array
35+
*/
2936
public function extract(string $url, $configs): array
3037
{
3138
$crawler = $this->getCrawler($url);

Diff for: src/Scraper/Listeners/Scrape.php

+4-12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Softonic\LaravelIntelligentScraper\Scraper\Listeners;
44

55
use Illuminate\Contracts\Queue\ShouldQueue;
6+
use Illuminate\Support\Collection;
67
use Psr\Log\LoggerInterface;
78
use Softonic\LaravelIntelligentScraper\Scraper\Application\XpathFinder;
89
use Softonic\LaravelIntelligentScraper\Scraper\Events\InvalidConfiguration;
@@ -52,12 +53,7 @@ public function handle(ScrapeRequest $scrapeRequest)
5253
}
5354
}
5455

55-
/**
56-
* @param ScrapeRequest $scrapeRequest
57-
*
58-
* @return mixed
59-
*/
60-
private function loadConfiguration(ScrapeRequest $scrapeRequest)
56+
private function loadConfiguration(ScrapeRequest $scrapeRequest): Collection
6157
{
6258
$this->logger->info("Loading scrapping configuration for type '$scrapeRequest->type'");
6359

@@ -69,15 +65,11 @@ private function loadConfiguration(ScrapeRequest $scrapeRequest)
6965
return $config;
7066
}
7167

72-
/**
73-
* @param ScrapeRequest $scrapeRequest
74-
* @param $config
75-
*/
76-
private function extractData(ScrapeRequest $scrapeRequest, $config): void
68+
private function extractData(ScrapeRequest $scrapeRequest, Collection $config): void
7769
{
7870
$this->logger->info("Extracting data from $scrapeRequest->url for type '$scrapeRequest->type'");
7971

80-
list('data' => $data, 'variant' => $variant) = $this->xpathFinder->extract($scrapeRequest->url, $config);
72+
['data' => $data, 'variant' => $variant] = $this->xpathFinder->extract($scrapeRequest->url, $config);
8173
event(new Scraped($scrapeRequest, $data, $variant));
8274
}
8375
}

Diff for: src/Scraper/Listeners/ScrapeFailedListener.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Softonic\LaravelIntelligentScraper\Scraper\Listeners;
44

5-
use Softonic\LaravelIntelligentScraper\Scraper\Events\ScrapeFailed;
65
use Illuminate\Contracts\Queue\ShouldQueue;
6+
use Softonic\LaravelIntelligentScraper\Scraper\Events\ScrapeFailed;
77

88
class ScrapeFailedListener implements ShouldQueue
99
{

Diff for: src/Scraper/Listeners/ScrapedListener.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Softonic\LaravelIntelligentScraper\Scraper\Listeners;
44

5-
use Softonic\LaravelIntelligentScraper\Scraper\Events\Scraped;
65
use Illuminate\Contracts\Queue\ShouldQueue;
6+
use Softonic\LaravelIntelligentScraper\Scraper\Events\Scraped;
77

88
class ScrapedListener implements ShouldQueue
99
{

Diff for: src/Scraper/Models/Configuration.php

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ class Configuration extends Model
3838
'xpaths',
3939
];
4040

41+
public function getXpathsAttribute($xpaths): array
42+
{
43+
return (array) $this->castAttribute('xpaths', $xpaths);
44+
}
45+
4146
public function scopeWithType($query, string $type)
4247
{
4348
return $query->where('type', $type);

Diff for: tests/Unit/Scraper/Repositories/ConfigurationTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ public function whenRecalculateItShouldStoreTheNewXpaths()
121121

122122
$this->assertEquals($configs[0]['name'], 'title');
123123
$this->assertEquals($configs[0]['type'], 'post');
124-
$this->assertEquals($configs[0]['xpaths'], '//*[@id="title"]');
124+
$this->assertEquals($configs[0]['xpaths'][0], '//*[@id="title"]');
125125
$this->assertEquals($configs[1]['name'], 'author');
126126
$this->assertEquals($configs[1]['type'], 'post');
127-
$this->assertEquals($configs[1]['xpaths'], '//*[@id="author"]');
127+
$this->assertEquals($configs[1]['xpaths'][0], '//*[@id="author"]');
128128
}
129129

130130
/**

0 commit comments

Comments
 (0)