Archived
1
0
Fork 0
forked from Anri/cal8tor

convert string date to datetime object

This commit is contained in:
Mylloon 2022-08-15 17:25:36 +02:00
parent c8026131cf
commit 6125911d0a
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -1,4 +1,7 @@
use regex::Regex; use std::collections::HashMap;
use chrono::{TimeZone, Utc};
use regex::{Captures, Regex};
use scraper::{Html, Selector}; use scraper::{Html, Selector};
pub async fn info() { 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 // d => date
// r => repetition // r => repetition
let re = Regex::new(r"(?P<d>\d{1,2} \w+ \d{4}).+(?P<r>\d)").unwrap(); let re = Regex::new(r"(?P<d>\d{1,2} \w+ \d{4}).+(?P<r>\d)").unwrap();
@ -25,14 +28,17 @@ pub async fn info() {
match element.inner_html() { match element.inner_html() {
e if e.starts_with("Début") => { e if e.starts_with("Début") => {
let captures = re.captures(&e).unwrap(); let captures = re.captures(&e).unwrap();
data.insert(
i, let date = captures.name("d").unwrap().as_str();
format!( let rep = captures.name("r").unwrap().as_str();
"{} pendant {}s",
captures.name("d").unwrap().as_str(), let from = Utc
captures.name("r").unwrap().as_str() .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") => { e if e.starts_with("Reprise") => {
let captures = re.captures(&e).unwrap(); let captures = re.captures(&e).unwrap();
@ -55,6 +61,7 @@ pub async fn info() {
println!("{:#?}", data); println!("{:#?}", data);
} }
/// Get info webpage
async fn get_webpage() -> Result<Html, Box<dyn std::error::Error>> { async fn get_webpage() -> Result<Html, Box<dyn std::error::Error>> {
/* let html = reqwest::get("https://informatique.up8.edu/licence-iv/edt").await?.text().await?; /* let html = reqwest::get("https://informatique.up8.edu/licence-iv/edt").await?.text().await?;
@ -63,3 +70,32 @@ async fn get_webpage() -> Result<Html, Box<dyn std::error::Error>> {
let html = include_str!("../target/debug2.html"); let html = include_str!("../target/debug2.html");
Ok(Html::parse_document(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::<Vec<_>>().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),
}
})
)
}