AnimeUltimeBridge.php 5.2 KB

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