catscii/src/main.rs

41 lines
952 B
Rust
Raw Normal View History

2023-05-13 15:38:07 -04:00
use serde::Deserialize;
#[tokio::main]
async fn main() {
2023-05-14 10:48:43 -04:00
let art = get_cat_ascii_art().await.unwrap();
println!("{art}");
2023-05-13 16:24:05 -04:00
}
2023-05-14 10:48:43 -04:00
async fn get_cat_ascii_art() -> color_eyre::Result<String> {
2023-05-13 15:38:07 -04:00
#[derive(Deserialize)]
struct CatImage {
url: String,
}
2023-05-14 10:34:23 -04:00
let api_url = "https://api.thecatapi.com/v1/images/search";
let client = reqwest::Client::default();
let image = client
.get(api_url)
.send()
.await?
.error_for_status()?
.json::<Vec<CatImage>>()
.await?
.pop()
.ok_or_else(|| color_eyre::eyre::eyre!("The Cat API returned no images"))?;
2023-05-14 10:48:43 -04:00
let image_bytes = client
2023-05-14 10:34:23 -04:00
.get(image.url)
.send()
.await?
.error_for_status()?
.bytes()
2023-05-14 10:48:43 -04:00
.await?;
let image = image::load_from_memory(&image_bytes)?;
let ascii_art = artem::convert(image, artem::options::OptionBuilder::new().build());
Ok(ascii_art)
}