@@ -27,11 +27,21 @@ class Configurator
27
27
*/
28
28
private $ variantGenerator ;
29
29
30
- public function __construct (Client $ client , XpathBuilder $ xpathBuilder , VariantGenerator $ variantGenerator )
31
- {
30
+ /**
31
+ * @var \Softonic\LaravelIntelligentScraper\Scraper\Repositories\Configuration
32
+ */
33
+ private $ configuration ;
34
+
35
+ public function __construct (
36
+ Client $ client ,
37
+ XpathBuilder $ xpathBuilder ,
38
+ \Softonic \LaravelIntelligentScraper \Scraper \Repositories \Configuration $ configuration ,
39
+ VariantGenerator $ variantGenerator
40
+ ) {
32
41
$ this ->client = $ client ;
33
42
$ this ->xpathBuilder = $ xpathBuilder ;
34
43
$ this ->variantGenerator = $ variantGenerator ;
44
+ $ this ->configuration = $ configuration ;
35
45
}
36
46
37
47
/**
@@ -41,14 +51,17 @@ public function __construct(Client $client, XpathBuilder $xpathBuilder, VariantG
41
51
*/
42
52
public function configureFromDataset ($ scrapedDataset ): Collection
43
53
{
54
+ $ type = $ scrapedDataset [0 ]['type ' ];
55
+ $ currentConfiguration = $ this ->configuration ->findByType ($ type );
56
+
44
57
$ result = [];
45
58
foreach ($ scrapedDataset as $ scrapedData ) {
46
59
if ($ crawler = $ this ->getCrawler ($ scrapedData )) {
47
- $ result [] = $ this ->findConfigByScrapedData ($ scrapedData , $ crawler );
60
+ $ result [] = $ this ->findConfigByScrapedData ($ scrapedData , $ crawler, $ currentConfiguration );
48
61
}
49
62
}
50
63
51
- $ finalConfig = $ this ->mergeConfiguration ($ result , $ scrapedDataset [ 0 ][ ' type ' ] );
64
+ $ finalConfig = $ this ->mergeConfiguration ($ result , $ type );
52
65
53
66
$ this ->checkConfiguration ($ scrapedDataset [0 ]['data ' ], $ finalConfig );
54
67
@@ -74,21 +87,25 @@ private function getCrawler($scrapedData)
74
87
*
75
88
* If the data is not valid anymore, it is deleted from dataset.
76
89
*
77
- * @param ScrapedDataset $scrapedData
78
- * @param Crawler $crawler
90
+ * @param ScrapedDataset $scrapedData
91
+ * @param Crawler $crawler
92
+ * @param Configuration[] $currentConfiguration
79
93
*
80
94
* @return array
81
95
*/
82
- private function findConfigByScrapedData ($ scrapedData , $ crawler )
96
+ private function findConfigByScrapedData ($ scrapedData , $ crawler, $ currentConfiguration )
83
97
{
84
98
$ result = [];
85
99
86
100
foreach ($ scrapedData ['data ' ] as $ field => $ value ) {
87
101
try {
88
- $ result [$ field ] = $ this ->xpathBuilder ->find (
89
- $ crawler ->getNode (0 ),
90
- $ value
91
- );
102
+ $ result [$ field ] = $ this ->getOldXpath ($ currentConfiguration , $ field , $ crawler );
103
+ if (!$ result [$ field ]) {
104
+ $ result [$ field ] = $ this ->xpathBuilder ->find (
105
+ $ crawler ->getNode (0 ),
106
+ $ value
107
+ );
108
+ }
92
109
$ this ->variantGenerator ->addConfig ($ field , $ result [$ field ]);
93
110
} catch (\UnexpectedValueException $ e ) {
94
111
$ this ->variantGenerator ->fieldNotFound ();
@@ -102,6 +119,19 @@ private function findConfigByScrapedData($scrapedData, $crawler)
102
119
return $ result ;
103
120
}
104
121
122
+ private function getOldXpath ($ currentConfiguration , $ field , $ crawler )
123
+ {
124
+ $ config = $ currentConfiguration ->firstWhere ('name ' , $ field );
125
+ foreach ($ config ['xpaths ' ] ?? [] as $ xpath ) {
126
+ $ isFound = $ crawler ->filterXPath ($ xpath )->count ();
127
+ if ($ isFound ) {
128
+ return $ xpath ;
129
+ }
130
+ }
131
+
132
+ return false ;
133
+ }
134
+
105
135
/**
106
136
* Merge configuration.
107
137
*
0 commit comments