[KleinanzeigenBridge] Add filter options

This commit is contained in:
knrdl 2024-01-20 17:17:04 +01:00 committed by Dag
parent a865b1073a
commit fffe4663cb

View file

@ -6,7 +6,7 @@ class KleinanzeigenBridge extends BridgeAbstract
const NAME = 'Kleinanzeigen Bridge';
const URI = 'https://www.kleinanzeigen.de';
const CACHE_TIMEOUT = 3600; // 1h
const DESCRIPTION = 'ebay Kleinanzeigen';
const DESCRIPTION = '(ebay) Kleinanzeigen';
const PARAMETERS = [
'By search' => [
@ -15,6 +15,11 @@ class KleinanzeigenBridge extends BridgeAbstract
'required' => false,
'title' => 'query term',
],
'category' => [
'name' => 'category',
'required' => false,
'title' => 'search category, e.g. "Damenschuhe" or "Notebooks"'
],
'location' => [
'name' => 'location',
'required' => false,
@ -24,9 +29,23 @@ class KleinanzeigenBridge extends BridgeAbstract
'name' => 'radius',
'required' => false,
'type' => 'number',
'title' => 'search radius in kilometers',
'title' => 'location radius in kilometers',
'defaultValue' => 10,
],
'minprice' => [
'name' => 'minimum price',
'required' => false,
'type' => 'number',
'title' => 'in euros',
'defaultValue' => '',
],
'maxprice' => [
'name' => 'maximum price',
'required' => false,
'type' => 'number',
'title' => 'in euros',
'defaultValue' => '',
],
'pages' => [
'name' => 'pages',
'required' => true,
@ -63,7 +82,7 @@ class KleinanzeigenBridge extends BridgeAbstract
case 'By profile':
return 'Kleinanzeigen Profil';
case 'By search':
return 'Kleinanzeigen ' . $this->getInput('query') . ' / ' . $this->getInput('location');
return 'Kleinanzeigen ' . $this->getInput('query') . ' ' . $this->getInput('category') . ' ' . $this->getInput('location');
default:
return parent::getName();
}
@ -87,31 +106,24 @@ class KleinanzeigenBridge extends BridgeAbstract
}
if ($this->queriedContext === 'By search') {
$locationID = '';
if ($this->getInput('location')) {
$json = getContents(self::URI . '/s-ort-empfehlungen.json?' . http_build_query(['query' => $this->getInput('location')]));
$jsonFile = json_decode($json, true);
$locationID = str_replace('_', '', array_key_first($jsonFile));
}
for ($i = 1; $i <= $this->getInput('pages'); $i++) {
$searchUrl = self::URI . '/s-walled-garden/';
if ($i != 1) {
$searchUrl .= 'seite:' . $i . '/';
}
if ($this->getInput('query')) {
$searchUrl .= urlencode($this->getInput('query')) . '/k0';
}
if ($locationID) {
$searchUrl .= 'l' . $locationID;
}
if ($this->getInput('radius')) {
$searchUrl .= 'r' . $this->getInput('radius');
}
$categoryId = $this->findCategoryId();
for ($page = 1; $page <= $this->getInput('pages'); $page++) {
$searchUrl = self::URI . '/s-suchanfrage.html?' . http_build_query([
'keywords' => $this->getInput('query'),
'locationStr' => $this->getInput('location'),
'locationId' => '',
'radius' => $this->getInput('radius') || '0',
'sortingField' => 'SORTING_DATE',
'categoryId' => $categoryId,
'pageNum' => $page,
'maxPrice' => $this->getInput('maxprice'),
'minPrice' => $this->getInput('minprice')
]);
$html = getSimpleHTMLDOM($searchUrl);
// end of list if returned page is not the expected one
if ($html->find('.pagination-current', 0)->plaintext != $i) {
if ($html->find('.pagination-current', 0)->plaintext != $page) {
break;
}
@ -147,4 +159,19 @@ class KleinanzeigenBridge extends BridgeAbstract
$this->items[] = $item;
}
private function findCategoryId()
{
if ($this->getInput('category')) {
$html = getSimpleHTMLDOM(self::URI . '/s-kategorie-baum.html');
foreach ($html->find('a[data-val]') as $element) {
$catId = (int)$element->getAttribute('data-val');
$catName = $element->plaintext;
if (str_contains(strtolower($catName), strtolower($this->getInput('category')))) {
return $catId;
}
}
}
return 0;
}
}