Compare commits

...

2 Commits

Author SHA1 Message Date
96dd41c42b
feat: change user agent 2024-06-11 04:22:09 +03:00
27e9531bda
feat: implement retry to safebooru fetches, update deps 2024-06-11 04:21:12 +03:00
3 changed files with 676 additions and 419 deletions

1062
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -4,13 +4,14 @@ version = "0.1.0"
edition = "2021"
[dependencies]
axum = {git = "https://github.com/tokio-rs/axum.git", version = "0.6"}
axum = {git = "https://github.com/tokio-rs/axum.git", version = "0.7", features = ["macros"]}
tokio = {version = "1", features = ["rt-multi-thread", "macros"]}
http = "0.2"
http = "1"
fastrand = {version = "2", features = ["std"]}
reqwest = {version = "0.11", default-features = false, features = ["rustls-tls-native-roots", "json"]}
reqwest = {version = "0.12", default-features = false, features = ["rustls-tls-native-roots", "json"]}
dashmap = "5"
maud = "0.25"
maud = "0.26"
signal-hook = "0.3"
serde_json = "1"
form_urlencoded = "1"
form_urlencoded = "1"
futures-util = "0.3"

View File

@ -7,6 +7,7 @@ use axum::{
use dashmap::DashMap;
use data::{Art, ArtKind, Data};
use error::AppResult;
use futures_util::TryFutureExt;
use http::Uri;
use maud::PreEscaped;
use std::{
@ -45,6 +46,7 @@ async fn main() {
axum::serve(listener, app).await.unwrap();
}
#[axum::debug_handler]
async fn show_art(state: State<AppState>) -> AppResult<axum::response::Response> {
let art = state.data.lock().unwrap().pick_random_art().clone();
let image_link = if let Some(image_link) = state.direct_links.get(&art.url) {
@ -120,10 +122,21 @@ async fn fetch_safebooru_image_link(http: &reqwest::Client, url: &Uri) -> AppRes
}
let url = format!("https://safebooru.org/index.php?page=dapi&s=post&q=index&json=1&id={id}");
println!("[safebooru] trying to fetch url: {url}");
let req = http.get(url).build()?;
let resp = http.execute(req).await?.error_for_status()?;
let data: Vec<serde_json::Map<String, serde_json::Value>> = resp.json().await?;
type Data = Vec<serde_json::Map<String, serde_json::Value>>;
async fn try_request(count: usize, url: &str, http: &reqwest::Client) -> AppResult<Data> {
println!("[safebooru] trying to fetch url (count {count}): {url}");
let req = http.get(url).build()?;
let resp = http.execute(req).await?.error_for_status()?;
let data = resp.json::<Data>().await?;
AppResult::Ok(data)
}
let data = try_request(0, &url, http)
.or_else(|_| try_request(1, &url, http))
.or_else(|_| try_request(2, &url, http))
.or_else(|_| try_request(3, &url, http))
.or_else(|_| try_request(4, &url, http))
.await?;
let image_filename = data[0].get("image").unwrap().as_str().unwrap();
let image_directory = data[0].get("directory").unwrap().as_str().unwrap();
@ -176,6 +189,7 @@ impl AppState {
direct_links: Default::default(),
http: reqwest::ClientBuilder::new()
.redirect(reqwest::redirect::Policy::none())
.user_agent("limbusart 0.1.0")
.build()
.unwrap(),
}),