Skip to content

Commit

Permalink
Add download command
Browse files Browse the repository at this point in the history
  • Loading branch information
shigedangao committed Feb 28, 2024
1 parent 4b330eb commit 77d2191
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 38 deletions.
94 changes: 80 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions nado/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ scraper = "0.18.1"
tokio = { version = "1.34.0", features = ["full"] }
reqwest = "0.11.22"
indicatif = "0.17.7"
tempfile = "3.10.0"
58 changes: 58 additions & 0 deletions nado/src/cmd/download.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use super::{CommandRunner, DownloadArgs};
use anyhow::{anyhow, Result};
use std::fs::{self, File};
use std::io::ErrorKind;
use std::io::{copy, Cursor};
use std::process::Command;
use tempfile::Builder;

// Constant
const URL: &str = "https://www.mdbg.net/chinese/export/cedict/cedict_1_0_ts_utf-8_mdbg.zip";
const CEDICT: &str = "cedict";

pub struct Downloader {
args: DownloadArgs,
}

impl Downloader {
pub fn new(args: DownloadArgs) -> Self {
Downloader { args }
}
}

impl CommandRunner for Downloader {
async fn run(&self) -> Result<()> {
let tmp_dir = Builder::new().prefix(CEDICT).tempdir()?;
let bytes = reqwest::get(URL).await?.bytes().await?;
let mut content = Cursor::new(bytes);

let fname = tmp_dir.path().join("cedict.zip");
let mut dest = File::create(&fname)?;

copy(&mut content, &mut dest)?;

// Check whether the targeted path exist
fs::create_dir_all(&self.args.output_path)
.map_err(|err| {
if let ErrorKind::AlreadyExists = err.kind() {
return Ok(());
}

Err(err)
})
.map_err(|err| anyhow!("Unable to create the directory {:?}", err))?;

// Unzip the file to the targeted path
let output = Command::new("unzip")
.arg("-o")
.arg(fname.to_str().unwrap())
.arg("-d")
.arg(format!("{}/cedict.u8", self.args.output_path))
.output()
.map_err(|err| anyhow!("Expect to unzip the targeted cedict file {err}"))?;

dbg!(output);

Ok(())
}
}
22 changes: 15 additions & 7 deletions nado/src/cmd/generate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{CliArgs, CommandRunner, OutputFormat};
use super::{CommandRunner, GenerateArgs, OutputFormat};
use crate::hsk;
use crate::progress::ProgressBuilder;
use crate::{hsk::HSKLevel, util};
Expand All @@ -19,7 +19,9 @@ const CSV_HEADERS: [&str; 8] = [
];

#[derive(Debug)]
pub struct Gen;
pub struct Gen {
args: GenerateArgs,
}

#[derive(Debug, Default, Clone, Serialize)]
pub struct CedictItem {
Expand All @@ -31,9 +33,15 @@ pub struct CedictItem {
pub hsk_level: Option<HSKLevel>,
}

impl Gen {
pub fn new(args: GenerateArgs) -> Self {
Self { args }
}
}

impl CommandRunner for Gen {
async fn run(args: &CliArgs) -> Result<()> {
let path = PathBuf::from(&args.file_path);
async fn run(&self) -> Result<()> {
let path = PathBuf::from(&self.args.file_path);

println!("📖 - Loading cedict dictionary");
// Load the Cedict dictionary
Expand Down Expand Up @@ -75,15 +83,15 @@ impl CommandRunner for Gen {

println!(
"🖊️ - Generating target file with the path {}",
args.output_path
self.args.output_path
);

let output = match args.output_format {
let output = match self.args.output_format {
OutputFormat::Json => serde_json::to_string(&items)?,
OutputFormat::Csv => util::as_csv_string(&items, Some(CSV_HEADERS.to_vec()))?,
};

std::fs::write(&args.output_path, output)?;
std::fs::write(&self.args.output_path, output)?;

Ok(())
}
Expand Down
Loading

0 comments on commit 77d2191

Please sign in to comment.