From d399fd0bd00c9ea073e5b057de70c9ffdd9356f8 Mon Sep 17 00:00:00 2001 From: Vika Shleina Date: Thu, 15 Jul 2021 04:32:29 +0300 Subject: Renamed main executable to kittybox, added tools The new tools are: - kittybox-bulk-import, a bare-bones Micropub client that reads a JSON list of posts and then sends them one by one to the Micropub endpoint - pyindieblog-export, my personal tool which directly connects to Pyindieblog's redis instance and extracts data from it in JSON format suitable for use with kittybox-bulk-import. --- src/bin/pyindieblog_to_kittybox.rs | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/bin/pyindieblog_to_kittybox.rs (limited to 'src/bin/pyindieblog_to_kittybox.rs') diff --git a/src/bin/pyindieblog_to_kittybox.rs b/src/bin/pyindieblog_to_kittybox.rs new file mode 100644 index 0000000..7935da5 --- /dev/null +++ b/src/bin/pyindieblog_to_kittybox.rs @@ -0,0 +1,48 @@ +use std::collections::HashMap; +use std::fs::File; +use anyhow::{Error, Result, Context, anyhow, bail}; +use mobc_redis::redis; +use mobc_redis::redis::AsyncCommands; +use serde::{Serialize, Deserialize}; +use serde_json::json; + +#[derive(Default, Serialize, Deserialize)] +struct PyindieblogData { + posts: Vec, + cards: Vec +} + +#[async_std::main] +async fn main() -> Result<()> { + let mut args = std::env::args(); + args.next(); // skip argv[0] which is the name + let redis_uri = args.next().ok_or(anyhow!("No Redis URI provided"))?; + let client = redis::Client::open(redis_uri.as_str()).with_context(|| format!("Failed to construct Redis client on {}", redis_uri))?; + + let filename = args.next().ok_or(anyhow!("No filename provided for export"))?; + + let mut data: Vec; + + let file = File::create(filename)?; + + let mut conn = client.get_async_std_connection().await.with_context(|| "Failed to connect to the Redis server")?; + + data = conn.hgetall::<&str, HashMap>("posts").await? + .values() + .map(|s| serde_json::from_str::(s) + .with_context(|| format!("Failed to parse the following entry: {:?}", s))) + .collect::, anyhow::Error>>() + .with_context(|| "Failed to export h-entries from pyindieblog")?; + data.extend(conn.hgetall::<&str, HashMap>("hcards").await? + .values() + .map(|s| serde_json::from_str::(s) + .with_context(|| format!("Failed to parse the following card: {:?}", s))) + .collect::, anyhow::Error>>() + .with_context(|| "Failed to export h-cards from pyindieblog")?); + + data.sort_by_key(|v| v["properties"]["published"][0].as_str().map(|s| s.to_string())); + + serde_json::to_writer(file, &data)?; + + Ok(()) +} -- cgit 1.4.1