snippets/cron/generate_journees_mondiales.php

79 lines
2.8 KiB
PHP
Raw Normal View History

<?php
require 'vendor/autoload.php';
$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);
@mkdir(dirname($outputFile), 0777, true);
$monthsTranslation = [
'January' => 'janvier', 'February' => 'février', 'March' => 'mars',
'April' => 'avril', 'May' => 'mai', 'June' => 'juin', 'July' => 'juillet',
'August' => 'août', 'September' => 'septembre', 'October' => 'octobre',
'November' => 'novembre', 'December' => 'décembre'
];
$currentDate = date("j F");
$currentDateParts = explode(' ', $currentDate);
$currentDay = $currentDateParts[0];
$currentMonth = $monthsTranslation[$currentDateParts[1]] ?? $currentDateParts[1];
$currentDateFr = $currentDay . ' ' . $currentMonth;
$html = file_get_contents($eventsUrl);
if (!$html) die;
preg_match_all(
'/<li[^>]*><a href="([^"]+)"[^>]*><time datetime="([^"]+)"[^>]*>([^<]+)<\/time> : ([^<]+)<\/a><\/li>/',
$html,
$matches,
PREG_SET_ORDER
);
$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]),
"event" => htmlspecialchars($match[4])
] : null;
}, $matches));
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;
}
$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");
$channel->addChild('link', $eventsUrl);
$channel->addChild('description', "Flux des journées mondiales pour la date du $currentDateFr.");
$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.");
}
$rss->asXML($outputFile);
echo "Flux RSS généré à $outputFile avec " . count($events) . " événements pour aujourd'hui.\n";