Add formatting, start to pull thread

This commit is contained in:
Thomas Gideon 2023-07-15 09:20:51 -04:00
parent 9d7e8992da
commit aa77393903

View file

@ -1,23 +1,48 @@
use anyhow::{format_err, Result}; use anyhow::{format_err, Result};
use chrono::{DateTime, Local, NaiveDate, TimeZone, Utc}; use chrono::{DateTime, Local, NaiveDate, TimeZone, Utc};
use html2md::parse_html; use html2md::parse_html;
use megalodon::{generator, megalodon::GetLocalTimelineInputOptions, response::Response}; use megalodon::{
entities::status::Status, generator, megalodon::GetLocalTimelineInputOptions,
response::Response,
};
use std::env; use std::env;
#[derive(Debug)] #[derive(Debug)]
struct Post { struct Post {
id: String,
content: String, content: String,
created_at: DateTime<Utc>, created_at: DateTime<Utc>,
} }
// TODO implement try_from that looks for descendants and adds them
impl From<&Status> for Post {
fn from(status: &Status) -> Self {
let Status {
id,
created_at,
content,
..
} = status;
let id = id.clone();
let created_at = created_at.clone();
let content = parse_html(&content);
Post {
id,
created_at,
content,
}
}
}
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
env_logger::init(); env_logger::init();
// TODO add clap and argument for date
let start = Local let start = Local
.from_local_datetime( .from_local_datetime(
&NaiveDate::from_ymd_opt(2023, 7, 1) &NaiveDate::from_ymd_opt(2023, 7, 1)
.ok_or_else(|| format_err!("Invallid date!"))? .ok_or_else(|| format_err!("Invalid date!"))?
.and_hms_opt(0, 0, 0) .and_hms_opt(0, 0, 0)
.expect("Failed to construct time!"), .expect("Failed to construct time!"),
) )
@ -51,26 +76,50 @@ async fn main() -> Result<()> {
}; };
if let Some(last) = json.last() { if let Some(last) = json.last() {
println!(
"Checking {:?} against {:?}: {}",
last.created_at,
start,
last.created_at > start
);
if last.created_at > start { if last.created_at > start {
max_id.replace(last.id.clone()); max_id.replace(last.id.clone());
continue; continue;
} }
} }
println!( println!(
"{:#?}", "{}",
json.iter() json.iter()
.filter(|json| start <= json.created_at && json.created_at <= end) .filter(|json| start <= json.created_at && json.created_at <= end)
.map(|json| Post { .map(Post::from)
content: parse_html(&json.content), .map(|post| {
created_at: json.created_at format!(
"{} ({})
> {}",
post.created_at.format("%H:%M"),
post.id,
post.content
)
}) })
.collect::<Vec<Post>>() .collect::<Vec<String>>()
.join("\n\n")
);
let context = client
.get_status_context(String::from("110638913257555200"), None)
.await?;
println!(
"{}",
context
.json
.descendants
.iter()
.map(Post::from)
.map(|post| {
format!(
">
> {} ({})
>> {}",
post.created_at.format("%H:%M"),
post.id,
post.content
)
})
.collect::<Vec<String>>()
.join("\n")
); );
break; break;
} }