FourchanBridge.php 2.2 KB

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