AnimeUltimeBridge.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. .substr($requestFilter, 0, 2).'-'
  44. .substr($requestFilter, 2, 4));
  45. $release = $daySection->next_sibling()->next_sibling()->first_child(); //<h3>day</h3><br /><table><tr> <-- useful data in table rows
  46. //Process each release of that day, ignoring first table row: contains table headers
  47. while (!is_null($release = $release->next_sibling())) {
  48. if (count($release->find('td')) > 0) {
  49. //Retrieve metadata from table columns
  50. $item_link_element = $release->find('td', 0)->find('a', 0);
  51. $item_uri = self::URI.$item_link_element->href;
  52. $item_name = html_entity_decode($item_link_element->plaintext);
  53. $item_episode = html_entity_decode(str_pad($release->find('td', 1)->plaintext, 2, '0', STR_PAD_LEFT));
  54. $item_fansub = $release->find('td', 2)->plaintext;
  55. $item_type = $release->find('td', 4)->plaintext;
  56. if (!empty($item_uri)) {
  57. //Retrieve description from description page and convert relative image src info absolute image src
  58. $html_item = getContents($item_uri)
  59. or returnServerError('Could not request Anime-Ultime: '.$item_uri);
  60. $item_description = substr(
  61. $html_item,
  62. strpos($html_item, 'class="principal_contain" align="center">')
  63. + 41
  64. );
  65. $item_description = substr($item_description,
  66. 0,
  67. strpos($item_description, '<div id="table">')
  68. );
  69. $item_description = str_replace(
  70. 'src="images', 'src="'.self::URI.'images',
  71. $item_description
  72. );
  73. $item_description = str_replace("\r", '', $item_description);
  74. $item_description = str_replace("\n", '', $item_description);
  75. $item_description = utf8_encode($item_description);
  76. //Build and add final item
  77. $item = array();
  78. $item['uri'] = $item_uri;
  79. $item['title'] = $item_name.' '.$item_type.' '.$item_episode;
  80. $item['author'] = $item_fansub;
  81. $item['timestamp'] = $item_date;
  82. $item['content'] = $item_description;
  83. $this->items[] = $item;
  84. $processedOK++;
  85. //Stop processing once limit is reached
  86. if ($processedOK >= 10)
  87. return;
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
  94. public function getName() {
  95. $typeFilter = array_search(
  96. $this->getInput('type'),
  97. self::PARAMETERS[$this->queriedContext]['type']['values']
  98. );
  99. return 'Latest '.$typeFilter.' - Anime-Ultime Bridge';
  100. }
  101. }