1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lbc\Crawler; |
4
|
|
|
|
5
|
|
|
use Lbc\Filter\CitySanitizer; |
6
|
|
|
use Lbc\Filter\CpSanitizer; |
7
|
|
|
use Lbc\Filter\DefaultSanitizer; |
8
|
|
|
use Lbc\Filter\KeySanitizer; |
9
|
|
|
use Lbc\Filter\PriceSanitizer; |
10
|
|
|
use Lbc\Parser\AdUrlParser; |
11
|
|
|
use League\Uri\Schemes\Http; |
12
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class AdCrawler |
16
|
|
|
* @package Lbc\Crawler |
17
|
|
|
*/ |
18
|
|
|
class AdCrawler extends CrawlerAbstract |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param $url |
22
|
|
|
* @return AdUrlParser |
23
|
|
|
*/ |
24
|
14 |
|
protected function setUrlParser($url) |
25
|
|
|
{ |
26
|
14 |
|
$this->url = new AdUrlParser($url); |
27
|
14 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Return a full ad information |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
14 |
|
public function getAll() |
35
|
|
|
{ |
36
|
14 |
|
return array_merge( |
37
|
|
|
[ |
38
|
6 |
|
'id' => $this->getUrlParser()->getId(), |
|
|
|
|
39
|
6 |
|
'category' => $this->getUrlParser()->getCategory(), |
40
|
6 |
|
], |
41
|
6 |
|
$this->getPictures(), |
42
|
6 |
|
$this->getProperties(), |
43
|
6 |
|
$this->getDescription() |
44
|
6 |
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Return an array with the Thumbs pictures url |
49
|
|
|
* |
50
|
|
|
* @param Crawler $node |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
8 |
|
public function getPictures(Crawler $node = null) |
54
|
|
|
{ |
55
|
8 |
|
$node = $node ?: $this->node; |
56
|
|
|
|
57
|
8 |
|
$images = []; |
58
|
8 |
|
$images_thumbs = []; |
59
|
|
|
|
60
|
|
|
$node |
61
|
8 |
|
->filter('.adview_main script') |
62
|
|
|
->each(function (Crawler $crawler) use (&$images, &$images_thumbs) { |
63
|
8 |
|
preg_match_all( |
64
|
8 |
|
'#//img.+.leboncoin.fr/.*\.jpg#', |
65
|
8 |
|
$crawler->html(), |
66
|
|
|
$matches |
67
|
8 |
|
); |
68
|
|
|
|
69
|
8 |
|
if (count($matches[0]) > 0) { |
70
|
8 |
|
foreach ($matches[0] as $image) { |
71
|
8 |
|
if (preg_match('/thumb/', $image)) { |
72
|
8 |
|
array_push( |
73
|
8 |
|
$images_thumbs, |
74
|
8 |
|
(string)Http::createFromString($image) |
75
|
8 |
|
->withScheme($this->sheme) |
76
|
8 |
|
); |
77
|
8 |
|
} else { |
78
|
8 |
|
array_push( |
79
|
8 |
|
$images, |
80
|
8 |
|
(string)Http::createFromString($image) |
81
|
8 |
|
->withScheme($this->sheme) |
82
|
8 |
|
); |
83
|
|
|
} |
84
|
8 |
|
} |
85
|
8 |
|
} |
86
|
8 |
|
}); |
87
|
|
|
|
88
|
|
|
return [ |
89
|
8 |
|
'images' => $images, |
90
|
8 |
|
'images_thumbs' => $images_thumbs, |
91
|
8 |
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return the common information (price, cp, city) |
96
|
|
|
* |
97
|
|
|
* @param Crawler $node |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
8 |
|
public function getProperties(Crawler $node = null) |
102
|
|
|
{ |
103
|
8 |
|
$node = $node ?: $this->node; |
104
|
|
|
|
105
|
8 |
|
$properties = []; |
106
|
|
|
|
107
|
8 |
|
$properties['title'] = DefaultSanitizer::clean( |
108
|
8 |
|
$this->node->filter('h1')->text() |
109
|
8 |
|
); |
110
|
|
|
|
111
|
8 |
|
$node->filter('h2') |
112
|
8 |
|
->each(function (Crawler $crawler) use (&$properties) { |
113
|
8 |
|
$properties = array_merge( |
114
|
8 |
|
$properties, |
115
|
8 |
|
$this->sanitize( |
116
|
8 |
|
$crawler->filter('.property')->text(), |
117
|
8 |
|
$crawler->filter('.value')->text() |
118
|
8 |
|
) |
119
|
8 |
|
); |
120
|
8 |
|
}); |
121
|
|
|
|
122
|
8 |
|
return ['properties' => $properties]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Return the description |
127
|
|
|
* |
128
|
|
|
* @param Crawler $node |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
8 |
|
public function getDescription(Crawler $node = null) |
|
|
|
|
132
|
|
|
{ |
133
|
8 |
|
return ['description' => $this->node->filter("p#description")->text()]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Transform the properties name into a snake_case string |
138
|
|
|
* |
139
|
|
|
* @param string $value |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
8 |
|
private function sanitize($key, $value) |
143
|
|
|
{ |
144
|
8 |
|
$key = KeySanitizer::clean($key); |
145
|
|
|
|
146
|
|
|
switch ($key) { |
147
|
8 |
|
case 'prix': |
148
|
8 |
|
return ['price' => PriceSanitizer::clean($value)]; |
149
|
|
|
break; |
|
|
|
|
150
|
8 |
|
case 'ville': |
151
|
|
|
return [ |
152
|
8 |
|
'city' => CitySanitizer::clean($value), |
153
|
8 |
|
'cp' => CpSanitizer::clean($value), |
154
|
8 |
|
]; |
155
|
|
|
break; |
|
|
|
|
156
|
8 |
|
default: |
157
|
8 |
|
return [$key => DefaultSanitizer::clean($value)]; |
158
|
8 |
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: