af_comics_dilbert.php 2.0 KB

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