Git Product home page Git Product logo

imageboard-api's Introduction

ImageBoard (Booru) API

Maven Central

ImageBoard API is a simple asynchronous Java API wrapper around the most popular danbooru-compatible booru APIs. Pretty much, it is a Booru library for Java. The interface also supports other types of custom boards given a little tweaking. Releases are published in Maven Central.

This helps you craft requests and search/fetch images on the danbooru-compatible imageboards (boorus) that exist out there.

Supported Booru Image Boards

  • Rule34
  • e621
  • Konachan
  • Yande.re
  • Danbooru
  • Safebooru
  • Gelbooru
  • e926

Creating your own ImageBoard instance is possible, but would require a little tweaking. Please refer to the ImageBoards.java and CustomBoard.java to set up boards that are not included with this library. The boorus listed above are available by default with no further configuration required by the user.

Adding ImageBoardAPI to your project

imageboard-api is available in Maven Central (Sonatype). Check the Sonatype respository for more information

Gradle

repositories {
    mavenCentral()
}

dependencies {
    implementation 'io.github.kodehawa:imageboard-api:2.6.1.1'
}

Set Up

There is a ImageBoards class located under utils, that one contains static, pre-created ImageBoardAPI objects for you, but you can roll your own.

Implementation

You can find implementation details and a lot of examples in the tests for this project and in Mantaro

The User-Agent must be initialized to make requests. You can initialize it with ImageBoard.setUserAgent(). The default one was blocked by most imageboards, setting your own is the best to avoid UA blocking.

Examples

Random Images

import net.kodehawa.lib.imageboards.entities.BoardImage;
import net.kodehawa.lib.imageboards.DefaultImageBoards;

public class RandomImages {
    public static void main(String[] args) {
        // Asynchronous GET
        // 60 random images
        DefaultImageBoards.KONACHAN.get().async(images -> {
            for (BoardImage image : images) System.out.println(image.getURL());
        });
        
        // Asynchronous GET
        // 30 random images
        ImageBoards.KONACHAN.get(30).async(images -> {
            for (BoardImage image : images) System.out.println(image.getURL());
        });

        // Blocking GET
        // 5 random image
        BoardImage image = DefaultImageBoards.KONACHAN.get(5).blocking().get(0);
        System.out.println(image.getURL());
        System.out.println(image.getRating());
        System.out.println(image.getTags());
        System.out.println(image.getHeight());
        System.out.println(image.getWidth());
    }
}

Image Tag Search

import net.kodehawa.lib.imageboards.entities.BoardImage;
import net.kodehawa.lib.imageboards.DefaultImageBoards;

public class TagImages {
    public static void main(String[] args) {
        // Asynchronous GET
        // 20 images tagged with animal_ears
        DefaultImageBoards.KONACHAN.search(20, "animal_ears").async(images -> {
            for (BoardImage image : images) System.out.println(image.getURL());
        });

        // Blocking GET
        // 60 images tagged with animal_ears
        BoardImage image = DefaultImageBoards.KONACHAN.search("animal_ears").blocking().get(0);
        System.out.println(image.getURL());
        System.out.println(image.getRating());
        System.out.println(image.getTags());
        System.out.println(image.getHeight());
        System.out.println(image.getWidth());
    }
}

Filter bad results

You might want to filter BoardImage#isPending, as the tags/rating of those is almost guaranteed to never be correct.

For filtering other results, you can filter tags from BoardImage#getTags and use a .contains call on the list.

imageboard-api's People

Contributors

avarel avatar bytealex avatar dependabot-preview[bot] avatar dependabot[bot] avatar fabricio20 avatar insanusmokrassar avatar kodehawa avatar nachtraben avatar natanbc avatar shindoumihou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

imageboard-api's Issues

"Random images" isnt really random

Ive noticed that when grabbing images from danbooru, whether that be by using an asynchronous call or through creating ImageBoard object, the images it generates arent randomizing everytime. When printing out the image, its always the same image. If a span of time passes, however, there is a chance that this image changes from what it was. When printing out this new image however, it simply keeps printing out this same, new image. Is there a way to randomize the images everytime?

Gelbooru has switched to a new rating system

Gelbooru recently updated their ratings to have 4 values (general, sensitive, questionable, explicit) instead of the previous 3.

This means there is no way of searching for general or sensitive images there.

[ Feature Req. ] e926 support

e926 is an entirely SFW mirror of e621. Creating a default binding for it would be useful for grabbing safe furry images using the API.

Maven Dependency

Can't download anything with the maven dependency (tried most of the versions)

OkHTTP Timeout

caused by: java.net.sockettimeoutexception: timeout at okhttp3.internal.http2.http2stream$streamtimeout.newtimeoutexception(http2stream.kt:677) ~[paradoxium.jar:?] at okhttp3.internal.http2.http2stream$streamtimeout.exitandthrowiftimedout(http2stream.kt:686) ~[paradoxium.jar:?] at okhttp3.internal.http2.http2stream.takeheaders(http2stream.kt:143) ~[paradoxium.jar:?] at okhttp3.internal.http2.http2exchangecodec.readresponseheaders(http2exchangecodec.kt:96) ~[paradoxium.jar:?] at okhttp3.internal.connection.exchange.readresponseheaders(exchange.kt:106) ~[paradoxium.jar:?] at okhttp3.internal.http.callserverinterceptor.intercept(callserverinterceptor.kt:79) ~[paradoxium.jar:?] at okhttp3.internal.http.realinterceptorchain.proceed(realinterceptorchain.kt:109) ~[paradoxium.jar:?] at okhttp3.internal.connection.connectinterceptor.intercept(connectinterceptor.kt:34) ~[paradoxium.jar:?] at okhttp3.internal.http.realinterceptorchain.proceed(realinterceptorchain.kt:109) ~[paradoxium.jar:?] at okhttp3.internal.cache.cacheinterceptor.intercept(cacheinterceptor.kt:95) ~[paradoxium.jar:?] at okhttp3.internal.http.realinterceptorchain.proceed(realinterceptorchain.kt:109) ~[paradoxium.jar:?] at okhttp3.internal.http.bridgeinterceptor.intercept(bridgeinterceptor.kt:83) ~[paradoxium.jar:?] at okhttp3.internal.http.realinterceptorchain.proceed(realinterceptorchain.kt:109) ~[paradoxium.jar:?] at okhttp3.internal.http.retryandfollowupinterceptor.intercept(retryandfollowupinterceptor.kt:76) ~[paradoxium.jar:?] at okhttp3.internal.http.realinterceptorchain.proceed(realinterceptorchain.kt:109) ~[paradoxium.jar:?] at okhttp3.internal.connection.realcall.getresponsewithinterceptorchain$okhttp(realcall.kt:201) ~[paradoxium.jar:?] at okhttp3.internal.connection.realcall$asynccall.run(realcall.kt:517) [paradoxium.jar:?]

There seems to be an issue with this, though it could be because of my code.
Here's a snippet of the send() part of my code.

private void send(Server server, TextChannel textChannel) {
               
 DefaultImageBoards.SAFEBOORU.search(rand.nextInt(35708), 50, "1girl").submit().whenComplete((p, throwable) -> {
                    List<SafebooruImage> image = GreatFilter.filter(p).stream().filter(safebooruImage -> !(GreatFilter.containsCustomExcludedTags(safebooruImage.getTags(), waifuFilter))).collect(Collectors.toList());

                    if (throwable == null) { // Checks if throwable is null.
                        if (!image.isEmpty()) {
                            MessageAPI.image(p.get(rand.nextInt(p.size() - 1)).getURL()).send(textChannel); // Sends the message containing the image.
                        } else {
                            send(server, textChannel); // Repeating the entire thing because the list is empty (don't know if this is a good thing), I could simply just redirect to Reddit source if this is bad though.
                        }
                    } else { // If throwable isn't null then redirect to Reddit source in an attempt to save.
                        reddit(server, textChannel);
                    }
                }).exceptionally(ExceptionLogger.get());
        }

I also don't know if this is an issue with the API but none of the methods I use except for the API uses OkHTTP.
Extra Details:

  • The code is running on Java 11, Ubuntu 18.04.
  • I do have OkHttp on my pom.xml (v4.8.1 during the error, is this a cause?)

Image searching with out of bounds exception

Whenever i am searching up an tag, lets say the tag has 20 images. The method I'm currently using uses an Random generator to select between 1 and 100. Is there a way to get a random image without having to use a generator?

Here's the code i'm currently using.

int generation = gen.nextInt(100)+1;
		R34image = DefaultImageBoards.RULE34.search(100, combined ).blocking().get(generation);
    	
    	String output = R34image.getTags() + "\n **Rating:** " + R34image.getScore() + "\n **Size**: " + R34image.getWidth() + "x" + R34image.getHeight() + "\n" + R34image.getURL();
        
    	channel.sendMessage("__Image Search for:__ **"+combined+ "** \n" + output);

Safebooru error on linux

https://pastebin.com/j3Gbac3g

 int limitGet = DefaultImageBoards.SAFEBOORU.search(combined).blocking().size();
            BoardImage SFWbooru = (BoardImage) ((List) DefaultImageBoards.SAFEBOORU.search(combined).blocking()).get(Others.gen.nextInt(limitGet) + 1);

This error only seems to occur on the Linux device that runs my program, i do not know when exactly it started, but it makes using the image board impossible on Linux.
I've tested with a stand alone program as well to confirm my own program was not at fault, and it failed the same way.

Exception in thread "OkHttp Dispatcher" java.lang.NullPointerException.

As the title says, I received an error on console when trying to fetch images, though they don't really have much impact on performance or haven't caused any issues to my Discord bot as far as I can tell, I wonder if I wrote something wrong here.

Here is the error message.
Exception in thread "OkHttp Dispatcher" java.lang.NullPointerException at net.kodehawa.lib.imageboards.requests.RequestAction$1.onFailure(RequestAction.java:104) at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:525) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:830)

And here is the code I am using.
` public static void konachan(String tag) {

// Async Getter DefaultImageBoards.KONACHAN.search(50, tag).async(images -> {
for (BoardImage image : images){
// Check if image is SFW.
if (image.getRating() == Rating.SAFE) {
// Stores URL in a list to be fetched later.
urls.add(image.getURL());
}
}
});`

Is it a problem on my side, or something else...?

IllegalStateException

Using this code will throw an exception

DefaultImageBoards.DANBOORU.search(...).blocking()

Error message: User-Agent must be initialized to make requests, Initialize with ImageBoard.setUserAgent()

Invalid URLs (Danbooru)

The current Danbooru impl is prepending https://danbooru.donmai.us to the url if the url does not start with it, however, there are multiple instances where Danbooru will return their other domains such as raikou1 or raikou2.

This results in the following URL:
https://danbooru.donmai.ushttps//raikou1.donmai.us/XX/XX/XXXXXX.jpg

At the same time, you can notice the original URL is also broken with https//.

NoClassDefFound Error on build Jar

I really like this api, it's simple to use, and works perfectly when I run it through IntelliJ.
Once I build the Jar however, the API breaks and the class "DefaultImageBoards" is just gone.

Missing Artifact

I’m using jcenrer (bintray), and when trying to add imageboard-api to my Pom.xml I get
“Missing artifact net.kodehawa:imageboard-api:jar:2.0.3_2”

Random image.

Sorry to be a nuisance, but I'm just struggling with the random image. When I call on it it returns with the same image every time. May seem dumb, but I have no idea why.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.