2014-05-26 00:30:46 +02:00
|
|
|
<?php
|
2017-02-11 16:16:56 +01:00
|
|
|
class DanbooruBridge extends BridgeAbstract {
|
|
|
|
|
2018-08-09 21:55:43 +02:00
|
|
|
const MAINTAINER = 'mitsukarenai, logmanoriginal';
|
2017-02-11 16:16:56 +01:00
|
|
|
const NAME = 'Danbooru';
|
|
|
|
const URI = 'http://donmai.us/';
|
|
|
|
const CACHE_TIMEOUT = 1800; // 30min
|
|
|
|
const DESCRIPTION = 'Returns images from given page';
|
|
|
|
|
|
|
|
const PARAMETERS = array(
|
|
|
|
'global' => array(
|
|
|
|
'p' => array(
|
|
|
|
'name' => 'page',
|
|
|
|
'defaultValue' => 1,
|
|
|
|
'type' => 'number'
|
|
|
|
),
|
|
|
|
't' => array(
|
|
|
|
'name' => 'tags'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
0 => array()
|
|
|
|
);
|
|
|
|
|
|
|
|
const PATHTODATA = 'article';
|
|
|
|
const IDATTRIBUTE = 'data-id';
|
2017-08-05 22:24:48 +02:00
|
|
|
const TAGATTRIBUTE = 'alt';
|
2017-02-11 16:16:56 +01:00
|
|
|
|
|
|
|
protected function getFullURI(){
|
|
|
|
return $this->getURI()
|
|
|
|
. 'posts?&page=' . $this->getInput('p')
|
|
|
|
. '&tags=' . urlencode($this->getInput('t'));
|
|
|
|
}
|
|
|
|
|
2017-08-05 22:24:48 +02:00
|
|
|
protected function getTags($element){
|
|
|
|
return $element->find('img', 0)->getAttribute(static::TAGATTRIBUTE);
|
|
|
|
}
|
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
protected function getItemFromElement($element){
|
2017-07-25 14:40:34 +02:00
|
|
|
// Fix links
|
|
|
|
defaultLinkTo($element, $this->getURI());
|
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
$item = array();
|
2017-07-25 14:40:34 +02:00
|
|
|
$item['uri'] = $element->find('a', 0)->href;
|
2018-06-29 23:55:33 +02:00
|
|
|
$item['postid'] = (int)preg_replace('/[^0-9]/', '', $element->getAttribute(static::IDATTRIBUTE));
|
2017-02-11 16:16:56 +01:00
|
|
|
$item['timestamp'] = time();
|
2017-07-25 14:40:34 +02:00
|
|
|
$thumbnailUri = $element->find('img', 0)->src;
|
2017-08-05 22:24:48 +02:00
|
|
|
$item['tags'] = $this->getTags($element);
|
2017-02-11 16:16:56 +01:00
|
|
|
$item['title'] = $this->getName() . ' | ' . $item['postid'];
|
|
|
|
$item['content'] = '<a href="'
|
|
|
|
. $item['uri']
|
|
|
|
. '"><img src="'
|
|
|
|
. $thumbnailUri
|
|
|
|
. '" /></a><br>Tags: '
|
|
|
|
. $item['tags'];
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function collectData(){
|
2018-08-09 21:55:43 +02:00
|
|
|
$content = getContents($this->getFullURI())
|
2017-02-11 16:16:56 +01:00
|
|
|
or returnServerError('Could not request ' . $this->getName());
|
|
|
|
|
2018-08-09 21:55:43 +02:00
|
|
|
$html = Fix_Simple_Html_Dom::str_get_html($content);
|
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
foreach($html->find(static::PATHTODATA) as $element) {
|
|
|
|
$this->items[] = $this->getItemFromElement($element);
|
|
|
|
}
|
|
|
|
}
|
2014-05-26 00:30:46 +02:00
|
|
|
}
|
2018-08-09 21:55:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class is a monkey patch to 'extend' simplehtmldom to recognize <source>
|
|
|
|
* tags (HTML5) as self closing tag. This patch should be removed once
|
|
|
|
* simplehtmldom was fixed. This seems to be a issue with more tags:
|
|
|
|
* https://sourceforge.net/p/simplehtmldom/bugs/83/
|
|
|
|
*
|
|
|
|
* The tag itself is valid according to Mozilla:
|
|
|
|
*
|
|
|
|
* The HTML <picture> element serves as a container for zero or more <source>
|
|
|
|
* elements and one <img> element to provide versions of an image for different
|
|
|
|
* display device scenarios. The browser will consider each of the child <source>
|
|
|
|
* elements and select one corresponding to the best match found; if no matches
|
|
|
|
* are found among the <source> elements, the file specified by the <img>
|
|
|
|
* element's src attribute is selected. The selected image is then presented in
|
|
|
|
* the space occupied by the <img> element.
|
|
|
|
*
|
|
|
|
* -- https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture
|
|
|
|
*
|
|
|
|
* Notice: This class uses parts of the original simplehtmldom, adjusted to pass
|
|
|
|
* the guidelines of RSS-Bridge (formatting)
|
|
|
|
*/
|
|
|
|
final class Fix_Simple_Html_Dom extends simple_html_dom {
|
|
|
|
|
|
|
|
/* copy from simple_html_dom, added 'source' at the end */
|
|
|
|
protected $self_closing_tags = array(
|
|
|
|
'img' => 1,
|
|
|
|
'br' => 1,
|
|
|
|
'input' => 1,
|
|
|
|
'meta' => 1,
|
|
|
|
'link' => 1,
|
|
|
|
'hr' => 1,
|
|
|
|
'base' => 1,
|
|
|
|
'embed' => 1,
|
|
|
|
'spacer' => 1,
|
|
|
|
'source' => 1
|
|
|
|
);
|
|
|
|
|
|
|
|
/* copy from simplehtmldom, changed 'simple_html_dom' to 'Fix_Simple_Html_Dom' */
|
|
|
|
public static function str_get_html($str,
|
|
|
|
$lowercase = true,
|
|
|
|
$forceTagsClosed = true,
|
|
|
|
$target_charset = DEFAULT_TARGET_CHARSET,
|
|
|
|
$stripRN = true,
|
|
|
|
$defaultBRText = DEFAULT_BR_TEXT,
|
|
|
|
$defaultSpanText = DEFAULT_SPAN_TEXT)
|
|
|
|
{
|
|
|
|
$dom = new Fix_Simple_Html_Dom(null,
|
|
|
|
$lowercase,
|
|
|
|
$forceTagsClosed,
|
|
|
|
$target_charset,
|
|
|
|
$stripRN,
|
|
|
|
$defaultBRText,
|
|
|
|
$defaultSpanText);
|
|
|
|
|
|
|
|
if (empty($str) || strlen($str) > MAX_FILE_SIZE) {
|
|
|
|
|
|
|
|
$dom->clear();
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$dom->load($str, $lowercase, $stripRN);
|
|
|
|
|
|
|
|
return $dom;
|
|
|
|
}
|
|
|
|
}
|