mirror of
https://framagit.org/veretcle/tootube.git
synced 2025-07-20 20:41:17 +02:00
First functional version
This commit is contained in:
61
src/lib.rs
61
src/lib.rs
@@ -1,3 +1,14 @@
|
||||
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;
|
||||
@@ -5,13 +16,59 @@ 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.unwrap());
|
||||
let dl_url = get_max_resolution_dl(latest_vid.streaming_playlists.as_ref().unwrap());
|
||||
|
||||
println!("{dl_url}");
|
||||
let local_path = dl_video(&dl_url)
|
||||
.unwrap_or_else(|e| panic!("Cannot download video at URL {}: {}", dl_url, e));
|
||||
|
||||
let resumable_upload_id = create_resumable_upload(&config.youtube, &latest_vid)
|
||||
.unwrap_or_else(|e| panic!("Cannot retrieve the upload’s resumable id: {e}"));
|
||||
|
||||
upload_video(&local_path, &resumable_upload_id, &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));
|
||||
}
|
||||
|
Reference in New Issue
Block a user