Friendlier weather output
This commit is contained in:
parent
7fefcbac46
commit
5e0ca7232a
2 changed files with 39 additions and 12 deletions
|
|
@ -110,7 +110,7 @@ impl Store {
|
|||
}
|
||||
|
||||
fn fetch_weather(location: &str) -> Result<String, String> {
|
||||
let path = format!("/{}?format=%l:+%C+%t+feels+%f+%w+%h&m", url_encode(location));
|
||||
let path = format!("/{}?format=%l|%c|%C|%t|%f|%w|%h&m", url_encode(location));
|
||||
let mut last = "no response".to_string();
|
||||
for attempt in 0..3 {
|
||||
if attempt > 0 {
|
||||
|
|
@ -118,16 +118,9 @@ fn fetch_weather(location: &str) -> Result<String, String> {
|
|||
}
|
||||
match https_get("wttr.in", &path) {
|
||||
Ok(body) => {
|
||||
let line: String = body
|
||||
.lines()
|
||||
.map(str::trim)
|
||||
.find(|l| !l.is_empty())
|
||||
.unwrap_or("")
|
||||
.chars()
|
||||
.take(250)
|
||||
.collect();
|
||||
if !line.is_empty() {
|
||||
return Ok(line);
|
||||
let raw = body.lines().map(str::trim).find(|l| !l.is_empty()).unwrap_or("");
|
||||
if !raw.is_empty() {
|
||||
return Ok(format_weather(raw, location));
|
||||
}
|
||||
last = "empty response".to_string();
|
||||
}
|
||||
|
|
@ -143,6 +136,24 @@ fn fetch_weather(location: &str) -> Result<String, String> {
|
|||
Err(last)
|
||||
}
|
||||
|
||||
pub fn format_weather(raw: &str, location: &str) -> String {
|
||||
let f: Vec<&str> = raw.split('|').map(str::trim).collect();
|
||||
if f.len() >= 7 {
|
||||
let temp = f[3].trim_start_matches('+');
|
||||
let feels = f[4].trim_start_matches('+');
|
||||
let msg = format!(
|
||||
"{} {}: {}, {} (feels like {}) · 💨 {} · 💧 {}",
|
||||
f[1], f[0], f[2], temp, feels, f[5], f[6]
|
||||
);
|
||||
return msg.chars().take(250).collect();
|
||||
}
|
||||
let low = raw.to_lowercase();
|
||||
if low.contains("not found") || low.contains("unknown location") {
|
||||
return format!("couldn't find '{location}' — try a city name or postcode");
|
||||
}
|
||||
raw.chars().take(250).collect()
|
||||
}
|
||||
|
||||
struct HttpErr {
|
||||
msg: String,
|
||||
retry: bool,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue