FourchanBridge.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. if(!is_null($this->getInput('c')) && !is_null($this->getInput('t'))) {
  21. return static::URI . $this->getInput('c') . '/thread/' . $this->getInput('t');
  22. }
  23. return parent::getURI();
  24. }
  25. public function collectData(){
  26. $html = getSimpleHTMLDOM($this->getURI())
  27. or returnServerError('Could not request 4chan, thread not found');
  28. foreach($html->find('div.postContainer') as $element) {
  29. $item = array();
  30. $item['id'] = $element->find('.post', 0)->getAttribute('id');
  31. $item['uri'] = $this->getURI() . '#' . $item['id'];
  32. $item['timestamp'] = $element->find('span.dateTime', 0)->getAttribute('data-utc');
  33. $item['author'] = $element->find('span.name', 0)->plaintext;
  34. $file = $element->find('.file', 0);
  35. if(!empty($file)) {
  36. $item['image'] = $element->find('.file a', 0)->href;
  37. $item['imageThumb'] = $element->find('.file img', 0)->src;
  38. if(!isset($item['imageThumb']) and strpos($item['image'], '.swf') !== false)
  39. $item['imageThumb'] = 'http://i.imgur.com/eO0cxf9.jpg';
  40. }
  41. if(!empty($element->find('span.subject', 0)->innertext)) {
  42. $item['subject'] = $element->find('span.subject', 0)->innertext;
  43. }
  44. $item['title'] = 'reply ' . $item['id'] . ' | ' . $item['author'];
  45. if(isset($item['subject'])) {
  46. $item['title'] = $item['subject'] . ' - ' . $item['title'];
  47. }
  48. $content = $element->find('.postMessage', 0)->innertext;
  49. $content = str_replace('href="#p', 'href="' . $this->getURI() . '#p', $content);
  50. $item['content'] = '<span id="' . $item['id'] . '">' . $content . '</span>';
  51. if(isset($item['image'])) {
  52. $item['content'] = '<a href="'
  53. . $item['image']
  54. . '"><img alt="'
  55. . $item['id']
  56. . '" src="'
  57. . $item['imageThumb']
  58. . '" /></a><br>'
  59. .$item['content'];
  60. }
  61. $this->items[] = $item;
  62. }
  63. $this->items = array_reverse($this->items);
  64. }
  65. }