Compare commits
No commits in common. "96dd41c42b73d352042b83c104320681d410a14b" and "91d0585000c96c2e8cced36ccd269566f732fd07" have entirely different histories.
96dd41c42b
...
91d0585000
1062
Cargo.lock
generated
1062
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
11
Cargo.toml
11
Cargo.toml
@ -4,14 +4,13 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
axum = {git = "https://github.com/tokio-rs/axum.git", version = "0.7", features = ["macros"]}
|
axum = {git = "https://github.com/tokio-rs/axum.git", version = "0.6"}
|
||||||
tokio = {version = "1", features = ["rt-multi-thread", "macros"]}
|
tokio = {version = "1", features = ["rt-multi-thread", "macros"]}
|
||||||
http = "1"
|
http = "0.2"
|
||||||
fastrand = {version = "2", features = ["std"]}
|
fastrand = {version = "2", features = ["std"]}
|
||||||
reqwest = {version = "0.12", default-features = false, features = ["rustls-tls-native-roots", "json"]}
|
reqwest = {version = "0.11", default-features = false, features = ["rustls-tls-native-roots", "json"]}
|
||||||
dashmap = "5"
|
dashmap = "5"
|
||||||
maud = "0.26"
|
maud = "0.25"
|
||||||
signal-hook = "0.3"
|
signal-hook = "0.3"
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
form_urlencoded = "1"
|
form_urlencoded = "1"
|
||||||
futures-util = "0.3"
|
|
22
src/main.rs
22
src/main.rs
@ -7,7 +7,6 @@ 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::{
|
||||||
@ -46,7 +45,6 @@ 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) {
|
||||||
@ -122,21 +120,10 @@ 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}");
|
||||||
type Data = Vec<serde_json::Map<String, serde_json::Value>>;
|
println!("[safebooru] trying to fetch url: {url}");
|
||||||
async fn try_request(count: usize, url: &str, http: &reqwest::Client) -> AppResult<Data> {
|
let req = http.get(url).build()?;
|
||||||
println!("[safebooru] trying to fetch url (count {count}): {url}");
|
let resp = http.execute(req).await?.error_for_status()?;
|
||||||
let req = http.get(url).build()?;
|
let data: Vec<serde_json::Map<String, serde_json::Value>> = resp.json().await?;
|
||||||
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();
|
||||||
@ -189,7 +176,6 @@ impl AppState {
|
|||||||
direct_links: Default::default(),
|
direct_links: Default::default(),
|
||||||
http: reqwest::ClientBuilder::new()
|
http: reqwest::ClientBuilder::new()
|
||||||
.redirect(reqwest::redirect::Policy::none())
|
.redirect(reqwest::redirect::Policy::none())
|
||||||
.user_agent("limbusart 0.1.0")
|
|
||||||
.build()
|
.build()
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
}),
|
}),
|
||||||
|
Loading…
Reference in New Issue
Block a user