]> git.wh0rd.org - tt-rss.git/blame - plugins/af_redditimgur/init.php
update_rss_feed: set basic feed info if title is [Unknown] (fixes batch subscribe)
[tt-rss.git] / plugins / af_redditimgur / init.php
CommitLineData
cc85704f 1<?php
0862a602 2class Af_RedditImgur extends Plugin {
19c73507
AD
3 private $host;
4
d2a421e3 5 function about() {
7a866114
AD
6 return array(1.0,
7 "Inline image links in Reddit RSS feeds",
8 "fox");
9 }
10
d2a421e3 11 function init($host) {
19c73507
AD
12 $this->host = $host;
13
14 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
15 }
16
17 function hook_article_filter($article) {
0b5ef30d 18
cc85704f 19 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
a16d7a5d
AD
20 $doc = new DOMDocument();
21 @$doc->loadHTML($article["content"]);
cc85704f 22
a16d7a5d
AD
23 if ($doc) {
24 $xpath = new DOMXPath($doc);
25 $entries = $xpath->query('(//a[@href]|//img[@src])');
cc85704f 26
ce7d5e87
AD
27 $found = false;
28
a16d7a5d
AD
29 foreach ($entries as $entry) {
30 if ($entry->hasAttribute("href")) {
299aeb30 31
9875d717 32 if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
33
1ae46c50
AD
34 $video = $doc->createElement('video');
35 $video->setAttribute("autoplay", "1");
36 $video->setAttribute("loop", "1");
299aeb30 37
1ae46c50
AD
38 $source = $doc->createElement('source');
39 $source->setAttribute("src", str_replace(".gifv", ".mp4", $entry->getAttribute("href")));
40 $source->setAttribute("type", "video/mp4");
299aeb30 41
1ae46c50
AD
42 $video->appendChild($source);
43
44 $br = $doc->createElement('br');
45 $entry->parentNode->insertBefore($video, $entry);
46 $entry->parentNode->insertBefore($br, $entry);
47
98e20510
AD
48 $img = $doc->createElement('img');
49 $img->setAttribute("src",
50 "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
51
52 $entry->parentNode->insertBefore($img, $entry);
53
1ae46c50 54 $found = true;
299aeb30 55 }
9875d717 56
7adf9556 57 if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9])?$/i", $entry->getAttribute("href"))) {
cc85704f 58
a16d7a5d
AD
59 $img = $doc->createElement('img');
60 $img->setAttribute("src", $entry->getAttribute("href"));
cc85704f 61
7ff4d1fa
AD
62 $br = $doc->createElement('br');
63 $entry->parentNode->insertBefore($img, $entry);
64 $entry->parentNode->insertBefore($br, $entry);
ce7d5e87
AD
65
66 $found = true;
67 }
68
69 // links to imgur pages
70 $matches = array();
248c5a6a 71 if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
ce7d5e87 72
1af9f54f 73 $token = $matches[2];
ce7d5e87
AD
74
75 $album_content = fetch_file_contents($entry->getAttribute("href"),
76 false, false, false, false, 10);
77
78 if ($album_content && $token) {
79 $adoc = new DOMDocument();
80 @$adoc->loadHTML($album_content);
81
82 if ($adoc) {
83 $axpath = new DOMXPath($adoc);
84 $aentries = $axpath->query('(//img[@src])');
85
86 foreach ($aentries as $aentry) {
e44ea762 87 if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
ce7d5e87
AD
88 $img = $doc->createElement('img');
89 $img->setAttribute("src", $aentry->getAttribute("src"));
7ff4d1fa
AD
90
91 $br = $doc->createElement('br');
92
ce7d5e87 93 $entry->parentNode->insertBefore($img, $entry);
7ff4d1fa
AD
94 $entry->parentNode->insertBefore($br, $entry);
95
ce7d5e87 96 $found = true;
35055d05
AD
97
98 break;
ce7d5e87
AD
99 }
100 }
101 }
102 }
a16d7a5d 103 }
35055d05
AD
104
105 // linked albums, ffs
47cdc58c 106 if (preg_match("/^https?:\/\/imgur.com\/(a|album)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
35055d05
AD
107
108 $album_content = fetch_file_contents($entry->getAttribute("href"),
109 false, false, false, false, 10);
110
111 if ($album_content) {
112 $adoc = new DOMDocument();
113 @$adoc->loadHTML($album_content);
114
115 if ($adoc) {
116 $axpath = new DOMXPath($adoc);
47cdc58c 117 $aentries = $axpath->query("//meta[@property='og:image']");
98e20510 118 $urls = array();
35055d05
AD
119
120 foreach ($aentries as $aentry) {
7ff4d1fa 121
98e20510
AD
122 if (!in_array($aentry->getAttribute("content"), $urls)) {
123 $img = $doc->createElement('img');
124 $img->setAttribute("src", $aentry->getAttribute("content"));
125 $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
7ff4d1fa 126
98e20510 127 $br = $doc->createElement('br');
7ff4d1fa 128
98e20510
AD
129 $entry->parentNode->insertBefore($img, $entry);
130 $entry->parentNode->insertBefore($br, $entry);
131
132 array_push($urls, $aentry->getAttribute("content"));
133
134 $found = true;
135 }
35055d05
AD
136 }
137 }
138 }
139 }
cc85704f
AD
140 }
141
a16d7a5d
AD
142 // remove tiny thumbnails
143 if ($entry->hasAttribute("src")) {
144 if ($entry->parentNode && $entry->parentNode->parentNode) {
145 $entry->parentNode->parentNode->removeChild($entry->parentNode);
146 }
cc85704f
AD
147 }
148 }
149
a16d7a5d 150 $node = $doc->getElementsByTagName('body')->item(0);
cc85704f 151
ce7d5e87 152 if ($node && $found) {
cc38c8e5 153 $article["content"] = $doc->saveXML($node);
a16d7a5d 154 }
cc85704f 155 }
cc85704f
AD
156 }
157
158 return $article;
159 }
106a3de9
AD
160
161 function api_version() {
162 return 2;
163 }
164
cc85704f
AD
165}
166?>