snippets/generate_indieblog_rss.php
SansGuidon 9ed4c26438 add(script) to generate indieblog rss feed
I'm hosting this on apache on cloudron and running it hourly
2024-11-21 00:32:08 +00:00

64 lines
2.3 KiB
PHP

<?php
// Configuration
$jsonUrl = "https://indieblog.page/export"; // Source JSON URL
$rssFile = "/app/data/public/indieblog_feed.xml"; // RSS output file
$cacheFile = "/app/data/cache/indieblog_cache.txt"; // Cache for processed IDs
$recentCutoff = time() - 86400; // Articles published within the last 24 hours
// Ensure necessary directories exist
@mkdir(dirname($rssFile), 0777, true);
@mkdir(dirname($cacheFile), 0777, true);
// Load JSON data
$jsonData = @file_get_contents($jsonUrl);
if (!$jsonData) {
die("Error: Unable to fetch JSON from $jsonUrl\n");
}
// Decode JSON into an array
$data = json_decode($jsonData, true);
if (!$data) {
die("Error: Invalid JSON data from $jsonUrl\n");
}
// Load cache of already processed IDs
$includedIds = file_exists($cacheFile) ? file($cacheFile, FILE_IGNORE_NEW_LINES) : [];
// Filter recent items
$recentItems = array_filter($data, function ($item) use ($recentCutoff, $includedIds) {
return isset($item['published'], $item['itemid']) &&
$item['published'] >= $recentCutoff &&
!in_array($item['itemid'], $includedIds);
});
// Sort items by publication date (newest first)
usort($recentItems, fn($a, $b) => $b['published'] <=> $a['published']);
// Generate RSS feed
$rss = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"></rss>');
$channel = $rss->addChild('channel');
$channel->addChild('title', 'IndieBlog Feed (Recent)');
$channel->addChild('link', 'https://indieblog.page/');
$channel->addChild('description', 'RSS feed of articles published in the last 24 hours');
$channel->addChild('language', 'en');
// Add recent items to RSS feed
$newEntries = [];
foreach ($recentItems as $item) {
$rssItem = $channel->addChild('item');
$rssItem->addChild('title', htmlspecialchars($item['itemtitle'] ?? 'No title'));
$rssItem->addChild('link', htmlspecialchars($item['itemurl'] ?? ''));
$rssItem->addChild('description', htmlspecialchars($item['feedtitle'] ?? ''));
$rssItem->addChild('pubDate', date(DATE_RSS, $item['published']));
$newEntries[] = $item['itemid'];
}
// Update cache with newly processed IDs
if (!empty($newEntries)) {
file_put_contents($cacheFile, implode("\n", array_merge($includedIds, $newEntries)));
}
// Save RSS feed
$rss->asXML($rssFile);