download the posts
This commit is contained in:
parent
cd7b5434e6
commit
2423f080a1
2 changed files with 33 additions and 1 deletions
23
src/download.rs
Normal file
23
src/download.rs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
11
src/main.rs
11
src/main.rs
|
@ -1,5 +1,6 @@
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
|
mod download;
|
||||||
mod parse;
|
mod parse;
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
|
@ -31,6 +32,7 @@ struct Cli {
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
|
|
||||||
|
// Retrieve user's posts
|
||||||
let posts = parse::get_posts(
|
let posts = parse::get_posts(
|
||||||
cli.scheme.to_lowercase(),
|
cli.scheme.to_lowercase(),
|
||||||
cli.username.to_lowercase(),
|
cli.username.to_lowercase(),
|
||||||
|
@ -38,5 +40,12 @@ async fn main() {
|
||||||
)
|
)
|
||||||
.await;
|
.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;
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue