feat(journeesmondiales) include event description in feed item body

This commit is contained in:
SansGuidon 2025-01-15 07:34:45 +00:00
parent 4e508f419b
commit 4cfa80e9fc

View File

@ -1,17 +1,13 @@
<?php
// Require necessary libraries
require 'vendor/autoload.php';
// Configuration
$eventsUrl = "https://www.journee-mondiale.com/les-journees-mondiales.htm";
$outputFile = "/app/data/public/daily_events_rss.xml";
$timezone = "Europe/Brussels";
date_default_timezone_set($timezone);
// Create output directory if it doesn't exist
@mkdir(dirname($outputFile), 0777, true);
// Translate month names from English to French
$monthsTranslation = [
'January' => 'janvier', 'February' => 'février', 'March' => 'mars',
'April' => 'avril', 'May' => 'mai', 'June' => 'juin', 'July' => 'juillet',
@ -19,18 +15,14 @@ $monthsTranslation = [
'November' => 'novembre', 'December' => 'décembre'
];
// Get today's date in French
$currentDate = date("j F");
$currentDateParts = explode(' ', $currentDate);
$currentDay = $currentDateParts[0];
$currentMonth = $monthsTranslation[$currentDateParts[1]] ?? $currentDateParts[1];
$currentDateFr = $currentDay . ' ' . $currentMonth;
// Fetch and parse events from the webpage
$html = file_get_contents($eventsUrl);
if (!$html) {
die("Erreur : Impossible de récupérer les événements depuis $eventsUrl");
}
if (!$html) die;
preg_match_all(
'/<li[^>]*><a href="([^"]+)"[^>]*><time datetime="([^"]+)"[^>]*>([^<]+)<\/time> : ([^<]+)<\/a><\/li>/',
@ -39,15 +31,9 @@ preg_match_all(
PREG_SET_ORDER
);
if (empty($matches)) {
die("Erreur : Aucun événement trouvé dans le contenu HTML.");
}
// Extract and filter events for today
$events = array_filter(array_map(function ($match) use ($currentDateFr) {
$eventDate = str_replace(["\xc2\xa0", "1 "], [" ", "1er "], mb_strtolower(trim($match[2])));
$currentDateNormalized = str_replace("\xc2\xa0", " ", mb_strtolower($currentDateFr));
return $eventDate === $currentDateNormalized ? [
"url" => htmlspecialchars($match[1]),
"date" => htmlspecialchars($match[2]),
@ -55,12 +41,21 @@ $events = array_filter(array_map(function ($match) use ($currentDateFr) {
] : null;
}, $matches));
if (empty($events)) {
echo "Aucun événement trouvé pour aujourd'hui ($currentDateFr).\n";
exit;
if (empty($events)) exit;
function fetchEventContent($url) {
$contentHtml = file_get_contents($url);
if (!$contentHtml) return null;
preg_match(
'/<section[^>]*itemprop="description"[^>]*>(.*?)<\/section>/s',
$contentHtml,
$contentMatch
);
return $contentMatch[1] ?? null;
}
// 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', "Flux RSS des journées mondiales");
@ -70,14 +65,14 @@ $channel->addChild('language', 'fr');
$channel->addChild('pubDate', date(DATE_RSS));
foreach ($events as $event) {
$content = fetchEventContent($event['url']);
$item = $channel->addChild('item');
$item->addChild('title', $event['event']);
$item->addChild('link', $event['url']);
$item->addChild('guid', $event['url']);
$item->addChild('pubDate', date(DATE_RSS));
$item->addChild('description', $content ? htmlspecialchars($content) : "Description indisponible.");
}
// Save RSS feed to file
$rss->asXML($outputFile);
echo "Flux RSS généré à $outputFile avec " . count($events) . " événements pour aujourd'hui.\n";