#news #api #api-key #io #latest #source #newsdata

newsdata-io-api

Newsdata.io API的Rust绑定。

2个版本

0.9.1 2024年7月21日
0.9.0 2024年7月21日

#133压缩

Download history 189/week @ 2024-07-16 42/week @ 2024-07-23 3/week @ 2024-07-30 1/week @ 2024-08-06

每月 235 次下载

MIT 许可证

44KB
610

newsdata-io-api

此crate提供Newsdata.io API的Rust客户端。

安装

将以下内容添加到您的 Cargo.toml

[dependencies]
newsdata-io-api = "0.9.0"

用法

  1. 获取API密钥您可以从Newsdata.io获取免费的API密钥。

  2. 创建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()));
  1. 发送API请求NewsdataIO实例提供各种API请求的方法,包括以下内容
    1. 最新新闻
    2. 加密新闻
    3. 新闻存档
    4. 新闻来源

示例

获取最新新闻

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