ShanaprojectBridge.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 'seasons' page searching for 'Season Anime List'
  10. $html = getSimpleHTMLDOM($this->getURI() . '/seasons');
  11. if(!$html)
  12. returnServerError('Could not load \'seasons\' page!');
  13. $season = $html->find('div.follows_menu/a', 1);
  14. if(!$season)
  15. returnServerError('Could not find \'Season Anime List\'!');
  16. $html = getSimpleHTMLDOM($this->getURI() . $season->href);
  17. if(!$html)
  18. returnServerError('Could not load \'Season Anime List\' from \'' . $season->innertext . '\'!');
  19. return $html;
  20. }
  21. // Extracts the anime title
  22. private function ExtractAnimeTitle($anime){
  23. $title = $anime->find('a', 0);
  24. if(!$title)
  25. returnServerError('Could not find anime title!');
  26. return trim($title->innertext);
  27. }
  28. // Extracts the anime URI
  29. private function ExtractAnimeURI($anime){
  30. $uri = $anime->find('a', 0);
  31. if(!$uri)
  32. returnServerError('Could not find anime URI!');
  33. return $this->getURI() . $uri->href;
  34. }
  35. // Extracts the anime release date (timestamp)
  36. private function ExtractAnimeTimestamp($anime){
  37. $timestamp = $anime->find('span.header_info_block', 1);
  38. if(!$timestamp)
  39. returnServerError('Could not find anime timestamp!');
  40. return strtotime($timestamp->innertext);
  41. }
  42. // Extracts the anime studio name (author)
  43. private function ExtractAnimeAuthor($anime){
  44. $author = $anime->find('span.header_info_block', 2);
  45. if(!$author)
  46. return; // Sometimes the studio is unknown, so leave empty
  47. return trim($author->innertext);
  48. }
  49. // Extracts the episode information (x of y released)
  50. private function ExtractAnimeEpisodeInformation($anime){
  51. $episode = $anime->find('div.header_info_episode', 0);
  52. if(!$episode)
  53. returnServerError('Could not find anime episode information!');
  54. return preg_replace('/\r|\n/', ' ', $episode->plaintext);
  55. }
  56. // Extracts the background image
  57. private function ExtractAnimeBackgroundImage($anime){
  58. // Getting the picture is a little bit tricky as it is part of the style.
  59. // Luckily the style is part of the parent div :)
  60. if(preg_match("/url\(\/\/([^\)]+)\)/i", $anime->parent->style, $matches))
  61. return $matches[1];
  62. returnServerError('Could not extract background image!');
  63. }
  64. // Builds an URI to search for a specific anime (subber is left empty)
  65. private function BuildAnimeSearchURI($anime){
  66. return $this->getURI() . '/search/?title=' . urlencode($this->ExtractAnimeTitle($anime)) . '&subber=';
  67. }
  68. // Builds the content string for a given anime
  69. private function BuildAnimeContent($anime){
  70. // We'll use a template string to place our contents
  71. return '<a href="' . $this->ExtractAnimeURI($anime) . '">
  72. <img src="http://' . $this->ExtractAnimeBackgroundImage($anime) . '" alt="' . htmlspecialchars($this->ExtractAnimeTitle($anime)) . '" style="border: 1px solid black">
  73. </a><br>
  74. <p>' . $this->ExtractAnimeEpisodeInformation($anime) . '</p><br>
  75. <p><a href="' . $this->BuildAnimeSearchURI($anime) . '">Search episodes</a></p>';
  76. }
  77. public function collectData(){
  78. $html = $this->LoadSeasonAnimeList();
  79. $animes = $html->find('div.header_display_box_info');
  80. if(!$animes)
  81. returnServerError('Could not find anime headers!');
  82. foreach($animes as $anime){
  83. $item = array();
  84. $item['title'] = $this->ExtractAnimeTitle($anime);
  85. $item['author'] = $this->ExtractAnimeAuthor($anime);
  86. $item['uri'] = $this->ExtractAnimeURI($anime);
  87. $item['timestamp'] = $this->ExtractAnimeTimestamp($anime);
  88. $item['content'] = $this->BuildAnimeContent($anime);
  89. $this->items[] = $item;
  90. }
  91. }
  92. }