tyrolienne/src/main.rs

97 lines
2.8 KiB
Rust

mod zipline;
use std::{path::Path, time::Duration};
use color_eyre::eyre::{OptionExt, Result, bail};
use tracing::Level;
use tracing_subscriber::EnvFilter;
use urlencoding::encode;
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
struct Config {
zipline_url: String,
zipline_token: String,
}
impl Config {
fn fixed_url(&self) -> String {
let mut url = self.zipline_url.clone();
if !url.ends_with("/") {
url += "/";
}
if url.ends_with("api/") {
url.truncate(url.len() - 3);
}
url
}
}
fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env().add_directive(Level::INFO.into()))
.init();
color_eyre::install()?;
let Some(config_dir) = dirs::config_dir() else {
bail!("could not get config dir!");
};
std::fs::create_dir_all(&config_dir)?;
let config_file_path = config_dir.join("tyrolienne.toml");
let config_contents = match std::fs::read_to_string(&config_file_path) {
Ok(str) => str,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
let default_cfg = toml::to_string(&Config::default())?;
std::fs::write(&config_file_path, &default_cfg)?;
bail!("config file was not found, wrote default one at {config_file_path:?}");
}
Err(e) => bail!(e),
};
let config: Config = toml::from_str(&config_contents)?;
if config.zipline_token.is_empty() {
bail!("zipline token is empty! please set it at {config_file_path:?}");
}
if config.zipline_url.is_empty() {
bail!("zipline url is empty! please set it at {config_file_path:?}");
}
let args: Vec<String> = std::env::args().collect();
let file = args.get(1).ok_or_eyre(
"please specify the path to the file you want to upload as the first command line argument",
)?;
let folders = zipline::get_folders(&config)?;
let folder = &folders[0];
tracing::info!("uploading to folder '{}'...", folder.name);
let res = zipline::upload_file(&config, folder, Path::new(file))?;
let zp_file = &res.files[0];
tracing::info!("recalculating thumbnails...");
zipline::recalc_thumbnails(&config)?;
std::thread::sleep(Duration::from_secs(5));
tracing::info!("fetching thumbnail url...");
let res = zipline::get_file_details(&config, &zp_file.id)?;
let thumbnail_url = res
.thumbnail_url(&config)
.ok_or_eyre("could not get thumbnail url")?;
// TODO get w&h from video
let autocomp_url = format!(
"https://autocompressor.net/av1?v={}&i={}&w=1920&h=1080",
encode(&zp_file.url),
encode(&thumbnail_url)
);
tracing::info!("url: {autocomp_url}");
Ok(())
}