]> git.wh0rd.org - tt-rss.git/blob - plugins/af_redditimgur/init.php
af_redditimgur: embed youtube links
[tt-rss.git] / plugins / af_redditimgur / init.php
1 <?php
2 class Af_RedditImgur extends Plugin {
3 private $host;
4
5 function about() {
6 return array(1.0,
7 "Inline image links in Reddit RSS feeds",
8 "fox");
9 }
10
11 function init($host) {
12 $this->host = $host;
13
14 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
15 }
16
17 function hook_article_filter($article) {
18
19 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
20 $doc = new DOMDocument();
21 @$doc->loadHTML($article["content"]);
22
23 if ($doc) {
24 $xpath = new DOMXPath($doc);
25 $entries = $xpath->query('(//a[@href]|//img[@src])');
26
27 $found = false;
28
29 foreach ($entries as $entry) {
30 if ($entry->hasAttribute("href")) {
31
32 $matches = array();
33
34 if (preg_match("/https?:\/\/gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
35
36 $tmp = fetch_file_contents($entry->getAttribute("href"));
37
38 if ($tmp) {
39 $tmpdoc = new DOMDocument();
40 @$tmpdoc->loadHTML($tmp);
41
42 if ($tmpdoc) {
43 $tmpxpath = new DOMXPath($tmpdoc);
44 $source_meta = $tmpxpath->query("//meta[@property='og:video']")->item(0);
45
46 if ($source_meta) {
47 $source_stream = $source_meta->getAttribute("content");
48
49 if ($source_stream) {
50 $this->handle_as_video($doc, $entry, $source_stream);
51 $found = 1;
52 }
53 }
54 }
55 }
56
57 }
58
59 if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
60
61 /*$video = $doc->createElement('video');
62 $video->setAttribute("autoplay", "1");
63 $video->setAttribute("loop", "1");
64
65 $source = $doc->createElement('source');
66 $source->setAttribute("src", str_replace(".gifv", ".mp4", $entry->getAttribute("href")));
67 $source->setAttribute("type", "video/mp4");
68
69 $video->appendChild($source);
70
71 $br = $doc->createElement('br');
72 $entry->parentNode->insertBefore($video, $entry);
73 $entry->parentNode->insertBefore($br, $entry);
74
75 $img = $doc->createElement('img');
76 $img->setAttribute("src",
77 "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
78
79 $entry->parentNode->insertBefore($img, $entry);*/
80
81 $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href"));
82 $this->handle_as_video($doc, $entry, $source_stream);
83
84 $found = true;
85 }
86
87 $matches = array();
88 if (preg_match("/\/\/www\.youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) ||
89 preg_match("/\/\/www\.youtube\.com\/watch?v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
90 preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) {
91
92 $vid_id = $matches[1];
93
94 $iframe = $doc->createElement("iframe");
95 $iframe->setAttribute("class", "youtube-player");
96 $iframe->setAttribute("type", "text/html");
97 $iframe->setAttribute("width", "640");
98 $iframe->setAttribute("height", "385");
99 $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id");
100 $iframe->setAttribute("allowfullscreen", "1");
101 $iframe->setAttribute("frameborder", "0");
102
103 $br = $doc->createElement('br');
104 $entry->parentNode->insertBefore($iframe, $entry);
105 $entry->parentNode->insertBefore($br, $entry);
106
107 $found = true;
108 }
109
110 if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href"))) {
111 $img = $doc->createElement('img');
112 $img->setAttribute("src", $entry->getAttribute("href"));
113
114 $br = $doc->createElement('br');
115 $entry->parentNode->insertBefore($img, $entry);
116 $entry->parentNode->insertBefore($br, $entry);
117
118 $found = true;
119 }
120
121 // links to imgur pages
122 $matches = array();
123 if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
124
125 $token = $matches[2];
126
127 $album_content = fetch_file_contents($entry->getAttribute("href"),
128 false, false, false, false, 10);
129
130 if ($album_content && $token) {
131 $adoc = new DOMDocument();
132 @$adoc->loadHTML($album_content);
133
134 if ($adoc) {
135 $axpath = new DOMXPath($adoc);
136 $aentries = $axpath->query('(//img[@src])');
137
138 foreach ($aentries as $aentry) {
139 if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
140 $img = $doc->createElement('img');
141 $img->setAttribute("src", $aentry->getAttribute("src"));
142
143 $br = $doc->createElement('br');
144
145 $entry->parentNode->insertBefore($img, $entry);
146 $entry->parentNode->insertBefore($br, $entry);
147
148 $found = true;
149
150 break;
151 }
152 }
153 }
154 }
155 }
156
157 // linked albums, ffs
158 if (preg_match("/^https?:\/\/imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
159
160 $album_content = fetch_file_contents($entry->getAttribute("href"),
161 false, false, false, false, 10);
162
163 if ($album_content) {
164 $adoc = new DOMDocument();
165 @$adoc->loadHTML($album_content);
166
167 if ($adoc) {
168 $axpath = new DOMXPath($adoc);
169 $aentries = $axpath->query("//meta[@property='og:image']");
170 $urls = array();
171
172 foreach ($aentries as $aentry) {
173
174 if (!in_array($aentry->getAttribute("content"), $urls)) {
175 $img = $doc->createElement('img');
176 $img->setAttribute("src", $aentry->getAttribute("content"));
177 $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
178
179 $br = $doc->createElement('br');
180
181 $entry->parentNode->insertBefore($img, $entry);
182 $entry->parentNode->insertBefore($br, $entry);
183
184 array_push($urls, $aentry->getAttribute("content"));
185
186 $found = true;
187 }
188 }
189 }
190 }
191 }
192 }
193
194 // remove tiny thumbnails
195 if ($entry->hasAttribute("src")) {
196 if ($entry->parentNode && $entry->parentNode->parentNode) {
197 $entry->parentNode->parentNode->removeChild($entry->parentNode);
198 }
199 }
200 }
201
202 $node = $doc->getElementsByTagName('body')->item(0);
203
204 if ($node && $found) {
205 $article["content"] = $doc->saveXML($node);
206 }
207 }
208 }
209
210 return $article;
211 }
212
213 function api_version() {
214 return 2;
215 }
216
217 private function handle_as_video($doc, $entry, $source_stream) {
218
219 $video = $doc->createElement('video');
220 $video->setAttribute("autoplay", "1");
221 $video->setAttribute("controls", "1");
222 $video->setAttribute("loop", "1");
223
224 $source = $doc->createElement('source');
225 $source->setAttribute("src", $source_stream);
226 $source->setAttribute("type", "video/mp4");
227
228 $video->appendChild($source);
229
230 $br = $doc->createElement('br');
231 $entry->parentNode->insertBefore($video, $entry);
232 $entry->parentNode->insertBefore($br, $entry);
233
234 $img = $doc->createElement('img');
235 $img->setAttribute("src",
236 "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
237
238 $entry->parentNode->insertBefore($img, $entry);
239 }
240 }
241 ?>