#weather #api #api-wrapper #data #open-meteo #accessing #forecast

open-meteo-api

一个简单的 open-meteo API 包装器,用于获取天气数据

1 个不稳定版本

0.1.4 2024年6月21日
0.1.3 2023年10月6日
0.1.2 2023年10月6日
0.1.1 2023年10月6日
0.1.0 2023年10月6日

#1873 in 网页开发

Download history 4/week @ 2024-05-19 9/week @ 2024-06-02 1/week @ 2024-06-09 126/week @ 2024-06-16 21/week @ 2024-06-23 20/week @ 2024-06-30

每月 266 次下载

MIT 许可证

25KB
513

open-meteo-rust

用 Rust 编写的 open-meteo API 简单包装器

use std::error::Error;
use open_meteo_api::query::OpenMeteo;
use open_meteo_api::models::TimeZone;

// how to use

async fn example() -> Result<(), Box<dyn Error>> {

    // parsed json with (almost) all data you may need
    // for more info see open-meteo.com/en/docs
    // sign up to get a free api key here https://geocode.maps.co/

    let data1 = OpenMeteo::new() 
            .location("London", "your api key").await? // add location
            .forecast_days(10)?  // add forecast data
            .current_weather()?  // add current weather data
            .past_days(10)? // add past days data
            .time_zone(TimeZone::EuropeLondon)? // set time zone for using .daily()
            .hourly()? // add hourly weather data
            .daily()? // add daily weather data
            .query()
            .await?;

    // using start date and end date

    let data2 = OpenMeteo::new()
            .coordinates(51.0, 0.0)? // you can also use .coordinates(lat, lon) to set location
            .start_date("2023-09-01")?
            .end_date("2023-09-10")?
            .time_zone(TimeZone::EuropeLondon)?
            .hourly()?
            .daily()?
            .query()
            .await?;

    // accessing data fields
    // current_weather, hourly_units, hourly, daily_units, daily have Option type
    // fields of ".hourly" and ".daily" have Vec<Option<T>> type
    
    let temperature = data1.current_weather.unwrap().temperature;
    let temperature_2m = data2.hourly.unwrap().temperature_2m;

    println!("{}", temperature );
    println!("{:?}", temperature_2m);
        
    Ok(())
}

依赖关系

~6–17MB
~250K SLoC