#music #youtube #tokio #google-api #client #internal #asynchronous

ytmapi-rs

使用 Google 内部 API 的异步(tokio)纯 Rust API,用于 YouTube 音乐

12 个版本

0.0.12 2024 年 8 月 17 日
0.0.11 2024 年 8 月 12 日
0.0.9 2024 年 7 月 31 日
0.0.4 2024 年 6 月 27 日
0.0.1 2023 年 12 月 3 日

#2013Web 编程

Download history 40/week @ 2024-04-26 3/week @ 2024-05-03 4/week @ 2024-05-17 9/week @ 2024-05-24 9/week @ 2024-05-31 115/week @ 2024-06-07 18/week @ 2024-06-14 43/week @ 2024-06-21 170/week @ 2024-06-28 111/week @ 2024-07-05 164/week @ 2024-07-12 254/week @ 2024-07-19 176/week @ 2024-07-26 179/week @ 2024-08-02 150/week @ 2024-08-09

每月 763 次下载
youtui 中使用

MIT 许可证

385KB
9K SLoC

关于

Ytmapi-rs - YouTube 音乐的异步 API - 使用 Google 的内部 API、Tokio 和 Reqwest。灵感来源于 https://github.com/sigma67/ytmusicapi/

此项目未得到 Google 的支持或认可。

请参阅 docs.rs 以获取文档和用法示例。


lib.rs:

ytmapi_rs

将库集成到 YouTube 音乐的内部 API 中。

示例

有关使用 builder 的其他示例,请参阅 builder 模块。

#[tokio::main]
pub async fn main() -> Result<(), ytmapi_rs::Error> {
    let cookie_path = std::path::Path::new("./cookie.txt");
    let yt = ytmapi_rs::YtMusic::from_cookie_file(cookie_path).await?;
    yt.get_search_suggestions("Beatles").await?;
    let result = yt.get_search_suggestions("Beatles").await?;
    println!("{:?}", result);
    Ok(())
}

OAuth 用法,使用工作流程和 builder 方法重用 Client

#[tokio::main]
pub async fn main() -> Result<(), ytmapi_rs::Error> {
    let client = ytmapi_rs::Client::new().unwrap();
    let (code, url) = ytmapi_rs::generate_oauth_code_and_url(&client).await?;
    println!("Go to {url}, finish the login flow, and press enter when done");
    let mut _buf = String::new();
    let _ = std::io::stdin().read_line(&mut _buf);
    let token = ytmapi_rs::generate_oauth_token(&client, code).await?;
    // NOTE: The token can be re-used until it expires, and refreshed once it has,
    // so it's recommended to save it to a file here.
    let yt = ytmapi_rs::YtMusicBuilder::new_with_client(client)
        .with_oauth_token(token)
        .build()
        .unwrap();
    let result = yt.get_search_suggestions("Beatles").await?;
    println!("{:?}", result);
    Ok(())
}

可选功能

TLS

注意:当使用标准构造函数构建多个功能时,reqwest 将优先使用 default-tls。使用 YtMusicBuilder 确保首选 TLS 的选择。有关更多信息,请参阅 reqwest 文档 https://docs.rs/reqwest/latest/reqwest/tls/index.html

  • default-tls (默认启用):使用 reqwest 的默认 TLS - 编写时是 native-tls。
  • native-tls:此功能允许使用依赖供应商 TLS 的 native-tls crate。
  • rustls-tls:此功能允许使用用 rust 编写的 rustls crate。

其他

  • simplified_queries:向 YtMusic 添加方便的方法。
  • serde_json:启用与 serde_json 的某些互操作性函数。

依赖项

~8–23MB
~395K SLoC