DEVELOPMENT ENVIRONMENT

~liljamo/wallhavenapirandom

ref: e5033534b8695fde5f6325ee9303a9d18d27390c wallhavenapirandom/src/main.rs -rw-r--r-- 2.8 KiB
e5033534Jonni Liljamo feat: get api key from env 1 year, 8 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
98
99
100
101
102
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}")]
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 = env!("WH_API_KEY");

    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}"
    );

    log::info!("doing the thing with args 'atl={atl} rats={rats} pur={pur} cats={cats} q={q}'");

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

    match search_resp {
        Err(err) => {
            log::error!(
                "oh nej! we hast failed! like, in the 'search_resp' thingy. err is '{err}'"
            );
        }
        Ok(search_resp) => {
            let json = search_resp.json::<WHSearchRes>().await;

            match json {
                Err(err) => {
                    log::error!("rövhål! failed in the 'json' match. err is '{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<()> {
    log::info!("startedth uppeth");
    HttpServer::new(|| App::new().service(random))
        .bind(("0.0.0.0", 3000))?
        .run()
        .await
}