feat: implement retry to safebooru fetches, update deps

This commit is contained in:
dusk 2024-06-11 04:21:12 +03:00
parent 91d0585000
commit 27e9531bda
Signed by: dusk
SSH Key Fingerprint: SHA256:Abmvag+juovVufZTxyWY8KcVgrznxvBjQpJesv071Aw
3 changed files with 675 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" edition = "2021"
[dependencies] [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"]} tokio = {version = "1", features = ["rt-multi-thread", "macros"]}
http = "0.2" http = "1"
fastrand = {version = "2", features = ["std"]} 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" dashmap = "5"
maud = "0.25" maud = "0.26"
signal-hook = "0.3" signal-hook = "0.3"
serde_json = "1" 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 dashmap::DashMap;
use data::{Art, ArtKind, Data}; use data::{Art, ArtKind, Data};
use error::AppResult; use error::AppResult;
use futures_util::TryFutureExt;
use http::Uri; use http::Uri;
use maud::PreEscaped; use maud::PreEscaped;
use std::{ use std::{
@ -45,6 +46,7 @@ async fn main() {
axum::serve(listener, app).await.unwrap(); axum::serve(listener, app).await.unwrap();
} }
#[axum::debug_handler]
async fn show_art(state: State<AppState>) -> AppResult<axum::response::Response> { async fn show_art(state: State<AppState>) -> AppResult<axum::response::Response> {
let art = state.data.lock().unwrap().pick_random_art().clone(); let art = state.data.lock().unwrap().pick_random_art().clone();
let image_link = if let Some(image_link) = state.direct_links.get(&art.url) { 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}"); 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}"); 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 req = http.get(url).build()?;
let resp = http.execute(req).await?.error_for_status()?; let resp = http.execute(req).await?.error_for_status()?;
let data: Vec<serde_json::Map<String, serde_json::Value>> = resp.json().await?; 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_filename = data[0].get("image").unwrap().as_str().unwrap();
let image_directory = data[0].get("directory").unwrap().as_str().unwrap(); let image_directory = data[0].get("directory").unwrap().as_str().unwrap();