Move to new, DRM-free, endpoint

This commit is contained in:
bytedream 2024-04-03 15:48:15 +02:00
parent ba8028737d
commit e694046b07
8 changed files with 245 additions and 331 deletions

View file

@ -1,17 +1,17 @@
use anyhow::{bail, Result};
use crunchyroll_rs::media::{Resolution, Stream, VariantData};
use crunchyroll_rs::media::{Resolution, Stream, StreamData};
use crunchyroll_rs::Locale;
pub async fn variant_data_from_stream(
stream: &Stream,
resolution: &Resolution,
subtitle: Option<Locale>,
) -> Result<Option<(VariantData, VariantData, bool)>> {
) -> Result<Option<(StreamData, StreamData, bool)>> {
// sometimes Crunchyroll marks episodes without real subtitles that they have subtitles and
// reports that only hardsub episode are existing. the following lines are trying to prevent
// potential errors which might get caused by this incorrect reporting
// (https://github.com/crunchy-labs/crunchy-cli/issues/231)
let mut hardsub_locales = stream.streaming_hardsub_locales();
let mut hardsub_locales: Vec<Locale> = stream.hard_subs.keys().cloned().collect();
let (hardsub_locale, mut contains_hardsub) = if !hardsub_locales
.contains(&Locale::Custom("".to_string()))
&& !hardsub_locales.contains(&Locale::Custom(":".to_string()))
@ -29,39 +29,29 @@ pub async fn variant_data_from_stream(
(subtitle, hardsubs_requested)
};
let mut streaming_data = match stream.dash_streaming_data(hardsub_locale).await {
let (mut videos, mut audios) = match stream.stream_data(hardsub_locale).await {
Ok(data) => data,
Err(e) => {
// the error variant is only `crunchyroll_rs::error::Error::Input` when the requested
// hardsub is not available
if let crunchyroll_rs::error::Error::Input { .. } = e {
contains_hardsub = false;
stream.dash_streaming_data(None).await?
stream.stream_data(None).await?
} else {
bail!(e)
}
}
};
streaming_data
.0
.sort_by(|a, b| a.bandwidth.cmp(&b.bandwidth).reverse());
streaming_data
.1
.sort_by(|a, b| a.bandwidth.cmp(&b.bandwidth).reverse());
}
.unwrap();
videos.sort_by(|a, b| a.bandwidth.cmp(&b.bandwidth).reverse());
audios.sort_by(|a, b| a.bandwidth.cmp(&b.bandwidth).reverse());
let video_variant = match resolution.height {
u64::MAX => Some(streaming_data.0.into_iter().next().unwrap()),
u64::MIN => Some(streaming_data.0.into_iter().last().unwrap()),
_ => streaming_data
.0
u64::MAX => Some(videos.into_iter().next().unwrap()),
u64::MIN => Some(videos.into_iter().last().unwrap()),
_ => videos
.into_iter()
.find(|v| resolution.height == v.resolution.height),
.find(|v| resolution.height == v.resolution().unwrap().height),
};
Ok(video_variant.map(|v| {
(
v,
streaming_data.1.first().unwrap().clone(),
contains_hardsub,
)
}))
Ok(video_variant.map(|v| (v, audios.first().unwrap().clone(), contains_hardsub)))
}