Compare commits

...

2 commits

Author SHA1 Message Date
1e09947b68
wip: parsing school range 2022-08-15 15:46:58 +02:00
449b200629
add regex 2022-08-15 15:46:41 +02:00
3 changed files with 70 additions and 1 deletions

27
Cargo.lock generated
View file

@ -2,6 +2,15 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
"memchr",
]
[[package]]
name = "autocfg"
version = "1.1.0"
@ -42,6 +51,7 @@ checksum = "ec8a7b6a70fde80372154c65702f00a0f56f3e1c36abbc6c440484be248856db"
name = "cal8tor"
version = "0.1.0"
dependencies = [
"regex",
"reqwest",
"scraper",
"tokio",
@ -893,6 +903,23 @@ dependencies = [
"bitflags",
]
[[package]]
name = "regex"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244"
[[package]]
name = "remove_dir_all"
version = "0.5.3"

View file

@ -9,3 +9,4 @@ edition = "2021"
reqwest = { version = "0.11" }
tokio = { version = "1", features = ["full"] }
scraper = "0.13.0"
regex = "1"

View file

@ -1,3 +1,4 @@
use regex::Regex;
use scraper::{Html, Selector};
pub async fn info() {
@ -5,13 +6,53 @@ pub async fn info() {
// Selectors
let sel_ul = Selector::parse("ul").unwrap();
let sel_li = Selector::parse("li").unwrap();
// Find the raw infos in html page
let mut raw_data = Vec::new();
for (i, data) in document.select(&sel_ul).enumerate() {
if [1, 2].contains(&i) {
println!("\n{} - {:#?}", data.value().name(), data.inner_html());
raw_data.push(data);
}
}
let mut data = std::collections::HashMap::new();
// d => date
// r => repetition
let re = Regex::new(r"(?P<d>\d{1,2} \w+ \d{4}).+(?P<r>\d)").unwrap();
for (i, ul) in raw_data.into_iter().enumerate() {
for element in ul.select(&sel_li) {
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()
),
);
}
e if e.starts_with("Reprise") => {
let captures = re.captures(&e).unwrap();
captures.name("g");
data.insert(
i,
format!(
"{} puis reprise {} pendant {}s",
data.get(&i).unwrap(),
captures.name("d").unwrap().as_str(),
captures.name("r").unwrap().as_str()
),
);
}
_ => (),
}
}
}
println!("{:#?}", data);
}
async fn get_webpage() -> Result<Html, Box<dyn std::error::Error>> {