Skip to main content

ac_rustube/blocking/
mod.rs

1//! Blocking wrappers for using `rustube` in a synchronous context.
2//!
3//! Downloading videos with the blocking API works exactly like described for the asynchronous API 
4//! in the [`crate`] documentation, and in the [`Video`](crate::Video) documentation, except for the
5//! last step:
6//! ```no_run
7//!# use rustube::blocking::Video;
8//!# use url::Url;
9//!# fn main() -> Result<(), Box<dyn std::error::Error>> {
10//! let url = Url::parse("https://youtube.com/watch?iv=5jlI4uzZGjU")?;
11//! let path_to_video = Video::from_url(&url)?
12//!    .best_quality()
13//!    .unwrap()
14//!    .blocking_download()?;
15//!#  Ok(())
16//!# }
17//!```
18//!   
19//! As you can see, there's no corresponding synchronous version of [`Stream`](crate::Stream), but
20//! only a few methods prefixed with `blocking_`. This is not the most beautiful solution and may 
21//! change in the future. 
22//!  
23//! Another option is using the [`block`] macro:
24//! ```no_run
25//!# use rustube::blocking::Video;
26//!# use rustube::block;
27//!# use url::Url;
28//!# fn main() -> Result<(), Box<dyn std::error::Error>> {
29//! let url = Url::parse("https://youtube.com/watch?iv=5jlI4uzZGjU")?;
30//! let video = Video::from_url(&url)?;
31//! let best_quality = video.best_quality().unwrap();
32//!  
33//! let path_to_video = block!(best_quality.download());
34//!#  Ok(())
35//!# }
36//!```
37//!    
38//!This macro will utilize the [`Runtime`](tokio::runtime::Runtime) created for you by `rustube`, 
39//! and block on the provided future (You can also use it for other asynchronous stuff, not related 
40//! to `rustube`).
41
42use once_cell::sync::Lazy;
43
44use tokio::runtime::Runtime;
45
46#[doc(inline)]
47#[cfg(feature = "descramble")]
48pub use descrambler::VideoDescrambler;
49#[doc(inline)]
50#[cfg(feature = "fetch")]
51pub use fetcher::VideoFetcher;
52#[doc(inline)]
53#[cfg(feature = "descramble")]
54pub use video::Video;
55
56/// A [`Runtime`](tokio::runtime::Runtime) for executing asynchronous code. 
57pub static RT: Lazy<Runtime> = Lazy::new(||
58    Runtime::new().expect("Unable to start the tokio Runtime")
59);
60
61/// A convenient macro for executing asynchronous code in a synchronous context.
62#[macro_export]
63#[cfg(feature = "blocking")]
64macro_rules! block {
65    (async $future:block) => { $crate::blocking::RT.block_on(async $future) };
66    (async move $future:block) => { $crate::blocking::RT.block_on(async move $future) };
67    ($future:expr) => {
68        $crate::blocking::RT.block_on(async {
69            $future.await
70        })
71    };
72}
73
74#[doc(hidden)]
75#[cfg(feature = "fetch")]
76pub mod fetcher;
77#[doc(hidden)]
78#[cfg(feature = "descramble")]
79pub mod descrambler;
80#[doc(hidden)]
81#[cfg(feature = "descramble")]
82pub mod video;
83
84
85/// A synchronous wrapper around [`download_best_quality`](crate::download_best_quality).
86#[inline]
87#[cfg(all(feature = "download", feature = "regex"))]
88pub fn download_best_quality(video_identifier: &str) -> crate::Result<std::path::PathBuf> {
89    block!(crate::download_best_quality(video_identifier))
90}
91
92/// A synchronous wrapper around [`download_worst_quality`](crate::download_worst_quality).
93#[inline]
94#[cfg(all(feature = "download", feature = "regex"))]
95pub fn download_worst_quality(video_identifier: &str) -> crate::Result<std::path::PathBuf> {
96    block!(crate::download_worst_quality(video_identifier))
97}