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