GBAtempBridge.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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-02';
  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. 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. 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. function build_item($uri, $title, $author, $timestamp, $thumnail, $content) {
  53. $item = new \Item();
  54. $item->uri = $uri;
  55. $item->title = $title;
  56. $item->author = $author;
  57. $item->timestamp = $timestamp;
  58. $item->thumbnailUri = $thumnail;
  59. $item->content = $content;
  60. return $item;
  61. }
  62. function cleanup_post_content($content, $site_url) {
  63. $content = str_replace(':arrow:', '&#x27a4;', $content);
  64. $content = str_replace('href="attachments/', 'href="'.$site_url.'attachments/', $content);
  65. $content = $this->StripWithDelimiters($content, '<script', '</script>');
  66. return $content;
  67. }
  68. function fetch_post_content($uri, $site_url) {
  69. $html = $this->file_get_html($uri) or $this->returnError('Could not request GBAtemp: '.$uri, 500);
  70. $content = $html->find('div.messageContent', 0)->innertext;
  71. return $this->cleanup_post_content($content, $site_url);
  72. }
  73. public function collectData(array $param) {
  74. $typeFilter = '';
  75. if (!empty($param['type'])) {
  76. if ($param['type'] == 'N' || $param['type'] == 'R' || $param['type'] == 'T' || $param['type'] == 'F') {
  77. $typeFilter = $param['type'];
  78. if ($typeFilter == 'N') { $this->filter = 'News'; }
  79. if ($typeFilter == 'R') { $this->filter = 'Review'; }
  80. if ($typeFilter == 'T') { $this->filter = 'Tutorial'; }
  81. if ($typeFilter == 'F') { $this->filter = 'Forum'; }
  82. } else $this->returnError('The provided type filter is invalid. Expecting N, R, T, or F.', 400);
  83. } else $this->returnError('Please provide a type filter. Expecting N, R, T, or F.', 400);
  84. $html = $this->file_get_html($this->getURI()) or $this->returnError('Could not request GBAtemp.', 500);
  85. if ($typeFilter == 'N') {
  86. foreach ($html->find('li[class=news_item full]') as $newsItem) {
  87. $url = $this->getURI().$newsItem->find('a', 0)->href;
  88. $img = $this->getURI().$newsItem->find('img', 0)->src;
  89. $time = intval($this->ExtractFromDelimiters($newsItem->find('abbr.DateTime', 0)->outertext, 'data-time="', '"'));
  90. $author = $newsItem->find('a.username', 0)->plaintext;
  91. $title = $newsItem->find('a', 1)->plaintext;
  92. $content = $this->fetch_post_content($url, $this->getURI());
  93. $this->items[] = $this->build_item($url, $title, $author, $time, $img, $content);
  94. }
  95. } else if ($typeFilter == 'R') {
  96. foreach ($html->find('li.portal_review') as $reviewItem) {
  97. $url = $this->getURI().$reviewItem->find('a', 0)->href;
  98. $img = $this->getURI().$this->ExtractFromDelimiters($reviewItem->find('a', 0)->style, 'image:url(', ')');
  99. $title = $reviewItem->find('span.review_title', 0)->plaintext;
  100. $content = $this->file_get_html($url) or $this->returnError('Could not request GBAtemp: '.$uri, 500);
  101. $author = $content->find('a.username', 0)->plaintext;
  102. $time = intval($this->ExtractFromDelimiters($content->find('abbr.DateTime', 0)->outertext, 'data-time="', '"'));
  103. $intro = '<p><b>'.($content->find('div#review_intro', 0)->plaintext).'</b></p>';
  104. $review = $content->find('div#review_main', 0)->innertext;
  105. $subheader = '<p><b>'.$content->find('div.review_subheader', 0)->plaintext.'</b></p>';
  106. $procons = $content->find('table.review_procons', 0)->outertext;
  107. $scores = $content->find('table.reviewscores', 0)->outertext;
  108. $content = $this->cleanup_post_content($intro.$review.$subheader.$procons.$scores, $this->getURI());
  109. $this->items[] = $this->build_item($url, $title, $author, $time, $img, $content);
  110. }
  111. } else if ($typeFilter == 'T') {
  112. foreach ($html->find('li.portal-tutorial') as $tutorialItem) {
  113. $url = $this->getURI().$tutorialItem->find('a', 0)->href;
  114. $title = $tutorialItem->find('a', 0)->plaintext;
  115. $time = intval($this->ExtractFromDelimiters($tutorialItem->find('abbr.DateTime', 0)->outertext, 'data-time="', '"'));
  116. $author = $tutorialItem->find('a.username', 0)->plaintext;
  117. $content = $this->fetch_post_content($url, $this->getURI());
  118. $this->items[] = $this->build_item($url, $title, $author, $time, '', $content);
  119. }
  120. } else if ($typeFilter == 'F') {
  121. foreach ($html->find('li.rc_item') as $postItem) {
  122. $url = $this->getURI().$postItem->find('a', 1)->href;
  123. $title = $postItem->find('a', 1)->plaintext;
  124. $time = intval($this->ExtractFromDelimiters($postItem->find('abbr.DateTime', 0)->outertext, 'data-time="', '"'));
  125. $author = $postItem->find('a.username', 0)->plaintext;
  126. $content = $this->fetch_post_content($url, $this->getURI());
  127. $this->items[] = $this->build_item($url, $title, $author, $time, '', $content);
  128. }
  129. }
  130. }
  131. public function getName() {
  132. return 'GBAtemp'.(empty($this->filter) ? '' : ' '.$this->filter).' Bridge';
  133. }
  134. public function getURI() {
  135. return 'http://gbatemp.net/';
  136. }
  137. public function getCacheDuration() {
  138. return ($this->filter === 'Forum') ? 300 : 3600; // 5 minutes / 1 hour
  139. }
  140. }