DanbooruBridge.php 1.6 KB

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