#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

15 releases

new 0.0.15 Oct 26, 2024
0.0.14 Oct 24, 2024
0.0.13 Sep 4, 2024
0.0.12 Aug 17, 2024
0.0.1 Dec 3, 2023

#1133 in Web programming

Download history 116/week @ 2024-07-06 240/week @ 2024-07-13 181/week @ 2024-07-20 180/week @ 2024-07-27 172/week @ 2024-08-03 164/week @ 2024-08-10 198/week @ 2024-08-17 23/week @ 2024-08-24 167/week @ 2024-08-31 30/week @ 2024-09-07 29/week @ 2024-09-14 44/week @ 2024-09-21 89/week @ 2024-09-28 14/week @ 2024-10-05 11/week @ 2024-10-12 129/week @ 2024-10-19

250 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

~8–24MB
~395K SLoC