-
Notifications
You must be signed in to change notification settings - Fork 1k
/
OLXBridge.php
211 lines (177 loc) · 7.97 KB
/
OLXBridge.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
class OLXBridge extends BridgeAbstract
{
const NAME = 'OLX';
const DESCRIPTION = <<<'EOF'
Returns the search results from the OLX auctioning platforms
(Bulgaria, Kazakhstan, Poland, Portugal, Romania, Ukraine and Uzbekistan only)
EOF;
const URI = 'https://www.olx.com';
const MAINTAINER = 'wrobelda';
const PARAMETERS = [[
'url' => [
'name' => 'Search URL',
'title' => 'Copy the URL from your browser\'s address bar after searching for your items and paste it here',
'pattern' => '^(https:\/\/)?(www.)?olx\.(bg|kz|pl|pt|ro|ua|uz).*$',
'exampleValue' => 'https://www.olx.pl/d/oferty/q-cebula/',
'required' => true,
],
'includePostsWithoutPricetag' => [
'type' => 'checkbox',
'name' => 'Include posts without price tag'
],
'includeFeaturedPosts' => [
'type' => 'checkbox',
'name' => 'Include featured posts'
],
'shippingOfferedOnly' => [
'type' => 'checkbox',
'name' => 'Only posts with shipping offered'
]
]];
private function getHostname()
{
$scheme = parse_url($this->getInput('url'), PHP_URL_SCHEME);
$host = parse_url($this->getInput('url'), PHP_URL_HOST);
return $scheme . '://' . $host;
}
public function getURI()
{
if ($this->getInput('url')) {
# make sure we order by the most recently listed offers
$uri = trim(preg_replace('/([?&])search%5Border%5D=[^&]+(&|$)/', '$1', $this->getInput('url')), '?&/');
$uri = preg_replace('/([?&])view=[^&]+(&|$)/', '', $uri);
$uri .= (parse_url($uri, PHP_URL_QUERY) ? '&' : '?') . 'search%5Border%5D=created_at:desc';
return $uri;
} else {
return parent::getURI();
}
}
public function getName()
{
$url = $this->getInput('url');
if (!$url) {
return parent::getName();
}
$parsedUrl = Url::fromString($url);
$paths = explode('/', $parsedUrl->getPath());
$query = array_reduce($paths, function ($q, $p) {
if (preg_match('/^q-(.+)$/i', $p, $matches)) {
$q[] = str_replace('-', ' ', urldecode($matches[1]));
}
return $q;
});
if ($query) {
return $query[0];
}
return parent::getName();
}
public function collectData()
{
$html = getSimpleHTMLDOM($this->getURI());
$html = defaultLinkTo($html, $this->getHostname());
$isoLang = $html->find('meta[http-equiv=Content-Language]', 0)->content;
# the second grid, if any, has extended results from additional categories, outside of original search scope
$listing_grid = $html->find("div[data-testid='listing-grid']", 0);
$results = $listing_grid->find("div[data-cy='l-card']");
foreach ($results as $post) {
$item = [];
if (!$this->getInput('includeFeaturedPosts') && $post->find('div[data-testid="adCard-featured"]', 0)) {
continue;
}
$price = $post->find('p[data-testid="ad-price"]', 0)->plaintext ?? '';
if (!$this->getInput('includePostsWithoutPricetag') && !$price) {
continue;
}
$negotiable = $post->find('p[data-testid="ad-price"] span.css-e2218f', 0)->plaintext ?? false;
if ($negotiable) {
$price = trim(str_replace($negotiable, '', $price));
$negotiable = '(' . $negotiable . ')';
}
if ($post->find('h4', 0)->plaintext != '') {
$item['uri'] = $post->find('a', 0)->href;
$item['title'] = $post->find('h4', 0)->plaintext;
}
# ignore the date component, as it is too convoluted — use the deep-crawled one; see below
$locationAndDate = $post->find('p[data-testid="location-date"]', 0)->plaintext;
$locationAndDateArray = explode(' - ', $locationAndDate, 2);
$location = trim($locationAndDateArray[0]);
# OLX only shows 5 results before images get lazy-loaded, so we have to deep-crawl *almost* all the results.
# Given that, do deep-crawl *all* the results, which allows to aso obtain the ID, the simplified location
# and date strings, as well as the detailed description.
$articleHTMLContent = getSimpleHTMLDOMCached($item['uri']);
$articleHTMLContent = defaultLinkTo($articleHTMLContent, $this->getHostname());
$shippingOffered = $articleHTMLContent->find('img[alt="Safety Badge"]', 0)->src ?? false;
if ($this->getInput('shippingOfferedOnly') && !$shippingOffered) {
continue;
}
# Extract a clean ID without resorting to the convoluted CSS class or sibling selectors. Should be always present.
$refreshLink = $articleHTMLContent->find('a[data-testid=refresh-link]', 0)->href ?? false;
if ($refreshLink) {
parse_str(parse_url($refreshLink, PHP_URL_QUERY), $refreshQuery);
$item['uid'] = $refreshQuery['ad-id'];
} else {
# may be an imported offer from a sibling auto-moto classifieds platform
$item['uid'] = $articleHTMLContent->find('span[id=ad_id]', 0)->plaintext;
}
$img = $articleHTMLContent->find('meta[property="og:image"]', 0)->content ?? false;
if ($img) {
$item['enclosures'] = [$img . '#.image'];
}
$isoDate = $articleHTMLContent->find('meta[property="og:updated_time"]', 0)->content ?? false;
if ($isoDate) {
$item['timestamp'] = strtotime($isoDate);
} else {
$date = $articleHTMLContent->find('span[data-cy="ad-posted-at"]', 0)->plaintext;
# Relative, today
if (preg_match('/^.*\s(\d\d:\d\d)$/i', $date, $matches)) {
$item['timestamp'] = strtotime($matches[1]);
} else {
# full, localized date
$formatter = new IntlDateFormatter($isoLang, IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
$item['timestamp'] = $formatter->parse($date);
}
}
$descriptionHtml = $articleHTMLContent->find('div[data-cy="ad_description"] div', 0)->innertext ?? false;
if (!$descriptionHtml) {
$descriptionHtml = $articleHTMLContent->find('div[id="description"] div[data-read-more]', 0)->innertext ?? false;
}
$item['categories'] = [];
$breadcrumbs = $articleHTMLContent->find('li[data-testid="breadcrumb-item"]');
foreach ($breadcrumbs as $breadcrumb) {
$category = $breadcrumb->find('a[href!="/"]', 0) ?? false;
if ($category) {
$item['categories'][] = $category->plaintext;
}
}
$parameters = $articleHTMLContent->find('div.parametersArea li');
foreach ($parameters as $parameter) {
$category = $parameter->find('a', 0)->plaintext ?? false;
if ($category = empty($category) ? false : trim($category)) {
if ($category == 'Tak') {
$category = $parameter->find('span', 0)->plaintext ?? '';
} elseif ($category == 'Nie') {
continue;
}
$item['categories'][] = $category;
}
}
$item['content'] = <<<CONTENT
<table>
<tbody>
<tr>
<td>
<p>$location</p>
<p><span style="font-weight:bold">$price</span> $negotiable <span><img src="$shippingOffered"</img></span></p>
</td>
</tr>
<tr>
<td>$descriptionHtml</td>
</tr>
</tbody>
</table>
CONTENT;
$this->items[] = $item;
}
}
}