Files
tootube/src/lib.rs
2023-10-16 21:23:29 +02:00

57 lines
1.8 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use bytes::Bytes;
use futures_core::stream::Stream;
use log::debug;
use std::error::Error;
mod error;
mod config;
pub use config::parse_toml;
use config::Config;
mod peertube;
use peertube::{get_latest_video, get_max_resolution_dl};
mod youtube;
pub use youtube::register;
use youtube::{add_video_to_playlists, create_resumable_upload, now_kiss};
async fn get_dl_video_stream(
u: &str,
) -> Result<impl Stream<Item = Result<Bytes, reqwest::Error>>, Box<dyn Error>> {
Ok(reqwest::get(u).await?.bytes_stream())
}
#[tokio::main]
pub async fn run(config: Config, pl: Vec<String>) {
// Get the latest video object
let latest_vid = get_latest_video(&config.peertube.base_url)
.await
.unwrap_or_else(|e| {
panic!("Cannot retrieve the latest video, something must have gone terribly wrong: {e}")
});
let dl_url = get_max_resolution_dl(latest_vid.streaming_playlists.as_ref().unwrap());
debug!("PT download URL: {}", &dl_url);
let pt_stream = get_dl_video_stream(&dl_url)
.await
.unwrap_or_else(|e| panic!("Cannot download video at URL {}: {}", dl_url, e));
let resumable_upload_url = create_resumable_upload(&config.youtube, &latest_vid)
.await
.unwrap_or_else(|e| panic!("Cannot retrieve the uploads resumable id: {e}"));
debug!("YT upload URL: {}", &resumable_upload_url);
let yt_video_id = now_kiss(pt_stream, &resumable_upload_url, &config.youtube)
.await
.unwrap_or_else(|e| panic!("Cannot resume upload!: {e}"));
debug!("YT video ID: {}", &yt_video_id);
if !pl.is_empty() {
add_video_to_playlists(&config.youtube, &yt_video_id, &pl)
.await
.unwrap_or_else(|e| panic!("Cannot add video to playlist(s): {e}"));
}
}