ShanaprojectBridge.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. class ShanaprojectBridge extends BridgeAbstract {
  3. const MAINTAINER = 'logmanoriginal';
  4. const NAME = 'Shanaproject Bridge';
  5. const URI = 'http://www.shanaproject.com';
  6. const DESCRIPTION = 'Returns a list of anime from the current Season Anime List';
  7. // Returns an html object for the Season Anime List (latest season)
  8. private function loadSeasonAnimeList(){
  9. // First we need to find the URI to the latest season from the
  10. // 'seasons' page searching for 'Season Anime List'
  11. $html = getSimpleHTMLDOM($this->getURI() . '/seasons');
  12. if(!$html)
  13. returnServerError('Could not load \'seasons\' page!');
  14. $season = $html->find('div.follows_menu/a', 1);
  15. if(!$season)
  16. returnServerError('Could not find \'Season Anime List\'!');
  17. $html = getSimpleHTMLDOM($this->getURI() . $season->href);
  18. if(!$html)
  19. returnServerError(
  20. 'Could not load \'Season Anime List\' from \''
  21. . $season->innertext
  22. . '\'!'
  23. );
  24. return $html;
  25. }
  26. // Extracts the anime title
  27. private function extractAnimeTitle($anime){
  28. $title = $anime->find('a', 0);
  29. if(!$title)
  30. returnServerError('Could not find anime title!');
  31. return trim($title->innertext);
  32. }
  33. // Extracts the anime URI
  34. private function extractAnimeUri($anime){
  35. $uri = $anime->find('a', 0);
  36. if(!$uri)
  37. returnServerError('Could not find anime URI!');
  38. return $this->getURI() . $uri->href;
  39. }
  40. // Extracts the anime release date (timestamp)
  41. private function extractAnimeTimestamp($anime){
  42. $timestamp = $anime->find('span.header_info_block', 1);
  43. if(!$timestamp)
  44. return null;
  45. return strtotime($timestamp->innertext);
  46. }
  47. // Extracts the anime studio name (author)
  48. private function extractAnimeAuthor($anime){
  49. $author = $anime->find('span.header_info_block', 2);
  50. if(!$author)
  51. return; // Sometimes the studio is unknown, so leave empty
  52. return trim($author->innertext);
  53. }
  54. // Extracts the episode information (x of y released)
  55. private function extractAnimeEpisodeInformation($anime){
  56. $episode = $anime->find('div.header_info_episode', 0);
  57. if(!$episode)
  58. returnServerError('Could not find anime episode information!');
  59. return preg_replace('/\r|\n/', ' ', $episode->plaintext);
  60. }
  61. // Extracts the background image
  62. private function extractAnimeBackgroundImage($anime){
  63. // Getting the picture is a little bit tricky as it is part of the style.
  64. // Luckily the style is part of the parent div :)
  65. if(preg_match('/url\(\/\/([^\)]+)\)/i', $anime->parent->style, $matches))
  66. return $matches[1];
  67. returnServerError('Could not extract background image!');
  68. }
  69. // Builds an URI to search for a specific anime (subber is left empty)
  70. private function buildAnimeSearchUri($anime){
  71. return $this->getURI()
  72. . '/search/?title='
  73. . urlencode($this->extractAnimeTitle($anime))
  74. . '&subber=';
  75. }
  76. // Builds the content string for a given anime
  77. private function buildAnimeContent($anime){
  78. // We'll use a template string to place our contents
  79. return '<a href="'
  80. . $this->extractAnimeUri($anime)
  81. . '"><img src="http://'
  82. . $this->extractAnimeBackgroundImage($anime)
  83. . '" alt="'
  84. . htmlspecialchars($this->extractAnimeTitle($anime))
  85. . '" style="border: 1px solid black"></a><br><p>'
  86. . $this->extractAnimeEpisodeInformation($anime)
  87. . '</p><br><p><a href="'
  88. . $this->buildAnimeSearchUri($anime)
  89. . '">Search episodes</a></p>';
  90. }
  91. public function collectData(){
  92. $html = $this->loadSeasonAnimeList();
  93. $animes = $html->find('div.header_display_box_info');
  94. if(!$animes)
  95. returnServerError('Could not find anime headers!');
  96. foreach($animes as $anime) {
  97. $item = array();
  98. $item['title'] = $this->extractAnimeTitle($anime);
  99. $item['author'] = $this->extractAnimeAuthor($anime);
  100. $item['uri'] = $this->extractAnimeUri($anime);
  101. $item['timestamp'] = $this->extractAnimeTimestamp($anime);
  102. $item['content'] = $this->buildAnimeContent($anime);
  103. $this->items[] = $item;
  104. }
  105. }
  106. }