Now dictionary is downloaded automatically

This commit is contained in:
leca 2024-05-14 01:11:42 +03:00
parent 8207a76a2f
commit 3896f6b044
3 changed files with 23 additions and 5 deletions

View File

@ -9,8 +9,10 @@ edition = "2021"
gtk = { version = "0.8.1", package = "gtk4", features = ["v4_8"] }
rand = "0.8.5"
reqwest = "0.12.4"
rusqlite = "0.31.0"
sha256 = "1.5.0"
tokio = { version = "1.37.0", features = ["full"] }
[build-dependencies]
dirs = "5.0.1"

View File

@ -1,8 +1,9 @@
use rusqlite::Connection;
use std::fs;
use std::io::ErrorKind;
use core::panic;
use std::fs::{self, File};
use std::io::{ErrorKind, Write};
use crate::db::get_db_path;
use crate::db::{get_db_path, get_program_home_path};
#[derive(PartialEq)]
pub struct DictionaryEntry {
@ -40,6 +41,19 @@ fn get_sym_pos_or_panic(path: &String, line: &str, sym: &str, line_number: usize
),
}
}
#[tokio::main]
async fn download_dictionary(path: String) {
let link = "https://git.foxarmy.org/leca/learn-hieroglyphs/raw/branch/master/dictionary.txt";
let response = reqwest::get(link).await;
let mut file = match File::create(&path) {
Err(e) => panic!("Could not create dictionary file. {}", e),
Ok(file) => file
};
let content = response.unwrap().bytes().await.unwrap();
file.write_all(&content).unwrap();
import(&path);
}
fn read_and_parse(path: &String) -> Vec<DictionaryEntry> {
let mut result: Vec<DictionaryEntry> = Vec::new();
@ -47,7 +61,8 @@ fn read_and_parse(path: &String) -> Vec<DictionaryEntry> {
Ok(string) => string,
Err(kind) => match kind.kind() {
ErrorKind::NotFound => {
println!("No dictionary file found. Assuming it's empty. Perhaps, you want to download it from my git? https://git.foxarmy.org/leca/learn-hieroglyphs");
println!("No dictionary file found. Downloading it to {}/dictionary.txt from https://git.foxarmy.org/leca/learn-hieroglyphs", get_program_home_path());
download_dictionary(path.to_owned());
return Vec::new();
}
ErrorKind::PermissionDenied => {

View File

@ -10,6 +10,7 @@ mod card_gobject;
use crate::ui::menu::MenuScene;
use db::get_program_home_path;
use gtk::glib::closure_local;
use gtk::glib::subclass::types::ObjectSubclassIsExt;
use gtk::{gio, glib, Application, Button};
@ -38,7 +39,7 @@ fn main() -> glib::ExitCode {
db::init(); // Create database if not exists, create program's home if not exists, etc...
dictionary::import(&String::from("./dictionary.txt")); // Read file, parse it, deduplicate and append to the database.
dictionary::import(&(get_program_home_path() + "/dictionary.txt")); // Read file, parse it, deduplicate and append to the database.
let app: Application = Application::builder().application_id(APP_ID).build();