download the posts

This commit is contained in:
Mylloon 2022-08-20 20:24:29 +02:00
parent cd7b5434e6
commit 2423f080a1
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 33 additions and 1 deletions

23
src/download.rs Normal file
View file

@ -0,0 +1,23 @@
/// Download all the posts from the raw endpoint
pub async fn download_posts(posts: (String, Vec<String>), 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();
}
}

View file

@ -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;
}