From 6125911d0ab5f70cc2420d04a592869ed290e9c7 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 15 Aug 2022 17:25:36 +0200 Subject: [PATCH] convert string date to datetime object --- src/info.rs | 56 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/src/info.rs b/src/info.rs index 6b4a2ad..f5704d0 100644 --- a/src/info.rs +++ b/src/info.rs @@ -1,4 +1,7 @@ -use regex::Regex; +use std::collections::HashMap; + +use chrono::{TimeZone, Utc}; +use regex::{Captures, Regex}; use scraper::{Html, Selector}; pub async fn info() { @@ -16,7 +19,7 @@ pub async fn info() { } } - let mut data = std::collections::HashMap::new(); + let mut data = HashMap::new(); // d => date // r => repetition let re = Regex::new(r"(?P\d{1,2} \w+ \d{4}).+(?P\d)").unwrap(); @@ -25,14 +28,17 @@ pub async fn info() { match element.inner_html() { e if e.starts_with("Début") => { let captures = re.captures(&e).unwrap(); - data.insert( - i, - format!( - "{} pendant {}s", - captures.name("d").unwrap().as_str(), - captures.name("r").unwrap().as_str() - ), - ); + + let date = captures.name("d").unwrap().as_str(); + let rep = captures.name("r").unwrap().as_str(); + + let from = Utc + .datetime_from_str(&anglophonization(date), "%e %B %Y %H:%M") + .unwrap(); + + println!("'{}' -> {}", date, from); + + data.insert(i, format!("{} pendant {}s", date, rep)); } e if e.starts_with("Reprise") => { let captures = re.captures(&e).unwrap(); @@ -55,6 +61,7 @@ pub async fn info() { println!("{:#?}", data); } +/// Get info webpage async fn get_webpage() -> Result> { /* let html = reqwest::get("https://informatique.up8.edu/licence-iv/edt").await?.text().await?; @@ -63,3 +70,32 @@ async fn get_webpage() -> Result> { let html = include_str!("../target/debug2.html"); Ok(Html::parse_document(html)) } + +/// Turn a french date to an english one +fn anglophonization(date: &str) -> String { + let dico = HashMap::from([ + ("janvier", "january"), + ("mars", "march"), + ("septembre", "september"), + ("novembre", "november"), + ]); + + // New regex of all the french month + let re = Regex::new(&format!( + "({})", + dico.keys().cloned().collect::>().join("|") + )) + .unwrap(); + + format!( + // Use 12:00 for chrono parser + "{} 12:00", + // Replace french by english month + re.replace_all(date, |cap: &Captures| { + match &cap[0] { + month if dico.contains_key(month) => dico.get(month).unwrap(), + month => panic!("Unknown month: {}", month), + } + }) + ) +}