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

This commit is contained in:
Andrew Dolgov 2019-08-14 17:04:14 +03:00
parent 68e2b05f65
commit d4df57e1a4
3 changed files with 30 additions and 10 deletions

View File

@ -803,7 +803,11 @@ class API extends Handler {
} }
$headline_row["content"] = DiskCache::rewriteUrls($headline_row['content']); $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); array_push($headlines, $headline_row);
} }

View File

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

View File

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