Expand description
Retrieve current weather conditions and 7-days forecast for Canadian cities from the Meteorological Service of Canada (MSC).
§Description
Provides a CityPageStream to asynchronously recieive weather and forecast data from MSC as
they are published. Initialize by providing a site and Language to
subscribe to.
A convenience function, get_latest is also provided, and can be used to retrieve the latest
data for a specific location and language. This is also called when initially polling the
CityPageStream so weather data is immediately available instead of having to wait for new
data to be published.
Polling CityPageStream returns a url to the xml file. Once retrieved, this can be
deserialized into SiteData.
§Basic usage
use futures_util::stream::StreamExt;
use quick_xml::de::from_str;
use msc_citypage::{CityPageStream, Error, Language, SiteData, sites::Ontario};
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
// Initialize the stream for English weather data from Toronto, Ontario
let mut stream = CityPageStream::new(Ontario::Toronto, Language::English).await?;
while let Some(result) = stream.next().await {
match result {
Ok(url) => {
println!("Received: {}", url);
let response = reqwest::get(url).await?;
let xml_str = response.text().await?;
// Deserialize the xml
match from_str::<SiteData>(&xml_str) {
Ok(site_data) => println!("{:?}", site_data),
Err(e) => eprintln!("Deserialization error: {}", e),
}
}
Err(e) => eprintln!("Stream error: {}", e),
}
}
Ok(())
}§Data source
- Environment and Climate Change Canada
Re-exports§
pub use client::latest::get_latest;pub use client::stream::CityPageStream;pub use error::Error;pub use models::site_data::SiteData;