Bladeren bron

RSS thumbnails (#5972)

* Added addtional media:content for thumbnails

* Fix whitespace

* More attributes for enclosures

* Fix variable

* Fix variable

* No duplicates

---------

Co-authored-by: root <root@ha-server.lan>
Alexandre Alapetite 2 jaren geleden
bovenliggende
commit
25166c218b
2 gewijzigde bestanden met toevoegingen van 24 en 14 verwijderingen
  1. 1 1
      app/Models/Entry.php
  2. 23 13
      app/views/index/rss.phtml

+ 1 - 1
app/Models/Entry.php

@@ -314,7 +314,7 @@ HTML;
 	}
 
 	/**
-	 * @return array{'url':string,'type'?:string,'medium'?:string,'length'?:int,'title'?:string,'description'?:string,'credit'?:string,'height'?:int,'width'?:int,'thumbnails'?:array<string>}|null
+	 * @return array{'url':string,'height'?:int,'width'?:int,'time'?:string}|null
 	 */
 	public function thumbnail(bool $searchEnclosures = true): ?array {
 		$thumbnail = $this->attributeArray('thumbnail') ?? [];

+ 23 - 13
app/views/index/rss.phtml

@@ -41,28 +41,38 @@ foreach ($this->entries as $item) {
 						echo "\t\t\t", '<category>', $category, '</category>', "\n";
 					}
 				}
+
+				$enclosures = iterator_to_array($item->enclosures(false));
 				$thumbnail = $item->thumbnail(false);
 				if (!empty($thumbnail['url'])) {
 					// https://www.rssboard.org/media-rss#media-thumbnails
 					echo "\t\t\t", '<media:thumbnail url="' . $thumbnail['url']
 						. (empty($thumbnail['width']) ? '' : '" width="' . $thumbnail['width'])
 						. (empty($thumbnail['height']) ? '' : '" height="' . $thumbnail['height'])
-						. (empty($thumbnail['type']) ? '' : '" type="' . $thumbnail['type'])
+						. (empty($thumbnail['time']) ? '' : '" time="' . $thumbnail['time'])
 						. '" />', "\n";
+					// Mostly for MailChimp + Feedbro which do not support <media:thumbnail> https://mailchimp.com/help/rss-merge-tags/
+					$thumbnail['medium'] ??= 'image';
+					array_unshift($enclosures, $thumbnail);
 				}
-				$enclosures = $item->enclosures(false);
-				if (is_iterable($enclosures)) {
-					foreach ($enclosures as $enclosure) {
-						// https://www.rssboard.org/media-rss
-						echo "\t\t\t", '<media:content url="' . $enclosure['url']
-							. (empty($enclosure['medium']) ? '' : '" medium="' . $enclosure['medium'])
-							. (empty($enclosure['type']) ? '' : '" type="' . $enclosure['type'])
-							. (empty($enclosure['length']) ? '' : '" length="' . $enclosure['length'])
-							. '">'
-							. (empty($enclosure['title']) ? '' : '<media:title type="html">' . $enclosure['title'] . '</media:title>')
-							. (empty($enclosure['credit']) ? '' : '<media:credit>' . $enclosure['credit'] . '</media:credit>')
-							. '</media:content>', "\n";
+
+				$urls = [];
+				foreach ($enclosures as $enclosure) {
+					if (empty($enclosure['url']) || isset($urls[$enclosure['url']])) {
+						continue;
 					}
+					$urls[$enclosure['url']] = true;
+					// https://www.rssboard.org/media-rss
+					echo "\t\t\t", '<media:content url="' . $enclosure['url']
+						. (empty($enclosure['medium']) ? '' : '" medium="' . $enclosure['medium'])
+						. (empty($enclosure['type']) ? '' : '" type="' . $enclosure['type'])
+						. (empty($enclosure['length']) ? '' : '" length="' . $enclosure['length'])
+						. (empty($enclosure['height']) ? '' : '" height="' . $enclosure['height'])
+						. (empty($enclosure['width']) ? '' : '" width="' . $enclosure['width'])
+						. '">'
+						. (empty($enclosure['title']) ? '' : '<media:title type="html">' . $enclosure['title'] . '</media:title>')
+						. (empty($enclosure['credit']) ? '' : '<media:credit>' . $enclosure['credit'] . '</media:credit>')
+						. '</media:content>', "\n";
 				}
 			?>
 			<description><![CDATA[<?php