rss-bridge/bridges/CastorusBridge.php

118 lines
3.2 KiB
PHP
Raw Normal View History

2016-08-05 14:16:00 +03:00
<?php
class CastorusBridge extends BridgeAbstract {
public $maintainer = "logmanoriginal";
public $name = "Castorus Bridge";
public $uri = 'http://www.castorus.com';
public $description = "Returns the latest changes";
public $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)'
)
)
);
2016-08-05 14:16:00 +03:00
// Extracts the tile from an actitiy
private function ExtractActivityTitle($activity){
2016-08-05 14:16:00 +03:00
$title = $activity->find('a', 0);
if(!$title)
$this->returnServerError('Cannot find title!');
2016-08-05 14:16:00 +03:00
return htmlspecialchars(trim($title->plaintext));
}
// Extracts the url from an actitiy
private function ExtractActivityUrl($activity){
2016-08-05 14:16:00 +03:00
$url = $activity->find('a', 0);
if(!$url)
$this->returnServerError('Cannot find url!');
return $this->uri . $url->href;
2016-08-05 14:16:00 +03:00
}
// Extracts the time from an activity
private function ExtractActivityTime($activity){
// Unfortunately the time is part of the parent node,
2016-08-05 14:16:00 +03:00
// so we have to clear all child nodes first
$nodes = $activity->find('*');
if(!$nodes)
$this->returnServerError('Cannot find nodes!');
2016-08-05 14:16:00 +03:00
foreach($nodes as $node){
$node->outertext = '';
}
return strtotime($activity->innertext);
}
// Extracts the price change
private function ExtractActivityPrice($activity){
2016-08-05 14:16:00 +03:00
$price = $activity->find('span', 1);
if(!$price)
$this->returnServerError('Cannot find price!');
2016-08-05 14:16:00 +03:00
return $price->innertext;
}
public function collectData(){
if(isset($this->getInput('zip')))
$zip_filter = trim($this->getInput('zip'));
if(isset($this->getInput('city')))
$city_filter = trim($this->getInput('city'));
$html = $this->getSimpleHTMLDOM($this->uri);
2016-08-05 14:16:00 +03:00
if(!$html)
$this->returnServerError('Could not load data from ' . $this->uri . '!');
2016-08-05 14:16:00 +03:00
$activities = $html->find('div#activite/li');
if(!$activities)
$this->returnServerError('Failed to find activities!');
2016-08-05 14:16:00 +03:00
foreach($activities as $activity){
$item = array();
2016-08-05 14:16:00 +03: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>'
2016-08-05 14:16:00 +03:00
. $this->ExtractActivityPrice($activity) . '</p>';
if(isset($zip_filter) && !(substr($item['title'], 0, strlen($zip_filter)) === $zip_filter)){
continue; // Skip this item
}
if(isset($city_filter) && !(substr($item['title'], strpos($item['title'], ' ') + 1, strlen($city_filter)) === $city_filter)){
continue; // Skip this item
}
2016-08-05 14:16:00 +03:00
$this->items[] = $item;
}
}
public function getCacheDuration(){
return 600; // 10 minutes
2016-08-05 14:16:00 +03:00
}
}