add special files support
This commit is contained in:
parent
7da61ae3ab
commit
506da11422
3 changed files with 47 additions and 15 deletions
|
@ -20,4 +20,9 @@ folder named after your username:
|
||||||
$ prose_dl <username>
|
$ prose_dl <username>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Will download the special files too:
|
||||||
|
```bash
|
||||||
|
$ prose_dl -s <username>
|
||||||
|
```
|
||||||
|
|
||||||
More info with the `--help` option.
|
More info with the `--help` option.
|
||||||
|
|
|
@ -1,23 +1,46 @@
|
||||||
/// Download all the posts from the raw endpoint
|
/// Download all the posts
|
||||||
pub async fn download_posts(posts: (String, Vec<String>), dir: String) {
|
pub async fn download_posts(
|
||||||
|
posts: (String, Vec<String>),
|
||||||
|
dir: String,
|
||||||
|
download_special_files: bool,
|
||||||
|
) {
|
||||||
// Create folder, silently ignore if already exists
|
// Create folder, silently ignore if already exists
|
||||||
std::fs::create_dir(&dir).unwrap_or_default();
|
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
|
// Endpoint name
|
||||||
let endpoint = "raw";
|
let endpoint = "raw";
|
||||||
|
|
||||||
for post in posts.1 {
|
let data = reqwest::get(format!("{}/{}/{}", url, endpoint, post_name))
|
||||||
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
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.text()
|
.text()
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
|
||||||
.as_bytes(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
.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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,10 @@ struct Cli {
|
||||||
/// Scheme: HTTP/HTTPS
|
/// Scheme: HTTP/HTTPS
|
||||||
#[clap(long, value_parser, default_value = "https")]
|
#[clap(long, value_parser, default_value = "https")]
|
||||||
scheme: String,
|
scheme: String,
|
||||||
|
|
||||||
|
/// Download special files
|
||||||
|
#[clap(short, takes_value = false)]
|
||||||
|
special_files: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
@ -47,5 +51,5 @@ async fn main() {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Download the posts
|
// Download the posts
|
||||||
download::download_posts(posts, directory).await;
|
download::download_posts(posts, directory, cli.special_files).await;
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue