GBAtempBridge.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. class GBAtempBridge extends BridgeAbstract {
  3. const MAINTAINER = 'ORelio';
  4. const NAME = 'GBAtemp';
  5. const URI = 'http://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. } return false;
  26. }
  27. private function StripWithDelimiters($string, $start, $end) {
  28. while (strpos($string, $start) !== false) {
  29. $section_to_remove = substr($string, strpos($string, $start));
  30. $section_to_remove = substr($section_to_remove, 0, strpos($section_to_remove, $end) + strlen($end));
  31. $string = str_replace($section_to_remove, '', $string);
  32. } return $string;
  33. }
  34. private function build_item($uri, $title, $author, $timestamp, $content) {
  35. $item = array();
  36. $item['uri'] = $uri;
  37. $item['title'] = $title;
  38. $item['author'] = $author;
  39. $item['timestamp'] = $timestamp;
  40. $item['content'] = $content;
  41. return $item;
  42. }
  43. private function cleanup_post_content($content, $site_url) {
  44. $content = str_replace(':arrow:', '&#x27a4;', $content);
  45. $content = str_replace('href="attachments/', 'href="'.$site_url.'attachments/', $content);
  46. $content = $this->StripWithDelimiters($content, '<script', '</script>');
  47. return $content;
  48. }
  49. private function fetch_post_content($uri, $site_url) {
  50. $html = getSimpleHTMLDOM($uri);
  51. if(!$html){
  52. return 'Could not request GBAtemp '.$uri;
  53. }
  54. $content = $html->find('div.messageContent', 0)->innertext;
  55. return $this->cleanup_post_content($content, $site_url);
  56. }
  57. public function collectData(){
  58. $html = getSimpleHTMLDOM(self::URI)
  59. or returnServerError('Could not request GBAtemp.');
  60. switch($this->getInput('type')){
  61. case 'N':
  62. foreach ($html->find('li[class=news_item full]') as $newsItem) {
  63. $url = self::URI.$newsItem->find('a', 0)->href;
  64. $time = intval($this->ExtractFromDelimiters($newsItem->find('abbr.DateTime', 0)->outertext, 'data-time="', '"'));
  65. $author = $newsItem->find('a.username', 0)->plaintext;
  66. $title = $newsItem->find('a', 1)->plaintext;
  67. $content = $this->fetch_post_content($url, self::URI);
  68. $this->items[] = $this->build_item($url, $title, $author, $time, $content);
  69. }
  70. case 'R':
  71. foreach ($html->find('li.portal_review') as $reviewItem) {
  72. $url = self::URI.$reviewItem->find('a', 0)->href;
  73. $title = $reviewItem->find('span.review_title', 0)->plaintext;
  74. $content = getSimpleHTMLDOM($url) or returnServerError('Could not request GBAtemp: '.$uri);
  75. $author = $content->find('a.username', 0)->plaintext;
  76. $time = intval($this->ExtractFromDelimiters($content->find('abbr.DateTime', 0)->outertext, 'data-time="', '"'));
  77. $intro = '<p><b>'.($content->find('div#review_intro', 0)->plaintext).'</b></p>';
  78. $review = $content->find('div#review_main', 0)->innertext;
  79. $subheader = '<p><b>'.$content->find('div.review_subheader', 0)->plaintext.'</b></p>';
  80. $procons = $content->find('table.review_procons', 0)->outertext;
  81. $scores = $content->find('table.reviewscores', 0)->outertext;
  82. $content = $this->cleanup_post_content($intro.$review.$subheader.$procons.$scores, self::URI);
  83. $this->items[] = $this->build_item($url, $title, $author, $time, $content);
  84. }
  85. case 'T':
  86. foreach ($html->find('li.portal-tutorial') as $tutorialItem) {
  87. $url = self::URI.$tutorialItem->find('a', 0)->href;
  88. $title = $tutorialItem->find('a', 0)->plaintext;
  89. $time = intval($this->ExtractFromDelimiters($tutorialItem->find('abbr.DateTime', 0)->outertext, 'data-time="', '"'));
  90. $author = $tutorialItem->find('a.username', 0)->plaintext;
  91. $content = $this->fetch_post_content($url, self::URI);
  92. $this->items[] = $this->build_item($url, $title, $author, $time, $content);
  93. }
  94. case 'F':
  95. foreach ($html->find('li.rc_item') as $postItem) {
  96. $url = self::URI.$postItem->find('a', 1)->href;
  97. $title = $postItem->find('a', 1)->plaintext;
  98. $time = intval($this->ExtractFromDelimiters($postItem->find('abbr.DateTime', 0)->outertext, 'data-time="', '"'));
  99. $author = $postItem->find('a.username', 0)->plaintext;
  100. $content = $this->fetch_post_content($url, self::URI);
  101. $this->items[] = $this->build_item($url, $title, $author, $time, $content);
  102. }
  103. }
  104. }
  105. public function getName() {
  106. $type=array_search(
  107. $this->getInput('type'),
  108. self::PARAMETERS[$this->queriedContext]['type']['values']
  109. );
  110. return 'GBAtemp '.$type.' Bridge';
  111. }
  112. public function getCacheDuration() {
  113. return ($this->filter === 'Forum') ? 300 : 3600; // 5 minutes / 1 hour
  114. }
  115. }