aboutsummaryrefslogtreecommitdiff
path: root/src/utils/weather.rs
blob: b284807960035bad30adcc1eb5fd9cf0f086d63f (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
use crate::config::CONFIG;
use crate::types::ThreadsData;

// will make a GET request from wttr.in
pub async fn get_weather() -> ThreadsData {
    let format = if CONFIG.weather.format.is_empty() {
        String::from("%l:+%t")
    } else {
        CONFIG.weather.format.clone()
    };

    let url = format!("http://wttr.in/{}?format=\"{}", CONFIG.weather.city, format);
    let err_string = String::from("Error");
    let res = match minreq::get(url).send() {
        Ok(resp) => {
            if resp.status_code != 200 {
                String::from("service error")
            } else {
                match resp.as_str() {
                    Ok(res_str) => res_str.trim_matches('"').to_string(),
                    Err(_) => err_string,
                }
            }
        }
        Err(_) => err_string,
    };

    let data = format!("  {}  {}  {}", CONFIG.weather.icon, res, CONFIG.seperator);
    ThreadsData::Weather(data)
}