From f2a7119a11c96ae75a49b784f2cddcd7d7157a63 Mon Sep 17 00:00:00 2001 From: VC Date: Fri, 29 Sep 2023 17:59:07 +0200 Subject: [PATCH] fix: dl function --- src/peertube.rs | 53 +++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/peertube.rs b/src/peertube.rs index 409c81a..ed0b4bf 100644 --- a/src/peertube.rs +++ b/src/peertube.rs @@ -46,19 +46,17 @@ pub fn get_latest_video(u: &str) -> Result> { /// This returns the direct download URL for with the maximum resolution pub fn get_max_resolution_dl(p: &Vec) -> String { - let max_res = p[0].files.iter().fold(std::u16::MIN, |a,b| a.resolution.id.max(&b.resolution.id)); -// let mut res = 0; -// let mut dl_url = String::new(); -// -// for i in p[0].files.iter() { -// if i.resolution.id > res { -// res = i.resolution.id; -// dl_url = i.file_download_url.clone(); -// } -// } -// -// dl_url - "blah".to_string() + let mut res = 0; + let mut dl_url = String::new(); + + for i in p[0].files.iter() { + if i.resolution.id > res { + res = i.resolution.id; + dl_url = i.file_download_url.clone(); + } + } + + dl_url } /// This gets all the crispy details about one particular video @@ -75,16 +73,27 @@ mod tests { #[test] fn test_get_max_resolution_dl() { - let str_plist: Vec = vec![ - PeerTubeVideoStreamingPlaylists { - files: vec![PeerTubeVideoStreamingPlaylistsFiles { id: 1025, resolution: PeerTubeVideoStreamingPlaylistsFilesResolution { id: 990 }, file_download_url: "meh!".to_string() }, -PeerTubeVideoStreamingPlaylistsFiles { id: 1027, resolution: PeerTubeVideoStreamingPlaylistsFilesResolution { id: 1720 }, file_download_url: "blah".to_string() }, -PeerTubeVideoStreamingPlaylistsFiles { id: 1026, resolution: PeerTubeVideoStreamingPlaylistsFilesResolution { id: 360 }, file_download_url: "nope".to_string() }, - ] - } - ]; + let str_plist: Vec = + vec![PeerTubeVideoStreamingPlaylists { + files: vec![ + PeerTubeVideoStreamingPlaylistsFiles { + id: 1025, + resolution: PeerTubeVideoStreamingPlaylistsFilesResolution { id: 990 }, + file_download_url: "meh!".to_string(), + }, + PeerTubeVideoStreamingPlaylistsFiles { + id: 1027, + resolution: PeerTubeVideoStreamingPlaylistsFilesResolution { id: 1720 }, + file_download_url: "blah".to_string(), + }, + PeerTubeVideoStreamingPlaylistsFiles { + id: 1026, + resolution: PeerTubeVideoStreamingPlaylistsFilesResolution { id: 360 }, + file_download_url: "nope".to_string(), + }, + ], + }]; assert_eq!("blah".to_string(), get_max_resolution_dl(&str_plist)); } } -