AnimeUltimeBridge.php 5.2 KB

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