diff --git a/Cargo.toml b/Cargo.toml index 6d803a7..945b07c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" [dependencies] anyhow = "1.0.71" chrono = "0.4.26" -clap = { version = "4.3.12", features = ["default", "derive"] } +clap = { version = "4.3.12", features = ["default", "derive", "env"] } env_logger = "0.10.0" html2md = "0.2.14" log = "0.4.19" diff --git a/src/format.rs b/src/format.rs index 2931952..928190d 100644 --- a/src/format.rs +++ b/src/format.rs @@ -1,7 +1,6 @@ use anyhow::Result; use chrono::Local; use html2md::parse_html; -use log::debug; use megalodon::{ entities::{Account, Attachment, Status}, response::Response, @@ -46,7 +45,7 @@ pub(super) async fn format_status( "{}{}", ancestor, if !thread.is_empty() { - thread + format!("\n{}", thread) } else { String::default() } @@ -55,25 +54,20 @@ pub(super) async fn format_status( fn format_attachments(depth: usize, attachments: &[Attachment]) -> String { let prefix = vec![">"; depth].join(""); - debug!("Attachments {:?}", attachments); if attachments.is_empty() { String::default() } else { - let mut editor = DefaultEditor::new().unwrap(); attachments .iter() .map(|a| { Url::parse(&a.url).unwrap().open(); + let mut editor = DefaultEditor::new().unwrap(); let src = if let Ok(line) = editor.readline("Filename: ") { line } else { a.url.clone() }; - format!( - " -{} ", - prefix, src - ) + format!("{} ", prefix, src) }) .collect::>() .join("\n") diff --git a/src/main.rs b/src/main.rs index ebea522..f8adbee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,10 +9,9 @@ use megalodon::{ response::Response, Megalodon, }; - use tokio_stream::{iter, StreamExt}; -use std::env; +use std::{env, fs::File, io::prelude::*}; use self::{ format::format_status, @@ -28,6 +27,12 @@ mod range; #[derive(Debug, Parser)] #[command()] struct Config { + #[arg(short, long, env = "MASTODON_URL")] + url: String, + #[arg(short, long, env = "MASTODON_ACCESS_TOKEN")] + access_token: String, + #[arg(short, long)] + output_dir: String, #[arg(required = true)] date: String, #[arg(short, long)] @@ -36,15 +41,21 @@ struct Config { #[tokio::main] async fn main() -> Result<()> { - let Config { date, verbose } = Config::parse(); + let Config { + date, + verbose, + url, + access_token, + output_dir, + } = Config::parse(); let level = if verbose { "debug" } else { "off" }; env::set_var("RUST_LOG", format!("{}={}", module_path!(), level)); env_logger::init(); - let day = try_create_range(date)?; + let day = try_create_range(date.clone())?; - let client = create_client()?; + let client = create_client(url, access_token)?; let Response { json: account, .. } = client.verify_account_credentials().await?; debug!("Fetching posts for date, {}.", day.end.format("%Y-%m-%d")); @@ -97,23 +108,26 @@ async fn main() -> Result<()> { } } reversed.reverse(); - println!("{}", reversed.join("\n\n")); + let output = format!("{}{}.md", output_dir, date); + let mut f = File::options().append(true).open(&output)?; + f.write_all(&reversed.join("\n\n").as_bytes())?; + println!("Appended matching posts to {}.", output); Ok(()) } +// Only ones authored by the user, on the date requested, that aren't a reply to any other status fn filter_statuses<'a>(account: &Account, day: &Range, json: &'a Vec) -> Vec<&'a Status> { json.iter() - .filter(|json| { - json.account.id == account.id - && day.start <= json.created_at - && json.created_at <= day.end + .filter(|status| { + status.account.id == account.id + && status.in_reply_to_id.is_none() + && day.start <= status.created_at + && status.created_at <= day.end }) .collect::>() } -fn create_client() -> Result> { - let url = env::var("MASTODON_URL")?; - let token = env::var("MASTODON_ACCESS_TOKEN")?; +fn create_client(url: String, token: String) -> Result> { Ok(generator(megalodon::SNS::Mastodon, url, Some(token), None)) }