aboutsummaryrefslogtreecommitdiff
path: root/src/hooks/wa.rs
blob: b6d4eff497e80841709e1f83be81711cb4a3c275 (plain)
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
use anyhow::{bail, Context, Error, Result};
use reqwest::{get, Url};
use serde::{Deserialize, Serialize};
use serde_json::Result as SerdeJsonResult;
use tracing::trace;

#[derive(Serialize, Deserialize, Debug)]
struct WaResult {
    queryresult: QueryResult,
}

#[derive(Serialize, Deserialize, Debug)]
struct QueryResult {
    pods: Vec<Pod>,
}

#[derive(Serialize, Deserialize, Debug)]
struct Pod {
    title: String,
    id: String,
    subpods: Vec<SubPod>,
}

#[derive(Serialize, Deserialize, Debug)]
struct SubPod {
    title: String,
    plaintext: String,
}

fn parse_json(str_data: &str) -> SerdeJsonResult<WaResult> {
    let w: WaResult = serde_json::from_str(str_data)?;
    Ok(w)
}

#[tracing::instrument]
async fn wa_query(query_str: &str) -> Result<String, Error> {
    let app_id = "XXX"; // TODO: Get from env
    let api_url = format!(
        "http://api.wolframalpha.com/v2/query?input={}&appid={}&output=json",
        query_str, app_id
    );

    let body = get(Url::parse(&api_url).context("Failed to parse url")?)
        .await
        .context("Failed to make request")?
        .text()
        .await
        .context("failed to get request response text")?;

    let full_wa_res = parse_json(&body)?;
    trace!("got full_wa_res: {:?}", full_wa_res);

    let pod_plaintexts = full_wa_res.queryresult.pods
        .iter()
        .filter(|it| it.id.to_lowercase() != "input")
        .map(|pod| {
            let subpod_texts = pod
                .subpods
                .iter()
                .map(|subpod| subpod.plaintext.clone())
                .collect::<Vec<String>>()
                .join(", ");

            format!("{}: {}", &pod.title, subpod_texts)
        })
        .collect::<Vec<String>>()
        .join(" - ");

    Ok(pod_plaintexts)
}

#[cfg(test)]
mod tests {

    use super::wa_query;
    use anyhow::{Error, Result};

    #[tokio::test]
    // async fn test_query_result_json_parsing() -> Result<(), Error> {
    async fn test_query_result_json_parsing() {
        let res = wa_query("weather graz").await.unwrap();
        assert_eq!(res, "asdf");
    }
}