Compare commits
2 commits
4274d0ead1
...
aa77393903
Author | SHA1 | Date | |
---|---|---|---|
Thomas Gideon | aa77393903 | ||
Thomas Gideon | 9d7e8992da |
2174
Cargo.lock
generated
Normal file
2174
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
|
@ -6,3 +6,10 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.71"
|
||||
chrono = "0.4.26"
|
||||
env_logger = "0.10.0"
|
||||
html2md = "0.2.14"
|
||||
megalodon = "0.8.3"
|
||||
rss = "2.0.4"
|
||||
tokio = { version = "1.28.2", features = ["default", "full"] }
|
||||
|
|
128
src/main.rs
128
src/main.rs
|
@ -1,3 +1,127 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use anyhow::{format_err, Result};
|
||||
use chrono::{DateTime, Local, NaiveDate, TimeZone, Utc};
|
||||
use html2md::parse_html;
|
||||
use megalodon::{
|
||||
entities::status::Status, generator, megalodon::GetLocalTimelineInputOptions,
|
||||
response::Response,
|
||||
};
|
||||
use std::env;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Post {
|
||||
id: String,
|
||||
content: String,
|
||||
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]
|
||||
async fn main() -> Result<()> {
|
||||
env_logger::init();
|
||||
|
||||
// TODO add clap and argument for date
|
||||
let start = Local
|
||||
.from_local_datetime(
|
||||
&NaiveDate::from_ymd_opt(2023, 7, 1)
|
||||
.ok_or_else(|| format_err!("Invalid date!"))?
|
||||
.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!(
|
||||
"{}",
|
||||
json.iter()
|
||||
.filter(|json| start <= json.created_at && json.created_at <= end)
|
||||
.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
|
||||
)
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n")
|
||||
);
|
||||
break;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue