From d4df57e1a4a54cfa5bc303eaae3716af2cab1b17 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 14 Aug 2019 17:04:14 +0300 Subject: [PATCH] Article::get_article_image() - also return stream URI if possible --- classes/api.php | 6 +++++- classes/article.php | 27 +++++++++++++++++++++------ classes/handler/public.php | 7 ++++--- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/classes/api.php b/classes/api.php index 0c5c30135..44c9841ce 100755 --- a/classes/api.php +++ b/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); } diff --git a/classes/article.php b/classes/article.php index f22c28066..e35381d0f 100755 --- a/classes/article.php +++ b/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('' . 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]; } } diff --git a/classes/handler/public.php b/classes/handler/public.php index b476805e1..06c01df57 100755 --- a/classes/handler/public.php +++ b/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 .= ""; - $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 .= "";