]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_redditimgur/init.php
2 class Af_RedditImgur
extends Plugin
{
7 "Inline images (and other content) in Reddit RSS feeds",
11 function init($host) {
14 $host->add_hook($host::HOOK_ARTICLE_FILTER
, $this);
15 $host->add_hook($host::HOOK_PREFS_TAB
, $this);
18 function hook_prefs_tab($args) {
19 if ($args != "prefFeeds") return;
21 print "<div id=\"af_redditimgur_prefs\" dojoType=\"dijit.layout.AccordionPane\" title=\"".__('af_redditimgur settings')."\">";
23 $enable_readability = $this->host
->get($this, "enable_readability");
24 $enable_readability_checked = $enable_readability ?
"checked" : "";
26 print "<form dojoType=\"dijit.form.Form\">";
28 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
30 if (this.validate()) {
31 console.log(dojo.objectToQuery(this.getValues()));
32 new Ajax.Request('backend.php', {
33 parameters: dojo.objectToQuery(this.getValues()),
34 onComplete: function(transport) {
35 notify_info(transport.responseText);
42 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">";
43 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"save\">";
44 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"af_redditimgur\">";
46 print "<h3>" . __("Global settings") . "</h3>";
48 print_notice("Uses Readability (full-text-rss) implementation by <a target='_blank' href='https://bitbucket.org/fivefilters/'>FiveFilters.org</a>");
51 print "<input dojoType=\"dijit.form.CheckBox\" id=\"enable_readability\"
52 $enable_readability_checked name=\"enable_readability\"> ";
54 print "<label for=\"enable_readability\">" . __("Extract missing content using Readability") . "</label>";
56 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
57 __("Save")."</button>";
65 $enable_readability = checkbox_to_sql_bool($_POST["enable_readability"]) == "true";
67 $this->host
->set($this, "enable_readability", $enable_readability);
69 echo __("Configuration saved");
72 private function inline_stuff($article, &$doc, $xpath) {
74 $entries = $xpath->query('(//a[@href]|//img[@src])');
78 foreach ($entries as $entry) {
79 if ($entry->hasAttribute("href")) {
83 if (preg_match("/https?:\/\/gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
85 $tmp = fetch_file_contents($entry->getAttribute("href"));
88 $tmpdoc = new DOMDocument();
89 @$tmpdoc->loadHTML($tmp);
92 $tmpxpath = new DOMXPath($tmpdoc);
93 $source_meta = $tmpxpath->query("//meta[@property='og:video']")->item(0);
96 $source_stream = $source_meta->getAttribute("content");
99 $this->handle_as_video($doc, $entry, $source_stream);
108 if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
110 $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href"));
111 $this->handle_as_video($doc, $entry, $source_stream);
117 if (preg_match("/\.youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) ||
118 preg_match("/\.youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
119 preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) {
121 $vid_id = $matches[1];
123 $iframe = $doc->createElement("iframe");
124 $iframe->setAttribute("class", "youtube-player");
125 $iframe->setAttribute("type", "text/html");
126 $iframe->setAttribute("width", "640");
127 $iframe->setAttribute("height", "385");
128 $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id");
129 $iframe->setAttribute("allowfullscreen", "1");
130 $iframe->setAttribute("frameborder", "0");
132 $br = $doc->createElement('br');
133 $entry->parentNode
->insertBefore($iframe, $entry);
134 $entry->parentNode
->insertBefore($br, $entry);
139 if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href"))) {
140 $img = $doc->createElement('img');
141 $img->setAttribute("src", $entry->getAttribute("href"));
143 $br = $doc->createElement('br');
144 $entry->parentNode
->insertBefore($img, $entry);
145 $entry->parentNode
->insertBefore($br, $entry);
150 // links to imgur pages
152 if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
154 $token = $matches[2];
156 $album_content = fetch_file_contents($entry->getAttribute("href"),
157 false, false, false, false, 10);
159 if ($album_content && $token) {
160 $adoc = new DOMDocument();
161 @$adoc->loadHTML($album_content);
164 $axpath = new DOMXPath($adoc);
165 $aentries = $axpath->query('(//img[@src])');
167 foreach ($aentries as $aentry) {
168 if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
169 $img = $doc->createElement('img');
170 $img->setAttribute("src", $aentry->getAttribute("src"));
172 $br = $doc->createElement('br');
174 $entry->parentNode
->insertBefore($img, $entry);
175 $entry->parentNode
->insertBefore($br, $entry);
186 // linked albums, ffs
187 if (preg_match("/^https?:\/\/imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
189 $album_content = fetch_file_contents($entry->getAttribute("href"),
190 false, false, false, false, 10);
192 if ($album_content) {
193 $adoc = new DOMDocument();
194 @$adoc->loadHTML($album_content);
197 $axpath = new DOMXPath($adoc);
198 $aentries = $axpath->query("//meta[@property='og:image']");
201 foreach ($aentries as $aentry) {
203 if (!in_array($aentry->getAttribute("content"), $urls)) {
204 $img = $doc->createElement('img');
205 $img->setAttribute("src", $aentry->getAttribute("content"));
206 $entry->parentNode
->insertBefore($doc->createElement('br'), $entry);
208 $br = $doc->createElement('br');
210 $entry->parentNode
->insertBefore($img, $entry);
211 $entry->parentNode
->insertBefore($br, $entry);
213 array_push($urls, $aentry->getAttribute("content"));
223 // remove tiny thumbnails
224 if ($entry->hasAttribute("src")) {
225 if ($entry->parentNode
&& $entry->parentNode
->parentNode
) {
226 $entry->parentNode
->parentNode
->removeChild($entry->parentNode
);
234 function hook_article_filter($article) {
236 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
237 $doc = new DOMDocument();
238 @$doc->loadHTML($article["content"]);
239 $xpath = new DOMXPath($doc);
241 $found = $this->inline_stuff($article, $doc, $xpath);
243 if (!$found && $this->host
->get($this, "enable_readability") && mb_strlen(strip_tags($article["content"])) <= 150) {
244 if (!class_exists("Readability")) require_once(__DIR__
. "/classes/Readability.php");
246 $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
248 if ($content_link && strpos($content_link->getAttribute("href"), "reddit.com") === FALSE) {
250 $tmp = fetch_file_contents($content_link->getAttribute("href"));
253 $r = new Readability($tmp, $content_link->getAttribute("href"));
256 //$article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"];
258 $tmpxpath = new DOMXPath($r->dom
);
260 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
262 foreach ($entries as $entry) {
263 if ($entry->hasAttribute("href")) {
264 $entry->setAttribute("href",
265 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("href")));
269 if ($entry->hasAttribute("src")) {
270 $entry->setAttribute("src",
271 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("src")));
277 $article["content"] = $r->articleContent
->innerHTML
. "<hr/>" . $article["content"];
279 $doc = new DOMDocument();
280 @$doc->loadHTML($article["content"]);
281 $xpath = new DOMXPath($doc);
283 $found = $this->inline_stuff($article, $doc, $xpath);
291 $node = $doc->getElementsByTagName('body')->item(0);
293 if ($node && $found) {
294 $article["content"] = $doc->saveXML($node);
301 function api_version() {
305 private function handle_as_video($doc, $entry, $source_stream) {
307 $video = $doc->createElement('video');
308 $video->setAttribute("autoplay", "1");
309 $video->setAttribute("controls", "1");
310 $video->setAttribute("loop", "1");
312 $source = $doc->createElement('source');
313 $source->setAttribute("src", $source_stream);
314 $source->setAttribute("type", "video/mp4");
316 $video->appendChild($source);
318 $br = $doc->createElement('br');
319 $entry->parentNode
->insertBefore($video, $entry);
320 $entry->parentNode
->insertBefore($br, $entry);
322 $img = $doc->createElement('img');
323 $img->setAttribute("src",
324 "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
326 $entry->parentNode
->insertBefore($img, $entry);