FourchanBridge.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * RssBridge4chan
  4. * @name 4chan
  5. * @homepage https://www.4chan.org/
  6. * @description Returns posts from the specified thread
  7. * @maintainer mitsukarenai
  8. * @update 2015-02-01
  9. * @use1(t="Thread URL")
  10. */
  11. class FourchanBridge extends BridgeAbstract{
  12. public function collectData(array $param){
  13. if (!isset($param['t']))
  14. $this->returnError('You must specify the thread URL (?t=...)', 400);
  15. $thread = parse_url($param['t']) or $this->returnError('This URL seems malformed, please check it.', 400);
  16. if($thread['host'] !== 'boards.4chan.org')
  17. $this->returnError('4chan thread URL only.', 400);
  18. if(strpos($thread['path'], 'thread/') === FALSE)
  19. $this->returnError('You must specify the thread URL.', 400);
  20. $url = 'https://boards.4chan.org'.$thread['path'].'';
  21. $html = file_get_html($url) or $this->returnError("Could not request 4chan, thread not found", 404);
  22. foreach($html->find('div.postContainer') as $element) {
  23. $item = new \Item();
  24. $item->id = $element->find('.post', 0)->getAttribute('id');
  25. $item->uri = $url.'#'.$item->id;
  26. $item->timestamp = $element->find('span.dateTime', 0)->getAttribute('data-utc');
  27. $item->author = $element->find('span.name', 0)->plaintext;
  28. if(!empty($element->find('.file', 0) ) ) {
  29. $item->image = $element->find('.file a', 0)->href;
  30. $item->imageThumb = $element->find('.file img', 0)->src;
  31. if(empty($item->imageThumb) and strpos($item->image, '.swf') !== FALSE)
  32. $item->imageThumb = 'http://i.imgur.com/eO0cxf9.jpg';
  33. }
  34. if(!empty($element->find('span.subject', 0)->innertext )) {
  35. $item->subject = $element->find('span.subject', 0)->innertext;
  36. }
  37. $item->title = (!empty($item->subject) ? $item->subject.' - ' : '' ) . 'reply '.$item->id.' | '.$item->author;
  38. $item->content = (!empty($item->image) ? '<a href="'.$item->image.'"><img alt="'.$item->id.'" src="'.$item->imageThumb.'" /></a><br>' : '') . '<span id="'.$item->id.'">'.$element->find('.postMessage', 0)->innertext.'</span>';
  39. $this->items[] = $item;
  40. }
  41. $this->items = array_reverse($this->items);
  42. }
  43. public function getName(){
  44. return '4chan';
  45. }
  46. public function getURI(){
  47. return 'https://www.4chan.org/';
  48. }
  49. public function getCacheDuration(){
  50. return 300; // 5min
  51. }
  52. }