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

ytmapi-rs

An asynchronous (tokio) pure Rust API for Youtube Music using Google's internal API

16 releases

0.0.16 Nov 18, 2024
0.0.15 Oct 26, 2024
0.0.13 Sep 4, 2024
0.0.12 Aug 17, 2024
0.0.1 Dec 3, 2023

#1277 in Web programming

Download history 25/week @ 2024-08-22 131/week @ 2024-08-29 67/week @ 2024-09-05 30/week @ 2024-09-12 37/week @ 2024-09-19 75/week @ 2024-09-26 37/week @ 2024-10-03 11/week @ 2024-10-10 9/week @ 2024-10-17 308/week @ 2024-10-24 38/week @ 2024-10-31 21/week @ 2024-11-07 178/week @ 2024-11-14 64/week @ 2024-11-21 37/week @ 2024-11-28 32/week @ 2024-12-05

315 downloads per month
Used in youtui

MIT license

430KB
10K SLoC

About

Ytmapi-rs - an asynchronous API for youtube music - using Google's internal API, Tokio and Reqwest. Inspired by https://github.com/sigma67/ytmusicapi/.

This project is not supported or endorsed by Google.

Please refer to docs.rs for documentation and usage examples.


lib.rs:

ytmapi_rs

Library into YouTube Music's internal API.

Examples

For additional examples using builder, see builder module.

#[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 usage, using the workflow, and builder method to re-use the 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(())
}

Optional Features

TLS

NOTE: reqwest will prefer to utilise default-tls if multiple features are built when using the standard constructors. Use YtMusicBuilder to ensure the preferred choice of TLS is used. See reqwest docs for more information https://docs.rs/reqwest/latest/reqwest/tls/index.html.

  • default-tls (enabled by default): Utilises the default TLS from reqwest - at the time of writing is native-tls.
  • native-tls: This feature allows use of the the native-tls crate, reliant on vendors tls.
  • rustls-tls: This feature allows use of the rustls crate, written in rust.

Other

  • simplified_queries: Adds convenience methods to YtMusic.
  • serde_json: Enables some interoperability functions with serde_json.

Dependencies

~9–24MB
~389K SLoC