Files
tootube/src/lib.rs

75 lines
2.0 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 std::{
error::Error,
fs::create_dir_all,
fs::{remove_file, File},
};
use reqwest::Url;
mod error;
use error::TootubeError;
mod config;
pub use config::parse_toml;
use config::Config;
mod peertube;
use peertube::{get_latest_video, get_max_resolution_dl};
mod youtube;
use youtube::{create_resumable_upload, upload_video};
const TMP_DIR: &str = "/tmp/tootube";
fn dl_video(u: &str) -> Result<String, Box<dyn Error>> {
// create dir
create_dir_all(TMP_DIR)?;
// get file
let mut response = reqwest::blocking::get(u)?;
// create local file
let url = Url::parse(u)?;
let dest_filename = url
.path_segments()
.ok_or_else(|| {
TootubeError::new(&format!(
"Cannot determine the destination filename for {u}"
))
})?
.last()
.ok_or_else(|| {
TootubeError::new(&format!(
"Cannot determine the destination filename for {u}"
))
})?;
let dest_filepath = format!("{TMP_DIR}/{dest_filename}");
let mut dest_file = File::create(&dest_filepath)?;
response.copy_to(&mut dest_file)?;
Ok(dest_filepath)
}
pub fn run(config: Config) {
// Get the latest video object
let latest_vid = get_latest_video(&config.peertube.base_url).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());
let local_path = dl_video(&dl_url)
.unwrap_or_else(|e| panic!("Cannot download video at URL {}: {}", dl_url, e));
let resumable_upload_url = create_resumable_upload(&config.youtube, &latest_vid)
.unwrap_or_else(|e| panic!("Cannot retrieve the uploads resumable id: {e}"));
upload_video(&local_path, &resumable_upload_url, &config.youtube)
.unwrap_or_else(|e| panic!("Cannot resume upload!: {e}"));
remove_file(&local_path).unwrap_or_else(|e| panic!("Cannot delete file {}: {}", local_path, e));
}