Compare commits
No commits in common. "b7d5df6f2aade961cd3f39095c6a33986b645e13" and "807e8071da9933cdbf8cb4561ec8328c24ed8e7f" have entirely different histories.
b7d5df6f2a
...
807e8071da
29
README.md
29
README.md
@ -10,42 +10,21 @@ The Open Meteo CLI allows you to retrieve weather data for specific coordinates.
|
||||
|
||||
Get the current weather for a given coordinate.
|
||||
|
||||
```
|
||||
```plaintext
|
||||
open-meteo current-weather [OPTIONS] --latitude <LATITUDE> --longitude <LONGITUDE>
|
||||
```
|
||||
|
||||
#### Options
|
||||
|
||||
- `-l, --latitude <LATITUDE>`: Latitude as a decimal number.
|
||||
- `-L, --longitude <LONGITUDE>`: Longitude as a decimal number.
|
||||
|
||||
#### Flags
|
||||
- `-a, --all`: Prints all available parameters.
|
||||
- `-d, --is-day`: Prints if it is day or night.
|
||||
- `-t, --temperature`: Prints the decimal temperature.
|
||||
- `-w, --windspeed`: Prints the decimal wind speed.
|
||||
- `-W, --winddirection`: Prints the wind direction angle.
|
||||
- `-c, --clean`: Cleans the output and only shows the values separated by commas.
|
||||
- The order of values is as follows: is_day, temperature, windspeed, winddirection.
|
||||
|
||||
#### Examples
|
||||
- Standard usage: `open-meteo-cli current-weather -l 5 -L 5 -a`
|
||||
|
||||
```
|
||||
=== Current weather for Latitude: 5, Longitude: 5 ===
|
||||
Night
|
||||
Temperature: 28.4°C
|
||||
Wind speed: 17.0 km/h
|
||||
Wind direction: 212.0°
|
||||
=== Weather data by Open-Meteo.com ===
|
||||
```
|
||||
|
||||
- Clean: `open-meteo-cli current-weather -l 12 -L=-3.4 -d -t -w -c`
|
||||
|
||||
```
|
||||
0,26.5,15.5
|
||||
```
|
||||
- `-c, --clean`: Cleans the output and only shows the values separated by commas. The order of values is as follows: is_day, temperature, windspeed, winddirection. Example output: `1,22.4,12.5,170.0`.
|
||||
|
||||
## Attribution
|
||||
|
||||
The weather data used in this application is provided by [Open-Meteo.com](https://open-meteo.com/) under the [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) license.
|
||||
We would like to express our gratitude to Open-Meteo.com for their contribution to this project.
|
||||
The weather data used in this application is provided by [Open-Meteo.com](https://open-meteo.com/) under the [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) license. We would like to express our gratitude to Open-Meteo.com for their contribution to this project.
|
||||
|
@ -13,7 +13,6 @@ pub enum Arguments {
|
||||
#[structopt(short = "L", long, help = "Longitude as a decimal number")]
|
||||
longitude: f32,
|
||||
|
||||
// Flags
|
||||
#[structopt(short = "a", long, help = "Prints all the parameters")]
|
||||
all: bool,
|
||||
#[structopt(short = "d", long, help = "Prints if it's day or night")]
|
||||
@ -28,7 +27,8 @@ pub enum Arguments {
|
||||
short = "c",
|
||||
long,
|
||||
help = "Cleans the output and only shows the values separated by commas.
|
||||
- ORDER: is_day, temperature, windspeed, winddirection"
|
||||
- ORDER: is_day, temperature, windspeed, winddirection
|
||||
- EXAMPLE: 1,22.4,12.5,170.0"
|
||||
)]
|
||||
clean: bool,
|
||||
},
|
||||
|
@ -1,5 +1,3 @@
|
||||
use serde_json::Value;
|
||||
|
||||
pub struct CurrentWeather {
|
||||
current_weather: serde_json::Value,
|
||||
clean: bool,
|
||||
@ -13,60 +11,12 @@ impl CurrentWeather {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn extract_simple_data(
|
||||
&self,
|
||||
key: &str,
|
||||
description: &str,
|
||||
end_text: Option<&str>,
|
||||
) -> String {
|
||||
let data = self.extract_raw(key);
|
||||
self.parse_simple_data(data, description, end_text)
|
||||
}
|
||||
|
||||
pub fn extract_raw(&self, key: &str) -> &Value {
|
||||
&self.current_weather[key]
|
||||
}
|
||||
|
||||
fn parse_simple_data(&self, data: &Value, descriptor: &str, end_text: Option<&str>) -> String {
|
||||
pub fn extract_data(&self, data_name: &str, data_description: &str) -> String {
|
||||
let data = &self.current_weather[data_name];
|
||||
if self.clean {
|
||||
format!("{data}")
|
||||
} else {
|
||||
let end_text = end_text.unwrap_or("");
|
||||
format!("{descriptor}: {data}{end_text}")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_custom_data(&self, data: &Value, custom: &str) -> String {
|
||||
if self.clean {
|
||||
format!("{data}")
|
||||
} else {
|
||||
format!("{custom}")
|
||||
format!("{data_description}: {data}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_extract_data_clean() {
|
||||
let current_weather = serde_json::json!({
|
||||
"temperature": 25,
|
||||
});
|
||||
let weather = CurrentWeather::new(current_weather, true);
|
||||
assert_eq!(weather.extract_simple_data("temperature", "", None), "25");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_extract_data_not_clean() {
|
||||
let current_weather = serde_json::json!({
|
||||
"temperature": 25,
|
||||
});
|
||||
let weather = CurrentWeather::new(current_weather, false);
|
||||
assert_eq!(
|
||||
weather.extract_simple_data("temperature", "Temperature", Some("°C")),
|
||||
"Temperature: 25°C"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
26
src/main.rs
26
src/main.rs
@ -43,43 +43,25 @@ fn main() {
|
||||
let current_weather = CurrentWeather::new(current_weather_data, clean);
|
||||
|
||||
if is_day || all {
|
||||
let is_day = current_weather.extract_raw("is_day");
|
||||
if is_day == "1" {
|
||||
string_vec.push(current_weather.parse_custom_data(is_day, "Day"));
|
||||
} else {
|
||||
string_vec.push(current_weather.parse_custom_data(is_day, "Night"));
|
||||
}
|
||||
string_vec.push(current_weather.extract_data("is_day", "Is day"));
|
||||
}
|
||||
|
||||
if temperature || all {
|
||||
string_vec.push(current_weather.extract_simple_data(
|
||||
"temperature",
|
||||
"Temperature",
|
||||
Some("°C"),
|
||||
));
|
||||
string_vec.push(current_weather.extract_data("temperature", "Temperature"));
|
||||
}
|
||||
|
||||
if windspeed || all {
|
||||
string_vec.push(current_weather.extract_simple_data(
|
||||
"windspeed",
|
||||
"Wind speed",
|
||||
Some(" km/h"),
|
||||
));
|
||||
string_vec.push(current_weather.extract_data("windspeed", "Wind speed"));
|
||||
}
|
||||
|
||||
if winddirection || all {
|
||||
string_vec.push(current_weather.extract_simple_data(
|
||||
"winddirection",
|
||||
"Wind direction",
|
||||
Some("°"),
|
||||
));
|
||||
string_vec.push(current_weather.extract_data("winddirection", "Wind direction"));
|
||||
}
|
||||
|
||||
if clean {
|
||||
let final_string = string_vec.join(",");
|
||||
println!("{final_string}");
|
||||
} else {
|
||||
string_vec.push(String::from("=== Weather data by Open-Meteo.com ==="));
|
||||
let final_string = string_vec.join("\n");
|
||||
println!("{final_string}");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user