Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GelbooruBridge] + inheriting Bridges. Switch to using Gelbooru API #2472

Merged
merged 6 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions bridges/BooruprojectBridge.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?php
require_once('GelbooruBridge.php');
require_once('DanbooruBridge.php');

class BooruprojectBridge extends GelbooruBridge {
class BooruprojectBridge extends DanbooruBridge {

const MAINTAINER = 'mitsukarenai';
const NAME = 'Booruproject';
const URI = 'http://booru.org/';
const URI = 'https://booru.org/';
const DESCRIPTION = 'Returns images from given page of booruproject';
const PARAMETERS = array(
'global' => array(
'p' => array(
'name' => 'page',
'defaultValue' => 0,
'type' => 'number'
),
't' => array(
Expand All @@ -25,8 +26,30 @@ class BooruprojectBridge extends GelbooruBridge {
)
);

const PATHTODATA = '.thumb';
const IDATTRIBUTE = 'id';
const TAGATTRIBUTE = 'title';
const PIDBYPAGE = 20;

protected function getFullURI(){
return $this->getURI()
. 'index.php?page=post&s=list&pid='
. ($this->getInput('p') ? ($this->getInput('p') - 1) * static::PIDBYPAGE : '')
. '&tags=' . urlencode($this->getInput('t'));
}

protected function getTags($element){
$tags = parent::getTags($element);
$tags = explode(' ', $tags);

// Remove statistics from the tags list (identified by colon)
foreach($tags as $key => $tag) {
if(strpos($tag, ':') !== false) unset($tags[$key]);
}

return implode(' ', $tags);
}

public function getURI(){
if(!is_null($this->getInput('i'))) {
return 'http://' . $this->getInput('i') . '.booru.org/';
Expand Down
88 changes: 70 additions & 18 deletions bridges/GelbooruBridge.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,87 @@
<?php
require_once('DanbooruBridge.php');

class GelbooruBridge extends DanbooruBridge {
class GelbooruBridge extends BridgeAbstract {

const MAINTAINER = 'mitsukarenai';
const NAME = 'Gelbooru';
const URI = 'http://gelbooru.com/';
const URI = 'https://gelbooru.com/';
const DESCRIPTION = 'Returns images from given page';

const PATHTODATA = '.thumb';
const IDATTRIBUTE = 'id';
const TAGATTRIBUTE = 'title';

const PIDBYPAGE = 63;
const PARAMETERS = array(
'global' => array(
'p' => array(
'name' => 'page',
'defaultValue' => 0,
'type' => 'number'
),
't' => array(
'name' => 'tags',
'exampleValue' => 'pinup',
'title' => 'Tags to search for'
),
'l' => array(
'name' => 'limit',
'exampleValue' => 100,
'title' => 'How many posts to retrieve (hard limit of 1000)'
)
),
0 => array()
);

protected function getFullURI(){
return $this->getURI()
. 'index.php?page=post&s=list&pid='
. ($this->getInput('p') ? ($this->getInput('p') - 1) * static::PIDBYPAGE : '')
. 'index.php?&page=dapi&s=post&q=index&json=1&pid=' . $this->getInput('p')
. '&limit=' . $this->getInput('l')
. '&tags=' . urlencode($this->getInput('t'));
}

protected function getTags($element){
$tags = parent::getTags($element);
$tags = explode(' ', $tags);
/*
This function is superfluous for GelbooruBridge, but useful
for Bridges that inherit from it
*/
protected function buildThumbnailURI($element){
return $this->getURI() . 'thumbnails/' . $element->directory
. '/thumbnail_' . $element->md5 . '.jpg';
}

protected function getItemFromElement($element){
$item = array();
$item['uri'] = $this->getURI() . 'index.php?page=post&s=view&id='
. $element->id;
$item['postid'] = $element->id;
$item['author'] = $element->owner;
$item['timestamp'] = date('d F Y H:i:s', $element->change);
$item['tags'] = $element->tags;
$item['title'] = $this->getName() . ' | ' . $item['postid'];

// Remove statistics from the tags list (identified by colon)
foreach($tags as $key => $tag) {
if(strpos($tag, ':') !== false) unset($tags[$key]);
if (isset($element->preview_url)) {
$thumbnailUri = $element->preview_url;
} else{
$thumbnailUri = $this->buildThumbnailURI($element);
}

return implode(' ', $tags);
$item['content'] = '<a href="' . $item['uri'] . '"><img src="'
. $thumbnailUri . '" /></a><br><br><b>Tags:</b> '
. $item['tags'] . '<br><br>' . $item['timestamp'];

return $item;
}

public function collectData(){
$content = getContents($this->getFullURI());

// Most other Gelbooru-based boorus put their content in the root of
// the JSON. This check is here for Bridges that inherit from this one
$posts = json_decode($content);
if (isset($posts->post)) {
$posts = $posts->post;
}

if (is_null($posts)) {
returnServerError('No posts found.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want this? It generates HTTP 500 Internal Server Error from us. Maybe better to give user an empty feed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to how the API works, this should only ever occur when someone is testing/creating a new feed via the web interface. Once they have a working feed URL, it should never generate this error. It may generate the exact same result every single time it's queried, if it's an unpopular tag, but the results should never shrink/disappear. Unless somehow all items with the queried tag are subsequently deleted from the site. If the the site itself goes down or the API changes/stops working, that should fail at a different point in the code.

}

foreach($posts as $post) {
$this->items[] = $this->getItemFromElement($post);
}
}
}
7 changes: 5 additions & 2 deletions bridges/MspabooruBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ class MspabooruBridge extends GelbooruBridge {

const MAINTAINER = 'mitsukarenai';
const NAME = 'Mspabooru';
const URI = 'http://mspabooru.com/';
const URI = 'https://mspabooru.com/';
const DESCRIPTION = 'Returns images from given page';
const PIDBYPAGE = 50;

protected function buildThumbnailURI($element){
return $this->getURI() . 'thumbnails/' . $element->directory
. '/thumbnail_' . $element->image;
}
}
1 change: 0 additions & 1 deletion bridges/Rule34Bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ class Rule34Bridge extends GelbooruBridge {
const URI = 'https://rule34.xxx/';
const DESCRIPTION = 'Returns images from given page';

const PIDBYPAGE = 50;
}
6 changes: 5 additions & 1 deletion bridges/SafebooruBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ class SafebooruBridge extends GelbooruBridge {
const URI = 'https://safebooru.org/';
const DESCRIPTION = 'Returns images from given page';

const PIDBYPAGE = 40;
protected function buildThumbnailURI($element){
$regex = '/\.\w+$/';
return $this->getURI() . 'thumbnails/' . $element->directory
. '/thumbnail_' . preg_replace($regex, '.jpg', $element->image);
}
}
6 changes: 5 additions & 1 deletion bridges/TbibBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ class TbibBridge extends GelbooruBridge {
const URI = 'https://tbib.org/';
const DESCRIPTION = 'Returns images from given page';

const PIDBYPAGE = 50;
protected function buildThumbnailURI($element){
$regex = '/\.\w+$/';
return $this->getURI() . 'thumbnails/' . $element->directory
. '/thumbnail_' . preg_replace($regex, '.jpg', $element->image);
}
}
5 changes: 4 additions & 1 deletion bridges/XbooruBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ class XbooruBridge extends GelbooruBridge {
const URI = 'https://xbooru.com/';
const DESCRIPTION = 'Returns images from given page';

const PIDBYPAGE = 50;
protected function buildThumbnailURI($element){
return $this->getURI() . 'thumbnails/' . $element->directory
. '/thumbnail_' . $element->hash . '.jpg';
}
}