From 97926b839e51abf6d8e4ec616bc7231ecaac6da8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20Mazi=C3=A8re?= Date: Sun, 4 Sep 2016 01:18:06 +0200 Subject: [PATCH] [bridges] one bridge for technically different imageboard solutions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Danbooru serves HTML content while Moebooru send content in JSON format Signed-off-by: Pierre Mazière --- bridges/DanbooruBridge.php | 75 +++++++++++++++++++++++--------------- bridges/MoebooruBridge.php | 49 +++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 30 deletions(-) create mode 100644 bridges/MoebooruBridge.php diff --git a/bridges/DanbooruBridge.php b/bridges/DanbooruBridge.php index 3ee8cb6c..9f0a8c6a 100644 --- a/bridges/DanbooruBridge.php +++ b/bridges/DanbooruBridge.php @@ -1,39 +1,54 @@ array( - 'name'=>'page', - 'type'=>'number' - ), - 't'=>array('name'=>'tags') - )); + const PARAMETERS = array( + 'global'=>array( + 'p'=>array( + 'name'=>'page', + 'defaultValue'=>1, + 'type'=>'number' + ), + 't'=>array('name'=>'tags') + ), + 0=>array() + ); - public function collectData(){ - $page = $this->getInput('p')?$this->getInput('p'):1; - $tags = urlencode($this->getInput('t')); + const PATHTODATA='article'; + const IDATTRIBUTE='data-id'; - $html = $this->getSimpleHTMLDOM(self::URI."posts?&page=$page&tags=$tags") - or $this->returnServerError('Could not request Danbooru.'); - foreach($html->find('div[id=posts] article') as $element) { - $item = array(); - $item['uri'] = self::URI.$element->find('a', 0)->href; - $item['postid'] = (int)preg_replace("/[^0-9]/",'', $element->getAttribute('data-id')); - $item['timestamp'] = time(); - $thumbnailUri = self::URI.$element->find('img', 0)->src; - $item['tags'] = $element->find('img', 0)->getAttribute('alt'); - $item['title'] = 'Danbooru | '.$item['postid']; - $item['content'] = '
Tags: '.$item['tags']; - $this->items[] = $item; - } + protected function getFullURI(){ + return $this->getURI().'posts?' + .'&page='.$this->getInput('p') + .'&tags='.urlencode($this->getInput('t')); + } + + protected function getItemFromElement($element){ + $item = array(); + $item['uri'] = $this->getURI().$element->find('a', 0)->href; + $item['postid'] = (int)preg_replace("/[^0-9]/",'', $element->getAttribute(static::IDATTRIBUTE)); + $item['timestamp'] = time(); + $thumbnailUri = $this->getURI().$element->find('img', 0)->src; + $item['tags'] = $element->find('img', 0)->getAttribute('alt'); + $item['title'] = $this->getName().' | '.$item['postid']; + $item['content'] = '
Tags: '.$item['tags']; + return $item; + } + + public function collectData(){ + $html = $this->getSimpleHTMLDOM($this->getFullURI()) + or $this->returnServerError('Could not request '.$this->getName()); + + foreach($html->find(static::PATHTODATA) as $element) { + $this->items[] = $this->getItemFromElement($element); } + } - public function getCacheDuration(){ - return 1800; // 30 minutes - } + public function getCacheDuration(){ + return 1800; // 30 minutes + } } diff --git a/bridges/MoebooruBridge.php b/bridges/MoebooruBridge.php new file mode 100644 index 00000000..af76de43 --- /dev/null +++ b/bridges/MoebooruBridge.php @@ -0,0 +1,49 @@ +array( + 'name'=>'page', + 'defaultValue'=>1, + 'type'=>'number' + ), + 't'=>array('name'=>'tags') + )); + + protected function getFullURI(){ + return $this->getURI().'post?' + .'page='.$this->getInput('p') + .'&tags='.urlencode($this->getInput('t')); + } + + public function collectData(){ + $html = $this->getSimpleHTMLDOM($this->getFullURI()) + or $this->returnServerError('Could not request '.$this->getName()); + + + $input_json = explode('Post.register(', $html); + foreach($input_json as $element) + $data[] = preg_replace('/}\)(.*)/', '}', $element); + unset($data[0]); + + foreach($data as $datai) { + $json = json_decode($datai, TRUE); + $item = array(); + $item['uri'] = $this->getURI().'/post/show/'.$json['id']; + $item['postid'] = $json['id']; + $item['timestamp'] = $json['created_at']; + $item['imageUri'] = $json['file_url']; + $item['title'] = $this->getName().' | '.$json['id']; + $item['content'] = '
Tags: '.$json['tags']; + $this->items[] = $item; + } + } + + public function getCacheDuration(){ + return 1800; // 30 minutes + } +}