diff --git a/src/peertube.rs b/src/peertube.rs index d4e025a..d872adf 100644 --- a/src/peertube.rs +++ b/src/peertube.rs @@ -1,5 +1,5 @@ use serde::Deserialize; -use std::{boxed::Box, error::Error}; +use std::{boxed::Box, cmp::Ordering, error::Error}; #[derive(Debug, Deserialize)] pub struct PeerTubeVideos { @@ -22,7 +22,7 @@ pub struct PeerTubeVideoStreamingPlaylists { pub files: Vec, } -#[derive(Debug, Deserialize)] +#[derive(Eq, Debug, Deserialize)] pub struct PeerTubeVideoStreamingPlaylistsFiles { pub id: u64, pub resolution: PeerTubeVideoStreamingPlaylistsFilesResolution, @@ -30,7 +30,25 @@ pub struct PeerTubeVideoStreamingPlaylistsFiles { pub file_download_url: String, } -#[derive(Debug, Deserialize)] +impl Ord for PeerTubeVideoStreamingPlaylistsFiles { + fn cmp(&self, other: &Self) -> Ordering { + self.resolution.id.cmp(&other.resolution.id) + } +} + +impl PartialOrd for PeerTubeVideoStreamingPlaylistsFiles { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl PartialEq for PeerTubeVideoStreamingPlaylistsFiles { + fn eq(&self, other: &Self) -> bool { + self.resolution.id == other.resolution.id + } +} + +#[derive(Eq, Debug, Deserialize, PartialEq)] pub struct PeerTubeVideoStreamingPlaylistsFilesResolution { pub id: u16, } @@ -49,17 +67,7 @@ pub async 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: &[PeerTubeVideoStreamingPlaylists]) -> 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 + p[0].files.iter().max().unwrap().file_download_url.clone() } /// This gets all the crispy details about one particular video