Skip to content

Instantly share code, notes, and snippets.

@jackbaty
Created January 25, 2024 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackbaty/aaba9bbadec4753f927e554cbaa5d381 to your computer and use it in GitHub Desktop.
Save jackbaty/aaba9bbadec4753f927e554cbaa5d381 to your computer and use it in GitHub Desktop.
RSS-Bridge for my combined RSS feed
<?php
class JackBatyBridge extends FeedExpander
{
const MAINTAINER = 'jackbaty';
const NAME = 'JackBaty';
const URI = 'https://github.com/RSS-Bridge/rss-bridge';
const DESCRIPTION = <<<'TEXT'
This bridge merges the feeds from several of my websites..
TEXT;
public function collectData()
{
$limit = 20;
$feeds = [
'https://baty.net/feed',
'https://daily.baty.net/allposts.xml',
'https://wiki.baty.net/rss.xml',
'https://baty.blog/feed.rss',
];
// Remove empty values
$feeds = array_filter($feeds);
foreach ($feeds as $feed) {
if (count($feeds) > 1) {
// Allow one or more feeds to fail
try {
$this->collectExpandableDatas($feed);
} catch (HttpException $e) {
$this->logger->warning(sprintf('Exception in FeedMergeBridge: %s', create_sane_exception_message($e)));
$this->items[] = [
'title' => 'RSS-Bridge: ' . $e->getMessage(),
// Give current time so it sorts to the top
'timestamp' => time(),
];
continue;
} catch (\Exception $e) {
if (str_starts_with($e->getMessage(), 'Unable to parse xml')) {
// Allow this particular exception from FeedExpander
$this->logger->warning(sprintf('Exception in FeedMergeBridge: %s', create_sane_exception_message($e)));
continue;
}
throw $e;
}
} else {
$this->collectExpandableDatas($feed);
}
}
// Sort by timestamp descending
usort($this->items, function ($a, $b) {
$t1 = $a['timestamp'] ?? $a['uri'] ?? $a['title'];
$t2 = $b['timestamp'] ?? $b['uri'] ?? $b['title'];
return $t2 <=> $t1;
});
// Remove duplicates by using url as unique key
$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);
}
public function getIcon()
{
return 'https://cdn.jsdelivr.net/npm/famfamfam-silk@1.0.0/dist/png/folder_feed.png';
}
public function getName()
{
return $this->getInput('feed_name') ?: 'Everything from Jack Baty';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment