[Bridge] Extend RssExpander to load ATOM formats

This commit is contained in:
logmanoriginal 2016-09-03 22:17:36 +02:00
parent c365f35e47
commit 2aa9b8f026

View file

@ -605,9 +605,16 @@ abstract class RssExpander extends HttpCachingBridgeAbstract {
$rssContent = simplexml_load_string($content);
$this->debugMessage('loaded RSS from ' . $name);
// TODO insert RSS format detection
// For now we always assume RSS 2.0
$this->collect_RSS_2_0_data($rssContent);
if(isset($rssContent->channel[0])){ // RSS format
// TODO insert RSS format detection
// For now we always assume RSS 2.0
$this->collect_RSS_2_0_data($rssContent);
} elseif(isset($rssContent->entry[0])){ // ATOM format
$this->collect_ATOM_data($rssContent);
} else { // Unknown format
$this->returnServerError('The feed format is unknown!');
}
}
protected function collect_RSS_2_0_data($rssContent){
@ -620,6 +627,14 @@ abstract class RssExpander extends HttpCachingBridgeAbstract {
}
}
protected function collect_ATOM_data($content){
$this->load_ATOM_feed_data($content);
foreach($content->entry as $item){
$this->debugMessage('parsing item ' . var_export($item, true));
$this->items[] = $this->parseRSSItem($item);
}
}
protected function RSS_2_0_time_to_timestamp($item){
return DateTime::createFromFormat('D, d M Y H:i:s e', $item->pubDate)->getTimestamp();
}
@ -631,6 +646,38 @@ abstract class RssExpander extends HttpCachingBridgeAbstract {
$this->description = trim($rssContent->description);
}
protected function load_ATOM_feed_data($content){
$this->name = $content->title;
// Find most best link (only one, or first of 'alternate')
if(!isset($content->link)){
$this->uri = '';
} elseif (count($content->link) === 1){
$this->uri = $content->link[0]['href'];
} else {
$this->uri = '';
foreach($content->link as $link){
if(strtolower($link['rel']) === 'alternate'){
$this->uri = $link['rel'];
break;
}
}
}
if(isset($content->subtitle))
$this->description = $content->subtitle;
}
protected function parseATOMItem($feedItem){
$item = array();
if(isset($feedItem->id)) $item['uri'] = $feedItem->id;
if(isset($feedItem->title)) $item['title'] = $feedItem->title;
if(isset($feedItem->updated)) $item['timestamp'] = strtotime($feedItem->updated);
if(isset($feedItem->author)) $item['author'] = $feedItem->author->name;
if(isset($feedItem->content)) $item['content'] = $feedItem->content;
return $item;
}
/**
* Method should return, from a source RSS item given by lastRSS, one of our Items objects
* @param $item the input rss item