Expand description
§booru
An async Booru client for Rust.
Note: This project has been forked from booru-rs since September of 2023, but a lot has changed.
§Overview
The client currently supports:
- Gelbooru
- Safebooru
- Danbooru
- Rule 34
§Example
Remember to bring the Client trait into scope with use booru::client::Client;.
use booru::{
client::{gelbooru::GelbooruClient, generic::*, Client},
model::gelbooru::GelbooruRating,
};
#[tokio::main]
async fn main() {
let posts = GelbooruClient::builder()
.tag("kafuu_chino")
.tag("2girls")
.rating(GelbooruRating::General)
.sort(Sort::Score)
.limit(5)
.random()
.blacklist_tag(GelbooruRating::Explicit)
.build()
.get()
.await
.expect("There was an error retrieving posts from the API");
}§Customizing http client
If you want to customize http client, you can use builder_with_http_client:
use booru::{
client::{gelbooru::GelbooruClient, generic::*, Client},
model::gelbooru::GelbooruRating,
};
use reqwest;
use std::time::Duration;
#[tokio::main]
async fn main() {
let http_client_builder = reqwest::ClientBuilder::new()
.timeout(Duration::from_secs(10));
let posts = GelbooruClient::builder_with_http_client(http_client_builder)
.tag("kafuu_chino")
.limit(5)
.build()
.get()
.await
.expect("There was an error retrieving posts from the API");
}§Tests
[!WARNING] To run the
gelboorutests, set theGELBOORU_API_KEYandGELBOORU_USER_IDenv variables, then run withcargo test -- --nocapture --ignored.
# Run all tests except gelbooru
cargo test -- --nocapture
# Run all tests
export GELBOORU_API_KEY="your_api_key"
export GELBOORU_USER_ID="your_user_id"
cargo test -- --nocapture --ignored
### Get by page
use booru::{danbooru::DanbooruClient, Client};
#[tokio::main] async fn main() { let posts = DanbooruClient::builder() .tag(“kafuu_chino”) .limit(5) .build() .get_by_page(2) .await .expect(“There was an error retrieving posts from the API”);
println!("{:?}", posts);}
## Get by popular
use booru::{danbooru::DanbooruClient, Client};
#[tokio::main] async fn main() { let posts = DanbooruClient::builder() .limit(5) .build() .get_popular() .await .expect(“There was an error retrieving posts from the API”);
println!("{:?}", posts);}
## Get Autocomplete
```rust
use booru::{Client, danbooru::DanbooruClient};
use tracing::Level;
use tracing_subscriber::FmtSubscriber;
#[tokio::main]
async fn main() {
let subscriber = FmtSubscriber::builder()
// all spans/events with a level higher than TRACE (e.g, debug, info, warn, etc.)
// will be written to stdout.
.with_max_level(Level::DEBUG)
// completes the builder.
.finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
let posts = DanbooruClient::builder()
.limit(5)
.build()
.get_autocomplete("f")
.await
.expect("There was an error retrieving posts from the API");
println!("{:?}", posts);
}Re-exports§
pub use client::Client;pub use client::generic::AutoCompleteItem;pub use client::generic::Sort;