AnimeUltimeBridge.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. class AnimeUltimeBridge extends BridgeAbstract {
  3. private $filter = 'Releases';
  4. public function loadMetadatas() {
  5. $this->maintainer = 'ORelio';
  6. $this->name = 'Anime-Ultime';
  7. $this->uri = 'http://www.anime-ultime.net/';
  8. $this->description = 'Returns the 10 newest releases posted on Anime-Ultime';
  9. $this->update = '2015-10-30';
  10. $this->parameters[] =
  11. '[
  12. {
  13. "name" : "Type",
  14. "type" : "list",
  15. "identifier" : "type",
  16. "values" :
  17. [
  18. {
  19. "name" : "Everything",
  20. "value" : ""
  21. },
  22. {
  23. "name" : "Anime",
  24. "value" : "A"
  25. },
  26. {
  27. "name" : "Drama",
  28. "value" : "D"
  29. },
  30. {
  31. "name" : "Tokusatsu",
  32. "value" : "T"
  33. }
  34. ]
  35. }
  36. ]';
  37. }
  38. public function collectData(array $param) {
  39. //Add type filter if provided
  40. $typeFilter = '';
  41. if (!empty($param['type'])) {
  42. if ($param['type'] == 'A' || $param['type'] == 'D' || $param['type'] == 'T') {
  43. $typeFilter = $param['type'];
  44. if ($typeFilter == 'A') { $this->filter = 'Anime'; }
  45. if ($typeFilter == 'D') { $this->filter = 'Drama'; }
  46. if ($typeFilter == 'T') { $this->filter = 'Tokusatsu'; }
  47. } else $this->returnError('The provided type filter is invalid. Expecting A, D, T, or no filter', 400);
  48. }
  49. //Build date and filters for making requests
  50. $thismonth = date('mY').$typeFilter;
  51. $lastmonth = date('mY', mktime(0, 0, 0, date('n') - 1, 1, date('Y'))).$typeFilter;
  52. //Process each HTML page until having 10 releases
  53. $processedOK = 0;
  54. foreach (array($thismonth, $lastmonth) as $requestFilter) {
  55. //Retrive page contents
  56. $website = 'http://www.anime-ultime.net/';
  57. $url = $website.'history-0-1/'.$requestFilter;
  58. $html = $this->file_get_html($url) or $this->returnError('Could not request Anime-Ultime: '.$url, 500);
  59. //Relases are sorted by day : process each day individually
  60. foreach ($html->find('div.history', 0)->find('h3') as $daySection) {
  61. //Retrieve day and build date information
  62. $dateString = $daySection->plaintext;
  63. $day = intval(substr($dateString, strpos($dateString, ' ') + 1, 2));
  64. $item_date = strtotime(str_pad($day, 2, '0', STR_PAD_LEFT).'-'.substr($requestFilter, 0, 2).'-'.substr($requestFilter, 2, 4));
  65. $release = $daySection->next_sibling()->next_sibling()->first_child(); //<h3>day</h3><br /><table><tr> <-- useful data in table rows
  66. //Process each release of that day, ignoring first table row: contains table headers
  67. while (!is_null($release = $release->next_sibling())) {
  68. if (count($release->find('td')) > 0) {
  69. //Retrieve metadata from table columns
  70. $item_link_element = $release->find('td', 0)->find('a', 0);
  71. $item_uri = $website.$item_link_element->href;
  72. $item_name = html_entity_decode($item_link_element->plaintext);
  73. $item_image = $website.substr($item_link_element->onmouseover, 37, strpos($item_link_element->onmouseover, ' ', 37) - 37);
  74. $item_episode = html_entity_decode(str_pad($release->find('td', 1)->plaintext, 2, '0', STR_PAD_LEFT));
  75. $item_fansub = $release->find('td', 2)->plaintext;
  76. $item_type = $release->find('td', 4)->plaintext;
  77. if (!empty($item_uri)) {
  78. //Retrieve description from description page and convert relative image src info absolute image src
  79. $html_item = file_get_contents($item_uri) or $this->returnError('Could not request Anime-Ultime: '.$item_uri, 500);
  80. $item_description = substr($html_item, strpos($html_item, 'class="principal_contain" align="center">') + 41);
  81. $item_description = substr($item_description, 0, strpos($item_description, '<div id="table">'));
  82. $item_description = str_replace('src="images', 'src="'.$website.'images', $item_description);
  83. $item_description = str_replace("\r", '', $item_description);
  84. $item_description = str_replace("\n", '', $item_description);
  85. $item_description = utf8_encode($item_description);
  86. //Build and add final item
  87. $item = new \Item();
  88. $item->uri = $item_uri;
  89. $item->title = $item_name.' '.$item_type.' '.$item_episode;
  90. $item->author = $item_fansub;
  91. $item->timestamp = $item_date;
  92. $item->thumbnailUri = $item_image;
  93. $item->content = $item_description;
  94. $this->items[] = $item;
  95. $processedOK++;
  96. //Stop processing once limit is reached
  97. if ($processedOK >= 10)
  98. return;
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. public function getName() {
  106. return 'Latest '.$this->filter.' - Anime-Ultime Bridge';
  107. }
  108. public function getURI() {
  109. return 'http://www.anime-ultime.net/';
  110. }
  111. public function getCacheDuration() {
  112. return 3600*3; // 3 hours
  113. }
  114. }