gérer exception sur lecture de rss (ref #197)

This commit is contained in:
afornerot 2020-09-02 11:44:22 +02:00
parent ececb38afe
commit cbbd67c6ea
4 changed files with 166 additions and 151 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 27 KiB

View File

@ -1003,86 +1003,94 @@ class PagewidgetController extends Controller
$client = new \FeedIo\Adapter\Guzzle\Client($clientguzzle); $client = new \FeedIo\Adapter\Guzzle\Client($clientguzzle);
$logger = new \Psr\Log\NullLogger(); $logger = new \Psr\Log\NullLogger();
$feedIo = new \FeedIo\FeedIo($client, $logger); $feedIo = new \FeedIo\FeedIo($client, $logger);
$result = $feedIo->read($url);
$feedtitle = $result->getFeed()->getTitle();
$backup=[];
// Pour chaque articles try {
$nbflux=0; $result = $feedIo->read($url);
foreach ($result->getFeed() as $item) { }
$title = $item->getTitle(); catch (\Exception $e) {
$link = $item->getLink(); $result=null;
$description = substr(strip_tags($item->getDescription()),0,250);
$description = str_replace("¶","",$description);
$date = $item->getLastModified();
$dateformat = ($date?$item->getLastModified()->format('d/m/Y H:i:s'):"");
$content = $item->getDescription();
$media = $item->getMedias();
$mediaurl = "";
foreach ($item->getMedias() as $itemMedia) {
if($itemMedia->getType()=="image/jpeg")
$mediaurl=$itemMedia->getUrl();
}
foreach ($item->getAllElements() as $element) {
if($element->getName()=="content:encoded")
$content = $element->getValue();
}
if($mediaurl!="") {
$imgurl=$mediaurl;
}
else {
// Récupérer la première image du contenu
$imgcontent="";
$imgurl="";
$imgstart = stripos($content,"<img");
if($imgstart!== false) {
$imgend = stripos($content,">",$imgstart);
$imgcontent=substr($content,$imgstart,$imgend-$imgstart+1);
$imgstart = stripos($imgcontent,"src=");
$imgurl=substr($imgcontent,$imgstart+5);
$imgend = stripos($imgurl,'"');
if($imgend===false) $imgend = stripos($imgurl,"'");
$imgurl=substr($imgurl,0,$imgend);
}
}
// Garder le flux
$tmp=array(
"feedtitle" => $feedtitle,
"title" => $title,
"link" => $link,
"description" => $description,
"date" => $date,
"dateformat" => $dateformat,
"content" => $content,
"image" => $imgurl,
"color" => $flux->getColor(),
"fluxid" => $flux->getId(),
"fluxtitle" => $flux->getTitle(),
);
array_push($feeds,$tmp);
array_push($backup,$tmp);
$nbflux++;
if($nbflux>=$flux->getMaxread()&&$flux->getMaxread()>0) break;
} }
if($nbflux==0) unset($fluxs[$keyflux]);
// On sauvegarde le résultat en fichier on rechargera son contenu que s'il n'a pas été lu depuis 30mn if($result) {
$filesystem->dumpFile($filecache, serialize($backup)); $feedtitle = $result->getFeed()->getTitle();
$backup=[];
// Pour chaque articles
$nbflux=0;
foreach ($result->getFeed() as $item) {
$title = $item->getTitle();
$link = $item->getLink();
$description = substr(strip_tags($item->getDescription()),0,250);
$description = str_replace("&para;","",$description);
$date = $item->getLastModified();
$dateformat = ($date?$item->getLastModified()->format('d/m/Y H:i:s'):"");
$content = $item->getDescription();
$media = $item->getMedias();
$mediaurl = "";
foreach ($item->getMedias() as $itemMedia) {
if($itemMedia->getType()=="image/jpeg")
$mediaurl=$itemMedia->getUrl();
}
foreach ($item->getAllElements() as $element) {
if($element->getName()=="content:encoded")
$content = $element->getValue();
}
if($mediaurl!="") {
$imgurl=$mediaurl;
}
else {
// Récupérer la première image du contenu
$imgcontent="";
$imgurl="";
$imgstart = stripos($content,"<img");
if($imgstart!== false) {
$imgend = stripos($content,">",$imgstart);
$imgcontent=substr($content,$imgstart,$imgend-$imgstart+1);
$imgstart = stripos($imgcontent,"src=");
$imgurl=substr($imgcontent,$imgstart+5);
$imgend = stripos($imgurl,'"');
if($imgend===false) $imgend = stripos($imgurl,"'");
$imgurl=substr($imgurl,0,$imgend);
}
}
// Garder le flux
$tmp=array(
"feedtitle" => $feedtitle,
"title" => $title,
"link" => $link,
"description" => $description,
"date" => $date,
"dateformat" => $dateformat,
"content" => $content,
"image" => $imgurl,
"color" => $flux->getColor(),
"fluxid" => $flux->getId(),
"fluxtitle" => $flux->getTitle(),
);
array_push($feeds,$tmp);
array_push($backup,$tmp);
$nbflux++;
if($nbflux>=$flux->getMaxread()&&$flux->getMaxread()>0) break;
}
if($nbflux==0) unset($fluxs[$keyflux]);
// On sauvegarde le résultat en fichier on rechargera son contenu que s'il n'a pas été lu depuis 30mn
$filesystem->dumpFile($filecache, serialize($backup));
}
} }
} }
@ -1218,82 +1226,90 @@ class PagewidgetController extends Controller
$feeds=array(); $feeds=array();
// On lit le flux rss // On lit le flux rss
$result = $feedIo->read($url); try {
$feedtitle = $result->getFeed()->getTitle(); $result = $feedIo->read($url);
}
catch (\Exception $e) {
// Pour chaque articles $result=null;
$nbflux=0; }
foreach ($result->getFeed() as $item) {
$title = $item->getTitle();
$link = $item->getLink();
$description = substr(strip_tags($item->getDescription()),0,250);
$description = str_replace("&para;","",$description);
$date = $item->getLastModified();
$dateformat = ($date?$item->getLastModified()->format('d/m/Y H:i:s'):"");
$content = $item->getDescription();
$media = $item->getMedias();
$mediaurl = "";
foreach ($item->getMedias() as $itemMedia) { if($result) {
if($itemMedia->getType()=="image/jpeg") $feedtitle = $result->getFeed()->getTitle();
$mediaurl=$itemMedia->getUrl();
}
foreach ($item->getAllElements() as $element) {
if($element->getName()=="content:encoded")
$content = $element->getValue();
}
if($mediaurl!="") {
$imgurl=$mediaurl;
}
else {
// Récupérer la première image du contenu
$imgcontent="";
$imgurl="";
$imgstart = stripos($content,"<img");
if($imgstart!== false) {
$imgend = stripos($content,">",$imgstart);
$imgcontent=substr($content,$imgstart,$imgend-$imgstart+1);
$imgstart = stripos($imgcontent,"src=");
$imgurl=substr($imgcontent,$imgstart+5);
$imgend = stripos($imgurl,'"');
if($imgend===false) $imgend = stripos($imgurl,"'");
$imgurl=substr($imgurl,0,$imgend);
}
}
// Garder le flux
array_push($feeds,array( // Pour chaque articles
"feedtitle" => $feedtitle, $nbflux=0;
"title" => $title, foreach ($result->getFeed() as $item) {
"link" => $link, $title = $item->getTitle();
"description" => $description, $link = $item->getLink();
"date" => $date, $description = substr(strip_tags($item->getDescription()),0,250);
"dateformat" => $dateformat, $description = str_replace("&para;","",$description);
"content" => $content, $date = $item->getLastModified();
"image" => $imgurl, $dateformat = ($date?$item->getLastModified()->format('d/m/Y H:i:s'):"");
// "color" => $flux->getColor(), $content = $item->getDescription();
"fluxid" => 0, $media = $item->getMedias();
"fluxtitle" => $entity->getName(), $mediaurl = "";
));
$nbflux++; foreach ($item->getMedias() as $itemMedia) {
if($nbflux>=$nbarticle&&$nbarticle>0) break; if($itemMedia->getType()=="image/jpeg")
$mediaurl=$itemMedia->getUrl();
}
foreach ($item->getAllElements() as $element) {
if($element->getName()=="content:encoded")
$content = $element->getValue();
}
if($mediaurl!="") {
$imgurl=$mediaurl;
}
else {
// Récupérer la première image du contenu
$imgcontent="";
$imgurl="";
$imgstart = stripos($content,"<img");
if($imgstart!== false) {
$imgend = stripos($content,">",$imgstart);
$imgcontent=substr($content,$imgstart,$imgend-$imgstart+1);
$imgstart = stripos($imgcontent,"src=");
$imgurl=substr($imgcontent,$imgstart+5);
$imgend = stripos($imgurl,'"');
if($imgend===false) $imgend = stripos($imgurl,"'");
$imgurl=substr($imgurl,0,$imgend);
}
}
// Garder le flux
array_push($feeds,array(
"feedtitle" => $feedtitle,
"title" => $title,
"link" => $link,
"description" => $description,
"date" => $date,
"dateformat" => $dateformat,
"content" => $content,
"image" => $imgurl,
// "color" => $flux->getColor(),
"fluxid" => 0,
"fluxtitle" => $entity->getName(),
));
$nbflux++;
if($nbflux>=$nbarticle&&$nbarticle>0) break;
}
$libelle=array();
foreach ($feeds as $key => $valeur) {
$libelle[$key] = $valeur["date"];
}
array_multisort($libelle, SORT_DESC, $feeds);
// On sauvegarde le résultat en fichier on rechargera son contenu que s'il n'a pas été lu depuis 30mn
$filesystem->dumpFile($filecache, serialize($feeds));
} }
$libelle=array();
foreach ($feeds as $key => $valeur) {
$libelle[$key] = $valeur["date"];
}
array_multisort($libelle, SORT_DESC, $feeds);
// On sauvegarde le résultat en fichier on rechargera son contenu que s'il n'a pas été lu depuis 30mn
$filesystem->dumpFile($filecache, serialize($feeds));
} }
// Render // Render

View File

@ -56,7 +56,6 @@
{% set firstflux="" %} {% set firstflux="" %}
{% if fluxs is defined and fluxs is not empty %}
<div class="widgetbody" style="{{ stylewidgetbody }} "> <div class="widgetbody" style="{{ stylewidgetbody }} ">
{% if fluxs is defined %} {% if fluxs is defined %}
{% if fluxs|length > 1 %} {% if fluxs|length > 1 %}
@ -117,7 +116,7 @@
{% endfor %} {% endfor %}
</div> </div>
</div> </div>
{% endif %}
</div> </div>