2022-04-08 22:13:05 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class FeedMergeBridge extends FeedExpander
|
|
|
|
{
|
|
|
|
const MAINTAINER = 'dvikan';
|
|
|
|
const NAME = 'FeedMerge';
|
|
|
|
const URI = 'https://github.com/RSS-Bridge/rss-bridge';
|
|
|
|
const DESCRIPTION = <<<'TEXT'
|
|
|
|
This bridge merges two or more feeds into a single feed. Max 10 items are fetched from each feed.
|
|
|
|
TEXT;
|
|
|
|
|
|
|
|
const PARAMETERS = [
|
|
|
|
[
|
|
|
|
'feed_name' => [
|
|
|
|
'name' => 'Feed name',
|
|
|
|
'type' => 'text',
|
2023-07-08 18:07:08 +03:00
|
|
|
'exampleValue' => 'FeedMerge',
|
2022-04-08 22:13:05 +03:00
|
|
|
],
|
|
|
|
'feed_1' => [
|
|
|
|
'name' => 'Feed url',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => 'https://lorem-rss.herokuapp.com/feed?unit=day'
|
|
|
|
],
|
|
|
|
'feed_2' => ['name' => 'Feed url', 'type' => 'text'],
|
|
|
|
'feed_3' => ['name' => 'Feed url', 'type' => 'text'],
|
|
|
|
'feed_4' => ['name' => 'Feed url', 'type' => 'text'],
|
|
|
|
'feed_5' => ['name' => 'Feed url', 'type' => 'text'],
|
2022-09-08 19:44:15 +03:00
|
|
|
'feed_6' => ['name' => 'Feed url', 'type' => 'text'],
|
|
|
|
'feed_7' => ['name' => 'Feed url', 'type' => 'text'],
|
|
|
|
'feed_8' => ['name' => 'Feed url', 'type' => 'text'],
|
|
|
|
'feed_9' => ['name' => 'Feed url', 'type' => 'text'],
|
|
|
|
'feed_10' => ['name' => 'Feed url', 'type' => 'text'],
|
2022-06-22 19:34:05 +03:00
|
|
|
'limit' => self::LIMIT,
|
2022-04-08 22:13:05 +03:00
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2022-09-08 19:44:15 +03:00
|
|
|
/**
|
|
|
|
* todo: Consider a strategy which produces a shorter feed url
|
|
|
|
*/
|
2022-04-08 22:13:05 +03:00
|
|
|
public function collectData()
|
|
|
|
{
|
2022-06-22 19:34:05 +03:00
|
|
|
$limit = (int)($this->getInput('limit') ?: 10);
|
2022-04-08 22:13:05 +03:00
|
|
|
$feeds = [
|
|
|
|
$this->getInput('feed_1'),
|
|
|
|
$this->getInput('feed_2'),
|
|
|
|
$this->getInput('feed_3'),
|
|
|
|
$this->getInput('feed_4'),
|
|
|
|
$this->getInput('feed_5'),
|
2022-09-08 19:44:15 +03:00
|
|
|
$this->getInput('feed_6'),
|
|
|
|
$this->getInput('feed_7'),
|
|
|
|
$this->getInput('feed_8'),
|
|
|
|
$this->getInput('feed_9'),
|
|
|
|
$this->getInput('feed_10'),
|
2022-04-08 22:13:05 +03:00
|
|
|
];
|
2022-07-05 16:39:00 +03:00
|
|
|
|
2022-04-08 22:13:05 +03:00
|
|
|
// Remove empty values
|
|
|
|
$feeds = array_filter($feeds);
|
2022-07-05 16:39:00 +03:00
|
|
|
|
2022-04-08 22:13:05 +03:00
|
|
|
foreach ($feeds as $feed) {
|
2023-07-05 06:41:01 +03:00
|
|
|
if (count($feeds) > 1) {
|
|
|
|
// Allow one or more feeds to fail
|
|
|
|
try {
|
|
|
|
$this->collectExpandableDatas($feed);
|
|
|
|
} catch (HttpException $e) {
|
2023-09-21 23:05:55 +03:00
|
|
|
$this->logger->warning(sprintf('Exception in FeedMergeBridge: %s', create_sane_exception_message($e)));
|
2023-07-05 06:41:01 +03:00
|
|
|
$this->items[] = [
|
|
|
|
'title' => 'RSS-Bridge: ' . $e->getMessage(),
|
|
|
|
// Give current time so it sorts to the top
|
|
|
|
'timestamp' => time(),
|
|
|
|
];
|
|
|
|
continue;
|
2023-07-09 00:36:36 +03:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
if (str_starts_with($e->getMessage(), 'Unable to parse xml')) {
|
|
|
|
// Allow this particular exception from FeedExpander
|
2023-09-21 23:05:55 +03:00
|
|
|
$this->logger->warning(sprintf('Exception in FeedMergeBridge: %s', create_sane_exception_message($e)));
|
2023-07-09 00:36:36 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw $e;
|
2023-07-05 06:41:01 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->collectExpandableDatas($feed);
|
|
|
|
}
|
2022-04-08 22:13:05 +03:00
|
|
|
}
|
2022-07-05 16:39:00 +03:00
|
|
|
|
2022-06-22 19:34:05 +03:00
|
|
|
// Sort by timestamp descending
|
2022-11-08 23:17:32 +03:00
|
|
|
usort($this->items, function ($a, $b) {
|
|
|
|
$t1 = $a['timestamp'] ?? $a['uri'] ?? $a['title'];
|
|
|
|
$t2 = $b['timestamp'] ?? $b['uri'] ?? $b['title'];
|
|
|
|
return $t2 <=> $t1;
|
|
|
|
});
|
2022-07-05 16:39:00 +03:00
|
|
|
|
2022-09-08 19:44:15 +03:00
|
|
|
// Remove duplicates by using url as unique key
|
2022-07-05 16:39:00 +03:00
|
|
|
$items = [];
|
|
|
|
foreach ($this->items as $item) {
|
|
|
|
$index = $item['uri'] ?? null;
|
|
|
|
if ($index) {
|
|
|
|
// Overwrite duplicates
|
|
|
|
$items[$index] = $item;
|
|
|
|
} else {
|
|
|
|
$items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->items = array_slice(array_values($items), 0, $limit);
|
2022-04-08 22:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getIcon()
|
|
|
|
{
|
|
|
|
return 'https://cdn.jsdelivr.net/npm/famfamfam-silk@1.0.0/dist/png/folder_feed.png';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName()
|
|
|
|
{
|
2023-07-08 18:07:08 +03:00
|
|
|
return $this->getInput('feed_name') ?: 'FeedMerge';
|
2022-04-08 22:13:05 +03:00
|
|
|
}
|
|
|
|
}
|