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