GBAtempBridge.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. class GBAtempBridge extends BridgeAbstract {
  3. private $filter = '';
  4. public function loadMetadatas() {
  5. $this->maintainer = 'ORelio';
  6. $this->name = 'GBAtemp';
  7. $this->uri = $this->getURI();
  8. $this->description = 'GBAtemp is a user friendly underground video game community.';
  9. $this->update = '2016-08-09';
  10. $this->parameters[] =
  11. '[
  12. {
  13. "name" : "Type",
  14. "type" : "list",
  15. "identifier" : "type",
  16. "values" :
  17. [
  18. {
  19. "name" : "News",
  20. "value" : "N"
  21. },
  22. {
  23. "name" : "Reviews",
  24. "value" : "R"
  25. },
  26. {
  27. "name" : "Tutorials",
  28. "value" : "T"
  29. },
  30. {
  31. "name" : "Forum",
  32. "value" : "F"
  33. }
  34. ]
  35. }
  36. ]';
  37. }
  38. private function ExtractFromDelimiters($string, $start, $end) {
  39. if (strpos($string, $start) !== false) {
  40. $section_retrieved = substr($string, strpos($string, $start) + strlen($start));
  41. $section_retrieved = substr($section_retrieved, 0, strpos($section_retrieved, $end));
  42. return $section_retrieved;
  43. } return false;
  44. }
  45. private function StripWithDelimiters($string, $start, $end) {
  46. while (strpos($string, $start) !== false) {
  47. $section_to_remove = substr($string, strpos($string, $start));
  48. $section_to_remove = substr($section_to_remove, 0, strpos($section_to_remove, $end) + strlen($end));
  49. $string = str_replace($section_to_remove, '', $string);
  50. } return $string;
  51. }
  52. private function build_item($uri, $title, $author, $timestamp, $content) {
  53. $item = new \Item();
  54. $item->uri = $uri;
  55. $item->title = $title;
  56. $item->author = $author;
  57. $item->timestamp = $timestamp;
  58. $item->content = $content;
  59. return $item;
  60. }
  61. private function cleanup_post_content($content, $site_url) {
  62. $content = str_replace(':arrow:', '&#x27a4;', $content);
  63. $content = str_replace('href="attachments/', 'href="'.$site_url.'attachments/', $content);
  64. $content = $this->StripWithDelimiters($content, '<script', '</script>');
  65. return $content;
  66. }
  67. private function fetch_post_content($uri, $site_url) {
  68. $html = $this->file_get_html($uri) or $this->returnError('Could not request GBAtemp: '.$uri, 500);
  69. $content = $html->find('div.messageContent', 0)->innertext;
  70. return $this->cleanup_post_content($content, $site_url);
  71. }
  72. public function collectData(array $param) {
  73. $typeFilter = '';
  74. if (!empty($param['type'])) {
  75. if ($param['type'] == 'N' || $param['type'] == 'R' || $param['type'] == 'T' || $param['type'] == 'F') {
  76. $typeFilter = $param['type'];
  77. if ($typeFilter == 'N') { $this->filter = 'News'; }
  78. if ($typeFilter == 'R') { $this->filter = 'Review'; }
  79. if ($typeFilter == 'T') { $this->filter = 'Tutorial'; }
  80. if ($typeFilter == 'F') { $this->filter = 'Forum'; }
  81. } else $this->returnError('The provided type filter is invalid. Expecting N, R, T, or F.', 400);
  82. } else $this->returnError('Please provide a type filter. Expecting N, R, T, or F.', 400);
  83. $html = $this->file_get_html($this->getURI()) or $this->returnError('Could not request GBAtemp.', 500);
  84. if ($typeFilter == 'N') {
  85. foreach ($html->find('li[class=news_item full]') as $newsItem) {
  86. $url = $this->getURI().$newsItem->find('a', 0)->href;
  87. $time = intval($this->ExtractFromDelimiters($newsItem->find('abbr.DateTime', 0)->outertext, 'data-time="', '"'));
  88. $author = $newsItem->find('a.username', 0)->plaintext;
  89. $title = $newsItem->find('a', 1)->plaintext;
  90. $content = $this->fetch_post_content($url, $this->getURI());
  91. $this->items[] = $this->build_item($url, $title, $author, $time, $content);
  92. }
  93. } else if ($typeFilter == 'R') {
  94. foreach ($html->find('li.portal_review') as $reviewItem) {
  95. $url = $this->getURI().$reviewItem->find('a', 0)->href;
  96. $title = $reviewItem->find('span.review_title', 0)->plaintext;
  97. $content = $this->file_get_html($url) or $this->returnError('Could not request GBAtemp: '.$uri, 500);
  98. $author = $content->find('a.username', 0)->plaintext;
  99. $time = intval($this->ExtractFromDelimiters($content->find('abbr.DateTime', 0)->outertext, 'data-time="', '"'));
  100. $intro = '<p><b>'.($content->find('div#review_intro', 0)->plaintext).'</b></p>';
  101. $review = $content->find('div#review_main', 0)->innertext;
  102. $subheader = '<p><b>'.$content->find('div.review_subheader', 0)->plaintext.'</b></p>';
  103. $procons = $content->find('table.review_procons', 0)->outertext;
  104. $scores = $content->find('table.reviewscores', 0)->outertext;
  105. $content = $this->cleanup_post_content($intro.$review.$subheader.$procons.$scores, $this->getURI());
  106. $this->items[] = $this->build_item($url, $title, $author, $time, $content);
  107. }
  108. } else if ($typeFilter == 'T') {
  109. foreach ($html->find('li.portal-tutorial') as $tutorialItem) {
  110. $url = $this->getURI().$tutorialItem->find('a', 0)->href;
  111. $title = $tutorialItem->find('a', 0)->plaintext;
  112. $time = intval($this->ExtractFromDelimiters($tutorialItem->find('abbr.DateTime', 0)->outertext, 'data-time="', '"'));
  113. $author = $tutorialItem->find('a.username', 0)->plaintext;
  114. $content = $this->fetch_post_content($url, $this->getURI());
  115. $this->items[] = $this->build_item($url, $title, $author, $time, $content);
  116. }
  117. } else if ($typeFilter == 'F') {
  118. foreach ($html->find('li.rc_item') as $postItem) {
  119. $url = $this->getURI().$postItem->find('a', 1)->href;
  120. $title = $postItem->find('a', 1)->plaintext;
  121. $time = intval($this->ExtractFromDelimiters($postItem->find('abbr.DateTime', 0)->outertext, 'data-time="', '"'));
  122. $author = $postItem->find('a.username', 0)->plaintext;
  123. $content = $this->fetch_post_content($url, $this->getURI());
  124. $this->items[] = $this->build_item($url, $title, $author, $time, $content);
  125. }
  126. }
  127. }
  128. public function getName() {
  129. return 'GBAtemp'.(empty($this->filter) ? '' : ' '.$this->filter).' Bridge';
  130. }
  131. public function getURI() {
  132. return 'http://gbatemp.net/';
  133. }
  134. public function getCacheDuration() {
  135. return ($this->filter === 'Forum') ? 300 : 3600; // 5 minutes / 1 hour
  136. }
  137. }