_config = $config[$feed];
parent::__construct($feed, $options);
}
protected function _getCachePath() {
return '../cache/pagediff.%s';
}
protected function _getFeedUrl($feed) {
return $this->_config['url'];
}
private function _getItemCachePath() {
return sprintf('../cache/pagediff.items.%s', $this->_feed);
}
private function _getCachedContent() {
if (!file_exists($this->_getItemCachePath())) {
return [];
}
return unserialize(
file_get_contents(
$this->_getItemCachePath()
)
);
}
private function _saveCachedContent($content) {
return file_put_contents(
$this->_getItemCachePath(),
serialize($content)
);
}
private function _getContentFromSelector($tree, $selector) {
$node = $tree->find($selector['node']);
if ($node->count() == 0) {
return NULL;
}
if ($node->count() != 1) {
if (isset($selector['index'])) {
$node = $node->eq($selector['index']);
} else {
$node = $node->first();
}
}
if (isset($selector['html'])) {
return $node->innerHTML();
}
if (isset($selector['attr'])) {
$text = $node->attr()[$selector['attr']];
} else {
$text = $node->text();
}
if (isset($selector['transform'])) {
$text = sprintf($selector['transform'], $text);
}
return $text;
}
protected function _parseFeedContent($tree) {
$selectors = $this->_config['selectors'];
$items = $this->_getCachedContent();
$currentItem = [];
foreach (['id', 'link', 'name', 'text'] as $type) {
$currentItem[$type] = $this->_getContentFromSelector($tree, $selectors[$type]);
}
$currentItem['time'] = date('Y-m-d H:i:s');
if (!count($items) || $currentItem['id'] != $items[0]['id']) {
$items = array_merge([$currentItem], $items);
$this->_saveCachedContent($items);
}
return $items;
}
protected function _mapItems($items) {
return array_map(function($item) {
$i = new Item();
$i->ID = $item['id'];
$i->Title = $item['name'];
$i->Time = $item['time'];
$i->Text = $item['text'];
$i->Link = $item['link'];
return $i;
}, $items);
}
public function title() {
return $this->_config['title'];
}
}
?>