]> git.wh0rd.org - tt-rss.git/blob - plugins/af_comics/filters/af_comics_dilbert.php
565b44657c9c096d75889ccdda6a7ba4ed9e4ec0
[tt-rss.git] / plugins / af_comics / filters / af_comics_dilbert.php
1 <?php
2
3 class Af_Comics_Dilbert extends Af_ComicFilter {
4
5 function supported() {
6 return array("Dilbert");
7 }
8
9 function process(&$article) {
10 if (strpos($article["link"], "dilbert.com") !== FALSE ||
11 strpos($article["link"], "/DilbertDailyStrip") !== FALSE) {
12
13 $res = fetch_file_contents($article["link"], false, false, false,
14 false, false, 0,
15 "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0");
16
17 global $fetch_last_error_content;
18
19 if (!$res && $fetch_last_error_content)
20 $res = $fetch_last_error_content;
21
22 $doc = new DOMDocument();
23
24 if (@$doc->loadHTML($res)) {
25 $xpath = new DOMXPath($doc);
26
27 // Get the image container
28 $basenode = $xpath->query('(//div[@class="img-comic-container"]/a[@class="img-comic-link"])')->item(0);
29
30 // Get the comic title
31 $comic_title = $xpath->query('(//span[@class="comic-title-name"])')->item(0)->textContent;
32
33 // Get tags from the article
34 $matches = $xpath->query('(//p[contains(@class, "comic-tags")][1]//a)');
35 $tags = array();
36
37 foreach ($matches as $tag) {
38 // Only strings starting with a number sign are considered tags
39 if ( substr($tag->textContent, 0, 1) == '#' ) {
40 $tags[] = mb_strtolower(substr($tag->textContent, 1), 'utf-8');
41 }
42 }
43
44 // Get the current comics transcript and set it
45 // as the title so it will be visible on mousover
46 $transcript = $xpath->query('(//div[starts-with(@id, "js-toggle-transcript-")]//p)')->item(0);
47 if ($transcript) {
48 $basenode->setAttribute("title", $transcript->textContent);
49 }
50
51 if ($basenode) {
52 $article["content"] = $doc->saveXML($basenode);
53 }
54
55 // Add comic title to article type if not empty (mostly Sunday strips)
56 if ($comic_title) {
57 $article["title"] = $article["title"] . " - " . $comic_title;
58 }
59
60 if (!empty($tags)) {
61 // Ignore existing tags and just replace them all
62 $article["tags"] = array_unique($tags);
63 }
64
65 }
66
67 return true;
68 }
69
70 return false;
71 }
72 }
73 ?>