DanbooruBridge.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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('name'=>'tags')
  16. ),
  17. 0=>array()
  18. );
  19. const PATHTODATA='article';
  20. const IDATTRIBUTE='data-id';
  21. protected function getFullURI(){
  22. return $this->getURI().'posts?'
  23. .'&page='.$this->getInput('p')
  24. .'&tags='.urlencode($this->getInput('t'));
  25. }
  26. protected function getItemFromElement($element){
  27. $item = array();
  28. $item['uri'] = $this->getURI().$element->find('a', 0)->href;
  29. $item['postid'] = (int)preg_replace("/[^0-9]/",'', $element->getAttribute(static::IDATTRIBUTE));
  30. $item['timestamp'] = time();
  31. $thumbnailUri = $this->getURI().$element->find('img', 0)->src;
  32. $item['tags'] = $element->find('img', 0)->getAttribute('alt');
  33. $item['title'] = $this->getName().' | '.$item['postid'];
  34. $item['content'] = '<a href="' . $item['uri'] . '"><img src="' . $thumbnailUri . '" /></a><br>Tags: '.$item['tags'];
  35. return $item;
  36. }
  37. public function collectData(){
  38. $html = getSimpleHTMLDOM($this->getFullURI())
  39. or returnServerError('Could not request '.$this->getName());
  40. foreach($html->find(static::PATHTODATA) as $element) {
  41. $this->items[] = $this->getItemFromElement($element);
  42. }
  43. }
  44. }