mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 12:12:00 -06:00
Simplify retry segment download
This commit is contained in:
parent
c2ae622d01
commit
d0681c7f6c
1 changed files with 19 additions and 43 deletions
|
|
@ -4,10 +4,10 @@ use crunchyroll_rs::media::{Resolution, VariantData, VariantSegment};
|
||||||
use indicatif::{ProgressBar, ProgressFinish, ProgressStyle};
|
use indicatif::{ProgressBar, ProgressFinish, ProgressStyle};
|
||||||
use log::{debug, LevelFilter};
|
use log::{debug, LevelFilter};
|
||||||
use std::borrow::{Borrow, BorrowMut};
|
use std::borrow::{Borrow, BorrowMut};
|
||||||
use std::time::Duration;
|
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::sync::{mpsc, Arc, Mutex};
|
use std::sync::{mpsc, Arc, Mutex};
|
||||||
|
use std::time::Duration;
|
||||||
use tokio::task::JoinSet;
|
use tokio::task::JoinSet;
|
||||||
|
|
||||||
pub fn find_resolution(
|
pub fn find_resolution(
|
||||||
|
|
@ -77,54 +77,30 @@ pub async fn download_segments(
|
||||||
let thread_count = count.clone();
|
let thread_count = count.clone();
|
||||||
join_set.spawn(async move {
|
join_set.spawn(async move {
|
||||||
for (i, segment) in thread_segments.into_iter().enumerate() {
|
for (i, segment) in thread_segments.into_iter().enumerate() {
|
||||||
let response_res = thread_client
|
let mut retry_count = 0;
|
||||||
.get(&segment.url)
|
let mut buf = loop {
|
||||||
.timeout(Duration::from_secs(60u64))
|
let response = thread_client
|
||||||
.send()
|
|
||||||
.await;
|
|
||||||
let verfified_response = match response_res {
|
|
||||||
Ok(x) => x,
|
|
||||||
Err(y) => panic!("This is likely a netowrking error: {}", y),
|
|
||||||
};
|
|
||||||
let possible_error_in_response = verfified_response.bytes().await;
|
|
||||||
let mut buf = if let Ok(r) = possible_error_in_response {
|
|
||||||
r.to_vec()
|
|
||||||
} else {
|
|
||||||
debug!(
|
|
||||||
"Segment Failed to download: {}, retrying.",
|
|
||||||
num + (i * cpus)
|
|
||||||
);
|
|
||||||
let mut resp = thread_client
|
|
||||||
.get(&segment.url)
|
.get(&segment.url)
|
||||||
.timeout(Duration::from_secs(60u64))
|
.timeout(Duration::from_secs(10))
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap();
|
||||||
.bytes()
|
|
||||||
.await;
|
match response.bytes().await {
|
||||||
if resp.is_err() {
|
Ok(b) => break b.to_vec(),
|
||||||
let mut retry_ctr = 1;
|
Err(e) => {
|
||||||
loop {
|
if e.is_body() {
|
||||||
debug!(
|
if retry_count == 5 {
|
||||||
"Segment Failed to download: {}, retry {}.",
|
panic!("Max retry count reached ({}), multiple errors occured while receiving segment {}: {}", retry_count, num + (i * cpus), e)
|
||||||
num + (i * cpus),
|
}
|
||||||
retry_ctr
|
debug!("Failed to download segment {}. Retrying ({} out of 5 retries left)", num + (i * cpus), 5 - retry_count)
|
||||||
);
|
} else {
|
||||||
resp = thread_client
|
panic!("{}", e)
|
||||||
.get(&segment.url)
|
|
||||||
.timeout(Duration::from_secs(60u64))
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.bytes()
|
|
||||||
.await;
|
|
||||||
if resp.is_ok() {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
retry_ctr += 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
resp.unwrap().to_vec()
|
|
||||||
|
retry_count += 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
buf = VariantSegment::decrypt(buf.borrow_mut(), segment.key)?.to_vec();
|
buf = VariantSegment::decrypt(buf.borrow_mut(), segment.key)?.to_vec();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue