extern crate hls_m3u8;
#[macro_use]
extern crate log;
extern crate playlist_decoder;
extern crate reqwest;
extern crate url;
extern crate serde;
extern crate serde_json;
mod decodeerror;
mod lat_long;
mod request;
mod request_error;
mod streamcheck;
mod streamcheckerror;
mod streamcheckresult;
mod streaminfo;
mod external_check_result;
mod http_config;
use std::thread;
use std::time::Duration;
pub use decodeerror::DecodeError;
pub use external_check_result::StreamResult;
pub use http_config::extract_from_homepage_async;
#[cfg(feature = "blocking")]
pub use http_config::extract_from_homepage;
pub use http_config::MetaInfoFile;
pub use lat_long::LatLong;
use streamcheckerror::StreamCheckError;
use streamcheckresult::StreamCheckResult;
pub use streamcheckresult::UrlType;
pub use streaminfo::StreamInfo;
#[cfg(feature = "blocking")]
pub fn check(
url: &str,
timeout: u32,
agent: &str,
max_depth: u8,
max_retries: u8,
) -> StreamResult {
use tokio::runtime::Runtime;
let rt = Runtime::new().unwrap();
rt.block_on(async {
check_async(&url, timeout, agent, max_depth, max_retries).await
})
}
pub async fn check_async(
url: &str,
timeout: u32,
agent: &str,
max_depth: u8,
max_retries: u8,
) -> StreamResult {
let mut retries: u8 = 0;
let mut log_lines = vec![];
loop {
let mut urllist = vec![url.to_string()];
let mut already_checked = vec![];
debug!("Check retry {}", retries);
for depth in 1..max_depth {
debug!("Check depth {}", depth);
for url in urllist.clone() {
if already_checked.contains(&url) {
continue;
}
debug!("Check url '{}'", url);
log_lines.push(format!("Checking '{}' (retry {})", url, retries));
already_checked.push(url.clone());
let result = streamcheck::check(&mut log_lines, &url, agent, timeout).await;
match &result.info {
Ok(info) => match info {
UrlType::Stream(info) => {
return StreamResult::new(result.url(), Some(info.to_owned()), log_lines);
},
_ => {}
},
_ => {}
};
match &result.info {
Ok(info) => match info {
UrlType::Playlist(list) => {
log_lines.push(format!("Found playlist at '{}'", url));
for url in list.iter() {
log_lines.push(format!("Adding url '{}' to check list", url));
}
urllist.extend(list.clone());
debug!("Urllist updated: {:?}", urllist);
}
_ => {}
},
_ => {}
};
}
}
retries += 1;
if retries > max_retries {
break;
}
thread::sleep(Duration::from_secs(1));
}
return StreamResult::new(url, None, log_lines);
}