mirror of
https://framagit.org/veretcle/tootube.git
synced 2025-07-20 20:41:17 +02:00
105 lines
3.1 KiB
Rust
105 lines
3.1 KiB
Rust
use serde::Deserialize;
|
|
use std::{boxed::Box, error::Error};
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct PeerTubeVideos {
|
|
pub total: u64,
|
|
pub data: Vec<PeerTubeVideo>,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct PeerTubeVideo {
|
|
pub name: String,
|
|
pub uuid: String,
|
|
pub description: String,
|
|
#[serde(rename = "streamingPlaylists")]
|
|
pub streaming_playlists: Option<Vec<PeerTubeVideoStreamingPlaylists>>,
|
|
pub tags: Option<Vec<String>>,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct PeerTubeVideoStreamingPlaylists {
|
|
pub files: Vec<PeerTubeVideoStreamingPlaylistsFiles>,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct PeerTubeVideoStreamingPlaylistsFiles {
|
|
pub id: u64,
|
|
pub resolution: PeerTubeVideoStreamingPlaylistsFilesResolution,
|
|
#[serde(rename = "fileDownloadUrl")]
|
|
pub file_download_url: String,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct PeerTubeVideoStreamingPlaylistsFilesResolution {
|
|
pub id: u16,
|
|
}
|
|
|
|
/// This gets the last video uploaded to the PeerTube server
|
|
pub async fn get_latest_video(u: &str) -> Result<PeerTubeVideo, Box<dyn Error>> {
|
|
let body = reqwest::get(format!("{}/api/v1/videos?count=1&sort=-publishedAt", u))
|
|
.await?
|
|
.json::<PeerTubeVideos>()
|
|
.await?;
|
|
|
|
let vid = get_video_detail(u, &body.data[0].uuid).await?;
|
|
|
|
Ok(vid)
|
|
}
|
|
|
|
/// 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
|
|
}
|
|
|
|
/// This gets all the crispy details about one particular video
|
|
async fn get_video_detail(u: &str, v: &str) -> Result<PeerTubeVideo, Box<dyn Error>> {
|
|
let body = reqwest::get(format!("{}/api/v1/videos/{}", u, v))
|
|
.await?
|
|
.json::<PeerTubeVideo>()
|
|
.await?;
|
|
|
|
Ok(body)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_get_max_resolution_dl() {
|
|
let str_plist: Vec<PeerTubeVideoStreamingPlaylists> =
|
|
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));
|
|
}
|
|
}
|