1
0
mirror of https://github.com/RSS-Bridge/rss-bridge.git synced 2025-10-29 13:56:26 +01:00
Files
php-rss-bridge/bridges/SkyArteBridge.php
Dag f604ed842f feat: introduce convenience function get_sitemap (#4773)
* feat: introduce function get_sitemap

Convenience function to fetch and parse xml sitemap from url

* lint
2025-10-25 02:08:43 +02:00

145 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
class SkyArteBridge extends BridgeAbstract
{
const NAME = 'Sky Arte | Mostre ed eventi';
const URI = 'https://arte.sky.it';
const MAINTAINER = 'tillcash';
const CACHE_TIMEOUT = 60 * 60 * 6; // 6 hours
const MAX_ARTICLES = 5;
public function collectData()
{
$urls = get_sitemap('https://arte.sky.it/sitemap-mostre-eventi.xml');
$count = 0;
foreach ($urls as $url) {
$loc = $url['loc'];
if (!$loc) {
continue;
}
$json = $this->getJson($loc);
if (!$json) {
continue;
}
$event = $this->parseEventData($json);
$this->items[] = [
'title' => $event['title'],
'uri' => $loc,
'uid' => $loc,
'timestamp' => $url['lastmod'],
'content' => $event['content'],
'categories' => $event['categories'],
'enclosures' => $event['enclosures'],
];
if (++$count >= self::MAX_ARTICLES) {
break;
}
}
}
private function getJson(string $url): ?array
{
$html = getSimpleHTMLDOMCached($url, 259200); // 3 days cache
if (!$html) {
return null;
}
$script = $html->find('script#__NEXT_DATA__', 0);
if (!$script) {
return null;
}
$decoded = json_decode($script->innertext, true);
return is_array($decoded) ? $decoded : null;
}
private function parseEventData(array $json): array
{
$props = $json['props']['pageProps']['data'] ?? [];
$card = $props['card'] ?? [];
$info = $props['info'] ?? [];
$event = [
'title' => $card['title']['typography']['text'] ?? '(untitled)',
'content' => '',
'categories' => [],
'enclosures' => [],
];
// Artist & Curators
$artist = $info['artist']['text'] ?? '';
$curators = [];
if (!empty($info['curators']) && is_array($info['curators'])) {
foreach ($info['curators'] as $c) {
$curators[] = $c['text'] ?? '';
}
}
// Location, Dates, Categories
$location = '';
$dates = '';
if (!empty($card['informations']) && is_array($card['informations'])) {
foreach ($card['informations'] as $block) {
$icon = $block['iconRight']['Icon'] ?? '';
if ($icon === 'SvgLocation') {
$location = $block['textRight']['text'] ?? '';
}
if ($icon === 'SvgEventEmpty') {
$dates = $block['textRight']['text'] ?? '';
}
if (!empty($block['badge']['label']['text'])) {
$event['categories'][] = $block['badge']['label']['text'];
}
}
}
// Enclosure (image)
if (!empty($card['image']['src'])) {
$event['enclosures'][] = $card['image']['src'];
}
// HTML content building
$content = '';
if ($artist) {
$content .= '<p><strong>Artista:</strong> ' . htmlspecialchars($artist) . '</p>';
}
if ($curators) {
$content .= '<p><strong>Curatori:</strong> ' . htmlspecialchars(implode(', ', $curators)) . '</p>';
}
if ($location) {
$content .= '<p><strong>Luogo:</strong> ' . htmlspecialchars($location) . '</p>';
}
if ($dates) {
$content .= '<p><strong>Periodo:</strong> ' . htmlspecialchars($dates) . '</p>';
}
$description = $props['description'] ?? '';
if ($description) {
$description = preg_replace('~<h2>(.*?)</h2>~i', '<strong>$1</strong>', $description);
$description = nl2br($description);
$content .= '<br><hr><br><p>' . $description . '</p>';
}
$event['content'] = $content;
return $event;
}
}