Browse Source

Article::get_article_image() - also return stream URI if possible

Andrew Dolgov 1 year ago
parent
commit
d4df57e1a4
3 changed files with 30 additions and 10 deletions
  1. 5 1
      classes/api.php
  2. 21 6
      classes/article.php
  3. 4 3
      classes/handler/public.php

+ 5 - 1
classes/api.php

@@ -803,7 +803,11 @@ class API extends Handler {
 					}
 
 					$headline_row["content"] = DiskCache::rewriteUrls($headline_row['content']);
-					$headline_row["flavor_image"] = Article::get_article_image($enclosures, $line["content"], $line["site_url"]);
+
+					list ($flavor_image, $flavor_stream) = Article::get_article_image($enclosures, $line["content"], $line["site_url"]);
+
+					$headline_row["flavor_image"] = $flavor_image;
+					$headline_row["flavor_stream"] = $flavor_stream;
 
 					array_push($headlines, $headline_row);
 				}

+ 21 - 6
classes/article.php

@@ -826,6 +826,8 @@ class Article extends Handler_Protected {
 	static function get_article_image($enclosures, $content, $site_url) {
 
 		$article_image = false;
+		$article_stream = false;
+
 		$tmpdoc = new DOMDocument();
 
 		if (@$tmpdoc->loadHTML('<?xml encoding="UTF-8">' . mb_substr($content, 0, 131070))) {
@@ -837,10 +839,18 @@ class Article extends Handler_Protected {
 					$matches = [];
 					if ($rrr = preg_match("/\/embed\/([\w-]+)/", $e->getAttribute("src"), $matches)) {
 						$article_image = "https://img.youtube.com/vi/" . $matches[1] . "/hqdefault.jpg";
+						$article_stream = "https://youtu.be/" . $matches[1];
 						break;
 					}
 				} else if ($e->nodeName == "video") {
 					$article_image = $e->getAttribute("poster");
+
+					$src = $tmpxpath->query("//source[@src]", $e)->item(0);
+
+					if ($src) {
+						$article_stream = $src->getAttribute("src");
+					}
+
 					break;
 				} else if ($e->nodeName == 'img') {
 					if (mb_strpos($e->getAttribute("src"), "data:") !== 0) {
@@ -852,15 +862,20 @@ class Article extends Handler_Protected {
 		}
 
 		if ($article_image)
-			return rewrite_relative_url($site_url, $article_image);
+			$article_image = rewrite_relative_url($site_url, $article_image);
 
-		foreach ($enclosures as $enc) {
-			if (strpos($enc["content_type"], "image/") !== FALSE) {
-				return rewrite_relative_url($site_url, $enc["content_url"]);
+		if ($article_stream)
+			$article_stream = rewrite_relative_url($site_url, $article_stream);
+
+		if (!$article_image)
+			foreach ($enclosures as $enc) {
+				if (strpos($enc["content_type"], "image/") !== FALSE) {
+					$article_image = rewrite_relative_url($site_url, $enc["content_url"]);
+					break;
+				}
 			}
-		}
 
-		return false;
+		return [$article_image, $article_stream];
 	}
 
 }

+ 4 - 3
classes/handler/public.php

@@ -156,8 +156,9 @@ class Handler_Public extends Handler {
 					$tpl->setVariable('ARTICLE_ENCLOSURE_LENGTH', null, true);
 				}
 
-				$tpl->setVariable('ARTICLE_OG_IMAGE',
-                        Article::get_article_image($enclosures, $line['content'], $feed_site_url), true);
+				list ($og_image, $og_stream) = Article::get_article_image($enclosures, $line['content'], $feed_site_url);
+
+				$tpl->setVariable('ARTICLE_OG_IMAGE', $og_image, true);
 
 				$tpl->addBlock('entry');
 			}
@@ -381,7 +382,7 @@ class Handler_Public extends Handler {
 
             $rv .= "</head>";
 
-            $og_image = Article::get_article_image($enclosures, $line['content'], $line["site_url"]);
+            list ($og_image, $og_stream) = Article::get_article_image($enclosures, $line['content'], $line["site_url"]);
 
             if ($og_image) {
                 $rv .= "<meta property='og:image' content=\"" . htmlspecialchars($og_image) . "\"/>";