Handle image attachments, refactor more
This commit is contained in:
parent
01335b6929
commit
c90495a985
6 changed files with 334 additions and 111 deletions
81
src/format.rs
Normal file
81
src/format.rs
Normal file
|
@ -0,0 +1,81 @@
|
|||
use anyhow::Result;
|
||||
use chrono::Local;
|
||||
use html2md::parse_html;
|
||||
use log::debug;
|
||||
use megalodon::{
|
||||
entities::{Account, Attachment, Status},
|
||||
response::Response,
|
||||
Megalodon,
|
||||
};
|
||||
use rustyline::DefaultEditor;
|
||||
use url::Url;
|
||||
use url_open::UrlOpen;
|
||||
|
||||
pub(super) async fn format_status(
|
||||
client: &Box<dyn Megalodon + Send + Sync>,
|
||||
account: &Account,
|
||||
status: &Status,
|
||||
) -> Result<String> {
|
||||
let ancestor = format!(
|
||||
"{}
|
||||
> {}{}",
|
||||
status.created_at.with_timezone(&Local).format("%H:%M"),
|
||||
parse_html(&status.content).trim(),
|
||||
format_attachments(1, &status.media_attachments)
|
||||
);
|
||||
let Response { json, .. } = client.get_status_context(status.id.clone(), None).await?;
|
||||
let thread = json
|
||||
.descendants
|
||||
.into_iter()
|
||||
.filter(|s| {
|
||||
s.in_reply_to_account_id == Some(account.id.clone()) && s.account.id == account.id
|
||||
})
|
||||
.map(|status| {
|
||||
format!(
|
||||
">
|
||||
> {}
|
||||
>> {}{}",
|
||||
status.created_at.with_timezone(&Local).format("%H:%M"),
|
||||
parse_html(&status.content).trim(),
|
||||
format_attachments(2, &status.media_attachments)
|
||||
)
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n");
|
||||
Ok(format!(
|
||||
"{}{}",
|
||||
ancestor,
|
||||
if !thread.is_empty() {
|
||||
thread
|
||||
} else {
|
||||
String::default()
|
||||
}
|
||||
))
|
||||
}
|
||||
|
||||
fn format_attachments(depth: usize, attachments: &[Attachment]) -> String {
|
||||
let prefix = vec![">"; depth].join("");
|
||||
debug!("Attachments {:?}", attachments);
|
||||
if attachments.is_empty() {
|
||||
String::default()
|
||||
} else {
|
||||
let mut editor = DefaultEditor::new().unwrap();
|
||||
attachments
|
||||
.iter()
|
||||
.map(|a| {
|
||||
Url::parse(&a.url).unwrap().open();
|
||||
let src = if let Ok(line) = editor.readline("Filename: ") {
|
||||
line
|
||||
} else {
|
||||
a.url.clone()
|
||||
};
|
||||
format!(
|
||||
"
|
||||
{} <img src=\"{}\" />",
|
||||
prefix, src
|
||||
)
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n")
|
||||
}
|
||||
}
|
163
src/main.rs
163
src/main.rs
|
@ -1,28 +1,29 @@
|
|||
use anyhow::{bail, format_err, Result};
|
||||
use chrono::{DateTime, Local, LocalResult, NaiveDate, TimeZone, Utc};
|
||||
use anyhow::Result;
|
||||
use chrono::{DateTime, Local, Utc};
|
||||
use clap::{arg, command, Parser};
|
||||
use html2md::parse_html;
|
||||
use log::{debug, trace};
|
||||
use megalodon::{
|
||||
entities::Status, generator, megalodon::GetLocalTimelineInputOptions, response::Response,
|
||||
entities::{Account, Status},
|
||||
generator,
|
||||
megalodon::GetLocalTimelineInputOptions,
|
||||
response::Response,
|
||||
Megalodon,
|
||||
};
|
||||
|
||||
use tokio_stream::{iter, StreamExt};
|
||||
|
||||
use std::env;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Range {
|
||||
start: DateTime<Local>,
|
||||
end: DateTime<Local>,
|
||||
}
|
||||
use self::{
|
||||
format::format_status,
|
||||
page::{bounds_from, Page},
|
||||
range::try_create_range,
|
||||
range::Range,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Page<'a> {
|
||||
oldest_id: Option<String>,
|
||||
oldest: Option<&'a DateTime<Utc>>,
|
||||
newest: Option<&'a DateTime<Utc>>,
|
||||
}
|
||||
mod format;
|
||||
mod page;
|
||||
mod range;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
#[command()]
|
||||
|
@ -36,89 +37,54 @@ struct Config {
|
|||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
let Config { date, verbose } = Config::parse();
|
||||
if verbose {
|
||||
env::set_var("RUST_LOG", format!("{}=debug", module_path!()));
|
||||
} else {
|
||||
env::set_var("RUST_LOG", format!("{}=none", module_path!()));
|
||||
}
|
||||
|
||||
let level = if verbose { "debug" } else { "off" };
|
||||
env::set_var("RUST_LOG", format!("{}={}", module_path!(), level));
|
||||
env_logger::init();
|
||||
|
||||
let day = try_create_range(date)?;
|
||||
|
||||
debug!("Date {}", day.end.format("%Y-%m-%d"));
|
||||
|
||||
let client = create_client()?;
|
||||
let Response { json: account, .. } = client.verify_account_credentials().await?;
|
||||
|
||||
debug!("Fetching posts for date, {}.", day.end.format("%Y-%m-%d"));
|
||||
|
||||
// the server only provides a page of results at a time, keep the oldest status from any page
|
||||
// to request the next older page of statuses
|
||||
let mut last_id_on_page: Option<String> = None;
|
||||
debug!("Fetching posts");
|
||||
// store the formatted posts in server order, reversed chronologically, to reverse at the end
|
||||
// for regular chronological ordering
|
||||
let mut reversed = Vec::new();
|
||||
loop {
|
||||
let json = fetch_page(&client, &last_id_on_page).await?;
|
||||
let page = Page {
|
||||
newest: json.first().map(|s| &s.created_at),
|
||||
oldest_id: json.last().map(|s| s.id.clone()),
|
||||
oldest: json.last().map(|s| &s.created_at),
|
||||
};
|
||||
let statuses = fetch_page(&client, &last_id_on_page).await?;
|
||||
if statuses.is_empty() {
|
||||
debug!("No more posts in range.");
|
||||
break;
|
||||
}
|
||||
let page = bounds_from(&statuses);
|
||||
|
||||
trace!("Page bounds {:?}", page);
|
||||
|
||||
// this age comparison only applies after the first page is fetched; the rest of the loop
|
||||
// body handles if the requested date is newer than any statuses on the first page
|
||||
if last_id_on_page.is_some() && page_start_older_than(&page, &day) {
|
||||
break;
|
||||
}
|
||||
|
||||
// fetching returns 20 at a time, in reverse chronological order so may require skipping
|
||||
// pages after the requested date
|
||||
if let Some(oldest_id) = page_newer_than(&page, &day) {
|
||||
last_id_on_page.replace(oldest_id);
|
||||
continue;
|
||||
}
|
||||
|
||||
let json = json
|
||||
.clone()
|
||||
.into_iter()
|
||||
.filter(|json| {
|
||||
json.account.id == account.id
|
||||
&& day.start <= json.created_at
|
||||
&& json.created_at <= day.end
|
||||
})
|
||||
.collect::<Vec<Status>>();
|
||||
trace!("Filtered to {} post(s)", json.len());
|
||||
|
||||
let mut stream = iter(json);
|
||||
|
||||
// mapping the vector runs into thorny ownership issues and only produces futures, not
|
||||
// resolved values; a for in loop works with await but also runs into thorny ownership
|
||||
// issues; a stream resolves both because the stream takes ownership of the statuses and
|
||||
// can be iterated in a simple way that allows the use of await in the body
|
||||
let mut stream = iter(filter_statuses(&account, &day, &statuses));
|
||||
while let Some(status) = stream.next().await {
|
||||
let ancestor = format!(
|
||||
"{}
|
||||
> {}",
|
||||
status.created_at.with_timezone(&Local).format("%H:%M"),
|
||||
parse_html(&status.content).trim()
|
||||
);
|
||||
let Response { json, .. } = client.get_status_context(status.id, None).await?;
|
||||
let thread = json
|
||||
.descendants
|
||||
.into_iter()
|
||||
.filter(|s| {
|
||||
s.in_reply_to_account_id == Some(account.id.clone())
|
||||
&& s.account.id == account.id
|
||||
})
|
||||
.map(|status| {
|
||||
format!(
|
||||
">
|
||||
> {}
|
||||
>> {}",
|
||||
status.created_at.with_timezone(&Local).format("%H:%M"),
|
||||
parse_html(&status.content).trim()
|
||||
)
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n");
|
||||
reversed.push(format!(
|
||||
"{}{}",
|
||||
ancestor,
|
||||
if !thread.is_empty() {
|
||||
thread
|
||||
} else {
|
||||
String::default()
|
||||
}
|
||||
));
|
||||
reversed.push(format_status(&client, &account, status).await?);
|
||||
}
|
||||
|
||||
if page_end_older_than(&page, &day) {
|
||||
|
@ -135,6 +101,16 @@ async fn main() -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn filter_statuses<'a>(account: &Account, day: &Range, json: &'a Vec<Status>) -> Vec<&'a Status> {
|
||||
json.iter()
|
||||
.filter(|json| {
|
||||
json.account.id == account.id
|
||||
&& day.start <= json.created_at
|
||||
&& json.created_at <= day.end
|
||||
})
|
||||
.collect::<Vec<&Status>>()
|
||||
}
|
||||
|
||||
fn create_client() -> Result<Box<dyn Megalodon + Send + Sync>> {
|
||||
let url = env::var("MASTODON_URL")?;
|
||||
let token = env::var("MASTODON_ACCESS_TOKEN")?;
|
||||
|
@ -160,41 +136,6 @@ async fn fetch_page(
|
|||
Ok(json)
|
||||
}
|
||||
|
||||
fn try_create_range<S: AsRef<str>>(date: S) -> Result<Range> {
|
||||
Ok(Range {
|
||||
start: create_day_bound(&date, 0, 0, 0)?,
|
||||
end: create_day_bound(date, 23, 59, 59)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn create_day_bound<S: AsRef<str>>(
|
||||
day: S,
|
||||
hour: u32,
|
||||
minute: u32,
|
||||
second: u32,
|
||||
) -> Result<DateTime<Local>> {
|
||||
let ts: Vec<&str> = day.as_ref().split("-").collect();
|
||||
if ts.len() != 3 {
|
||||
bail!("Invalid date format! {}", day.as_ref())
|
||||
}
|
||||
let (year, month, day) = if let [year, month, day, ..] = &ts[..] {
|
||||
(year, month, day)
|
||||
} else {
|
||||
bail!("Invalid date format! {}", day.as_ref())
|
||||
};
|
||||
let b = Local.from_local_datetime(
|
||||
&NaiveDate::from_ymd_opt(year.parse()?, month.parse()?, day.parse()?)
|
||||
.ok_or_else(|| format_err!("Invalid date!"))?
|
||||
.and_hms_opt(hour, minute, second)
|
||||
.ok_or_else(|| format_err!("Invalid time!"))?,
|
||||
);
|
||||
if let LocalResult::Single(b) = b {
|
||||
Ok(b)
|
||||
} else {
|
||||
bail!("Cannot construct day boundary!")
|
||||
}
|
||||
}
|
||||
|
||||
fn page_newer_than(page: &Page, range: &Range) -> Option<String> {
|
||||
page.oldest
|
||||
.filter(|oldest| *oldest > &range.end)
|
||||
|
|
17
src/page.rs
Normal file
17
src/page.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
use chrono::{DateTime, Utc};
|
||||
use megalodon::entities::Status;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) struct Page<'a> {
|
||||
pub oldest_id: Option<String>,
|
||||
pub oldest: Option<&'a DateTime<Utc>>,
|
||||
pub newest: Option<&'a DateTime<Utc>>,
|
||||
}
|
||||
|
||||
pub(super) fn bounds_from<'a>(statuses: &'a Vec<Status>) -> Page<'a> {
|
||||
Page {
|
||||
newest: statuses.first().map(|s| &s.created_at),
|
||||
oldest_id: statuses.last().map(|s| s.id.clone()),
|
||||
oldest: statuses.last().map(|s| &s.created_at),
|
||||
}
|
||||
}
|
43
src/range.rs
Normal file
43
src/range.rs
Normal file
|
@ -0,0 +1,43 @@
|
|||
use anyhow::{bail, format_err, Result};
|
||||
use chrono::{DateTime, Local, LocalResult, NaiveDate, TimeZone};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) struct Range {
|
||||
pub start: DateTime<Local>,
|
||||
pub end: DateTime<Local>,
|
||||
}
|
||||
|
||||
pub(super) fn try_create_range<S: AsRef<str>>(date: S) -> Result<Range> {
|
||||
Ok(Range {
|
||||
start: create_day_bound(&date, 0, 0, 0)?,
|
||||
end: create_day_bound(date, 23, 59, 59)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn create_day_bound<S: AsRef<str>>(
|
||||
day: S,
|
||||
hour: u32,
|
||||
minute: u32,
|
||||
second: u32,
|
||||
) -> Result<DateTime<Local>> {
|
||||
let ts: Vec<&str> = day.as_ref().split("-").collect();
|
||||
if ts.len() != 3 {
|
||||
bail!("Invalid date format! {}", day.as_ref())
|
||||
}
|
||||
let (year, month, day) = if let [year, month, day, ..] = &ts[..] {
|
||||
(year, month, day)
|
||||
} else {
|
||||
bail!("Invalid date format! {}", day.as_ref())
|
||||
};
|
||||
let b = Local.from_local_datetime(
|
||||
&NaiveDate::from_ymd_opt(year.parse()?, month.parse()?, day.parse()?)
|
||||
.ok_or_else(|| format_err!("Invalid date!"))?
|
||||
.and_hms_opt(hour, minute, second)
|
||||
.ok_or_else(|| format_err!("Invalid time!"))?,
|
||||
);
|
||||
if let LocalResult::Single(b) = b {
|
||||
Ok(b)
|
||||
} else {
|
||||
bail!("Cannot construct day boundary!")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue