AnimeUltimeBridge.php 4.1 KB

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