Explorar o código

Expose podcast in API (#2898)

* Expose podcast in API

Expose RSS enclosures in our API, e.g. for clients supporting podcasts

* PHP CS?

* Annoying linter

* Light optimisation
Alexandre Alapetite %!s(int64=6) %!d(string=hai) anos
pai
achega
bdc4da6ad0
Modificáronse 3 ficheiros con 39 adicións e 0 borrados
  1. 22 0
      app/Models/Entry.php
  2. 5 0
      app/Models/Feed.php
  3. 12 0
      p/api/greader.php

+ 22 - 0
app/Models/Entry.php

@@ -59,6 +59,28 @@ class FreshRSS_Entry extends Minz_Model {
 	public function content() {
 		return $this->content;
 	}
+	public function enclosures() {
+		$results = [];
+		try {
+			if (strpos($this->content, '<p class="enclosure-content') !== false) {
+				$dom = new DOMDocument();
+				$dom->loadHTML($this->content, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING);
+				$xpath = new DOMXpath($dom);
+				$enclosures = $xpath->query('//div[@class="enclosure"]/p[@class="enclosure-content"]/*[@src]');
+				foreach ($enclosures as $enclosure) {
+					$results[] = [
+						'url' => $enclosure->getAttribute('src'),
+						'type' => $enclosure->getAttribute('data-type'),
+						'length' => $enclosure->getAttribute('data-length'),
+					];
+				}
+			}
+			return $results;
+		} catch (Exception $ex) {
+			return $results;
+		}
+	}
+
 	public function link() {
 		return $this->link;
 	}

+ 5 - 0
app/Models/Feed.php

@@ -373,13 +373,18 @@ class FreshRSS_Feed extends Minz_Model {
 						$elinks[$elink] = true;
 						$mime = strtolower($enclosure->get_type());
 						$medium = strtolower($enclosure->get_medium());
+						$length = $enclosure->get_length();
 						if ($medium === 'image' || strpos($mime, 'image/') === 0) {
 							$enclosureContent .= '<p class="enclosure-content"><img src="' . $elink . '" alt="" /></p>';
 						} elseif ($medium === 'audio' || strpos($mime, 'audio/') === 0) {
 							$enclosureContent .= '<p class="enclosure-content"><audio preload="none" src="' . $elink
+								. ($length == null ? '' : '" data-length="' . intval($length))
+								. '" data-type="' . htmlspecialchars($mime, ENT_COMPAT, 'UTF-8')
 								. '" controls="controls"></audio> <a download="" href="' . $elink . '">💾</a></p>';
 						} elseif ($medium === 'video' || strpos($mime, 'video/') === 0) {
 							$enclosureContent .= '<p class="enclosure-content"><video preload="none" src="' . $elink
+								. ($length == null ? '' : '" data-length="' . intval($length))
+								. '" data-type="' . htmlspecialchars($mime, ENT_COMPAT, 'UTF-8')
 								. '" controls="controls"></video> <a download="" href="' . $elink . '">💾</a></p>';
 						} elseif ($medium != '' || strpos($mime, 'application/') === 0 || strpos($mime, 'text/') === 0) {
 							$enclosureContent .= '<p class="enclosure-content"><a download="" href="' . $elink . '">💾</a></p>';

+ 12 - 0
p/api/greader.php

@@ -508,6 +508,18 @@ function entriesToArray($entries) {
 				//'htmlUrl' => $line['f_website'],
 			),
 		);
+		foreach ($entry->enclosures() as $enclosure) {
+			if (!empty($enclosure['url']) && !empty($enclosure['type'])) {
+				$media = [
+						'href' => $enclosure['url'],
+						'type' => $enclosure['type'],
+					];
+				if (!empty($enclosure['length'])) {
+					$media['length'] = intval($enclosure['length']);
+				}
+				$item['enclosure'][] = $media;
+			}
+		}
 		$author = $entry->authors(true);
 		$author = trim($author, '; ');
 		if ($author != '') {