FourchanBridge.php 2.4 KB

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