FourchanBridge.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. class FourchanBridge extends BridgeAbstract{
  3. const MAINTAINER = "mitsukarenai";
  4. const NAME = "4chan";
  5. const URI = "https://boards.4chan.org/";
  6. const DESCRIPTION = "Returns posts from the specified thread";
  7. const PARAMETERS = array( array(
  8. 'c'=>array(
  9. 'name'=>'Thread category',
  10. 'required'=>true
  11. ),
  12. 't'=>array(
  13. 'name'=>'Thread number',
  14. 'type'=>'number',
  15. 'required'=>true
  16. )
  17. ));
  18. public function getURI(){
  19. return static::URI.$this->getInput('c').'/thread/'.$this->getInput('t');
  20. }
  21. public function collectData(){
  22. $html = getSimpleHTMLDOM($this->getURI())
  23. or returnServerError("Could not request 4chan, thread not found");
  24. foreach($html->find('div.postContainer') as $element) {
  25. $item = array();
  26. $item['id'] = $element->find('.post', 0)->getAttribute('id');
  27. $item['uri'] = $this->getURI().'#'.$item['id'];
  28. $item['timestamp'] = $element->find('span.dateTime', 0)->getAttribute('data-utc');
  29. $item['author'] = $element->find('span.name', 0)->plaintext;
  30. $file=$element->find('.file', 0);
  31. if(!empty($file) ) {
  32. $item['image'] = $element->find('.file a', 0)->href;
  33. $item['imageThumb'] = $element->find('.file img', 0)->src;
  34. if(!isset($item['imageThumb']) and strpos($item['image'], '.swf') !== FALSE)
  35. $item['imageThumb'] = 'http://i.imgur.com/eO0cxf9.jpg';
  36. }
  37. if(!empty($element->find('span.subject', 0)->innertext )) {
  38. $item['subject'] = $element->find('span.subject', 0)->innertext;
  39. }
  40. $item['title'] = 'reply '.$item['id'].' | '.$item['author'];
  41. if(isset($item['subject'])){
  42. $item['title'] = $item['subject'].' - '.$item['title'];
  43. }
  44. $content = $element->find('.postMessage', 0)->innertext;
  45. $content = str_replace('href="#p','href="'.$this->getURI().'#p',$content);
  46. $item['content'] = '<span id="'.$item['id'].'">'.$content.'</span>';
  47. if(isset($item['image'])){
  48. $item['content'] = '<a href="'.$item['image'].'">'
  49. .'<img alt="'.$item['id'].'" src="'.$item['imageThumb'].'" />'
  50. .'</a><br>'
  51. .$item['content'];
  52. }
  53. $this->items[] = $item;
  54. }
  55. $this->items = array_reverse($this->items);
  56. }
  57. public function getCacheDuration(){
  58. return 300; // 5min
  59. }
  60. }