diff options
author | Vika <vika@fireburn.ru> | 2021-07-29 18:14:30 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2021-07-29 18:14:30 +0300 |
commit | 949a961c19ba994c3f0846e7b54d9a55a94d7b9a (patch) | |
tree | b9d2031e1b08dcebf8f22c6051dea4da6e4f40ab /src/bin | |
parent | bd38e2f19b8c2c353ad759c1631b93b0f8eafbb0 (diff) | |
download | kittybox-949a961c19ba994c3f0846e7b54d9a55a94d7b9a.tar.zst |
Appease rustfmt, clippy and cargo check
Diffstat (limited to 'src/bin')
-rw-r--r-- | src/bin/kittybox_bulk_import.rs | 36 | ||||
-rw-r--r-- | src/bin/pyindieblog_to_kittybox.rs | 59 |
2 files changed, 66 insertions, 29 deletions
diff --git a/src/bin/kittybox_bulk_import.rs b/src/bin/kittybox_bulk_import.rs index a5252b7..7e1f6af 100644 --- a/src/bin/kittybox_bulk_import.rs +++ b/src/bin/kittybox_bulk_import.rs @@ -1,6 +1,6 @@ -use std::io; +use anyhow::{anyhow, bail, Context, Result}; use std::fs::File; -use anyhow::{anyhow, Context, Result, bail}; +use std::io; #[async_std::main] async fn main() -> Result<()> { @@ -8,18 +8,22 @@ async fn main() -> Result<()> { if args.iter().skip(1).any(|s| s == "--help") { println!("Usage: {} <url> [file]", args[0]); println!("\nIf launched with no arguments, reads from stdin."); - println!("\nUse KITTYBOX_AUTH_TOKEN environment variable to authorize to the Micropub endpoint."); + println!( + "\nUse KITTYBOX_AUTH_TOKEN environment variable to authorize to the Micropub endpoint." + ); std::process::exit(0); } - let token = std::env::var("KITTYBOX_AUTH_TOKEN").map_err(|_| anyhow!("No auth token found! Use KITTYBOX_AUTH_TOKEN env variable."))?; + let token = std::env::var("KITTYBOX_AUTH_TOKEN") + .map_err(|_| anyhow!("No auth token found! Use KITTYBOX_AUTH_TOKEN env variable."))?; let data: Vec<serde_json::Value> = (if args.len() == 2 || (args.len() == 3 && args[2] == "-") { serde_json::from_reader(io::stdin()) } else if args.len() == 3 { serde_json::from_reader(File::open(&args[2]).with_context(|| "Error opening input file")?) } else { bail!("See `{} --help` for usage.", args[0]); - }).with_context(|| "Error while loading the input file")?; + }) + .with_context(|| "Error while loading the input file")?; let url = surf::Url::parse(&args[1])?; let client = surf::Client::new(); @@ -27,13 +31,25 @@ async fn main() -> Result<()> { let iter = data.into_iter(); for post in iter { - println!("Processing {}...", post["properties"]["url"][0].as_str().or_else(|| post["properties"]["published"][0].as_str().or_else(|| post["properties"]["name"][0].as_str().or(Some("<unidentified post>")))).unwrap()); - match client.post(&url) - .body(surf::http::Body::from_string( - serde_json::to_string(&post)?)) + println!( + "Processing {}...", + post["properties"]["url"][0] + .as_str() + .or_else(|| post["properties"]["published"][0] + .as_str() + .or_else(|| post["properties"]["name"][0] + .as_str() + .or(Some("<unidentified post>")))) + .unwrap() + ); + match client + .post(&url) + .body(surf::http::Body::from_string(serde_json::to_string(&post)?)) .header("Content-Type", "application/json") .header("Authorization", format!("Bearer {}", &token)) - .send().await { + .send() + .await + { Ok(mut response) => { if response.status() == 201 || response.status() == 202 { println!("Posted at {}", response.header("location").unwrap().last()); diff --git a/src/bin/pyindieblog_to_kittybox.rs b/src/bin/pyindieblog_to_kittybox.rs index c932e0a..b4e2b97 100644 --- a/src/bin/pyindieblog_to_kittybox.rs +++ b/src/bin/pyindieblog_to_kittybox.rs @@ -1,45 +1,66 @@ -use std::collections::HashMap; -use std::fs::File; -use anyhow::{Result, Context, anyhow}; +use anyhow::{anyhow, Context, Result}; use mobc_redis::redis; use mobc_redis::redis::AsyncCommands; -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::fs::File; #[derive(Default, Serialize, Deserialize)] struct PyindieblogData { posts: Vec<serde_json::Value>, - cards: Vec<serde_json::Value> + cards: Vec<serde_json::Value>, } #[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_else(|| 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 redis_uri = args + .next() + .ok_or_else(|| 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_else(|| anyhow!("No filename provided for export"))?; + let filename = args + .next() + .ok_or_else(|| anyhow!("No filename provided for export"))?; let mut data: Vec<serde_json::Value>; let file = File::create(filename)?; - let mut conn = client.get_async_std_connection().await.with_context(|| "Failed to connect to the Redis server")?; + let mut conn = client + .get_async_std_connection() + .await + .with_context(|| "Failed to connect to the Redis server")?; - data = conn.hgetall::<&str, HashMap<String, String>>("posts").await? + data = conn + .hgetall::<&str, HashMap<String, String>>("posts") + .await? .values() - .map(|s| serde_json::from_str::<serde_json::Value>(s) - .with_context(|| format!("Failed to parse the following entry: {:?}", s))) + .map(|s| { + serde_json::from_str::<serde_json::Value>(s) + .with_context(|| format!("Failed to parse the following entry: {:?}", s)) + }) .collect::<std::result::Result<Vec<serde_json::Value>, anyhow::Error>>() .with_context(|| "Failed to export h-entries from pyindieblog")?; - data.extend(conn.hgetall::<&str, HashMap<String, String>>("hcards").await? - .values() - .map(|s| serde_json::from_str::<serde_json::Value>(s) - .with_context(|| format!("Failed to parse the following card: {:?}", s))) - .collect::<std::result::Result<Vec<serde_json::Value>, anyhow::Error>>() - .with_context(|| "Failed to export h-cards from pyindieblog")?); + data.extend( + conn.hgetall::<&str, HashMap<String, String>>("hcards") + .await? + .values() + .map(|s| { + serde_json::from_str::<serde_json::Value>(s) + .with_context(|| format!("Failed to parse the following card: {:?}", s)) + }) + .collect::<std::result::Result<Vec<serde_json::Value>, 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())); + data.sort_by_key(|v| { + v["properties"]["published"][0] + .as_str() + .map(|s| s.to_string()) + }); serde_json::to_writer(file, &data)?; |