diff options
| author | Xe Iaso <me@christine.website> | 2022-10-30 14:22:56 -0400 |
|---|---|---|
| committer | Xe Iaso <me@christine.website> | 2022-10-30 14:22:56 -0400 |
| commit | c654d84537a50e164c57852fc89216eec8e55d69 (patch) | |
| tree | 7844201ca37fe998309f81b000dc86477d05e4ca /src | |
| parent | 79a0a167ee87e925091a55f7de88d02bcea42c92 (diff) | |
| download | xesite-c654d84537a50e164c57852fc89216eec8e55d69.tar.xz xesite-c654d84537a50e164c57852fc89216eec8e55d69.zip | |
start working on a mastodon post embed tag
Signed-off-by: Xe Iaso <me@christine.website>
Diffstat (limited to 'src')
| -rw-r--r-- | src/bin/fetch_mastodon_post.rs | 56 | ||||
| -rw-r--r-- | src/lib.rs | 7 |
2 files changed, 63 insertions, 0 deletions
diff --git a/src/bin/fetch_mastodon_post.rs b/src/bin/fetch_mastodon_post.rs new file mode 100644 index 0000000..06faa57 --- /dev/null +++ b/src/bin/fetch_mastodon_post.rs @@ -0,0 +1,56 @@ +use color_eyre::Result; +use sha2::{Digest, Sha256}; +use std::{env, fs}; +use tracing::debug; +use xesite_types::mastodon::{Toot, User}; + +#[tokio::main] +async fn main() -> Result<()> { + color_eyre::install()?; + tracing_subscriber::fmt::init(); + + let args: Vec<String> = env::args().collect(); + debug!("{args:?}"); + if args.len() != 2 { + eprintln!("Usage: {} <mastodon post URL>", args[0]); + } + + let mut post_url = args[1].clone(); + if !post_url.ends_with(".json") { + post_url = format!("{post_url}.json"); + } + + let toot: Toot = reqwest::get(&post_url) + .await? + .error_for_status()? + .json() + .await?; + + debug!("got post by {}", toot.attributed_to); + + fs::create_dir_all("./data/toots")?; + + let post_hash = xesite::hash_string(post_url); + + let mut fout = fs::File::create(&format!("./data/toots/{post_hash}.json"))?; + serde_json::to_writer_pretty(&mut fout, &toot)?; + + let user_url = format!("{}.json", toot.attributed_to); + + let user: User = reqwest::get(&user_url) + .await? + .error_for_status()? + .json() + .await?; + + fs::create_dir_all("./data/users")?; + + debug!("got user {} ({})", user.name, user.preferred_username); + + let user_hash = xesite::hash_string(user_url); + + let mut fout = fs::File::create(&format!("./data/toots/{user_hash}.json"))?; + serde_json::to_writer_pretty(&mut fout, &user)?; + + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..1fc1661 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,7 @@ +use sha2::{Digest, Sha256}; + +pub fn hash_string(inp: String) -> String { + let mut h = Sha256::new(); + h.update(&inp.as_bytes()); + hex::encode(h.finalize()) +} |
