]> git.wh0rd.org - tt-rss.git/blob - plugins/af_comics/init.php
Update af_comics to handle new GoComics site.
[tt-rss.git] / plugins / af_comics / init.php
1 <?php
2 class Af_Comics extends Plugin {
3
4 private $host;
5 private $filters = array();
6
7 function about() {
8 return array(2.0,
9 "Fixes RSS feeds of assorted comic strips",
10 "fox");
11 }
12
13 function init($host) {
14 $this->host = $host;
15
16 $host->add_hook($host::HOOK_FETCH_FEED, $this);
17 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
18 $host->add_hook($host::HOOK_PREFS_TAB, $this);
19
20 require_once __DIR__ . "/filter_base.php";
21
22 $filters = glob(__DIR__ . "/filters/*.php");
23
24 foreach ($filters as $file) {
25 require_once $file;
26 $filter_name = preg_replace("/\..*$/", "", basename($file));
27
28 $filter = new $filter_name();
29
30 if (is_subclass_of($filter, "Af_ComicFilter")) {
31 array_push($this->filters, $filter);
32 }
33 }
34
35 }
36
37 function hook_prefs_tab($args) {
38 if ($args != "prefPrefs") return;
39
40 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Feeds supported by af_comics')."\">";
41
42 print "<p>" . __("The following comics are currently supported:") . "</p>";
43
44 $comics = array("GoComics");
45
46 foreach ($this->filters as $f) {
47 foreach ($f->supported() as $comic) {
48 array_push($comics, $comic);
49 }
50 }
51
52 asort($comics);
53
54 print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
55 foreach ($comics as $comic) {
56 print "<li>$comic</li>";
57 }
58 print "</ul>";
59
60 print "</div>";
61 }
62
63 function hook_article_filter($article) {
64 $owner_uid = $article["owner_uid"];
65
66 foreach ($this->filters as $f) {
67 if ($f->process($article))
68 break;
69 }
70
71 return $article;
72
73 }
74
75 // GoComics dropped feed support so it needs to be handled when fetching the feed.
76 function hook_fetch_feed($feed_data, $fetch_url, $owner_uid, $feed, $last_article_timestamp, $auth_login, $auth_pass) {
77 if ($auth_login || $auth_pass)
78 return $feed_data;
79
80 if (preg_match('#^https?://feeds.feedburner.com/uclick/([a-z]+)#', $fetch_url, $comic)) {
81 $site_url = 'http://www.gocomics.com/' . $comic[1];
82
83 $article_link = $site_url . date('/Y/m/d');
84
85 $body = fetch_file_contents(array('url' => $article_link, 'type' => 'text/html', 'followlocation' => false));
86
87 require_once 'lib/MiniTemplator.class.php';
88
89 $feed_title = htmlspecialchars($comic[1]);
90 $site_url = htmlspecialchars($site_url);
91 $article_link = htmlspecialchars($article_link);
92
93 $tpl = new MiniTemplator();
94
95 $tpl->readTemplateFromFile('templates/generated_feed.txt');
96
97 $tpl->setVariable('FEED_TITLE', $feed_title, true);
98 $tpl->setVariable('VERSION', VERSION, true);
99 $tpl->setVariable('FEED_URL', htmlspecialchars($fetch_url), true);
100 $tpl->setVariable('SELF_URL', $site_url, true);
101
102 $tpl->setVariable('ARTICLE_UPDATED_ATOM', date('c'), true);
103 $tpl->setVariable('ARTICLE_UPDATED_RFC822', date(DATE_RFC822), true);
104
105 if ($body) {
106 $doc = new DOMDocument();
107
108 if (@$doc->loadHTML($body)) {
109 $xpath = new DOMXPath($doc);
110
111 $node = $xpath->query('//picture[contains(@class, "item-comic-image")]/img')->item(0);
112
113 if ($node) {
114 $tpl->setVariable('ARTICLE_ID', $article_link, true);
115 $tpl->setVariable('ARTICLE_LINK', $article_link, true);
116 $tpl->setVariable('ARTICLE_TITLE', date('l, F d, Y'), true);
117 $tpl->setVariable('ARTICLE_EXCERPT', '', true);
118 $tpl->setVariable('ARTICLE_CONTENT', $doc->saveXML($node), true);
119
120 $tpl->setVariable('ARTICLE_AUTHOR', '', true);
121 $tpl->setVariable('ARTICLE_SOURCE_LINK', $site_url, true);
122 $tpl->setVariable('ARTICLE_SOURCE_TITLE', $feed_title, true);
123
124 $tpl->addBlock('entry');
125 }
126 }
127 }
128
129 $tpl->addBlock('feed');
130
131 $tmp_data = '';
132
133 if ($tpl->generateOutputToString($tmp_data))
134 $feed_data = $tmp_data;
135 }
136
137 return $feed_data;
138 }
139
140 function api_version() {
141 return 2;
142 }
143
144 }
145 ?>