Hallo Helmut,
da ich mir nicht sicher bin ob Du das aktuellste Script hast oder ich es up-to-date im Forum gehalten habe und gestern 1,2 kleine Dinge verbessert habe … hier nochmal mein aktuelles Script:
<?php
################################################################################
# Script: Online.Feeds.ips.php
# Version: 2.1.20210203
# Author: Heiko Wilknitz (@pitti)
#
# Abruf von Newsfeeds aus dem Internet
#
# ------------------------------- Installation ---------------------------------
#
# Dieses Skript richtet automatisch alle nötigen Objekte bei manueller
# Ausführung auf der Console ein.
#
# --------------------------------- Changelog ----------------------------------
#
# v2.1.20210203 - Umstellung auf Discourse
# v2.0.20180310 - Cache Parameter
# ----------------------------- Konfigruration ---------------------------------
#
# Array einfach mit den gewünschten Newsfeeds erweitern!
$feeds = [
'IPSymcon' => 'https://community.symcon.de/latest.rss',
];
#
# Update Profil
$update = [
[1, '►', '', 0x008000],
];
#
################################################################################
switch ($_IPS['SENDER']) {
// INSTALL & INITIALIZE
case 'Execute':
Install($feeds, $update);
break;
// AKTION VIA TIMER
case 'TimerEvent':
$name = strstr(IPS_GetName($_IPS['EVENT']), 'Update', true);
foreach($feeds as $key => $value) {
if($name == $key) {
FeedReader($key, $value);
}
}
break;
// AKTION VIA WEBFRONT
case 'WebFront':
// Update
SetValue($_IPS['VARIABLE'], $_IPS['VALUE']);
foreach($feeds as $key => $value) {
FeedReader($key, $value);
}
break;
}
# ------------------------------- Functions ------------------------------------
// Erzeugen aller Variablen und Profile
function Install($feeds, $update)
{
// HTML Boxes for Feeds
foreach($feeds as $key => $value) {
$vid = CreateVariableByName($_IPS['SELF'], $key, 3);
IPS_SetVariableCustomProfile($vid, '~HTMLBox');
IPS_SetIcon($vid, "Talk");
}
// Manueller Update Button
$vpn = "IPS.Update";
$vid = CreateVariableByName($_IPS['SELF'], "Update", 1);
CreateProfileInteger($vpn, 'Script', '', '', 0, 0, 0, 0, $update);
IPS_SetVariableCustomProfile($vid, $vpn);
IPS_SetVariableCustomAction($vid, $_IPS['SELF']);
IPS_SetPosition($vid, -1);
SetValue($vid, 0);
}
// Funktion zur Datums Formatanpassung, kann individuell verändert werden.
function FeedReader($name, $url)
{
// Individuellen Parameter anhängen um Cacheing zu vermeiden
//$hit = date("YmdHi", time());
//$url = str_replace('<HIT>', $hit, $url);
$xml = simplexml_load_file($url);
//print_r($xml);
//setlocale(LC_TIME, 'deu_deu');
date_default_timezone_set('Europe/Berlin');
$news_count = count($xml->channel->item); // Anzahl der im RSS Feed vorhandenen News
// Letztes Update und Lebenszeit (Time To Live)
$news_last = $xml->channel->lastBuildDate;
$news_ttl = 60; // $xml->channel->ttl;
#echo $news_count;
$news_want = 100; // Anzahl der gewünschten, anzuzeigenden News
// Wenn Anzahl der vorhandenen News kleiner der gewünschten News ...
if($news_count < $news_want) {
// dann Anzahl der vorhandene News gleich den gewünschten News.
$news_want = $news_count;
}
// Update Time
$update = strtotime($news_last) + (($news_ttl)*60);
if($update <= time()) {
//IPS_LogMessage('FEEDS', 'Update Time:'. $update);
$update = time() + ($news_ttl*60);
}
$next = "<div style='font-size:14px;'>Nächste Aktualisierung am ".date("d.m.",$update)." um ".date("H:i",$update)." Uhr.</div><br />";
// HTML Styles, individuell anpasspar
$style = "";
$style = $style.'<style type="text/css">';
$style = $style.'table {width:100%;border-collapse: collapse;}';
$style = $style.'td.rss {vertical-align: top;text-align:left;padding:5px 10px 5px 10px;border: 1px solid rgba(255, 255, 255, 0.2);}';
$style = $style.'tr:nth-child(even) {background-color: rgba(0, 0, 0, 0.3);}';
$style = $style.'a.rlink { color: rgba(128, 128, 0, 1.0); text-decoration:none; font-weight:bold;}';
$style = $style.'div.rtitle { float:left;}';
$style = $style.'div.rctor { clear:both; color: rgba(192, 192, 192, 0.7); font-style:italic; font-size: small; padding: 2px 0px 5px 0px;}';
$style = $style.'div.rdate { float:right; color: rgba(192, 192, 192, 0.5); font-size: small;}';
$style = $style.'div.rdesc { font-size: smaller; width:90%;}';
$style = $style.'</style>';
// Feed in HTML Table umwandeln
$feed = $style.$next.'<table>';
//Hier werden die News, in der gewünschten Anzahl, eingelesen und formartiert.
for($i=0; $i<$news_want; $i++) {
$item = $xml->channel->item[$i];
$news = $item->title;
$news = ConvertEntity($news);
$desc = ConvertEntity($item->description);
$link = $item->link;
$date = $item->pubDate;
$date = FormatDate($date);
$cat = $item->category;
$ctor = $item->children('http://purl.org/dc/elements/1.1/')->creator;
// build html entry
$feed .= '<tr><td class="rss">';
$feed .= '<div class="rtitle"><a class="rlink" href="'.$link.'" target="_blank">'.$news.'</a></div>';
$feed .= '<div class="rdate">'.$date.'</div>';
$feed .= '<div class="rctor">'.$ctor.' - '.$cat.'</div>';
$feed .= '<div class="rdesc">'.$desc.'</div>';
$feed .= '</td></tr>';
}
// Fertig
$feed .= '</table>';
// Fürs Webfront in Variable schreiben (String-Variable mit ~HTMLBox Profil)
$vid = CreateVariableByName($_IPS['SELF'], $name, 3);
SetValue($vid, $feed);
// Neuen Aktualisierungstimer erzeugen
CreateEventByName($_IPS['SELF'], $name.'Update', 1, $update);
}
// Funktion zur Datums Formatanpassung, kann individuell verändert werden.
function FormatDate($date) {
$today = time();
$today = date("d.m.Y" , $today);
$yesterday = date("d.m.Y", strtotime("-1 days"));
$publ = date("d.m.Y", strtotime($date));
// bessere Lesbarkeit der Datumsangabe
if($today == $publ) {
$publ = 'Heute, '.strftime('%d %B %Y %H:%M:%S', strtotime($date));
}
else if($yesterday == $publ) {
$publ = 'Gestern, '.strftime('%d %B %Y %H:%M:%S', strtotime($date));
}
else {
$publ = strftime('%A, %d %B %Y %H:%M:%S', strtotime($date));
}
return $publ;
}
// Funktion zur News Formatierung , kann individuell erweitert werden.
function ConvertEntity($news){
$news=strip_tags($news);
$news=html_entity_decode($news);
$news=str_replace(" ", " ", $news);
$news=str_replace("ö", "ö", $news);
$news=str_replace("Ü", "Ü", $news);
$news=str_replace("ü", "ü", $news);
$news=str_replace("ä", "ä", $news);
$news=str_replace("ß", "ß", $news);
$news=str_replace("&ndash;", "-", $news);
$news=str_replace('&#039;', "`", $news);
$news=str_replace("&", "&", $news);
$news=str_replace("Ö", "Ö", $news);
$news=str_replace("„", "", $news);
$news=str_replace("“", "", $news);
$news=str_replace("–", "-", $news);
$news=str_replace("„", "", $news);
$news=str_replace("“", "", $news);
$news=str_replace("„", "", $news);
$news=str_replace("–", "-", $news);
$news=str_replace("–", "-", $news);
$news=preg_replace("/\r|\n/s", "", $news);
return $news;
}
// Erzeugt ein zyklisches Event
// id: int - Parent
// name: string - Event name
// type: 0 => Legt ein “ausgelöstes” Ereignis an
// 1 => Legt ein “zyklisches” Ereignis an
// 2 => Legt ein “Wochenplan” Ereignis an
// time int - timestamp
function CreateEventByName($id, $name, $type, $time)
{
if($time < time()) {
return -1;
}
$eid = @IPS_GetEventIDByName($name, $id);
if($eid===false) {
$eid = IPS_CreateEvent($type);
IPS_SetParent($eid, $id);
IPS_SetName($eid, $name);
}
// einmaliges Event
IPS_SetEventCyclic($eid, 1, 0, 0, 0, 0, 0);
IPS_SetEventCyclicDateFrom($eid, (int)date('j',$time), (int)date('n', $time), (int)date('Y', $time));
IPS_SetEventCyclicDateTo($eid, 0, 0, 0);
IPS_SetEventCyclicTimeFrom($eid, (int)date("H", $time), (int)date("i", $time), (int)date("s", $time));
IPS_SetEventCyclicTimeTo($eid, 0, 0, 0);
IPS_SetEventActive($eid, true);
return $eid;
}
// Erzeugt eine Variable unterhalb {id} mit dem Namen {name} vom Typ [type}
// Existiert die Variable schon wird diese zurückgeliefert.
// Types: 0 = Boolean, 1 = Integer, 2 = Float, 3 = String
function CreateVariableByName($id, $name, $type)
{
$vid = @IPS_GetVariableIDByName($name, $id);
if($vid===false) {
$vid = IPS_CreateVariable($type);
IPS_SetParent($vid, $id);
IPS_SetName($vid, $name);
}
return $vid;
}
// Erzeugt ein Variablenprofil vom Typ {type} mit Name n{name}
function CreateProfile($name, $type)
{
if(!IPS_VariableProfileExists($name)) {
IPS_CreateVariableProfile($name, $type);
}
else {
$profile = IPS_GetVariableProfile($name);
if($profile['ProfileType'] != $type)
throw new Exception("Variable profile type does not match for profile ".$name);
}
}
// Erzeugt ein Integer-Variablenprofil
function CreateProfileInteger($name, $icon, $prefix, $suffix, $minvalue, $maxvalue, $step, $digits, $asso = NULL)
{
CreateProfile($name, 1);
IPS_SetVariableProfileIcon($name, $icon);
IPS_SetVariableProfileText($name, $prefix, $suffix);
IPS_SetVariableProfileDigits($name, $digits);
if(sizeof($asso) !== 0){
$minvalue = 0;
$maxvalue = 0;
}
IPS_SetVariableProfileValues($name, $minvalue, $maxvalue, $step);
if(sizeof($asso) !== 0){
foreach($asso as $ass) {
IPS_SetVariableProfileAssociation($name, $ass[0], $ass[1], $ass[2], $ass[3]);
}
}
}
################################################################################
?>
Viel Erfolg
Heiko