mirror of
https://tt-rss.org/git/tt-rss.git
synced 2024-06-29 12:20:51 +02:00
add HOOK_ARTICLE_IMAGE for Article::get_article_image()
This commit is contained in:
parent
ffb842f752
commit
9d852e052c
|
@ -828,59 +828,65 @@ class Article extends Handler_Protected {
|
||||||
$article_image = "";
|
$article_image = "";
|
||||||
$article_stream = "";
|
$article_stream = "";
|
||||||
|
|
||||||
$tmpdoc = new DOMDocument();
|
foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_IMAGE) as $p) {
|
||||||
|
list ($article_image, $article_stream) = $p->hook_article_image($enclosures, $content, $site_url);
|
||||||
if (@$tmpdoc->loadHTML('<?xml encoding="UTF-8">' . mb_substr($content, 0, 131070))) {
|
|
||||||
$tmpxpath = new DOMXPath($tmpdoc);
|
|
||||||
$elems = $tmpxpath->query('(//img[@src]|//video[@poster]|//iframe[contains(@src , "youtube.com/embed/")])');
|
|
||||||
|
|
||||||
foreach ($elems as $e) {
|
|
||||||
if ($e->nodeName == "iframe") {
|
|
||||||
$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) {
|
|
||||||
$article_image = $e->getAttribute("src");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($article_image)
|
if (!$article_image && !$article_stream) {
|
||||||
$article_image = rewrite_relative_url($site_url, $article_image);
|
$tmpdoc = new DOMDocument();
|
||||||
|
|
||||||
if ($article_stream)
|
if (@$tmpdoc->loadHTML('<?xml encoding="UTF-8">' . mb_substr($content, 0, 131070))) {
|
||||||
$article_stream = rewrite_relative_url($site_url, $article_stream);
|
$tmpxpath = new DOMXPath($tmpdoc);
|
||||||
|
$elems = $tmpxpath->query('(//img[@src]|//video[@poster]|//iframe[contains(@src , "youtube.com/embed/")])');
|
||||||
|
|
||||||
if (!$article_image)
|
foreach ($elems as $e) {
|
||||||
foreach ($enclosures as $enc) {
|
if ($e->nodeName == "iframe") {
|
||||||
if (strpos($enc["content_type"], "image/") !== FALSE) {
|
$matches = [];
|
||||||
$article_image = rewrite_relative_url($site_url, $enc["content_url"]);
|
if ($rrr = preg_match("/\/embed\/([\w-]+)/", $e->getAttribute("src"), $matches)) {
|
||||||
break;
|
$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) {
|
||||||
|
$article_image = $e->getAttribute("src");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!$article_image)
|
||||||
|
foreach ($enclosures as $enc) {
|
||||||
|
if (strpos($enc["content_type"], "image/") !== FALSE) {
|
||||||
|
$article_image = $enc["content_url"];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($article_image)
|
||||||
|
$article_image = rewrite_relative_url($site_url, $article_image);
|
||||||
|
|
||||||
|
if ($article_stream)
|
||||||
|
$article_stream = rewrite_relative_url($site_url, $article_stream);
|
||||||
|
}
|
||||||
|
|
||||||
$cache = new DiskCache("images");
|
$cache = new DiskCache("images");
|
||||||
|
|
||||||
if ($cache->exists(sha1($article_image)))
|
if ($article_image && $cache->exists(sha1($article_image)))
|
||||||
$article_image = $cache->getUrl(sha1($article_image));
|
$article_image = $cache->getUrl(sha1($article_image));
|
||||||
|
|
||||||
if ($cache->exists(sha1($article_stream)))
|
if ($article_stream && $cache->exists(sha1($article_stream)))
|
||||||
$article_stream = $cache->getUrl(sha1($article_stream));
|
$article_stream = $cache->getUrl(sha1($article_stream));
|
||||||
|
|
||||||
return [$article_image, $article_stream];
|
return [$article_image, $article_stream];
|
||||||
|
|
|
@ -59,6 +59,7 @@ class PluginHost {
|
||||||
const HOOK_SEND_MAIL = 39;
|
const HOOK_SEND_MAIL = 39;
|
||||||
const HOOK_FILTER_TRIGGERED = 40;
|
const HOOK_FILTER_TRIGGERED = 40;
|
||||||
const HOOK_GET_FULL_TEXT = 41;
|
const HOOK_GET_FULL_TEXT = 41;
|
||||||
|
const HOOK_ARTICLE_IMAGE = 42;
|
||||||
|
|
||||||
const KIND_ALL = 1;
|
const KIND_ALL = 1;
|
||||||
const KIND_SYSTEM = 2;
|
const KIND_SYSTEM = 2;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user