feat: implement retry to safebooru fetches, update deps
This commit is contained in:
parent
91d0585000
commit
27e9531bda
1062
Cargo.lock
generated
1062
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -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"
|
21
src/main.rs
21
src/main.rs
@ -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>>;
|
||||||
let req = http.get(url).build()?;
|
async fn try_request(count: usize, url: &str, http: &reqwest::Client) -> AppResult<Data> {
|
||||||
let resp = http.execute(req).await?.error_for_status()?;
|
println!("[safebooru] trying to fetch url (count {count}): {url}");
|
||||||
let data: Vec<serde_json::Map<String, serde_json::Value>> = resp.json().await?;
|
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_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();
|
||||||
|
Loading…
Reference in New Issue
Block a user