From 1dd5f7be6a4c74e897245fb6932c350f9aa61fa2 Mon Sep 17 00:00:00 2001 From: Thomas Gideon Date: Wed, 6 Sep 2023 10:30:50 -0400 Subject: [PATCH] Create the entry if it doesn't exist. --- src/format.rs | 5 ++++- src/main.rs | 23 +++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/format.rs b/src/format.rs index 4ff619c..ad023ab 100644 --- a/src/format.rs +++ b/src/format.rs @@ -25,7 +25,10 @@ pub(super) async fn format_status( time_prefix = time_prefix, timestamp = status.created_at.with_timezone(&Local).format("%H:%M"), ancestor_prefix = ancestor_prefix, - status = apply_block_quote(depth, (&status.content).trim()), + status = apply_block_quote( + depth, + (status.content.replace("'", "'").replace("\"_", "\"")).trim() + ), attachments = format_attachments(depth, &status.media_attachments) ); let Response { json, .. } = client.get_status_context(status.id.clone(), None).await?; diff --git a/src/main.rs b/src/main.rs index e0b8d59..1f137ca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{Context, Result}; use chrono::{DateTime, Local, Utc}; use clap::{arg, command, Parser}; use log::{debug, trace}; @@ -9,6 +9,7 @@ use megalodon::{ response::Response, Megalodon, }; +use tokio::fs::try_exists; use tokio_stream::{iter, StreamExt}; use std::{env, fs::File, io::prelude::*}; @@ -121,9 +122,23 @@ async fn main() -> Result<()> { reversed.reverse(); if let Some(output_dir) = output_dir { - let output = format!("{}{}.md", output_dir, date); - let mut f = File::options().append(true).open(&output)?; - f.write_all(&reversed.join("\n\n").as_bytes())?; + let output = format!("{}/{}.md", output_dir.trim_end_matches("/"), date); + let mut f = match try_exists(&output).await { + Ok(exists) if exists => { + debug!("Appending {}", output); + File::options().append(true).open(&output)? + } + _ => { + debug!("Writing {}", output); + File::options() + .create(true) + .append(true) + .open(&output) + .with_context(|| format!("Failed to create {}", output))? + } + }; + f.write_all(&reversed.join("\n\n").as_bytes()) + .with_context(|| format!("Failed to write all to {}", output))?; println!("Appended matching posts to {}.", output); } else { println!("{}", reversed.join("\n\n"));