#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

new 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

#1206 in Web programming

Download history 186/week @ 2024-07-29 158/week @ 2024-08-05 320/week @ 2024-08-12 37/week @ 2024-08-19 27/week @ 2024-08-26 176/week @ 2024-09-02 14/week @ 2024-09-09 30/week @ 2024-09-16 53/week @ 2024-09-23 81/week @ 2024-09-30 11/week @ 2024-10-07 11/week @ 2024-10-14 278/week @ 2024-10-21 64/week @ 2024-10-28 21/week @ 2024-11-04 16/week @ 2024-11-11

380 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
~390K SLoC