summaryrefslogtreecommitdiff
path: root/providers
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2024-11-26 00:46:09 +0100
committeremkael <emkael@tlen.pl>2024-11-26 00:46:09 +0100
commitd786772c233d36a19e964ab61edd23de0c0a906d (patch)
tree0c95dd921a6d67ff6783726f51f739eecda8e204 /providers
parent8608dc8151440f109e2a48036fd508fe031d6bbc (diff)
PageDiff provider
Diffstat (limited to 'providers')
-rw-r--r--providers/Pagediff.php110
1 files changed, 110 insertions, 0 deletions
diff --git a/providers/Pagediff.php b/providers/Pagediff.php
new file mode 100644
index 0000000..8679532
--- /dev/null
+++ b/providers/Pagediff.php
@@ -0,0 +1,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'];
+ }
+
+}
+
+?>