feat: template generation
This commit is contained in:
parent
cea456db6c
commit
2fb8f1dbbf
144
Cargo.lock
generated
144
Cargo.lock
generated
|
@ -17,6 +17,21 @@ version = "1.0.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||
|
||||
[[package]]
|
||||
name = "android-tzdata"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
|
||||
|
||||
[[package]]
|
||||
name = "android_system_properties"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "0.6.15"
|
||||
|
@ -163,6 +178,12 @@ version = "2.6.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "1.7.1"
|
||||
|
@ -184,6 +205,20 @@ version = "1.0.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.38"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401"
|
||||
dependencies = [
|
||||
"android-tzdata",
|
||||
"iana-time-zone",
|
||||
"js-sys",
|
||||
"num-traits",
|
||||
"wasm-bindgen",
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.5.17"
|
||||
|
@ -257,6 +292,12 @@ version = "1.0.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0"
|
||||
|
||||
[[package]]
|
||||
name = "core-foundation-sys"
|
||||
version = "0.8.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
|
||||
|
||||
[[package]]
|
||||
name = "equivalent"
|
||||
version = "1.0.1"
|
||||
|
@ -418,6 +459,29 @@ dependencies = [
|
|||
"libm",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "iana-time-zone"
|
||||
version = "0.1.60"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141"
|
||||
dependencies = [
|
||||
"android_system_properties",
|
||||
"core-foundation-sys",
|
||||
"iana-time-zone-haiku",
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
"windows-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "iana-time-zone-haiku"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
|
||||
dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indenter"
|
||||
version = "0.3.3"
|
||||
|
@ -446,6 +510,15 @@ version = "1.0.11"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.70"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a"
|
||||
dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jsonrpsee"
|
||||
version = "0.24.3"
|
||||
|
@ -531,6 +604,12 @@ dependencies = [
|
|||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.4"
|
||||
|
@ -803,6 +882,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"askama",
|
||||
"base64",
|
||||
"chrono",
|
||||
"clap",
|
||||
"color-eyre",
|
||||
"futures",
|
||||
|
@ -1049,6 +1129,70 @@ version = "0.11.0+wasi-snapshot-preview1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.93"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"once_cell",
|
||||
"wasm-bindgen-macro",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-backend"
|
||||
version = "0.2.93"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"log",
|
||||
"once_cell",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.93"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"wasm-bindgen-macro-support",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro-support"
|
||||
version = "0.2.93"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-backend",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-shared"
|
||||
version = "0.2.93"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484"
|
||||
|
||||
[[package]]
|
||||
name = "windows-core"
|
||||
version = "0.52.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9"
|
||||
dependencies = [
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.52.0"
|
||||
|
|
|
@ -6,6 +6,7 @@ edition = "2021"
|
|||
[dependencies]
|
||||
askama = "0.12.1"
|
||||
base64 = "0.22.1"
|
||||
chrono = "0.4.38"
|
||||
clap = { version = "4.5.17", features = ["derive"] }
|
||||
color-eyre = "0.6.3"
|
||||
futures = "0.3.30"
|
||||
|
|
89
src/main.rs
89
src/main.rs
|
@ -1,10 +1,12 @@
|
|||
use std::fs::{create_dir_all, write, OpenOptions};
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
use std::fs::{create_dir_all, write, File, OpenOptions};
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Stdio;
|
||||
|
||||
use askama::Template;
|
||||
use base64::prelude::BASE64_STANDARD;
|
||||
use base64::Engine;
|
||||
use chrono::{DateTime, Local, Timelike};
|
||||
use clap::{command, Parser};
|
||||
use color_eyre::eyre::Result;
|
||||
use futures::future::join_all;
|
||||
|
@ -30,6 +32,69 @@ struct Config {
|
|||
///The data folder to use for temporary storage
|
||||
#[arg(short, long)]
|
||||
data_folder: String,
|
||||
|
||||
///The output folder where the posts will be generated
|
||||
#[arg(short, long)]
|
||||
output_folder: String,
|
||||
|
||||
///The time of day when generate the post
|
||||
#[arg(short, long)]
|
||||
time: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Template)]
|
||||
#[template(path = "post.md")]
|
||||
struct PostTemplate {
|
||||
date: String,
|
||||
date_title: String,
|
||||
messages: Vec<Message>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct Message {
|
||||
content: String,
|
||||
attachments: Vec<String>,
|
||||
time: String,
|
||||
}
|
||||
|
||||
async fn generate_post(config: &Config, client: &mut Client) -> Result<()> {
|
||||
let day: DateTime<Local> = Local::now();
|
||||
let _hour = day.hour();
|
||||
let folder: PathBuf = [config.output_folder.clone(), day.to_rfc3339()]
|
||||
.iter()
|
||||
.collect();
|
||||
if !folder.exists() {
|
||||
create_dir_all(&folder)?;
|
||||
}
|
||||
let mut template = PostTemplate {
|
||||
date: day.to_rfc3339(),
|
||||
date_title: day.format("%d.%m.%Y").to_string(),
|
||||
..Default::default()
|
||||
};
|
||||
let mut msgs = Vec::new();
|
||||
let file = File::open(format!("{}/texts.json", config.data_folder))?;
|
||||
let reader = BufReader::new(file);
|
||||
for line in reader.lines() {
|
||||
let v = serde_json::from_str(&line?)?;
|
||||
let mut attachments = Vec::new();
|
||||
if let Some(i) = get_msg_attachments(client, &v).await {
|
||||
for (id, content) in i {
|
||||
let path = folder.join(id);
|
||||
save_picture(&path, &content)?;
|
||||
attachments.push(id.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
msgs.push(Message {
|
||||
content: get_msg_text(&v).unwrap_or("").to_string(),
|
||||
attachments,
|
||||
time: get_msg_time(&v).unwrap(),
|
||||
});
|
||||
}
|
||||
template.messages = msgs;
|
||||
|
||||
println!("{}", template.render()?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
|
@ -58,10 +123,10 @@ async fn main() -> Result<()> {
|
|||
let sender = get_msg_sender(&v);
|
||||
let text = get_msg_text(&v);
|
||||
let at = get_msg_attachments(&mut client, &v).await;
|
||||
println!("{:?}", sender);
|
||||
if text.is_none() && at.is_none() {
|
||||
continue;
|
||||
} else if sender != Some(&config.allowed_sender) {
|
||||
println!("{:?}", sender);
|
||||
if let Some(x) = sender {
|
||||
client.send(
|
||||
None,
|
||||
|
@ -94,11 +159,15 @@ async fn main() -> Result<()> {
|
|||
};
|
||||
println!("{v}");
|
||||
println!("{:?}", text);
|
||||
if text == Some("post") {
|
||||
generate_post(&config, &mut client).await?;
|
||||
} else {
|
||||
let mut file = OpenOptions::new()
|
||||
.append(true)
|
||||
.create(true)
|
||||
.open(format!("{}/texts.json", config.data_folder))?;
|
||||
writeln!(file, "{}", v.to_string())?;
|
||||
}
|
||||
|
||||
//let at = get_msg_attachments(&mut client, &v).await;
|
||||
//if let Some(i) = at {
|
||||
|
@ -114,8 +183,8 @@ async fn main() -> Result<()> {
|
|||
}
|
||||
|
||||
/// make sure the directory exists before trying to save into it
|
||||
fn save_picture(name: &str, content: &str) -> Result<()> {
|
||||
write(format!("{}", name), BASE64_STANDARD.decode(content)?)?;
|
||||
fn save_picture(file: &PathBuf, content: &str) -> Result<()> {
|
||||
write(file, BASE64_STANDARD.decode(content)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -154,6 +223,14 @@ fn get_msg_sender(val: &Value) -> Option<&str> {
|
|||
val.pointer("/envelope/source").and_then(|x| x.as_str())
|
||||
}
|
||||
|
||||
fn get_msg_time(val: &Value) -> Option<String> {
|
||||
let time = val
|
||||
.pointer("/envelope/timestamp")
|
||||
.and_then(|x| x.as_i64())?;
|
||||
let time = DateTime::from_timestamp(time, 0)?;
|
||||
Some(time.format("%H:%M").to_string())
|
||||
}
|
||||
|
||||
fn get_msg_text(val: &Value) -> Option<&str> {
|
||||
val.pointer("/envelope/dataMessage/message")
|
||||
.and_then(|x| x.as_str())
|
||||
|
|
24
templates/post.md
Normal file
24
templates/post.md
Normal file
|
@ -0,0 +1,24 @@
|
|||
+++
|
||||
title = "Journal for {{ date_title }}"
|
||||
date = {{ date }}
|
||||
draft = false
|
||||
|
||||
[taxonomies]
|
||||
tags = ["Signal to blog"]
|
||||
+++
|
||||
|
||||
{% for msg in messages %}
|
||||
|
||||
# {{ msg.time }}
|
||||
|
||||
{% if msg.attachments.len() == 0 %}
|
||||
{{- msg.content -}}
|
||||
{% else %}
|
||||
{% for path in msg.attachments %}
|
||||
{{ "{{" }} figure(src="./{{ path }}" {% if loop.last -%}
|
||||
, caption="{{ msg.content }}"
|
||||
{%- endif -%}
|
||||
{%- endfor -%}
|
||||
) {{ "}}" }}
|
||||
{%- endif %}
|
||||
{% endfor %}
|
Loading…
Reference in a new issue