Expand description
§ytmapi_rs
Library into YouTube Music’s internal API.
§Examples
For additional examples using builder, see builder
module.
§Basic usage with a pre-created cookie file.
#[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
.
Modules§
- Available authorisation tokens.
- Builder implementation for YtMusic, to allow more complicated construction.
- This module contains the basic HTTP client used in this library.
- Re-usable core structures.
- This module contains the
Continuable
trait, allowing streaming of results that contain continuations. - Module to contain code related to errors that could be produced by the API.
- This module contains the representation of Json exposed in the default public API in this library.
- Results from parsing Innertube queries.
- Type safe queries to pass to the API, and the traits to allow you to implement new ones.
- simplified_
queries simplified-queries
This module contains the implementation for more convenient ways to call the API, in many cases without the need of building Query structs. This module contains purely additional implementations for YtMusic. To see the documentation, refer to theYtMusic
documentation itself.
Structs§
- Basic HTTP client using TLS wrapping a
reqwest::Client
, with the minimum required features to call YouTube Music queries. Clone is low cost, internals ofreqwest::Client
are wrapped in an Arc. - This type represents all errors this API could produce.
- A result from the api that has been checked for errors and processed into JSON.
- The raw result of a query to the API.
- A handle to the YouTube Music API, wrapping a http client. Generic over AuthToken, as different AuthTokens may allow different queries to be executed. It is recommended to re-use these as they internally contain a connection pool.
- Builder to build more complex YtMusic.
Functions§
- Generates a Browser Token when given a browser cookie. This requires a
Client
to run. - Generates a tuple containing fresh OAuthDeviceCode and corresponding url for you to authenticate yourself at. This requires a
Client
to run. (OAuthDeviceCode, URL) - Generates an OAuth Token when given an OAuthDeviceCode. This requires a
Client
to run. - Process a string of JSON as if it had been directly received from the api for a query. Note that this is generic across AuthToken, and you may need to provide the AuthToken type using ‘turbofish’.
Type Aliases§
- Alias for a Result with the error type ytmapi-rs::Error.