From 2423f080a1301bc3af765c5832141e6a2e552dde Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 20 Aug 2022 20:24:29 +0200 Subject: [PATCH] download the posts --- src/download.rs | 23 +++++++++++++++++++++++ src/main.rs | 11 ++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/download.rs diff --git a/src/download.rs b/src/download.rs new file mode 100644 index 0000000..0ce4d3e --- /dev/null +++ b/src/download.rs @@ -0,0 +1,23 @@ +/// Download all the posts from the raw endpoint +pub async fn download_posts(posts: (String, Vec), dir: String) { + // Create folder, silently ignore if already exists + std::fs::create_dir(&dir).unwrap_or_default(); + + // Endpoint name + let endpoint = "raw"; + + for post in posts.1 { + let mut file = std::fs::File::create(format!("{}/{}.md", dir, post)).unwrap(); + std::io::Write::write_all( + &mut file, + reqwest::get(format!("{}/{}/{}", posts.0, endpoint, post)) + .await + .unwrap() + .text() + .await + .unwrap() + .as_bytes(), + ) + .unwrap(); + } +} diff --git a/src/main.rs b/src/main.rs index b65330d..b51d9c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use clap::Parser; +mod download; mod parse; #[derive(Parser)] @@ -31,6 +32,7 @@ struct Cli { async fn main() { let cli = Cli::parse(); + // Retrieve user's posts let posts = parse::get_posts( cli.scheme.to_lowercase(), cli.username.to_lowercase(), @@ -38,5 +40,12 @@ async fn main() { ) .await; - println!("{:#?}", posts); + // Defines the output folder name + let directory = match cli.directory { + Some(loc) => loc, + None => cli.username, + }; + + // Download the posts + download::download_posts(posts, directory).await; }