mirror of
https://framagit.org/veretcle/tootube.git
synced 2025-07-20 12:31:19 +02:00
57 lines
1.8 KiB
Rust
57 lines
1.8 KiB
Rust
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 upload’s 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}"));
|
||
}
|
||
}
|