]> git.wh0rd.org - tt-rss.git/blame - plugins/af_comics/filters/af_comics_dilbert.php
pngcrush.sh
[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) {
1d619947 10 if (strpos($article["link"], "dilbert.com") !== FALSE ||
ea8d0fd5 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();
901dd67a 23
1d619947 24 if (@$doc->loadHTML($res)) {
901dd67a 25 $xpath = new DOMXPath($doc);
9fd58133 26
ea8d0fd5 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 }
9fd58133 50
901dd67a 51 if ($basenode) {
f3774b9d 52 $article["content"] = $doc->saveHTML($basenode);
901dd67a 53 }
ea8d0fd5 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
901dd67a 65 }
901dd67a
AD
66
67 return true;
68 }
69
70 return false;
71 }
ea8d0fd5 72}
73?>