diff --git a/README.md b/README.md index 65f6c7b..6a96cc4 100644 --- a/README.md +++ b/README.md @@ -20,4 +20,9 @@ folder named after your username: $ prose_dl ``` +Will download the special files too: +```bash +$ prose_dl -s +``` + More info with the `--help` option. diff --git a/src/download.rs b/src/download.rs index 0ce4d3e..19121ca 100644 --- a/src/download.rs +++ b/src/download.rs @@ -1,23 +1,46 @@ -/// Download all the posts from the raw endpoint -pub async fn download_posts(posts: (String, Vec), dir: String) { +/// Download all the posts +pub async fn download_posts( + posts: (String, Vec), + dir: String, + download_special_files: bool, +) { // Create folder, silently ignore if already exists std::fs::create_dir(&dir).unwrap_or_default(); + // Download all the posts + for post in posts.1 { + download(&posts.0, &dir, post, "md").await; + } + + // Check if specials files need to be downloaded + if download_special_files { + let special_files = [ + (String::from("_readme"), "md"), + (String::from("_styles"), "css"), + ]; + + for file in special_files { + download(&posts.0, &dir, file.0, file.1).await; + } + } +} + +/// Download a file from the raw endpoint +async fn download(url: &String, output_dir: &String, post_name: String, extension: &str) { // 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(), - ) + let data = reqwest::get(format!("{}/{}/{}", url, endpoint, post_name)) + .await + .unwrap() + .text() + .await .unwrap(); + + if data != "post not found\n" { + // Write file with the content + let mut file = + std::fs::File::create(format!("{}/{}.{}", output_dir, post_name, extension)).unwrap(); + std::io::Write::write_all(&mut file, data.as_bytes()).unwrap(); } } diff --git a/src/main.rs b/src/main.rs index b51d9c9..2b94dfc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,10 @@ struct Cli { /// Scheme: HTTP/HTTPS #[clap(long, value_parser, default_value = "https")] scheme: String, + + /// Download special files + #[clap(short, takes_value = false)] + special_files: bool, } #[tokio::main] @@ -47,5 +51,5 @@ async fn main() { }; // Download the posts - download::download_posts(posts, directory).await; + download::download_posts(posts, directory, cli.special_files).await; }