2023-07-15 12:47:57 +00:00
|
|
|
use anyhow::{format_err, Result};
|
|
|
|
use chrono::{DateTime, Local, NaiveDate, TimeZone, Utc};
|
|
|
|
use html2md::parse_html;
|
2023-07-15 13:20:51 +00:00
|
|
|
use megalodon::{
|
|
|
|
entities::status::Status, generator, megalodon::GetLocalTimelineInputOptions,
|
|
|
|
response::Response,
|
|
|
|
};
|
2023-07-15 12:47:57 +00:00
|
|
|
use std::env;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Post {
|
2023-07-15 13:20:51 +00:00
|
|
|
id: String,
|
2023-07-15 12:47:57 +00:00
|
|
|
content: String,
|
|
|
|
created_at: DateTime<Utc>,
|
|
|
|
}
|
|
|
|
|
2023-07-15 13:20:51 +00:00
|
|
|
// 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:47:57 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
|
|
|
env_logger::init();
|
|
|
|
|
2023-07-15 13:20:51 +00:00
|
|
|
// TODO add clap and argument for date
|
2023-07-15 12:47:57 +00:00
|
|
|
let start = Local
|
|
|
|
.from_local_datetime(
|
|
|
|
&NaiveDate::from_ymd_opt(2023, 7, 1)
|
2023-07-15 13:20:51 +00:00
|
|
|
.ok_or_else(|| format_err!("Invalid date!"))?
|
2023-07-15 12:47:57 +00:00
|
|
|
.and_hms_opt(0, 0, 0)
|
|
|
|
.expect("Failed to construct time!"),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let end = Local
|
|
|
|
.from_local_datetime(
|
|
|
|
&NaiveDate::from_ymd_opt(2023, 7, 1)
|
|
|
|
.ok_or_else(|| format_err!("Invallid date!"))?
|
|
|
|
.and_hms_opt(23, 59, 59)
|
|
|
|
.expect("Failed to construct time!"),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
println!("Date {:#?}", start);
|
|
|
|
|
|
|
|
let url = env::var("MASTODON_URL")?;
|
|
|
|
let token = env::var("MASTODON_ACCESS_TOKEN")?;
|
|
|
|
let client = generator(megalodon::SNS::Mastodon, url, Some(token), None);
|
|
|
|
let mut max_id: Option<String> = None;
|
|
|
|
loop {
|
|
|
|
let Response { json, .. } = if let Some(max_id) = max_id.as_ref() {
|
|
|
|
client
|
|
|
|
.get_local_timeline(Some(&GetLocalTimelineInputOptions {
|
|
|
|
max_id: Some(max_id.clone()),
|
|
|
|
..GetLocalTimelineInputOptions::default()
|
|
|
|
}))
|
|
|
|
.await?
|
|
|
|
} else {
|
|
|
|
client.get_local_timeline(None).await?
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(last) = json.last() {
|
|
|
|
if last.created_at > start {
|
|
|
|
max_id.replace(last.id.clone());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println!(
|
2023-07-15 13:20:51 +00:00
|
|
|
"{}",
|
2023-07-15 12:47:57 +00:00
|
|
|
json.iter()
|
|
|
|
.filter(|json| start <= json.created_at && json.created_at <= end)
|
2023-07-15 13:20:51 +00:00
|
|
|
.map(Post::from)
|
|
|
|
.map(|post| {
|
|
|
|
format!(
|
|
|
|
"{} ({})
|
|
|
|
> {}",
|
|
|
|
post.created_at.format("%H:%M"),
|
|
|
|
post.id,
|
|
|
|
post.content
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.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
|
|
|
|
)
|
2023-07-15 12:47:57 +00:00
|
|
|
})
|
2023-07-15 13:20:51 +00:00
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join("\n")
|
2023-07-15 12:47:57 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Ok(())
|
2023-06-03 19:59:08 +00:00
|
|
|
}
|