From 441ff411194a462ee49b807a2ff5ec63ce2f13f5 Mon Sep 17 00:00:00 2001 From: OMGeeky Date: Fri, 7 Apr 2023 16:53:33 +0200 Subject: [PATCH] tests --- .gitignore | 1 + Cargo.toml | 2 +- src/lib.rs | 107 +++++++++++++++++++++++++++++++++++------------------ 3 files changed, 73 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index 4fffb2f..863aa1c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target /Cargo.lock +/.idea/ \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 56cbb77..8c0d9d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,4 +25,4 @@ indicatif = "0.17" futures = "0.3" async-recursion = "1.0.0" log = "0.4.17" -simplelog = "0.12.1" \ No newline at end of file +simplelog = "0.12.1" diff --git a/src/lib.rs b/src/lib.rs index 7f75f1d..df09b0f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -732,6 +732,21 @@ async fn try_download(main_path: &PathBuf, part: &String, part_url: &String) -> Some(path) } +//endregion + +pub async fn get_client<'a>() -> Result> { + trace!("get_client"); + let config = load_config(); + info!("get_client: config: {:?}", config); + let client_id = ClientId::new(config.twitch_client_id); + let client_secret = ClientSecret::new(config.twitch_client_secret); + info!("creating TwitchClient"); + let client = TwitchClient::new(client_id, client_secret).await?; + Ok(client) +} + +//region static functions + fn get_base_url(url: &str) -> String { let mut base_url = url.to_string(); let mut i = base_url.len() - 1; @@ -746,63 +761,51 @@ fn get_base_url(url: &str) -> String { } fn convert_twitch_timestamp(time: Timestamp) -> DateTime { - let time1: String = time.take(); + let time1: String = time.take() as String; trace!("convert_twitch_timestamp: {}", time1); convert_twitch_time(&time1) } +/// parse a duration from a string like '2h49m47s' fn convert_twitch_duration(duration: &str) -> chrono::Duration { trace!("convert_twitch_duration: {}", duration); - // todo!("Parse duration that comes in like '2h49m47s'"); let duration = duration .replace("h", ":") .replace("m", ":") .replace("s", ""); - // let mut t = duration.split(":").collect::>(); - t.reverse(); - // - // - // let milis = t.pop(); - // let milis: i64 = milis.expect("at least the milis must be there").parse().expect(format!("Failed to parse milis: {:?}", milis).as_str()); - let secs = t.pop(); - let secs: i64 = match secs { + + let secs_str = t.pop(); + let secs: i64 = match secs_str { Some(secs) => secs .parse() .expect(format!("Failed to parse secs: {:?}", secs).as_str()), None => 0, }; - let mins = t.pop(); - let mins: i64 = match mins { + + let mins_str = t.pop(); + let mins: i64 = match mins_str { Some(mins) => mins .parse() .expect(format!("Failed to parse mins: {:?}", mins).as_str()), None => 0, }; - let hours = t.pop(); - let hours: i64 = match hours { + + let hours_str = t.pop(); + let hours: i64 = match hours_str { Some(hours) => hours .parse() .expect(format!("Failed to parse hours: {:?}", hours).as_str()), None => 0, }; - let milis =/* milis +*/ secs*1000 + mins*60*1000 + hours*60*60*1000; + println!("hours: {}, mins: {}, secs: {}", hours, mins, secs); + let millis =/* millis +*/ secs*1000 + mins*60*1000 + hours*60*60*1000; - let res = chrono::Duration::milliseconds(milis); + let res = chrono::Duration::milliseconds(millis); res - // - // let res = chrono::Duration::from_std( - // std::time::Duration::(duration.parse::().unwrap()), - // ) - // let end_time = convert_twitch_time_info(duration.to_string(), "%H:%M:%S%z"); - // let start_time = chrono::DateTime::from_utc( - // chrono::NaiveDateTime::from_timestamp_opt(0, 0).unwrap(), - // chrono::Utc, - // ); - // end_time - start_time } fn convert_twitch_time(time: &str) -> DateTime { @@ -829,16 +832,6 @@ fn convert_twitch_time_info(res: String, fmt: &str) -> DateTime { } //endregion -pub async fn get_client<'a>() -> Result> { - trace!("get_client"); - let config = load_config(); - info!("get_client: config: {:?}", config); - let client_id = ClientId::new(config.twitch_client_id); - let client_secret = ClientSecret::new(config.twitch_client_secret); - info!("creating TwitchClient"); - let client = TwitchClient::new(client_id, client_secret).await?; - Ok(client) -} //region Twitch access token structs (maybe find out how to extract those values directly without these structs) #[derive(Debug, Deserialize, Serialize)] @@ -856,3 +849,45 @@ pub struct VideoAccessTokenResponseDataAccessToken { pub signature: String, } //endregion + +//region tests + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_convert_twitch_time() { + let time = "2021-01-01T00:00:00Z"; + let time = convert_twitch_time(time); + assert_eq!(time.to_string(), "2021-01-01 00:00:00 UTC"); + } + + #[test] + fn test_convert_twitch_time_info() { + let time = "2021-01-01T00:00:00+00:00"; + let time = convert_twitch_time_info(time.to_string(), "%Y-%m-%dT%H:%M:%S%z"); + assert_eq!(time.to_string(), "2021-01-01 00:00:00 UTC"); + let time = "2021-01-01T00:00:00Z"; + let time = convert_twitch_time_info(time.to_string(), "%Y-%m-%dT%H:%M:%S%z"); + assert_eq!(time.to_string(), "2021-01-01 00:00:00 UTC"); + } + + #[test] + fn test_convert_twitch_duration() { + let duration = "2h49m47s"; + println!("duration: {:?}", duration); + let duration = convert_twitch_duration(duration); + println!("duration: {:?}", duration); + assert_eq!(duration.num_seconds(), 10187); + } + + #[test] + fn test_get_base_url() { + let url = "https://dqrpb9wgowsf5.cloudfront.net/5f3ee3729979d8e1eab3_halfwayhardcore_42051664507_1680818535/chunked/index-dvr.m3u8"; + let base_url = get_base_url(url); + assert_eq!(base_url, "https://dqrpb9wgowsf5.cloudfront.net/5f3ee3729979d8e1eab3_halfwayhardcore_42051664507_1680818535/chunked/"); + } +} + +//endregion