2016-08-05 13:16:00 +02:00
|
|
|
<?php
|
|
|
|
class CastorusBridge extends BridgeAbstract {
|
2017-02-11 16:16:56 +01:00
|
|
|
const MAINTAINER = 'logmanoriginal';
|
|
|
|
const NAME = 'Castorus Bridge';
|
2016-08-30 11:23:55 +02:00
|
|
|
const URI = 'http://www.castorus.com';
|
2016-09-25 17:04:28 +02:00
|
|
|
const CACHE_TIMEOUT = 600; // 10min
|
2017-02-11 16:16:56 +01:00
|
|
|
const DESCRIPTION = 'Returns the latest changes';
|
|
|
|
|
|
|
|
const PARAMETERS = array(
|
|
|
|
'Get latest changes' => array(),
|
|
|
|
'Get latest changes via ZIP code' => array(
|
|
|
|
'zip' => array(
|
|
|
|
'name' => 'ZIP code',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => '74910, 74',
|
|
|
|
'title' => 'Insert ZIP code (complete or partial)'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'Get latest changes via city name' => array(
|
|
|
|
'city' => array(
|
|
|
|
'name' => 'City name',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => 'Seyssel, Seys',
|
|
|
|
'title' => 'Insert city name (complete or partial)'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Extracts the title from an actitiy
|
|
|
|
private function extractActivityTitle($activity){
|
2016-08-05 13:16:00 +02:00
|
|
|
$title = $activity->find('a', 0);
|
|
|
|
|
|
|
|
if(!$title)
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Cannot find title!');
|
2016-08-09 14:57:42 +02:00
|
|
|
|
2016-08-05 13:16:00 +02:00
|
|
|
return htmlspecialchars(trim($title->plaintext));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extracts the url from an actitiy
|
2017-02-11 16:16:56 +01:00
|
|
|
private function extractActivityUrl($activity){
|
2016-08-05 13:16:00 +02:00
|
|
|
$url = $activity->find('a', 0);
|
|
|
|
|
|
|
|
if(!$url)
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Cannot find url!');
|
2016-08-09 14:57:42 +02:00
|
|
|
|
2016-08-30 11:23:55 +02:00
|
|
|
return self::URI . $url->href;
|
2016-08-05 13:16:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Extracts the time from an activity
|
2017-02-11 16:16:56 +01:00
|
|
|
private function extractActivityTime($activity){
|
2016-08-09 14:57:42 +02:00
|
|
|
// Unfortunately the time is part of the parent node,
|
2016-08-05 13:16:00 +02:00
|
|
|
// so we have to clear all child nodes first
|
|
|
|
$nodes = $activity->find('*');
|
|
|
|
|
|
|
|
if(!$nodes)
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Cannot find nodes!');
|
2016-08-09 14:57:42 +02:00
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($nodes as $node) {
|
2019-06-02 13:03:26 +02:00
|
|
|
$node->outertext = '';
|
2016-08-05 13:16:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return strtotime($activity->innertext);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extracts the price change
|
2017-02-11 16:16:56 +01:00
|
|
|
private function extractActivityPrice($activity){
|
2016-08-05 13:16:00 +02:00
|
|
|
$price = $activity->find('span', 1);
|
|
|
|
|
|
|
|
if(!$price)
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Cannot find price!');
|
2016-08-09 14:57:42 +02:00
|
|
|
|
2016-08-05 13:16:00 +02:00
|
|
|
return $price->innertext;
|
|
|
|
}
|
|
|
|
|
2016-08-25 01:24:53 +02:00
|
|
|
public function collectData(){
|
2017-02-11 16:16:56 +01:00
|
|
|
$zip_filter = trim($this->getInput('zip'));
|
|
|
|
$city_filter = trim($this->getInput('city'));
|
2016-08-05 14:59:26 +02:00
|
|
|
|
2016-09-25 23:22:33 +02:00
|
|
|
$html = getSimpleHTMLDOM(self::URI);
|
2016-08-05 13:16:00 +02:00
|
|
|
|
|
|
|
if(!$html)
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Could not load data from ' . self::URI . '!');
|
2016-08-09 14:57:42 +02:00
|
|
|
|
2016-08-05 13:16:00 +02:00
|
|
|
$activities = $html->find('div#activite/li');
|
|
|
|
|
|
|
|
if(!$activities)
|
2016-09-25 23:22:33 +02:00
|
|
|
returnServerError('Failed to find activities!');
|
2016-08-09 14:57:42 +02:00
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($activities as $activity) {
|
2016-08-22 18:55:59 +02:00
|
|
|
$item = array();
|
2016-08-05 13:16:00 +02:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
$item['title'] = $this->extractActivityTitle($activity);
|
|
|
|
$item['uri'] = $this->extractActivityUrl($activity);
|
|
|
|
$item['timestamp'] = $this->extractActivityTime($activity);
|
|
|
|
$item['content'] = '<a href="'
|
|
|
|
. $item['uri']
|
|
|
|
. '">'
|
|
|
|
. $item['title']
|
|
|
|
. '</a><br><p>'
|
|
|
|
. $this->extractActivityPrice($activity)
|
|
|
|
. '</p>';
|
|
|
|
|
|
|
|
if(isset($zip_filter)
|
2017-07-29 19:28:00 +02:00
|
|
|
&& !(substr($item['title'], 0, strlen($zip_filter)) === $zip_filter)) {
|
2016-08-05 14:59:26 +02:00
|
|
|
continue; // Skip this item
|
|
|
|
}
|
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
if(isset($city_filter)
|
2017-07-29 19:28:00 +02:00
|
|
|
&& !(substr($item['title'], strpos($item['title'], ' ') + 1, strlen($city_filter)) === $city_filter)) {
|
2016-08-05 14:59:26 +02:00
|
|
|
continue; // Skip this item
|
|
|
|
}
|
|
|
|
|
2016-08-05 13:16:00 +02:00
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|