From 1e3b54b33cb8c496ec623e1fba93629f0d0e8abd Mon Sep 17 00:00:00 2001 From: B0SE Date: Wed, 11 Sep 2024 15:35:07 +0200 Subject: [PATCH] add new version system --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/data.rs | 36 ++++++++++++++++++++---------------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 42d3fe7..0fc9388 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,7 +34,7 @@ dependencies = [ [[package]] name = "ani-dl" -version = "1.5.5" +version = "1.5.7" dependencies = [ "chrono", "colored", diff --git a/Cargo.toml b/Cargo.toml index 4a62a73..65813e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ani-dl" -version = "1.5.6" +version = "1.5.7" edition = "2021" license-file = "LICENSE" description = "Download and watch animes in VF and VOSTFR (aka french versions)" diff --git a/src/data.rs b/src/data.rs index 87a6e39..e22513e 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,33 +1,35 @@ -use chrono::DateTime; use directories::ProjectDirs; -use std::fs::metadata; use std::io; use std::path::Path; -use std::time::Duration; -use std::time::{SystemTime, UNIX_EPOCH}; const ANIME_DATA_URL: &str = "https://github.com/S3nda/ani-data/raw/main/anime_data.json"; -const LATEST_COMMIT_URL: &str = - "https://raw.githubusercontent.com/S3nda/ani-data/main/last_commit_time.txt"; +const LATEST_VERSION: &str = "https://raw.githubusercontent.com/S3nda/ani-data/main/version.txt"; -fn get_last_commit_time() -> SystemTime { - let file = reqwest::blocking::get(LATEST_COMMIT_URL) +fn get_last_version() -> u32 { + let file = reqwest::blocking::get(LATEST_VERSION) .unwrap() .text() .unwrap(); - let date_time = DateTime::parse_from_rfc3339(file.trim()).unwrap(); - UNIX_EPOCH + Duration::from_secs(date_time.timestamp() as u64) + file.trim().parse().unwrap() } -fn get_file_modification_time(file_path: &Path) -> SystemTime { - let meta = metadata(file_path).unwrap(); - meta.modified().unwrap() +fn get_local_version(file_path: &Path) -> u32 { + if !file_path.exists() { + let version = 1; + std::fs::write(file_path, version.to_string()).expect("Failed to write file"); + } + let file = std::fs::read_to_string(file_path).expect("Failed to read file"); + file.trim().parse().unwrap() +} + +fn set_local_version(file_path: &Path, version: u32) { + std::fs::write(file_path, version.to_string()).expect("Failed to write file"); } fn download_file(file_path: &Path) { let mut resp = reqwest::blocking::get(ANIME_DATA_URL) .expect("Echec lors du téléchargement du fichier, vérifiez votre connexion internet"); - if resp.content_length().unwrap() < 500_000 { + if resp.content_length().unwrap() < 500_000 && file_path.exists() { println!("Ignoring new file, file < 500KB, scrapper probably messed up again (complain to S3nda)."); return; } @@ -39,8 +41,9 @@ pub fn get_file(overwrite: bool) { let dir = ProjectDirs::from("", "B0SE", "ani-dl").expect("Failed to get project directory"); let data_dir = dir.data_dir(); let file_path = data_dir.join("anime_data.json"); + let ver_file_path = data_dir.join("version.txt"); - let last_commit_time = get_last_commit_time(); + let last_version = get_last_version(); if !data_dir.exists() { std::fs::create_dir_all(data_dir).expect("Failed to create data directory"); @@ -50,8 +53,9 @@ pub fn get_file(overwrite: bool) { download_file(&file_path); } - if last_commit_time > get_file_modification_time(&file_path) { + if last_version > get_local_version(&ver_file_path) { println!("Mise à jour des données..."); + set_local_version(&ver_file_path, last_version); download_file(&file_path); } }