]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_redditimgur/init.php
2 class Af_RedditImgur
extends Plugin
{
7 "Inline image links in Reddit RSS feeds",
11 function init($host) {
14 $host->add_hook($host::HOOK_ARTICLE_FILTER
, $this);
17 function hook_article_filter($article) {
19 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
20 $doc = new DOMDocument();
21 @$doc->loadHTML($article["content"]);
24 $xpath = new DOMXPath($doc);
25 $entries = $xpath->query('(//a[@href]|//img[@src])');
29 foreach ($entries as $entry) {
30 if ($entry->hasAttribute("href")) {
32 if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
34 $video = $doc->createElement('video');
35 $video->setAttribute("autoplay", "1");
36 $video->setAttribute("loop", "1");
38 $source = $doc->createElement('source');
39 $source->setAttribute("src", str_replace(".gifv", ".mp4", $entry->getAttribute("href")));
40 $source->setAttribute("type", "video/mp4");
42 $video->appendChild($source);
44 $br = $doc->createElement('br');
45 $entry->parentNode
->insertBefore($video, $entry);
46 $entry->parentNode
->insertBefore($br, $entry);
51 if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9])?$/i", $entry->getAttribute("href"))) {
53 $img = $doc->createElement('img');
54 $img->setAttribute("src", $entry->getAttribute("href"));
56 $br = $doc->createElement('br');
57 $entry->parentNode
->insertBefore($img, $entry);
58 $entry->parentNode
->insertBefore($br, $entry);
63 // links to imgur pages
65 if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
69 $album_content = fetch_file_contents($entry->getAttribute("href"),
70 false, false, false, false, 10);
72 if ($album_content && $token) {
73 $adoc = new DOMDocument();
74 @$adoc->loadHTML($album_content);
77 $axpath = new DOMXPath($adoc);
78 $aentries = $axpath->query('(//img[@src])');
80 foreach ($aentries as $aentry) {
81 if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
82 $img = $doc->createElement('img');
83 $img->setAttribute("src", $aentry->getAttribute("src"));
85 $br = $doc->createElement('br');
87 $entry->parentNode
->insertBefore($img, $entry);
88 $entry->parentNode
->insertBefore($br, $entry);
100 if (preg_match("/^https?:\/\/imgur.com\/(a|album)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
102 $album_content = fetch_file_contents($entry->getAttribute("href"),
103 false, false, false, false, 10);
105 if ($album_content) {
106 $adoc = new DOMDocument();
107 @$adoc->loadHTML($album_content);
110 $axpath = new DOMXPath($adoc);
111 $aentries = $axpath->query("//meta[@property='og:image']");
113 foreach ($aentries as $aentry) {
114 $img = $doc->createElement('img');
115 $img->setAttribute("src", $aentry->getAttribute("content"));
116 $entry->parentNode
->insertBefore($doc->createElement('br'), $entry);
118 $br = $doc->createElement('br');
120 $entry->parentNode
->insertBefore($img, $entry);
121 $entry->parentNode
->insertBefore($br, $entry);
130 // remove tiny thumbnails
131 if ($entry->hasAttribute("src")) {
132 if ($entry->parentNode
&& $entry->parentNode
->parentNode
) {
133 $entry->parentNode
->parentNode
->removeChild($entry->parentNode
);
138 $node = $doc->getElementsByTagName('body')->item(0);
140 if ($node && $found) {
141 $article["content"] = $doc->saveXML($node);
149 function api_version() {