Skip to content

Commit

Permalink
Merge pull request #21 from B0SEmc/new-version-system
Browse files Browse the repository at this point in the history
add new version system
  • Loading branch information
B0SEmc authored Sep 11, 2024
2 parents b5199e5 + 1e3b54b commit 7522765
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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)"
Expand Down
36 changes: 20 additions & 16 deletions src/data.rs
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -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");
Expand All @@ -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);
}
}

0 comments on commit 7522765

Please sign in to comment.