1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
<?php
namespace Providers;
require_once('HtmlFeed.php');
require_once('Item.php');
class Pagediff extends \Providers\HtmlFeed {
protected $_cacheTimeout = '1 hour';
public function __construct($feed, $options=[]) {
$config = json_decode(file_get_contents('../config/pagediff.json'), TRUE);
if (!isset($config[$feed])) {
throw new \Exception(sprintf('Feed %s not configured', $feed));
}
$this->_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'];
}
}
?>
|