DEVELOPMENT ENVIRONMENT

~liljamo/wallhavenapirandom

ref: 1ef5d361fe64d521257b4f855bc4069e69053264 wallhavenapirandom/src/main.rs -rw-r--r-- 2.5 KiB
1ef5d361Jonni Liljamo feat: initial version 1 year, 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use actix_web::{self, get, web, App, HttpResponse, HttpServer, Responder};
use rand::seq::SliceRandom;
use reqwest;
use serde::Deserialize;

#[derive(Deserialize)]
struct RandomArgs {
    /// e.g. 2560x1440
    atleast: String,
    /// e.g. 16x9
    /// e.g. 16x9
    /// or 16x9,16x10 for both, etc
    ratios: String,
    /// purity
    /// 100 sfw
    /// 010 sketchy
    /// 001 nsfw
    /// can combine
    /// e.g. 111 for all
    purity: String,
    /// categories
    /// 100 general
    /// 010 anime
    /// 001 people
    /// can combine
    /// e.g. 101 for no anime
    categories: String,
    /// query
    /// can be kinda anything
    /// check https://wallhaven.cc/help/api#search
    /// for shid
    q: String,
    /// api key from wallhaven.cc
    api_key: String,
}

#[derive(Deserialize)]
struct WHImgInfo {
    pub path: String,
}

#[derive(Deserialize)]
struct WHSearchRes {
    pub data: Vec<WHImgInfo>,
}

/// get redirected to random img
/// e.g. localhost:3000/random/2560x1440/16x9/100/001/+forest/APIKEY
#[get("/random/{atleast}/{ratios}/{purity}/{categories}/{q}/{api_key}")]
async fn random(args: web::Path<RandomArgs>) -> impl Responder {
    let atl = args.atleast.clone();
    let rats = args.ratios.clone();
    let pur = args.purity.clone();
    let cats = args.categories.clone();
    let q = args.q.clone();
    let api_key = args.api_key.clone();

    let wh_url = format!(
        "https://wallhaven.cc/api/v1/search?atleast={atl}&ratios={rats}&purity={pur}&categories={cats}&sorting=random&q={q}&apikey={api_key}"
    );

    let search_resp = reqwest::get(wh_url).await;

    match search_resp {
        Err(err) => {
            println!("search_resp: {}", err);
        }
        Ok(search_resp) => {
            let json = search_resp.json::<WHSearchRes>().await;

            match json {
                Err(err) => {
                    println!("json: {}", err);
                }
                Ok(res) => {
                    let imgs = res.data;
                    let url = imgs.choose(&mut rand::thread_rng()).unwrap();
                    return HttpResponse::TemporaryRedirect()
                        .append_header(("Location", url.path.clone()))
                        .finish();
                }
            }
        }
    }

    HttpResponse::TemporaryRedirect()
        .append_header(("Location", "https://http.cat/510"))
        .finish()
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new().service(random))
        .bind(("127.0.0.1", 3000))?
        .run()
        .await
}