DanbooruBridge.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. class DanbooruBridge extends BridgeAbstract {
  3. const MAINTAINER = 'mitsukarenai';
  4. const NAME = 'Danbooru';
  5. const URI = 'http://donmai.us/';
  6. const CACHE_TIMEOUT = 1800; // 30min
  7. const DESCRIPTION = 'Returns images from given page';
  8. const PARAMETERS = array(
  9. 'global' => array(
  10. 'p' => array(
  11. 'name' => 'page',
  12. 'defaultValue' => 1,
  13. 'type' => 'number'
  14. ),
  15. 't' => array(
  16. 'name' => 'tags'
  17. )
  18. ),
  19. 0 => array()
  20. );
  21. const PATHTODATA = 'article';
  22. const IDATTRIBUTE = 'data-id';
  23. protected function getFullURI(){
  24. return $this->getURI()
  25. . 'posts?&page=' . $this->getInput('p')
  26. . '&tags=' . urlencode($this->getInput('t'));
  27. }
  28. protected function getItemFromElement($element){
  29. $item = array();
  30. $item['uri'] = $this->getURI() . $element->find('a', 0)->href;
  31. $item['postid'] = (int)preg_replace("/[^0-9]/", '', $element->getAttribute(static::IDATTRIBUTE));
  32. $item['timestamp'] = time();
  33. $thumbnailUri = $this->getURI() . $element->find('img', 0)->src;
  34. $item['tags'] = $element->find('img', 0)->getAttribute('alt');
  35. $item['title'] = $this->getName() . ' | ' . $item['postid'];
  36. $item['content'] = '<a href="'
  37. . $item['uri']
  38. . '"><img src="'
  39. . $thumbnailUri
  40. . '" /></a><br>Tags: '
  41. . $item['tags'];
  42. return $item;
  43. }
  44. public function collectData(){
  45. $html = getSimpleHTMLDOM($this->getFullURI())
  46. or returnServerError('Could not request ' . $this->getName());
  47. foreach($html->find(static::PATHTODATA) as $element) {
  48. $this->items[] = $this->getItemFromElement($element);
  49. }
  50. }
  51. }