More clean up.
This commit is contained in:
parent
c90495a985
commit
17e99518ce
3 changed files with 31 additions and 23 deletions
|
@ -8,7 +8,7 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.71"
|
anyhow = "1.0.71"
|
||||||
chrono = "0.4.26"
|
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"
|
env_logger = "0.10.0"
|
||||||
html2md = "0.2.14"
|
html2md = "0.2.14"
|
||||||
log = "0.4.19"
|
log = "0.4.19"
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use chrono::Local;
|
use chrono::Local;
|
||||||
use html2md::parse_html;
|
use html2md::parse_html;
|
||||||
use log::debug;
|
|
||||||
use megalodon::{
|
use megalodon::{
|
||||||
entities::{Account, Attachment, Status},
|
entities::{Account, Attachment, Status},
|
||||||
response::Response,
|
response::Response,
|
||||||
|
@ -46,7 +45,7 @@ pub(super) async fn format_status(
|
||||||
"{}{}",
|
"{}{}",
|
||||||
ancestor,
|
ancestor,
|
||||||
if !thread.is_empty() {
|
if !thread.is_empty() {
|
||||||
thread
|
format!("\n{}", thread)
|
||||||
} else {
|
} else {
|
||||||
String::default()
|
String::default()
|
||||||
}
|
}
|
||||||
|
@ -55,25 +54,20 @@ pub(super) async fn format_status(
|
||||||
|
|
||||||
fn format_attachments(depth: usize, attachments: &[Attachment]) -> String {
|
fn format_attachments(depth: usize, attachments: &[Attachment]) -> String {
|
||||||
let prefix = vec![">"; depth].join("");
|
let prefix = vec![">"; depth].join("");
|
||||||
debug!("Attachments {:?}", attachments);
|
|
||||||
if attachments.is_empty() {
|
if attachments.is_empty() {
|
||||||
String::default()
|
String::default()
|
||||||
} else {
|
} else {
|
||||||
let mut editor = DefaultEditor::new().unwrap();
|
|
||||||
attachments
|
attachments
|
||||||
.iter()
|
.iter()
|
||||||
.map(|a| {
|
.map(|a| {
|
||||||
Url::parse(&a.url).unwrap().open();
|
Url::parse(&a.url).unwrap().open();
|
||||||
|
let mut editor = DefaultEditor::new().unwrap();
|
||||||
let src = if let Ok(line) = editor.readline("Filename: ") {
|
let src = if let Ok(line) = editor.readline("Filename: ") {
|
||||||
line
|
line
|
||||||
} else {
|
} else {
|
||||||
a.url.clone()
|
a.url.clone()
|
||||||
};
|
};
|
||||||
format!(
|
format!("{} <img src=\"{}\" />", prefix, src)
|
||||||
"
|
|
||||||
{} <img src=\"{}\" />",
|
|
||||||
prefix, src
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.collect::<Vec<String>>()
|
.collect::<Vec<String>>()
|
||||||
.join("\n")
|
.join("\n")
|
||||||
|
|
40
src/main.rs
40
src/main.rs
|
@ -9,10 +9,9 @@ use megalodon::{
|
||||||
response::Response,
|
response::Response,
|
||||||
Megalodon,
|
Megalodon,
|
||||||
};
|
};
|
||||||
|
|
||||||
use tokio_stream::{iter, StreamExt};
|
use tokio_stream::{iter, StreamExt};
|
||||||
|
|
||||||
use std::env;
|
use std::{env, fs::File, io::prelude::*};
|
||||||
|
|
||||||
use self::{
|
use self::{
|
||||||
format::format_status,
|
format::format_status,
|
||||||
|
@ -28,6 +27,12 @@ mod range;
|
||||||
#[derive(Debug, Parser)]
|
#[derive(Debug, Parser)]
|
||||||
#[command()]
|
#[command()]
|
||||||
struct Config {
|
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)]
|
#[arg(required = true)]
|
||||||
date: String,
|
date: String,
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
|
@ -36,15 +41,21 @@ struct Config {
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
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" };
|
let level = if verbose { "debug" } else { "off" };
|
||||||
env::set_var("RUST_LOG", format!("{}={}", module_path!(), level));
|
env::set_var("RUST_LOG", format!("{}={}", module_path!(), level));
|
||||||
env_logger::init();
|
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?;
|
let Response { json: account, .. } = client.verify_account_credentials().await?;
|
||||||
|
|
||||||
debug!("Fetching posts for date, {}.", day.end.format("%Y-%m-%d"));
|
debug!("Fetching posts for date, {}.", day.end.format("%Y-%m-%d"));
|
||||||
|
@ -97,23 +108,26 @@ async fn main() -> Result<()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
reversed.reverse();
|
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(())
|
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<Status>) -> Vec<&'a Status> {
|
fn filter_statuses<'a>(account: &Account, day: &Range, json: &'a Vec<Status>) -> Vec<&'a Status> {
|
||||||
json.iter()
|
json.iter()
|
||||||
.filter(|json| {
|
.filter(|status| {
|
||||||
json.account.id == account.id
|
status.account.id == account.id
|
||||||
&& day.start <= json.created_at
|
&& status.in_reply_to_id.is_none()
|
||||||
&& json.created_at <= day.end
|
&& day.start <= status.created_at
|
||||||
|
&& status.created_at <= day.end
|
||||||
})
|
})
|
||||||
.collect::<Vec<&Status>>()
|
.collect::<Vec<&Status>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_client() -> Result<Box<dyn Megalodon + Send + Sync>> {
|
fn create_client(url: String, token: String) -> Result<Box<dyn Megalodon + Send + Sync>> {
|
||||||
let url = env::var("MASTODON_URL")?;
|
|
||||||
let token = env::var("MASTODON_ACCESS_TOKEN")?;
|
|
||||||
Ok(generator(megalodon::SNS::Mastodon, url, Some(token), None))
|
Ok(generator(megalodon::SNS::Mastodon, url, Some(token), None))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue