FourchanBridge.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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->parameters[] = array(
  9. 't'=>array('name'=>'Thread URL')
  10. );
  11. }
  12. public function collectData(array $param){
  13. if (!isset($param['t']))
  14. $this->returnClientError('You must specify the thread URL (?t=...)');
  15. $thread = parse_url($param['t']) or $this->returnClientError('This URL seems malformed, please check it.');
  16. if($thread['host'] !== 'boards.4chan.org')
  17. $this->returnClientError('4chan thread URL only.');
  18. if(strpos($thread['path'], 'thread/') === FALSE)
  19. $this->returnClientError('You must specify the thread URL.');
  20. $url = 'https://boards.4chan.org'.$thread['path'].'';
  21. $html = $this->getSimpleHTMLDOM($url) or $this->returnServerError("Could not request 4chan, thread not found");
  22. foreach($html->find('div.postContainer') as $element) {
  23. $item = array();
  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(!isset($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'] = (isset($item['subject']) ? $item['subject'].' - ' : '' ) . 'reply '.$item['id'].' | '.$item['author'];
  38. $item['content'] = (isset($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 getCacheDuration(){
  44. return 300; // 5min
  45. }
  46. }