af_redditimgur: cleanup, rework to embed stuff from reddit-provided JSON first
This commit is contained in:
parent
2933483393
commit
1ded706f8f
|
@ -96,267 +96,360 @@ class Af_RedditImgur extends Plugin {
|
||||||
echo __("Configuration saved");
|
echo __("Configuration saved");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function process_post_media($data, $doc, $xpath, $anchor) {
|
||||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
$found = 0;
|
||||||
*/
|
|
||||||
private function inline_stuff($article, &$doc, $xpath) {
|
|
||||||
|
|
||||||
$entries = $xpath->query('(//a[@href]|//img[@src])');
|
if (is_array($data["media_metadata"])) {
|
||||||
$img_entries = $xpath->query("(//img[@src])");
|
foreach ($data["media_metadata"] as $media) {
|
||||||
|
$media_url = htmlspecialchars_decode($media["s"]["u"]);
|
||||||
|
|
||||||
|
Debug::log("found media_metadata (gallery): $media_url", Debug::$LOG_VERBOSE);
|
||||||
|
|
||||||
|
if ($media_url) {
|
||||||
|
$this->handle_as_image($doc, $anchor, $media_url);
|
||||||
|
$found = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// v.redd.it - see below
|
||||||
|
/* if (is_array($data["media"])) {
|
||||||
|
foreach ($data["media"] as $media) {
|
||||||
|
if (isset($media["fallback_url"])) {
|
||||||
|
$stream_url = $media["fallback_url"];
|
||||||
|
|
||||||
|
if (isset($data["preview"]["images"][0]["source"]))
|
||||||
|
$poster_url = $data["preview"]["images"][0]["source"]["url"];
|
||||||
|
else
|
||||||
|
$poster_url = "";
|
||||||
|
|
||||||
|
Debug::log("found stream fallback_url: $stream_url / poster $poster_url", Debug::$LOG_VERBOSE);
|
||||||
|
|
||||||
|
$this->handle_as_video($doc, $anchor, $stream_url, $poster_url);
|
||||||
|
}
|
||||||
|
|
||||||
|
$found = 1;
|
||||||
|
}
|
||||||
|
} */
|
||||||
|
|
||||||
|
if ($data["post_hint"] == "hosted:video") {
|
||||||
|
$media_url = $data["url"];
|
||||||
|
|
||||||
|
if (isset($data["preview"]["images"][0]["source"]))
|
||||||
|
$poster_url = htmlspecialchars_decode($data["preview"]["images"][0]["source"]["url"]);
|
||||||
|
else
|
||||||
|
$poster_url = "";
|
||||||
|
|
||||||
|
Debug::log("found hosted video url: $media_url / poster $poster_url, looking up fallback url...", Debug::$LOG_VERBOSE);
|
||||||
|
|
||||||
|
$fallback_url = $data["media"]["reddit_video"]["fallback_url"];
|
||||||
|
|
||||||
|
if ($fallback_url) {
|
||||||
|
Debug::log("found video fallback_url: $fallback_url", Debug::$LOG_VERBOSE);
|
||||||
|
$this->handle_as_video($doc, $anchor, $fallback_url, $poster_url);
|
||||||
|
|
||||||
|
$found = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($data["post_hint"] == "video") {
|
||||||
|
$media_url = $data["url"];
|
||||||
|
|
||||||
|
if (isset($data["preview"]["images"][0]["source"]))
|
||||||
|
$poster_url = htmlspecialchars_decode($data["preview"]["images"][0]["source"]["url"]);
|
||||||
|
else
|
||||||
|
$poster_url = "";
|
||||||
|
|
||||||
|
Debug::log("found video url: $media_url / poster $poster_url", Debug::$LOG_VERBOSE);
|
||||||
|
$this->handle_as_video($doc, $anchor, $media_url, $poster_url);
|
||||||
|
|
||||||
|
$found = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($data["post_hint"] == "image") {
|
||||||
|
$media_url = $data["url"];
|
||||||
|
|
||||||
|
Debug::log("found image url: $media_url", Debug::$LOG_VERBOSE);
|
||||||
|
$this->handle_as_image($doc, $anchor, $media_url);
|
||||||
|
|
||||||
|
$found = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $found;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function inline_stuff($article, &$doc, $xpath) {
|
||||||
|
|
||||||
$found = false;
|
$found = false;
|
||||||
|
|
||||||
foreach ($entries as $entry) {
|
// deal with json-provided media content first
|
||||||
if ($entry->hasAttribute("href")) {
|
if ($article["link"]) {
|
||||||
$entry_href = $entry->getAttribute("href");
|
Debug::log("JSON: requesting from URL: " . $article["link"] . "/.json", Debug::$LOG_VERBOSE);
|
||||||
|
|
||||||
Debug::log("processing href: " . $entry_href, Debug::$LOG_VERBOSE);
|
$tmp = UrlHelper::fetch($article["link"] . "/.json");
|
||||||
|
|
||||||
$matches = [];
|
// embed before reddit <table> post layout
|
||||||
|
$anchor = $xpath->query('//body/*')->item(0);
|
||||||
|
|
||||||
if (!$found && preg_match("/^https?:\/\/www\.reddit\.com\/gallery\/(.*)/", $entry_href, $matches)) {
|
if ($tmp && $anchor) {
|
||||||
Debug::log("handling as a reddit gallery: " . $matches[1], Debug::$LOG_VERBOSE);
|
$json = json_decode($tmp, true);
|
||||||
|
|
||||||
$tmp = UrlHelper::fetch($entry_href);
|
Debug::log("JSON: processing media elements...", Debug::$LOG_EXTENDED);
|
||||||
|
|
||||||
if ($tmp) {
|
if ($json) {
|
||||||
$tmpdoc = new DOMDocument();
|
foreach ($json as $listing) {
|
||||||
|
foreach ($listing["data"]["children"] as $child) {
|
||||||
|
|
||||||
if (@$tmpdoc->loadHTML($tmp)) {
|
$data = $child["data"];
|
||||||
$tmpxpath = new DOMXPath($tmpdoc);
|
|
||||||
|
|
||||||
$links = $tmpxpath->query("//figure/a[@href]");
|
if (is_array($data["crosspost_parent_list"])) {
|
||||||
|
Debug::log("JSON: processing child crosspost_parent_list", Debug::$LOG_EXTENDED);
|
||||||
|
|
||||||
foreach ($links as $link) {
|
foreach ($data["crosspost_parent_list"] as $parent) {
|
||||||
$link_href = $link->getAttribute("href");
|
if ($this->process_post_media($parent, $doc, $xpath, $anchor)) {
|
||||||
|
$found = 1;
|
||||||
if (strpos($link_href, "preview.redd.it") !== false) {
|
continue;
|
||||||
Debug::log("found URL: $link_href", Debug::$LOG_EXTENDED);
|
|
||||||
|
|
||||||
// TODO: could there be other media types? videos?
|
|
||||||
if (strpos($link_href, ".jpg") !== false) {
|
|
||||||
$img = $doc->createElement("img");
|
|
||||||
$img->setAttribute("src", $link_href);
|
|
||||||
|
|
||||||
$p = $doc->createElement("p");
|
|
||||||
$p->appendChild($img);
|
|
||||||
|
|
||||||
$entry->parentNode->insertBefore($p, $entry);
|
|
||||||
|
|
||||||
$found = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* skip other links going to reddit other than galleries (and any other blacklisted stuff) */
|
Debug::log("JSON: processing child data element...", Debug::$LOG_EXTENDED);
|
||||||
if (!$found && $this->is_blacklisted($entry_href, ["reddit.com"])) {
|
|
||||||
Debug::log("domain is blacklisted, skipping", Debug::$LOG_VERBOSE);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$found && preg_match("/^https?:\/\/twitter.com\/(.*?)\/status\/(.*)/", $entry_href, $matches)) {
|
|
||||||
Debug::log("handling as twitter: " . $matches[1] . " " . $matches[2], Debug::$LOG_VERBOSE);
|
|
||||||
|
|
||||||
$oembed_result = UrlHelper::fetch("https://publish.twitter.com/oembed?url=" . urlencode($entry_href));
|
|
||||||
|
|
||||||
if ($oembed_result) {
|
|
||||||
$oembed_result = json_decode($oembed_result, true);
|
|
||||||
|
|
||||||
if ($oembed_result && isset($oembed_result["html"])) {
|
|
||||||
|
|
||||||
$tmp = new DOMDocument();
|
|
||||||
if (@$tmp->loadHTML('<?xml encoding="utf-8" ?>' . $oembed_result["html"])) {
|
|
||||||
$p = $doc->createElement("p");
|
|
||||||
|
|
||||||
$p->appendChild($doc->importNode(
|
|
||||||
$tmp->getElementsByTagName("blockquote")->item(0), TRUE));
|
|
||||||
|
|
||||||
$br = $doc->createElement('br');
|
|
||||||
$entry->parentNode->insertBefore($p, $entry);
|
|
||||||
$entry->parentNode->insertBefore($br, $entry);
|
|
||||||
|
|
||||||
|
if (!$found && $this->process_post_media($data, $doc, $xpath, $anchor)) {
|
||||||
$found = 1;
|
$found = 1;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
if (!$found && preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry_href, $matches)) {
|
if (!$tmp) {
|
||||||
$entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]);
|
global $fetch_last_error;
|
||||||
|
Debug::log("JSON: failed to fetch post:" . $fetch_last_error, Debug::$LOG_EXTENDED);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$found && preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry_href, $matches)) {
|
if (!$anchor) {
|
||||||
|
Debug::log("JSON: anchor element not found, unable to embed", Debug::$LOG_EXTENDED);
|
||||||
Debug::log("Handling as Gfycat", Debug::$LOG_VERBOSE);
|
|
||||||
|
|
||||||
$source_stream = 'https://giant.gfycat.com/' . $matches[2] . '.mp4';
|
|
||||||
$poster_url = 'https://thumbs.gfycat.com/' . $matches[2] . '-mobile.jpg';
|
|
||||||
|
|
||||||
$content_type = $this->get_content_type($source_stream);
|
|
||||||
|
|
||||||
if (strpos($content_type, "video/") !== false) {
|
|
||||||
$this->handle_as_video($doc, $entry, $source_stream, $poster_url);
|
|
||||||
$found = 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!$found && preg_match("/https?:\/\/v\.redd\.it\/(.*)$/i", $entry_href, $matches)) {
|
if ($found) {
|
||||||
|
Debug::log("JSON: found media data, skipping further processing of content", Debug::$LOG_VERBOSE);
|
||||||
|
$this->remove_post_thumbnail($doc, $xpath);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
Debug::log("Handling as reddit inline video", Debug::$LOG_VERBOSE);
|
$entries = $xpath->query('//a[@href]');
|
||||||
|
|
||||||
$img = $img_entries->item(0);
|
foreach ($entries as $entry) {
|
||||||
|
$entry_href = $entry->getAttribute("href");
|
||||||
|
|
||||||
if ($img) {
|
$matches = [];
|
||||||
$poster_url = $img->getAttribute("src");
|
|
||||||
} else {
|
|
||||||
$poster_url = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get original article URL from v.redd.it redirects
|
/* skip links going back to reddit (and any other blacklisted stuff) */
|
||||||
$source_article_url = $this->get_location($matches[0]);
|
if (!$found && $this->is_blacklisted($entry_href, ["reddit.com"])) {
|
||||||
Debug::log("Resolved ".$matches[0]." to ".$source_article_url, Debug::$LOG_VERBOSE);
|
Debug::log("BODY: domain of $entry_href is blacklisted, skipping", Debug::$LOG_EXTENDED);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$source_stream = false;
|
Debug::log("BODY: processing URL: " . $entry_href, Debug::$LOG_VERBOSE);
|
||||||
|
|
||||||
if ($source_article_url) {
|
if (!$found && preg_match("/^https?:\/\/twitter.com\/(.*?)\/status\/(.*)/", $entry_href, $matches)) {
|
||||||
$j = json_decode(UrlHelper::fetch($source_article_url.".json"), true);
|
Debug::log("handling as twitter: " . $matches[1] . " " . $matches[2], Debug::$LOG_VERBOSE);
|
||||||
|
|
||||||
if ($j) {
|
$oembed_result = UrlHelper::fetch("https://publish.twitter.com/oembed?url=" . urlencode($entry_href));
|
||||||
foreach ($j as $listing) {
|
|
||||||
foreach ($listing["data"]["children"] as $child) {
|
if ($oembed_result) {
|
||||||
if ($child["data"]["url"] == $matches[0]) {
|
$oembed_result = json_decode($oembed_result, true);
|
||||||
try {
|
|
||||||
$source_stream = $child["data"]["media"]["reddit_video"]["fallback_url"];
|
if ($oembed_result && isset($oembed_result["html"])) {
|
||||||
}
|
|
||||||
catch (Exception $e) {
|
$tmp = new DOMDocument();
|
||||||
}
|
if (@$tmp->loadHTML('<?xml encoding="utf-8" ?>' . $oembed_result["html"])) {
|
||||||
break 2;
|
$p = $doc->createElement("p");
|
||||||
}
|
|
||||||
}
|
$p->appendChild($doc->importNode(
|
||||||
}
|
$tmp->getElementsByTagName("blockquote")->item(0), TRUE));
|
||||||
|
|
||||||
|
$br = $doc->createElement('br');
|
||||||
|
$entry->parentNode->insertBefore($p, $entry);
|
||||||
|
$entry->parentNode->insertBefore($br, $entry);
|
||||||
|
|
||||||
|
$found = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!$source_stream) {
|
if (!$found && preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry_href, $matches)) {
|
||||||
$source_stream = "https://v.redd.it/" . $matches[1] . "/DASH_600_K";
|
$entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!$found && preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry_href, $matches)) {
|
||||||
|
|
||||||
|
Debug::log("Handling as Gfycat", Debug::$LOG_VERBOSE);
|
||||||
|
|
||||||
|
$source_stream = 'https://giant.gfycat.com/' . $matches[2] . '.mp4';
|
||||||
|
$poster_url = 'https://thumbs.gfycat.com/' . $matches[2] . '-mobile.jpg';
|
||||||
|
|
||||||
|
$content_type = $this->get_content_type($source_stream);
|
||||||
|
|
||||||
|
if (strpos($content_type, "video/") !== false) {
|
||||||
$this->handle_as_video($doc, $entry, $source_stream, $poster_url);
|
$this->handle_as_video($doc, $entry, $source_stream, $poster_url);
|
||||||
$found = 1;
|
$found = 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!$found && preg_match("/https?:\/\/(www\.)?streamable.com\//i", $entry_href)) {
|
// imgur .gif -> .gifv
|
||||||
|
if (!$found && preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry_href)) {
|
||||||
|
Debug::log("Handling as imgur gif (->gifv)", Debug::$LOG_VERBOSE);
|
||||||
|
|
||||||
Debug::log("Handling as Streamable", Debug::$LOG_VERBOSE);
|
$entry->setAttribute("href",
|
||||||
|
str_replace(".gif", ".gifv", $entry_href));
|
||||||
|
}
|
||||||
|
|
||||||
$tmp = UrlHelper::fetch($entry_href);
|
if (!$found && preg_match("/\.(gifv|mp4)$/i", $entry_href)) {
|
||||||
|
Debug::log("Handling as imgur gifv", Debug::$LOG_VERBOSE);
|
||||||
|
|
||||||
if ($tmp) {
|
$source_stream = str_replace(".gifv", ".mp4", $entry_href);
|
||||||
$tmpdoc = new DOMDocument();
|
|
||||||
|
|
||||||
if (@$tmpdoc->loadHTML($tmp)) {
|
if (strpos($source_stream, "imgur.com") !== false)
|
||||||
$tmpxpath = new DOMXPath($tmpdoc);
|
$poster_url = str_replace(".mp4", "h.jpg", $source_stream);
|
||||||
|
|
||||||
$source_node = $tmpxpath->query("//video[contains(@class,'video-player-tag')]//source[contains(@src, '.mp4')]")->item(0);
|
$this->handle_as_video($doc, $entry, $source_stream, $poster_url);
|
||||||
$poster_node = $tmpxpath->query("//video[contains(@class,'video-player-tag') and @poster]")->item(0);
|
|
||||||
|
|
||||||
if ($source_node && $poster_node) {
|
$found = true;
|
||||||
$source_stream = $source_node->getAttribute("src");
|
}
|
||||||
$poster_url = $poster_node->getAttribute("poster");
|
|
||||||
|
|
||||||
$this->handle_as_video($doc, $entry, $source_stream, $poster_url);
|
$matches = array();
|
||||||
$found = 1;
|
if (!$found && (preg_match("/youtube\.com\/v\/([\w-]+)/", $entry_href, $matches) ||
|
||||||
}
|
preg_match("/youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry_href, $matches) ||
|
||||||
|
preg_match("/youtube\.com\/watch\?v=([\w-]+)/", $entry_href, $matches) ||
|
||||||
|
preg_match("/\/\/youtu.be\/([\w-]+)/", $entry_href, $matches))) {
|
||||||
|
|
||||||
|
$vid_id = $matches[1];
|
||||||
|
|
||||||
|
Debug::log("Handling as youtube: $vid_id", Debug::$LOG_VERBOSE);
|
||||||
|
|
||||||
|
$iframe = $doc->createElement("iframe");
|
||||||
|
$iframe->setAttribute("class", "youtube-player");
|
||||||
|
$iframe->setAttribute("type", "text/html");
|
||||||
|
$iframe->setAttribute("width", "640");
|
||||||
|
$iframe->setAttribute("height", "385");
|
||||||
|
$iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id");
|
||||||
|
$iframe->setAttribute("allowfullscreen", "1");
|
||||||
|
$iframe->setAttribute("frameborder", "0");
|
||||||
|
|
||||||
|
$br = $doc->createElement('br');
|
||||||
|
$entry->parentNode->insertBefore($iframe, $entry);
|
||||||
|
$entry->parentNode->insertBefore($br, $entry);
|
||||||
|
|
||||||
|
$found = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$found && (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?[$\?]/i", $entry_href) ||
|
||||||
|
/* mb_strpos($entry_href, "i.reddituploads.com") !== false || */
|
||||||
|
mb_strpos($this->get_content_type($entry_href), "image/") !== false)) {
|
||||||
|
|
||||||
|
Debug::log("Handling as a picture", Debug::$LOG_VERBOSE);
|
||||||
|
|
||||||
|
$img = $doc->createElement('img');
|
||||||
|
$img->setAttribute("src", $entry_href);
|
||||||
|
|
||||||
|
$br = $doc->createElement('br');
|
||||||
|
$entry->parentNode->insertBefore($img, $entry);
|
||||||
|
$entry->parentNode->insertBefore($br, $entry);
|
||||||
|
|
||||||
|
$found = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// imgur via link rel="image_src" href="..."
|
||||||
|
if (!$found && preg_match("/imgur/", $entry_href)) {
|
||||||
|
|
||||||
|
Debug::log("handling as imgur page/whatever", Debug::$LOG_VERBOSE);
|
||||||
|
|
||||||
|
$content = UrlHelper::fetch(["url" => $entry_href,
|
||||||
|
"http_accept" => "text/*"]);
|
||||||
|
|
||||||
|
if ($content) {
|
||||||
|
$cdoc = new DOMDocument();
|
||||||
|
|
||||||
|
if (@$cdoc->loadHTML($content)) {
|
||||||
|
$cxpath = new DOMXPath($cdoc);
|
||||||
|
|
||||||
|
$rel_image = $cxpath->query("//link[@rel='image_src']")->item(0);
|
||||||
|
|
||||||
|
if ($rel_image) {
|
||||||
|
|
||||||
|
$img = $doc->createElement('img');
|
||||||
|
$img->setAttribute("src", $rel_image->getAttribute("href"));
|
||||||
|
|
||||||
|
$br = $doc->createElement('br');
|
||||||
|
$entry->parentNode->insertBefore($img, $entry);
|
||||||
|
$entry->parentNode->insertBefore($br, $entry);
|
||||||
|
|
||||||
|
$found = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// imgur .gif -> .gifv
|
// wtf is this even
|
||||||
if (!$found && preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry_href)) {
|
if (!$found && preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry_href, $matches)) {
|
||||||
Debug::log("Handling as imgur gif (->gifv)", Debug::$LOG_VERBOSE);
|
$img_id = $matches[1];
|
||||||
|
|
||||||
$entry->setAttribute("href",
|
Debug::log("handling as gyazo: $img_id", Debug::$LOG_VERBOSE);
|
||||||
str_replace(".gif", ".gifv", $entry_href));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$found && preg_match("/\.(gifv|mp4)$/i", $entry_href)) {
|
$img = $doc->createElement('img');
|
||||||
Debug::log("Handling as imgur gifv", Debug::$LOG_VERBOSE);
|
$img->setAttribute("src", "https://i.gyazo.com/$img_id.jpg");
|
||||||
|
|
||||||
$source_stream = str_replace(".gifv", ".mp4", $entry_href);
|
$br = $doc->createElement('br');
|
||||||
|
$entry->parentNode->insertBefore($img, $entry);
|
||||||
|
$entry->parentNode->insertBefore($br, $entry);
|
||||||
|
|
||||||
if (strpos($source_stream, "imgur.com") !== false)
|
$found = true;
|
||||||
$poster_url = str_replace(".mp4", "h.jpg", $source_stream);
|
}
|
||||||
|
|
||||||
$this->handle_as_video($doc, $entry, $source_stream, $poster_url);
|
// let's try meta properties
|
||||||
|
if (!$found) {
|
||||||
|
Debug::log("looking for meta og:image", Debug::$LOG_VERBOSE);
|
||||||
|
|
||||||
$found = true;
|
$content = UrlHelper::fetch(["url" => $entry_href,
|
||||||
}
|
"http_accept" => "text/*"]);
|
||||||
|
|
||||||
$matches = array();
|
if ($content) {
|
||||||
if (!$found && (preg_match("/youtube\.com\/v\/([\w-]+)/", $entry_href, $matches) ||
|
$cdoc = new DOMDocument();
|
||||||
preg_match("/youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry_href, $matches) ||
|
|
||||||
preg_match("/youtube\.com\/watch\?v=([\w-]+)/", $entry_href, $matches) ||
|
|
||||||
preg_match("/\/\/youtu.be\/([\w-]+)/", $entry_href, $matches))) {
|
|
||||||
|
|
||||||
$vid_id = $matches[1];
|
if (@$cdoc->loadHTML($content)) {
|
||||||
|
$cxpath = new DOMXPath($cdoc);
|
||||||
|
|
||||||
Debug::log("Handling as youtube: $vid_id", Debug::$LOG_VERBOSE);
|
$og_image = $cxpath->query("//meta[@property='og:image']")->item(0);
|
||||||
|
$og_video = $cxpath->query("//meta[@property='og:video']")->item(0);
|
||||||
|
|
||||||
$iframe = $doc->createElement("iframe");
|
if ($og_video) {
|
||||||
$iframe->setAttribute("class", "youtube-player");
|
|
||||||
$iframe->setAttribute("type", "text/html");
|
|
||||||
$iframe->setAttribute("width", "640");
|
|
||||||
$iframe->setAttribute("height", "385");
|
|
||||||
$iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id");
|
|
||||||
$iframe->setAttribute("allowfullscreen", "1");
|
|
||||||
$iframe->setAttribute("frameborder", "0");
|
|
||||||
|
|
||||||
$br = $doc->createElement('br');
|
$source_stream = $og_video->getAttribute("content");
|
||||||
$entry->parentNode->insertBefore($iframe, $entry);
|
|
||||||
$entry->parentNode->insertBefore($br, $entry);
|
|
||||||
|
|
||||||
$found = true;
|
if ($source_stream) {
|
||||||
}
|
|
||||||
|
|
||||||
if (!$found && (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?[$\?]/i", $entry_href) ||
|
if ($og_image) {
|
||||||
mb_strpos($entry_href, "i.reddituploads.com") !== false ||
|
$poster_url = $og_image->getAttribute("content");
|
||||||
mb_strpos($this->get_content_type($entry_href), "image/") !== false)) {
|
} else {
|
||||||
|
$poster_url = false;
|
||||||
|
}
|
||||||
|
|
||||||
Debug::log("Handling as a picture", Debug::$LOG_VERBOSE);
|
$this->handle_as_video($doc, $entry, $source_stream, $poster_url);
|
||||||
|
$found = true;
|
||||||
|
}
|
||||||
|
|
||||||
$img = $doc->createElement('img');
|
} else if ($og_image) {
|
||||||
$img->setAttribute("src", $entry_href);
|
|
||||||
|
|
||||||
$br = $doc->createElement('br');
|
$og_src = $og_image->getAttribute("content");
|
||||||
$entry->parentNode->insertBefore($img, $entry);
|
|
||||||
$entry->parentNode->insertBefore($br, $entry);
|
|
||||||
|
|
||||||
$found = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// imgur via link rel="image_src" href="..."
|
|
||||||
if (!$found && preg_match("/imgur/", $entry_href)) {
|
|
||||||
|
|
||||||
Debug::log("handling as imgur page/whatever", Debug::$LOG_VERBOSE);
|
|
||||||
|
|
||||||
$content = UrlHelper::fetch(["url" => $entry_href,
|
|
||||||
"http_accept" => "text/*"]);
|
|
||||||
|
|
||||||
if ($content) {
|
|
||||||
$cdoc = new DOMDocument();
|
|
||||||
|
|
||||||
if (@$cdoc->loadHTML($content)) {
|
|
||||||
$cxpath = new DOMXPath($cdoc);
|
|
||||||
|
|
||||||
$rel_image = $cxpath->query("//link[@rel='image_src']")->item(0);
|
|
||||||
|
|
||||||
if ($rel_image) {
|
|
||||||
|
|
||||||
|
if ($og_src) {
|
||||||
$img = $doc->createElement('img');
|
$img = $doc->createElement('img');
|
||||||
$img->setAttribute("src", $rel_image->getAttribute("href"));
|
$img->setAttribute("src", $og_src);
|
||||||
|
|
||||||
$br = $doc->createElement('br');
|
$br = $doc->createElement('br');
|
||||||
$entry->parentNode->insertBefore($img, $entry);
|
$entry->parentNode->insertBefore($img, $entry);
|
||||||
|
@ -367,82 +460,10 @@ class Af_RedditImgur extends Plugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// wtf is this even
|
|
||||||
if (!$found && preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry_href, $matches)) {
|
|
||||||
$img_id = $matches[1];
|
|
||||||
|
|
||||||
Debug::log("handling as gyazo: $img_id", Debug::$LOG_VERBOSE);
|
|
||||||
|
|
||||||
$img = $doc->createElement('img');
|
|
||||||
$img->setAttribute("src", "https://i.gyazo.com/$img_id.jpg");
|
|
||||||
|
|
||||||
$br = $doc->createElement('br');
|
|
||||||
$entry->parentNode->insertBefore($img, $entry);
|
|
||||||
$entry->parentNode->insertBefore($br, $entry);
|
|
||||||
|
|
||||||
$found = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// let's try meta properties
|
|
||||||
if (!$found) {
|
|
||||||
Debug::log("looking for meta og:image", Debug::$LOG_VERBOSE);
|
|
||||||
|
|
||||||
$content = UrlHelper::fetch(["url" => $entry_href,
|
|
||||||
"http_accept" => "text/*"]);
|
|
||||||
|
|
||||||
if ($content) {
|
|
||||||
$cdoc = new DOMDocument();
|
|
||||||
|
|
||||||
if (@$cdoc->loadHTML($content)) {
|
|
||||||
$cxpath = new DOMXPath($cdoc);
|
|
||||||
|
|
||||||
$og_image = $cxpath->query("//meta[@property='og:image']")->item(0);
|
|
||||||
$og_video = $cxpath->query("//meta[@property='og:video']")->item(0);
|
|
||||||
|
|
||||||
if ($og_video) {
|
|
||||||
|
|
||||||
$source_stream = $og_video->getAttribute("content");
|
|
||||||
|
|
||||||
if ($source_stream) {
|
|
||||||
|
|
||||||
if ($og_image) {
|
|
||||||
$poster_url = $og_image->getAttribute("content");
|
|
||||||
} else {
|
|
||||||
$poster_url = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->handle_as_video($doc, $entry, $source_stream, $poster_url);
|
|
||||||
$found = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if ($og_image) {
|
|
||||||
|
|
||||||
$og_src = $og_image->getAttribute("content");
|
|
||||||
|
|
||||||
if ($og_src) {
|
|
||||||
$img = $doc->createElement('img');
|
|
||||||
$img->setAttribute("src", $og_src);
|
|
||||||
|
|
||||||
$br = $doc->createElement('br');
|
|
||||||
$entry->parentNode->insertBefore($img, $entry);
|
|
||||||
$entry->parentNode->insertBefore($br, $entry);
|
|
||||||
|
|
||||||
$found = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove tiny thumbnails
|
if ($found)
|
||||||
if ($entry->hasAttribute("src")) {
|
$this->remove_post_thumbnail($doc, $xpath);
|
||||||
if ($entry->parentNode && $entry->parentNode->parentNode) {
|
|
||||||
$entry->parentNode->parentNode->removeChild($entry->parentNode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $found;
|
return $found;
|
||||||
|
@ -510,6 +531,23 @@ class Af_RedditImgur extends Plugin {
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function remove_post_thumbnail($doc, $xpath) {
|
||||||
|
$thumb = $xpath->query("//td/a/img[@src]")->item(0);
|
||||||
|
|
||||||
|
if ($thumb)
|
||||||
|
$thumb->parentNode->parentNode->removeChild($thumb->parentNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function handle_as_image($doc, $entry, $image_url) {
|
||||||
|
$img = $doc->createElement("img");
|
||||||
|
$img->setAttribute("src", $image_url);
|
||||||
|
|
||||||
|
$p = $doc->createElement("p");
|
||||||
|
$p->appendChild($img);
|
||||||
|
|
||||||
|
$entry->parentNode->insertBefore($p, $entry);
|
||||||
|
}
|
||||||
|
|
||||||
private function handle_as_video($doc, $entry, $source_stream, $poster_url = false) {
|
private function handle_as_video($doc, $entry, $source_stream, $poster_url = false) {
|
||||||
|
|
||||||
Debug::log("handle_as_video: $source_stream", Debug::$LOG_VERBOSE);
|
Debug::log("handle_as_video: $source_stream", Debug::$LOG_VERBOSE);
|
||||||
|
@ -538,32 +576,34 @@ class Af_RedditImgur extends Plugin {
|
||||||
$entry->parentNode->insertBefore($img, $entry);*/
|
$entry->parentNode->insertBefore($img, $entry);*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: draw a form or something if url/article_url are not given
|
||||||
function testurl() {
|
function testurl() {
|
||||||
header("Content-type: text/plain");
|
header("Content-type: text/plain");
|
||||||
|
|
||||||
Debug::set_enabled(true);
|
Debug::set_enabled(true);
|
||||||
Debug::set_loglevel(Debug::$LOG_VERBOSE);
|
Debug::set_loglevel(Debug::$LOG_EXTENDED);
|
||||||
|
|
||||||
$url = htmlspecialchars($_REQUEST["url"]);
|
$url = clean($_REQUEST["url"]);
|
||||||
|
$article_url = clean($_REQUEST["article_url"]);
|
||||||
|
|
||||||
Debug::log("URL: $url", Debug::$LOG_VERBOSE);
|
Debug::log("URL: $url", Debug::$LOG_VERBOSE);
|
||||||
|
|
||||||
$doc = new DOMDocument();
|
$doc = new DOMDocument();
|
||||||
@$doc->loadHTML("<html><body><a href=\"$url\">[link]</a></body>");
|
@$doc->loadHTML("<html><body><table><tr><td><a href=\"$url\">[link]</a></td></tr></table></body>");
|
||||||
$xpath = new DOMXPath($doc);
|
$xpath = new DOMXPath($doc);
|
||||||
|
|
||||||
$found = $this->inline_stuff([], $doc, $xpath);
|
$found = $this->inline_stuff(["link" => $article_url], $doc, $xpath);
|
||||||
|
|
||||||
Debug::log("Inline result: $found");
|
Debug::log("Inline result: $found", Debug::$LOG_VERBOSE);
|
||||||
|
|
||||||
if (!$found) {
|
if (!$found) {
|
||||||
Debug::log("Readability result:");
|
Debug::log("Readability result:", Debug::$LOG_VERBOSE);
|
||||||
|
|
||||||
$article = $this->readability([], $url, $doc, $xpath);
|
$article = $this->readability([], $url, $doc, $xpath);
|
||||||
|
|
||||||
print_r($article);
|
print_r($article);
|
||||||
} else {
|
} else {
|
||||||
Debug::log("Resulting HTML:");
|
Debug::log("Resulting HTML:", Debug::$LOG_VERBOSE);
|
||||||
|
|
||||||
print $doc->saveHTML();
|
print $doc->saveHTML();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue