Skip to content

Commit

Permalink
dev: update jmdict metadata only if downloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueGreenMagick committed Jun 25, 2024
1 parent 923d40c commit b7d01af
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions dictionary/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ fn main() -> Result<()> {
std::fs::create_dir_all(&resources_dir)?;
std::fs::create_dir_all(&jmdict_dir)?;

let mut jmdict_downloaded = false;

if !jmdict_file_path.exists() {
println!("Downloading JMDict file from the web...");
download_jmdict(&jmdict_file_path)?;
jmdict_downloaded = true;
}

println!("Parsing downloaded JMDict xml file...",);
Expand All @@ -41,22 +44,24 @@ fn main() -> Result<()> {

write_yomikiri_dictionary(&mut output_index_writer, &mut output_writer, &entries)?;

println!("Writing metadata.json...");
let download_time = Utc::now();
let download_time = download_time.to_rfc3339_opts(chrono::SecondsFormat::Millis, true);
let index_file_size = fs::metadata(&output_index_path)?.len();
let entries_file_size = fs::metadata(&output_path)?.len();
let file_size = index_file_size + entries_file_size;
if (jmdict_downloaded) {
println!("Writing metadata.json...");
let download_time = Utc::now();
let download_time = download_time.to_rfc3339_opts(chrono::SecondsFormat::Millis, true);
let index_file_size = fs::metadata(&output_index_path)?.len();
let entries_file_size = fs::metadata(&output_path)?.len();
let file_size = index_file_size + entries_file_size;

let metadata_json = format!(
"{{
\"downloadDate\": \"{}\",
\"filesSize\": {}
}}",
download_time, file_size
);
let metadata_json_file = resources_dir.join("dictionary-metadata.json");
fs::write(&metadata_json_file, &metadata_json)?;
let metadata_json = format!(
"{{
\"downloadDate\": \"{}\",
\"filesSize\": {}
}}",
download_time, file_size
);
let metadata_json_file = resources_dir.join("dictionary-metadata.json");
fs::write(&metadata_json_file, &metadata_json)?;
}

println!("Data writing complete.");
Ok(())
Expand Down

0 comments on commit b7d01af

Please sign in to comment.