Compare commits

..

No commits in common. "01335b6929d0f61603f9b01b3141ba9eb7d9f9b7" and "b8a1c5461be433181d48183a3ef4edc5340a68f4" have entirely different histories.

View file

@ -1,6 +1,5 @@
use anyhow::{bail, format_err, Result}; use anyhow::{bail, format_err, Result};
use chrono::{DateTime, Local, LocalResult, NaiveDate, TimeZone, Utc}; use chrono::{DateTime, Local, LocalResult, NaiveDate, TimeZone, Utc};
use clap::{arg, command, Parser};
use html2md::parse_html; use html2md::parse_html;
use log::{debug, trace}; use log::{debug, trace};
use megalodon::{ use megalodon::{
@ -24,35 +23,20 @@ struct Page<'a> {
newest: Option<&'a DateTime<Utc>>, newest: Option<&'a DateTime<Utc>>,
} }
#[derive(Debug, Parser)]
#[command()]
struct Config {
#[arg(required = true)]
date: String,
#[arg(short, long)]
verbose: bool,
}
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
let Config { date, verbose } = Config::parse(); env::set_var("RUST_LOG", format!("{}=debug", module_path!()));
if verbose {
env::set_var("RUST_LOG", format!("{}=debug", module_path!()));
} else {
env::set_var("RUST_LOG", format!("{}=none", module_path!()));
}
env_logger::init(); env_logger::init();
let day = try_create_range(date)?; // TODO add clap and argument for date
let day = try_create_range("2023-07-01")?;
debug!("Date {}", day.end.format("%Y-%m-%d")); debug!("Date {}", day.end.format("%Y-%m-%d"));
let client = create_client()?; let client = create_client()?;
let Response { json: account, .. } = client.verify_account_credentials().await?;
let mut last_id_on_page: Option<String> = None; let mut last_id_on_page: Option<String> = None;
debug!("Fetching posts"); debug!("Fetching posts");
let mut reversed = Vec::new();
loop { loop {
let json = fetch_page(&client, &last_id_on_page).await?; let json = fetch_page(&client, &last_id_on_page).await?;
let page = Page { let page = Page {
@ -74,51 +58,35 @@ async fn main() -> Result<()> {
let json = json let json = json
.clone() .clone()
.into_iter() .into_iter()
.filter(|json| { .filter(|json| day.start <= json.created_at && json.created_at <= day.end)
json.account.id == account.id
&& day.start <= json.created_at
&& json.created_at <= day.end
})
.collect::<Vec<Status>>(); .collect::<Vec<Status>>();
trace!("Filtered to {} post(s)", json.len()); trace!("Filtered to {} post(s)", json.len());
let mut stream = iter(json); let mut stream = iter(json);
while let Some(status) = stream.next().await { while let Some(status) = stream.next().await {
let ancestor = format!( println!(
"{} "{}
> {}", > {}",
status.created_at.with_timezone(&Local).format("%H:%M"), status.created_at.with_timezone(&Local).format("%H:%M"),
parse_html(&status.content).trim() parse_html(&status.content)
); );
let Response { json, .. } = client.get_status_context(status.id, None).await?; let Response { json, .. } = client.get_status_context(status.id, None).await?;
let thread = json let thread = json
.descendants .descendants
.into_iter() .into_iter()
.filter(|s| {
s.in_reply_to_account_id == Some(account.id.clone())
&& s.account.id == account.id
})
.map(|status| { .map(|status| {
format!( format!(
"> ">
> {} > {}
>> {}", >> {}",
status.created_at.with_timezone(&Local).format("%H:%M"), status.created_at.with_timezone(&Local).format("%H:%M"),
parse_html(&status.content).trim() parse_html(&status.content)
) )
}) })
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join("\n"); .join("\n");
reversed.push(format!( println!("{}", thread);
"{}{}",
ancestor,
if !thread.is_empty() {
thread
} else {
String::default()
}
));
} }
if page_end_older_than(&page, &day) { if page_end_older_than(&page, &day) {
@ -130,8 +98,6 @@ async fn main() -> Result<()> {
last_id_on_page.replace(id.clone()); last_id_on_page.replace(id.clone());
} }
} }
reversed.reverse();
println!("{}", reversed.join("\n\n"));
Ok(()) Ok(())
} }