-
Notifications
You must be signed in to change notification settings - Fork 13
/
stream-episode.rs
37 lines (31 loc) · 1.19 KB
/
stream-episode.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use anyhow::Result;
use crunchyroll_rs::{Crunchyroll, Episode};
use std::env;
use std::io::Write;
#[tokio::main]
async fn main() -> Result<()> {
let email = env::var("EMAIL").expect("'EMAIL' environment variable not found");
let password = env::var("PASSWORD").expect("'PASSWORD' environment variable not found");
let crunchyroll = Crunchyroll::builder()
.login_with_credentials(email, password)
.await?;
let episode: Episode = crunchyroll.media_from_id("GRDKJZ81Y").await?;
let stream = episode.stream().await?;
let mut stream_data = stream.stream_data(None).await?.unwrap();
// sort after resolutions; best to worst
stream_data.video.sort_by(|a, b| {
a.resolution()
.unwrap()
.width
.cmp(&b.resolution().unwrap().width)
.reverse()
});
// get video segments of the stream with the best available resolution
let segments = stream_data.video[0].segments();
let sink = &mut std::io::sink();
for (i, segment) in segments.iter().enumerate() {
println!("Downloading segment {} of {}", i + 1, segments.len() + 1);
sink.write_all(&segment.data().await?)?;
}
Ok(())
}