GBAtempBridge.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. class GBAtempBridge extends BridgeAbstract {
  3. const MAINTAINER = 'ORelio';
  4. const NAME = 'GBAtemp';
  5. const URI = 'https://gbatemp.net/';
  6. const DESCRIPTION = 'GBAtemp is a user friendly underground video game community.';
  7. const PARAMETERS = array( array(
  8. 'type' => array(
  9. 'name' => 'Type',
  10. 'type' => 'list',
  11. 'required' => true,
  12. 'values' => array(
  13. 'News' => 'N',
  14. 'Reviews' => 'R',
  15. 'Tutorials' => 'T',
  16. 'Forum' => 'F'
  17. )
  18. )
  19. ));
  20. private function extractFromDelimiters($string, $start, $end){
  21. if(strpos($string, $start) !== false) {
  22. $section_retrieved = substr($string, strpos($string, $start) + strlen($start));
  23. $section_retrieved = substr($section_retrieved, 0, strpos($section_retrieved, $end));
  24. return $section_retrieved;
  25. }
  26. return false;
  27. }
  28. private function stripWithDelimiters($string, $start, $end){
  29. while(strpos($string, $start) !== false) {
  30. $section_to_remove = substr($string, strpos($string, $start));
  31. $section_to_remove = substr($section_to_remove, 0, strpos($section_to_remove, $end) + strlen($end));
  32. $string = str_replace($section_to_remove, '', $string);
  33. }
  34. return $string;
  35. }
  36. private function buildItem($uri, $title, $author, $timestamp, $content){
  37. $item = array();
  38. $item['uri'] = $uri;
  39. $item['title'] = $title;
  40. $item['author'] = $author;
  41. $item['timestamp'] = $timestamp;
  42. $item['content'] = $content;
  43. return $item;
  44. }
  45. private function cleanupPostContent($content, $site_url){
  46. $content = str_replace(':arrow:', '&#x27a4;', $content);
  47. $content = str_replace('href="attachments/', 'href="'.$site_url.'attachments/', $content);
  48. $content = $this->stripWithDelimiters($content, '<script', '</script>');
  49. return $content;
  50. }
  51. private function fetchPostContent($uri, $site_url){
  52. $html = getSimpleHTMLDOM($uri);
  53. if(!$html) {
  54. return 'Could not request GBAtemp ' . $uri;
  55. }
  56. $content = $html->find('div.messageContent', 0)->innertext;
  57. return $this->cleanupPostContent($content, $site_url);
  58. }
  59. public function collectData(){
  60. $html = getSimpleHTMLDOM(self::URI)
  61. or returnServerError('Could not request GBAtemp.');
  62. switch($this->getInput('type')) {
  63. case 'N':
  64. foreach($html->find('li[class=news_item full]') as $newsItem) {
  65. $url = self::URI . $newsItem->find('a', 0)->href;
  66. $time = intval(
  67. $this->extractFromDelimiters(
  68. $newsItem->find('abbr.DateTime', 0)->outertext,
  69. 'data-time="',
  70. '"'
  71. )
  72. );
  73. $author = $newsItem->find('a.username', 0)->plaintext;
  74. $title = $newsItem->find('a', 1)->plaintext;
  75. $content = $this->fetchPostContent($url, self::URI);
  76. $this->items[] = $this->buildItem($url, $title, $author, $time, $content);
  77. }
  78. case 'R':
  79. foreach($html->find('li.portal_review') as $reviewItem) {
  80. $url = self::URI . $reviewItem->find('a', 0)->href;
  81. $title = $reviewItem->find('span.review_title', 0)->plaintext;
  82. $content = getSimpleHTMLDOM($url)
  83. or returnServerError('Could not request GBAtemp: ' . $uri);
  84. $author = $content->find('a.username', 0)->plaintext;
  85. $time = intval(
  86. $this->extractFromDelimiters(
  87. $content->find('abbr.DateTime', 0)->outertext,
  88. 'data-time="',
  89. '"'
  90. )
  91. );
  92. $intro = '<p><b>' . ($content->find('div#review_intro', 0)->plaintext) . '</b></p>';
  93. $review = $content->find('div#review_main', 0)->innertext;
  94. $subheader = '<p><b>' . $content->find('div.review_subheader', 0)->plaintext . '</b></p>';
  95. $procons = $content->find('table.review_procons', 0)->outertext;
  96. $scores = $content->find('table.reviewscores', 0)->outertext;
  97. $content = $this->cleanupPostContent($intro . $review . $subheader . $procons . $scores, self::URI);
  98. $this->items[] = $this->buildItem($url, $title, $author, $time, $content);
  99. }
  100. case 'T':
  101. foreach($html->find('li.portal-tutorial') as $tutorialItem) {
  102. $url = self::URI . $tutorialItem->find('a', 0)->href;
  103. $title = $tutorialItem->find('a', 0)->plaintext;
  104. $time = intval(
  105. $this->extractFromDelimiters(
  106. $tutorialItem->find('abbr.DateTime', 0)->outertext,
  107. 'data-time="',
  108. '"'
  109. )
  110. );
  111. $author = $tutorialItem->find('a.username', 0)->plaintext;
  112. $content = $this->fetchPostContent($url, self::URI);
  113. $this->items[] = $this->buildItem($url, $title, $author, $time, $content);
  114. }
  115. case 'F':
  116. foreach($html->find('li.rc_item') as $postItem) {
  117. $url = self::URI . $postItem->find('a', 1)->href;
  118. $title = $postItem->find('a', 1)->plaintext;
  119. $time = intval(
  120. $this->extractFromDelimiters(
  121. $postItem->find('abbr.DateTime', 0)->outertext,
  122. 'data-time="',
  123. '"'
  124. )
  125. );
  126. $author = $postItem->find('a.username', 0)->plaintext;
  127. $content = $this->fetchPostContent($url, self::URI);
  128. $this->items[] = $this->buildItem($url, $title, $author, $time, $content);
  129. }
  130. }
  131. }
  132. public function getName() {
  133. if(!is_null($this->getInput('type'))) {
  134. $type = array_search(
  135. $this->getInput('type'),
  136. self::PARAMETERS[$this->queriedContext]['type']['values']
  137. );
  138. return 'GBAtemp ' . $type . ' Bridge';
  139. }
  140. return parent::getName();
  141. }
  142. }