DanbooruBridge.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. const TAGATTRIBUTE = 'alt';
  24. protected function getFullURI(){
  25. return $this->getURI()
  26. . 'posts?&page=' . $this->getInput('p')
  27. . '&tags=' . urlencode($this->getInput('t'));
  28. }
  29. protected function getTags($element){
  30. return $element->find('img', 0)->getAttribute(static::TAGATTRIBUTE);
  31. }
  32. protected function getItemFromElement($element){
  33. // Fix links
  34. defaultLinkTo($element, $this->getURI());
  35. $item = array();
  36. $item['uri'] = $element->find('a', 0)->href;
  37. $item['postid'] = (int)preg_replace('/[^0-9]/', '', $element->getAttribute(static::IDATTRIBUTE));
  38. $item['timestamp'] = time();
  39. $thumbnailUri = $element->find('img', 0)->src;
  40. $item['tags'] = $this->getTags($element);
  41. $item['title'] = $this->getName() . ' | ' . $item['postid'];
  42. $item['content'] = '<a href="'
  43. . $item['uri']
  44. . '"><img src="'
  45. . $thumbnailUri
  46. . '" /></a><br>Tags: '
  47. . $item['tags'];
  48. return $item;
  49. }
  50. public function collectData(){
  51. $html = getSimpleHTMLDOM($this->getFullURI())
  52. or returnServerError('Could not request ' . $this->getName());
  53. foreach($html->find(static::PATHTODATA) as $element) {
  54. $this->items[] = $this->getItemFromElement($element);
  55. }
  56. }
  57. }