]> git.wh0rd.org Git - tt-rss.git/blob - plugins/af_redditimgur/init.php
af_redditimgur: add <video> controls
[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                                                         if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href"))) {
88                                                                 $img = $doc->createElement('img');
89                                                                 $img->setAttribute("src", $entry->getAttribute("href"));
90
91                                                                 $br = $doc->createElement('br');
92                                                                 $entry->parentNode->insertBefore($img, $entry);
93                                                                 $entry->parentNode->insertBefore($br, $entry);
94
95                                                                 $found = true;
96                                                         }
97
98                                                         // links to imgur pages
99                                                         $matches = array();
100                                                         if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
101
102                                                                 $token = $matches[2];
103
104                                                                 $album_content = fetch_file_contents($entry->getAttribute("href"),
105                                                                         false, false, false, false, 10);
106
107                                                                 if ($album_content && $token) {
108                                                                         $adoc = new DOMDocument();
109                                                                         @$adoc->loadHTML($album_content);
110
111                                                                         if ($adoc) {
112                                                                                 $axpath = new DOMXPath($adoc);
113                                                                                 $aentries = $axpath->query('(//img[@src])');
114
115                                                                                 foreach ($aentries as $aentry) {
116                                                                                         if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
117                                                                                                 $img = $doc->createElement('img');
118                                                                                                 $img->setAttribute("src", $aentry->getAttribute("src"));
119
120                                                                                                 $br = $doc->createElement('br');
121
122                                                                                                 $entry->parentNode->insertBefore($img, $entry);
123                                                                                                 $entry->parentNode->insertBefore($br, $entry);
124
125                                                                                                 $found = true;
126
127                                                                                                 break;
128                                                                                         }
129                                                                                 }
130                                                                         }
131                                                                 }
132                                                         }
133
134                                                         // linked albums, ffs
135                                                         if (preg_match("/^https?:\/\/imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
136
137                                                                 $album_content = fetch_file_contents($entry->getAttribute("href"),
138                                                                         false, false, false, false, 10);
139
140                                                                 if ($album_content) {
141                                                                         $adoc = new DOMDocument();
142                                                                         @$adoc->loadHTML($album_content);
143
144                                                                         if ($adoc) {
145                                                                                 $axpath = new DOMXPath($adoc);
146                                                                                 $aentries = $axpath->query("//meta[@property='og:image']");
147                                                                                 $urls = array();
148
149                                                                                 foreach ($aentries as $aentry) {
150
151                                                                                         if (!in_array($aentry->getAttribute("content"), $urls)) {
152                                                                                                 $img = $doc->createElement('img');
153                                                                                                 $img->setAttribute("src", $aentry->getAttribute("content"));
154                                                                                                 $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
155
156                                                                                                 $br = $doc->createElement('br');
157
158                                                                                                 $entry->parentNode->insertBefore($img, $entry);
159                                                                                                 $entry->parentNode->insertBefore($br, $entry);
160
161                                                                                                 array_push($urls, $aentry->getAttribute("content"));
162
163                                                                                                 $found = true;
164                                                                                         }
165                                                                                 }
166                                                                         }
167                                                                 }
168                                                         }
169                                                 }
170
171                                                 // remove tiny thumbnails
172                                                 if ($entry->hasAttribute("src")) {
173                                                         if ($entry->parentNode && $entry->parentNode->parentNode) {
174                                                                 $entry->parentNode->parentNode->removeChild($entry->parentNode);
175                                                         }
176                                                 }
177                                         }
178
179                                         $node = $doc->getElementsByTagName('body')->item(0);
180
181                                         if ($node && $found) {
182                                                 $article["content"] = $doc->saveXML($node);
183                                         }
184                                 }
185                 }
186
187                 return $article;
188         }
189
190         function api_version() {
191                 return 2;
192         }
193
194         private function handle_as_video($doc, $entry, $source_stream) {
195
196                 $video = $doc->createElement('video');
197                 $video->setAttribute("autoplay", "1");
198                 $video->setAttribute("controls", "1");
199                 $video->setAttribute("loop", "1");
200
201                 $source = $doc->createElement('source');
202                 $source->setAttribute("src", $source_stream);
203                 $source->setAttribute("type", "video/mp4");
204
205                 $video->appendChild($source);
206
207                 $br = $doc->createElement('br');
208                 $entry->parentNode->insertBefore($video, $entry);
209                 $entry->parentNode->insertBefore($br, $entry);
210
211                 $img = $doc->createElement('img');
212                 $img->setAttribute("src",
213                         "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
214
215                 $entry->parentNode->insertBefore($img, $entry);
216         }
217 }
218 ?>