2个版本
0.9.1 | 2024年7月21日 |
---|---|
0.9.0 | 2024年7月21日 |
#133 在 压缩
每月 235 次下载
44KB
610 行
newsdata-io-api
此crate提供Newsdata.io API的Rust客户端。
安装
将以下内容添加到您的 Cargo.toml
[dependencies]
newsdata-io-api = "0.9.0"
用法
-
获取API密钥您可以从Newsdata.io获取免费的API密钥。
-
创建NewsdataIO实例
use newsdata_io_api::{NewsdataIO, Auth};
// Replace "YOUR_API_KEY" with your actual API key
let newsdata_io = NewsdataIO::new(Auth::new("YOUR_API_KEY".to_string()));
- 发送API请求NewsdataIO实例提供各种API请求的方法,包括以下内容
- 最新新闻
- 加密新闻
- 新闻存档
- 新闻来源
示例
获取最新新闻
use newsdata_io_api::{NewsdataIO, Auth, LatestNews, GetLatestNewsParams, Flag};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Replace "YOUR_API_KEY" with your actual API key
let newsdata_io = NewsdataIO::new(Auth::new("YOUR_API_KEY".to_string()));
// Get the latest news articles for the US in the business category
let response = newsdata_io
.get_latest(
&GetLatestNewsParams {
country: Some(vec!["us".to_string()]),
category: Some(vec!["business".to_string()]),
size: Some(10), // Get the top 10 articles
full_content: Some(Flag::True), // Include full content
..Default::default()
},
).unwrap();
println!("{}", response);
Ok(())
}
获取加密新闻
use newsdata_io_api::{NewsdataIO, Auth, CryptoNews, GetCryptoNewsParams, Flag};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Replace "YOUR_API_KEY" with your actual API key
let newsdata_io = NewsdataIO::new(Auth::new("YOUR_API_KEY".to_string()));
// Get the latest news articles for the US in the business category
let response = newsdata_io
.get_crypto_news(
&GetCryptoNewsParams {
coin: Some(vec!["BTC".to_string()]),
size: Some(10), // Get the top 10 articles
full_content: Some(Flag::True), // Include full content
..Default::default()
},
).unwrap();
println!("{}", response);
Ok(())
}
获取新闻存档
use newsdata_io_api::{NewsdataIO, Auth, NewsArchive, GetNewsArchiveParams};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Replace "YOUR_API_KEY" with your actual API key
let newsdata_io = NewsdataIO::new(Auth::new("YOUR_API_KEY".to_string()));
// Get the latest news articles for the US in the business category
let response = newsdata_io
.get_news_archive(
&GetNewsArchiveParams {
q: "business".to_string(),
..Default::default()
},
).unwrap();
println!("{}", response);
Ok(())
}
获取新闻来源
use newsdata_io_api::{NewsdataIO, Auth, NewsSources, GetNewsSourcesParams};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Replace "YOUR_API_KEY" with your actual API key
let newsdata_io = NewsdataIO::new(Auth::new("YOUR_API_KEY".to_string()));
// Get the latest news articles for the US in the business category
let response = newsdata_io
.get_news_sources(
&GetNewsSourcesParams {
country: Some(vec!["us".to_string()]),
..Default::default()
},
).unwrap();
println!("{}", response);
Ok(())
}
依赖项
~4–15MB
~217K SLoC