Merge branch 'rewrite'

This commit is contained in:
Mylloon 2023-09-29 00:12:41 +02:00
commit 9e5dd7009d
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
10 changed files with 553 additions and 351 deletions

175
Cargo.lock generated
View file

@ -88,7 +88,7 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b"
dependencies = [ dependencies = [
"windows-sys", "windows-sys 0.48.0",
] ]
[[package]] [[package]]
@ -98,7 +98,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58f54d10c6dfa51283a066ceab3ec1ab78d13fae00aa49243a45e4571fb79dfd" checksum = "58f54d10c6dfa51283a066ceab3ec1ab78d13fae00aa49243a45e4571fb79dfd"
dependencies = [ dependencies = [
"anstyle", "anstyle",
"windows-sys", "windows-sys 0.48.0",
] ]
[[package]] [[package]]
@ -160,10 +160,11 @@ checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223"
[[package]] [[package]]
name = "cal7tor" name = "cal7tor"
version = "0.5.0" version = "0.5.0-alpha"
dependencies = [ dependencies = [
"chrono", "chrono",
"clap", "clap",
"dialoguer",
"ics", "ics",
"regex", "regex",
"reqwest", "reqwest",
@ -198,7 +199,7 @@ dependencies = [
"js-sys", "js-sys",
"num-traits", "num-traits",
"wasm-bindgen", "wasm-bindgen",
"windows-targets", "windows-targets 0.48.5",
] ]
[[package]] [[package]]
@ -247,6 +248,19 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "console"
version = "0.15.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c926e00cc70edefdc64d3a5ff31cc65bb97a3460097762bd23afb4d8145fccf8"
dependencies = [
"encode_unicode",
"lazy_static",
"libc",
"unicode-width",
"windows-sys 0.45.0",
]
[[package]] [[package]]
name = "core-foundation" name = "core-foundation"
version = "0.9.3" version = "0.9.3"
@ -297,6 +311,19 @@ dependencies = [
"syn 1.0.109", "syn 1.0.109",
] ]
[[package]]
name = "dialoguer"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "658bce805d770f407bc62102fca7c2c64ceef2fbcb2b8bd19d2765ce093980de"
dependencies = [
"console",
"shell-words",
"tempfile",
"thiserror",
"zeroize",
]
[[package]] [[package]]
name = "dtoa" name = "dtoa"
version = "1.0.9" version = "1.0.9"
@ -318,6 +345,12 @@ version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a68a4904193147e0a8dec3314640e6db742afd5f6e634f428a6af230d9b3591" checksum = "3a68a4904193147e0a8dec3314640e6db742afd5f6e634f428a6af230d9b3591"
[[package]]
name = "encode_unicode"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
[[package]] [[package]]
name = "encoding_rs" name = "encoding_rs"
version = "0.8.33" version = "0.8.33"
@ -335,7 +368,7 @@ checksum = "136526188508e25c6fef639d7927dfb3e0e3084488bf202267829cf7fc23dbdd"
dependencies = [ dependencies = [
"errno-dragonfly", "errno-dragonfly",
"libc", "libc",
"windows-sys", "windows-sys 0.48.0",
] ]
[[package]] [[package]]
@ -743,7 +776,7 @@ checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2"
dependencies = [ dependencies = [
"libc", "libc",
"wasi", "wasi",
"windows-sys", "windows-sys 0.48.0",
] ]
[[package]] [[package]]
@ -868,7 +901,7 @@ dependencies = [
"libc", "libc",
"redox_syscall", "redox_syscall",
"smallvec", "smallvec",
"windows-targets", "windows-targets 0.48.5",
] ]
[[package]] [[package]]
@ -1126,7 +1159,7 @@ dependencies = [
"errno", "errno",
"libc", "libc",
"linux-raw-sys", "linux-raw-sys",
"windows-sys", "windows-sys 0.48.0",
] ]
[[package]] [[package]]
@ -1141,7 +1174,7 @@ version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c3733bf4cf7ea0880754e19cb5a462007c4a8c1914bff372ccc95b464f1df88" checksum = "0c3733bf4cf7ea0880754e19cb5a462007c4a8c1914bff372ccc95b464f1df88"
dependencies = [ dependencies = [
"windows-sys", "windows-sys 0.48.0",
] ]
[[package]] [[package]]
@ -1261,6 +1294,12 @@ dependencies = [
"stable_deref_trait", "stable_deref_trait",
] ]
[[package]]
name = "shell-words"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
[[package]] [[package]]
name = "signal-hook-registry" name = "signal-hook-registry"
version = "1.4.1" version = "1.4.1"
@ -1308,7 +1347,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2538b18701741680e0322a2302176d3253a35388e2e62f172f64f4f16605f877" checksum = "2538b18701741680e0322a2302176d3253a35388e2e62f172f64f4f16605f877"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys", "windows-sys 0.48.0",
] ]
[[package]] [[package]]
@ -1381,7 +1420,7 @@ dependencies = [
"fastrand", "fastrand",
"redox_syscall", "redox_syscall",
"rustix", "rustix",
"windows-sys", "windows-sys 0.48.0",
] ]
[[package]] [[package]]
@ -1395,6 +1434,26 @@ dependencies = [
"utf-8", "utf-8",
] ]
[[package]]
name = "thiserror"
version = "1.0.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1177e8c6d7ede7afde3585fd2513e611227efd6481bd78d2e82ba1ce16557ed4"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10712f02019e9288794769fba95cd6847df9874d49d871d062172f9dd41bc4cc"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.31",
]
[[package]] [[package]]
name = "tinyvec" name = "tinyvec"
version = "1.6.0" version = "1.6.0"
@ -1426,7 +1485,7 @@ dependencies = [
"signal-hook-registry", "signal-hook-registry",
"socket2 0.5.3", "socket2 0.5.3",
"tokio-macros", "tokio-macros",
"windows-sys", "windows-sys 0.48.0",
] ]
[[package]] [[package]]
@ -1687,7 +1746,16 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f"
dependencies = [ dependencies = [
"windows-targets", "windows-targets 0.48.5",
]
[[package]]
name = "windows-sys"
version = "0.45.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"
dependencies = [
"windows-targets 0.42.2",
] ]
[[package]] [[package]]
@ -1696,7 +1764,22 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [ dependencies = [
"windows-targets", "windows-targets 0.48.5",
]
[[package]]
name = "windows-targets"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071"
dependencies = [
"windows_aarch64_gnullvm 0.42.2",
"windows_aarch64_msvc 0.42.2",
"windows_i686_gnu 0.42.2",
"windows_i686_msvc 0.42.2",
"windows_x86_64_gnu 0.42.2",
"windows_x86_64_gnullvm 0.42.2",
"windows_x86_64_msvc 0.42.2",
] ]
[[package]] [[package]]
@ -1705,51 +1788,93 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [ dependencies = [
"windows_aarch64_gnullvm", "windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc", "windows_aarch64_msvc 0.48.5",
"windows_i686_gnu", "windows_i686_gnu 0.48.5",
"windows_i686_msvc", "windows_i686_msvc 0.48.5",
"windows_x86_64_gnu", "windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm", "windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc", "windows_x86_64_msvc 0.48.5",
] ]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
[[package]] [[package]]
name = "windows_aarch64_gnullvm" name = "windows_aarch64_gnullvm"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
[[package]] [[package]]
name = "windows_aarch64_msvc" name = "windows_aarch64_msvc"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_i686_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
[[package]] [[package]]
name = "windows_i686_gnu" name = "windows_i686_gnu"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
[[package]] [[package]]
name = "windows_i686_msvc" name = "windows_i686_msvc"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
[[package]] [[package]]
name = "windows_x86_64_gnu" name = "windows_x86_64_gnu"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
[[package]] [[package]]
name = "windows_x86_64_gnullvm" name = "windows_x86_64_gnullvm"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"
[[package]] [[package]]
name = "windows_x86_64_msvc" name = "windows_x86_64_msvc"
version = "0.48.5" version = "0.48.5"
@ -1763,5 +1888,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"windows-sys", "windows-sys 0.48.0",
] ]
[[package]]
name = "zeroize"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a0956f1ba7c7909bfb66c2e9e4124ab6f6482560f6628b5aaeba39207c9aad9"

View file

@ -1,6 +1,6 @@
[package] [package]
name = "cal7tor" name = "cal7tor"
version = "0.5.0" version = "0.5.0-alpha"
authors = ["Mylloon"] authors = ["Mylloon"]
edition = "2021" edition = "2021"
description = "Timetable extractor for the Paris Cité master's degree in IT" description = "Timetable extractor for the Paris Cité master's degree in IT"
@ -19,3 +19,4 @@ chrono = "0.4.28"
ics = "0.5" ics = "0.5"
uuid = { version = "1.4", features = ["v4", "fast-rng"] } uuid = { version = "1.4", features = ["v4", "fast-rng"] }
clap = { version = "4.4", features = ["derive"] } clap = { version = "4.4", features = ["derive"] }
dialoguer = "0.11"

View file

@ -1,8 +1,6 @@
# cal7tor • *cal*endar P*7* extrac*tor* # cal7tor • *cal*endar P*7* extrac*tor*
> !! Fork de [cal8tor](https://git.mylloon.fr/Anri/cal8tor) !! > Fork de [cal8tor](https://git.mylloon.fr/Anri/cal8tor)
>
> !! En cours de dev -> ne fonctionne pas !!
Extracteur d'emploi du temps pour les masters d'informatique de Paris Cité (Diderot) Extracteur d'emploi du temps pour les masters d'informatique de Paris Cité (Diderot)
@ -22,23 +20,25 @@ Pour afficher la page d'aide
$ cal7tor --help $ cal7tor --help
``` ```
## Voir le calendrier dans le terminal <!-- ## Voir le calendrier dans le terminal
Pour les LP par exemple, lance : > Cette partie est héritée de cal8tor et n'est actuellement pas compatible avec cal7tor.
Pour les M1 par exemple, lance :
```bash ```bash
$ cal7tor lp $ cal7tor M1
``` ```
> Le rendu peut parfois être difficile à lire, n'hésites pas à utiliser l'option > Le rendu peut parfois être difficile à lire, n'hésites pas à utiliser l'option
> `-c` (ou `--cl`) pour ajuster la longueur des cellules du planning. > `-c` (ou `--cl`) pour ajuster la longueur des cellules du planning. -->
## Exporter le calendrier au format `.ics` ## Exporter le calendrier au format `.ics`
Pour les LP par exemple, lance : Pour les M1 par exemple, lance :
```bash ```bash
$ cal8tor LP --export calendar.ics $ cal8tor M1 --export calendar.ics
``` ```
> Le fichier comprend le fuseau horaire pour `Europe/Paris` et est > Le fichier comprend le fuseau horaire pour `Europe/Paris` et est

140
src/filter.rs Normal file
View file

@ -0,0 +1,140 @@
use std::collections::HashMap;
use dialoguer::MultiSelect;
use crate::timetable::models::Course;
use crate::timetable::models::Timetable;
use crate::timetable::models::Type;
use crate::utils::fill_hours;
const DISCLAIMER: &str = "(selection avec ESPACE, ENTRER pour valider)";
/// Filter the timetable
pub fn timetable(timetable: Timetable) -> Timetable {
let mut my_timetable = timetable;
courses(&mut my_timetable);
tdtp(&mut my_timetable);
my_timetable
}
/// Exclude some courses
fn courses(timetable: &mut Timetable) {
let mut multiselected = vec![];
timetable.1 .1.iter().for_each(|day| {
day.courses.iter().for_each(|course_opt| {
if let Some(course) = course_opt {
if !multiselected.contains(&course.name) {
multiselected.push(course.name.to_owned());
}
}
})
});
let defaults = vec![true; multiselected.len()];
let selections = MultiSelect::new()
.with_prompt(format!("Choisis tes matières {}", DISCLAIMER))
.items(&multiselected[..])
.defaults(&defaults[..])
.interact()
.unwrap();
for day in &mut timetable.1 .1 {
day.courses.retain(|course_opt| {
if let Some(course) = course_opt {
// Remove courses not followed
for i in &selections {
if course.name == multiselected[*i] {
return true;
}
}
}
false
});
}
}
/// Filter the multiples TD/TP
fn tdtp(timetable: &mut Timetable) {
// Entry's name used for finding duplicates
let get_entry = |course: &Course| format!("{} - {:?}", course.name, course.typee);
let mut hours = vec![];
fill_hours(&mut hours);
// Names showed to the users
let get_selection = |data: &(&Course, String)| {
format!(
"{} - {} {}-{}",
data.0.name,
data.1,
hours[data.0.start].split_once('-').unwrap().0,
hours[data.0.start + data.0.size - 1]
.split_once('-')
.unwrap()
.1
)
};
// List of courses who will be TP/TD
let mut td_or_tp = vec![];
// Counter of appearing of TP/TD to know if a TP/TD have multiple possible course
let mut counts = HashMap::new();
timetable.1 .1.iter().for_each(|day| {
day.courses.iter().for_each(|course_opt| {
if let Some(course) = course_opt {
match course.typee {
Type::TD | Type::TP => {
td_or_tp.push((course, day.name.to_owned()));
let count = counts.entry(get_entry(course)).or_insert(0);
*count += 1;
}
_ => (),
}
}
})
});
// Keep only elements who have multiples TD/TP
td_or_tp.retain(|course| *counts.get(&get_entry(course.0)).unwrap() > 1);
let mut multiselected: Vec<String> = td_or_tp.iter().map(|el| get_selection(el)).collect();
multiselected.sort();
let defaults = vec![true; multiselected.len()];
let selections = MultiSelect::new()
.with_prompt(format!("Choisis tes horaires de TD/TP {}", DISCLAIMER))
.items(&multiselected[..])
.defaults(&defaults[..])
.interact()
.unwrap();
// Keep only wanted courses
for day in &mut timetable.1 .1 {
day.courses.retain(|course_opt| {
if let Some(course) = course_opt {
// Keep if it's a course
if course.typee == Type::Cours {
return true;
}
// Keep if its an -only one course- TD/TP
if *counts.get(&get_entry(course)).unwrap() == 1 {
return true;
}
// Remove courses not followed
for i in &selections {
if get_selection(&(course, day.name.to_owned())) == multiselected[*i] {
return true;
}
}
}
false
});
}
}

View file

@ -1,76 +1,58 @@
use chrono::{DateTime, Utc}; use chrono::{DateTime, Duration, Utc};
use regex::{Captures, Regex}; use regex::{Captures, Regex};
use scraper::{Html, Selector}; use scraper::Selector;
use std::collections::HashMap; use std::collections::HashMap;
pub async fn info(user_agent: &str) -> HashMap<usize, Vec<(DateTime<Utc>, i64)>> { use crate::utils::{get_semester, get_webpage, get_year};
let document = get_webpage(user_agent)
pub async fn info(
level: i8,
semester_opt: Option<i8>,
year_opt: Option<i32>,
user_agent: &str,
) -> HashMap<usize, Vec<(DateTime<Utc>, i64)>> {
let semester = get_semester(semester_opt);
let year = get_year(year_opt, semester);
// Fetch the timetable of the FIRST semester
let document = get_webpage(level, 1, &year, user_agent)
.await .await
.expect("Can't reach info website."); .expect("Can't reach info website.");
// Selectors // Selectors
let sel_ul = Selector::parse("ul").unwrap(); let sel_b = Selector::parse("b").unwrap();
let sel_li = Selector::parse("li").unwrap(); let sel_font = Selector::parse("font").unwrap();
// Find the raw infos in html page // Find when is the back-to-school date
let mut raw_data = Vec::new(); let raw_data = document
for (i, data) in document.select(&sel_ul).enumerate() { .select(&sel_b)
if [1, 2].contains(&i) { .find(|element| element.select(&sel_font).next().is_some())
raw_data.push(data); .unwrap()
} .inner_html();
}
let mut data = HashMap::new(); let re = Regex::new(r"\d{1,2} (septembre|octobre)").unwrap();
// d => date let date = re.captures(&raw_data).unwrap().get(0).unwrap().as_str();
// 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();
let start_date = get_date(captures.name("d").unwrap().as_str()); let weeks_s1_1 = 6; // Number of weeks in the first part of the first semester
let date_s1_1 = get_date(&format!("{} {}", date, year.split_once('-').unwrap().0)); // Get week of back-to-school
let weeks_s1_2 = 7; // Number of weeks in the second part of the first semester
let date_s1_2 = date_s1_1 + Duration::weeks(weeks_s1_1 + 1); // Add past weeks with the break-week
let rep: i64 = captures.name("r").unwrap().as_str().parse().unwrap(); let weeks_s2_1 = 6; // Number of weeks in the first part of the second semester
let date_s2_1 = date_s1_2 + Duration::weeks(weeks_s1_2 + 4); // 4 weeks of vacation between semester
let weeks_s2_2 = 7; // Number of weeks in the second part of the second semester
let date_s2_2 = date_s2_1 + Duration::weeks(weeks_s2_1 + 1); // Add past weeks with the break-week
data.insert(i + 1, vec![(start_date, rep)]); HashMap::from([
} (
e if e.starts_with("Reprise") => { 1_usize,
let captures = re.captures(&e).unwrap(); vec![(date_s1_1, weeks_s1_1), (date_s1_2, weeks_s1_2)],
captures.name("g"); ),
(
let start_date = get_date(captures.name("d").unwrap().as_str()); 2_usize,
vec![(date_s2_1, weeks_s2_1), (date_s2_2, weeks_s2_2)],
let rep: i64 = captures.name("r").unwrap().as_str().parse().unwrap(); ),
])
let it = i + 1;
let mut vec = data.get(&it).unwrap().to_owned();
vec.push((start_date, rep));
data.insert(it, vec);
}
_ => (),
}
}
}
data
}
/// Get info webpage
async fn get_webpage(user_agent: &str) -> Result<Html, Box<dyn std::error::Error>> {
let url = "https://informatique.up8.edu/licence-iv/edt";
// Use custom User-Agent
let client = reqwest::Client::builder().user_agent(user_agent).build()?;
let html = client.get(url).send().await?.text().await?;
// Panic on error
crate::utils::check_errors(&html, url);
Ok(Html::parse_document(&html))
} }
/// Turn a french date to an english one /// Turn a french date to an english one

View file

@ -1,6 +1,7 @@
use clap::Parser; use clap::Parser;
use regex::Regex; use regex::Regex;
mod filter;
mod ics; mod ics;
mod info; mod info;
mod timetable; mod timetable;
@ -9,14 +10,18 @@ mod utils;
#[derive(Parser)] #[derive(Parser)]
#[clap(version, about, long_about = None)] #[clap(version, about, long_about = None)]
struct Args { struct Args {
/// The class you want to get the timetable, i.e.: L2-A /// The class you want to get the timetable, i.e.: M1-LP
#[clap(value_parser)] #[clap(value_parser)]
class: String, class: String,
/// The semester you want (useful only in 3rd year, 1-2 use letter in class) /// The semester you want (1 or 2)
#[clap(short, long, value_parser, value_name = "SEMESTER NUMBER")] #[clap(short, long, value_parser, value_name = "SEMESTER NUMBER")]
semester: Option<i8>, semester: Option<i8>,
/// The year, default to the current year
#[clap(short, long, value_parser, value_name = "YEAR")]
year: Option<i32>,
/// Export to iCalendar format (.ics) /// Export to iCalendar format (.ics)
#[clap(short, long, value_name = "FILE NAME")] #[clap(short, long, value_name = "FILE NAME")]
export: Option<String>, export: Option<String>,
@ -30,39 +35,27 @@ struct Args {
async fn main() { async fn main() {
let args = Args::parse(); let args = Args::parse();
let matches = Regex::new(r"[Ll](?P<year>\d)[-–•·]?(?P<letter>.)?") let matches = Regex::new(r"(?i)M(?P<level>[1,2])")
.unwrap() .unwrap()
.captures(&args.class) .captures(&args.class)
.unwrap(); .unwrap();
let year = matches let level = matches
.name("year") .name("level")
.unwrap() .unwrap()
.as_str() .as_str()
.parse::<i8>() .parse::<i8>()
.unwrap(); .unwrap();
let letter = matches
.name("letter")
.map(|c| c.as_str().chars().next().expect("Error in letter"));
// Show a separator only if we need one let user_agent = format!("cal7tor/{}", env!("CARGO_PKG_VERSION"));
let seperator = match letter {
Some(_) => "-",
None => "",
};
let user_agent = format!("cal8tor/{}", env!("CARGO_PKG_VERSION")); println!("Récupération de l'emploi du temps des M{}...", level,);
let mut timetable = timetable::timetable(level, args.semester, args.year, &user_agent).await;
println!( timetable = filter::timetable(timetable);
"Récupération de l'emploi du temps des L{}{}{}...",
year,
seperator,
letter.unwrap_or_default().to_uppercase()
);
let timetable = timetable::timetable(year, args.semester, letter, &user_agent).await;
println!("Récupération des informations par rapport à l'année..."); println!("Récupération des informations par rapport à l'année...");
let info = info::info(&user_agent).await; let info = info::info(level, args.semester, args.year, &user_agent).await;
if args.export.is_some() { if args.export.is_some() {
// Export the calendar // Export the calendar
@ -73,6 +66,7 @@ async fn main() {
println!("Fichier .ICS construit et exporté => {}", filename); println!("Fichier .ICS construit et exporté => {}", filename);
} else { } else {
println!("\x1b[93mNOTICE: IT WON'T WORK!!!\x1b[0m");
// Show the calendar // Show the calendar
println!("Affichage..."); println!("Affichage...");
timetable::display(timetable, args.cl); timetable::display(timetable, args.cl);

View file

@ -1,224 +1,118 @@
use chrono::{Datelike, Duration, TimeZone, Utc}; use chrono::{Datelike, Duration, TimeZone, Utc};
use regex::Regex; use regex::Regex;
use scraper::{Html, Selector}; use scraper::Selector;
use std::collections::HashMap; use std::collections::HashMap;
use crate::utils::{ use crate::utils::{
self, capitalize, self, fill_hours, get_semester, get_webpage, get_year,
models::{Position, TabChar}, models::{Position, TabChar},
Capitalize,
}; };
pub mod models; pub mod models;
/// Fetch the timetable for a class /// Fetch the timetable for a class
pub async fn timetable( pub async fn timetable(
year: i8, level: i8,
semester_opt: Option<i8>, semester_opt: Option<i8>,
letter: Option<char>, year_opt: Option<i32>,
user_agent: &str, user_agent: &str,
) -> (Vec<String>, (usize, Vec<models::Day>)) { ) -> (Vec<String>, (usize, Vec<models::Day>)) {
let semester = get_semester(semester_opt, letter); let semester = get_semester(semester_opt);
let document = get_webpage(year, semester, letter, user_agent) let year = get_year(year_opt, semester);
let document = get_webpage(level, semester, &year, user_agent)
.await .await
.expect("Can't reach timetable website."); .expect("Can't reach timetable website.");
// Selectors // Selectors
let sel_table = Selector::parse("table").unwrap(); let sel_table = Selector::parse("table").unwrap();
let sel_tr = Selector::parse("tr").unwrap();
let sel_tbody = Selector::parse("tbody").unwrap(); let sel_tbody = Selector::parse("tbody").unwrap();
let sel_th = Selector::parse("th").unwrap();
let sel_td = Selector::parse("td").unwrap(); let sel_td = Selector::parse("td").unwrap();
let sel_em = Selector::parse("em").unwrap();
let sel_small = Selector::parse("small").unwrap(); let sel_small = Selector::parse("small").unwrap();
let sel_strong = Selector::parse("strong").unwrap(); let sel_b = Selector::parse("b").unwrap();
// Find the timetable // Find the timetable
let raw_timetable = document.select(&sel_table).next().unwrap(); let raw_timetable = document.select(&sel_table).next().unwrap();
// Find the slots available for the timetable
let raw_schedules = raw_timetable.select(&sel_tr).next().unwrap();
// Find availables schedules
let mut schedules = Vec::new(); let mut schedules = Vec::new();
for time in raw_schedules.select(&sel_th) { fill_hours(&mut schedules);
schedules.push(time.inner_html());
}
// Find the timetable values let mut timetable: Vec<models::Day> = Vec::new();
let raw_timetable_values = raw_timetable.select(&sel_tbody).next().unwrap();
// For each days raw_timetable
let mut timetable = Vec::new(); .select(&sel_tbody)
let span_regex = Regex::new(r"<span.*</span>").unwrap();
for day in raw_timetable_values.select(&sel_tr) {
let mut courses_vec = Vec::new();
let mut location_tracker = 0;
for course in day.select(&sel_td) {
if course.inner_html() == "" {
courses_vec.push(None);
location_tracker += 1;
} else {
courses_vec.push(Some(models::Course {
name: match course.select(&sel_em).next() {
Some(value) => span_regex.replace(&value.inner_html(), " ").to_string(),
None => span_regex
.replace(course.inner_html().split("<br>").next().unwrap(), " ")
.to_string(),
},
professor: match course
.select(&sel_small)
.next() .next()
.unwrap() .unwrap()
.inner_html() .select(&sel_td)
.split("<br>") .filter(|element| element.value().attr("title").is_some())
.next() .for_each(|i| {
{ let matches =
Some(data) => { Regex::new(r"(?P<type>COURS|TD|TP) (?P<name>.*) : (?P<day>(lundi|mardi|mercredi|jeudi|vendredi)) (?P<startime>.*) \(durée : (?P<duration>.*)\)")
if data.contains("</strong>") { .unwrap()
// This is the room, so there is no professor assigned .captures(i.value().attr("title").unwrap())
// to this courses yet .unwrap();
None
} else { let day = matches
Some(data.to_string()) .name("day")
} .unwrap()
} .as_str()
None => None, .capitalize();
},
room: capitalize(&mut match course.select(&sel_strong).next() { let startime = matches
Some(el) => el.inner_html().replace("<br>", ""), .name("startime")
// Error in the site, silently passing... (the room is probably at the professor member) .unwrap()
None => String::new(), .as_str();
}),
start: location_tracker, let binding = i.select(&sel_b).last().unwrap().inner_html();
size: match course.value().attr("colspan") { let course = models::Course{
Some(i) => i.parse().unwrap(), typee: match matches
None => 1, .name("type")
.unwrap()
.as_str() {
"COURS" => models::Type::Cours,
"TP" => models::Type::TP,
"TD" => models::Type::TD,
_ => panic!("Unknown type of course")
}, },
name: matches
.name("name")
.unwrap()
.as_str().to_owned(),
professor: if let Some(raw_prof) = i.select(&sel_small).last() {
match raw_prof.inner_html() {
i if i.starts_with("<span") => None,
i => Some(i),
}
} else { None },
room: Regex::new(r"(<table.*<\/table>|<br>.*?<br>.*?)?<br>(?P<location>.*?)<br>")
.unwrap()
.captures(&binding)
.unwrap().name("location")
.unwrap()
.as_str().to_owned(),
start: schedules.iter().position(|r| r.starts_with(startime)).unwrap(),
size: i.value().attr("rowspan").unwrap().parse::<usize>().unwrap(),
dtstart: None, dtstart: None,
dtend: None, dtend: None,
})); };
match &courses_vec[courses_vec.len() - 1] {
Some(course) => location_tracker += course.size,
None => location_tracker += 1,
}
}
}
// Search for the day in the timetable
if let Some(existing_day) = timetable.iter_mut().find(|x| x.name == day) {
existing_day.courses.push(Some(course));
} else {
// Day with the name doesn't exist, create a new Day
timetable.push(models::Day { timetable.push(models::Day {
name: day.select(&sel_th).next().unwrap().inner_html(), name: day.to_owned(),
courses: courses_vec, courses: vec![Some(course)],
}) });
}
if !check_consistency(&schedules, &timetable) {
panic!("Error when building the timetable.");
} }
});
(schedules, (semester as usize, timetable)) (schedules, (semester as usize, timetable))
} }
/// Get timetable webpage
async fn get_webpage(
year: i8,
semester: i8,
letter: Option<char>,
user_agent: &str,
) -> Result<Html, Box<dyn std::error::Error>> {
let url = {
let panic_semester_message = "Unknown semester.";
let panic_letter_message = "Unknown letter.";
let base_url = "https://informatique.up8.edu/licence-iv/edt";
let allow_letters_1 = match semester {
1 => ['a', 'b', 'c'],
2 => ['x', 'y', 'z'],
_ => panic!("{}", panic_semester_message),
};
let allow_letters_2_3 = match semester {
1 => ['a', 'b'],
2 => ['x', 'y'],
_ => panic!("{}", panic_semester_message),
};
match year {
1 => {
let c = letter.expect(panic_letter_message).to_ascii_lowercase();
if allow_letters_1.contains(&c) {
format!("{}/l1-{}.html", base_url, c)
} else {
panic!("{}", panic_letter_message)
}
}
2 => {
let c = letter.expect(panic_letter_message).to_ascii_lowercase();
if allow_letters_2_3.contains(&c) {
format!("{}/l2-{}.html", base_url, c)
} else {
panic!("{}", panic_letter_message)
}
}
3 => {
let c = letter.expect(panic_letter_message).to_ascii_lowercase();
if allow_letters_2_3.contains(&c) {
format!("{}/l3-{}.html", base_url, c)
} else {
panic!("{}", panic_letter_message)
}
}
_ => panic!("Unknown year."),
}
};
// Use custom User-Agent
let client = reqwest::Client::builder().user_agent(user_agent).build()?;
let html = client.get(&url).send().await?.text().await?;
// Panic on error
crate::utils::check_errors(&html, &url);
// Parse document
let document = Html::parse_document(&html);
Ok(document)
}
/// Check if the timetable is well built
fn check_consistency(schedules: &[String], timetable: &Vec<models::Day>) -> bool {
let mut checker = true;
for day in timetable {
let mut i = 0;
for course in &day.courses {
match course {
Some(course_it) => {
// Checks the consistency of course start times
if i != course_it.start {
checker = false;
break;
}
// Keep the track of how many courses are in the day
i += course_it.size
}
None => i += 1,
}
}
// The counter should be the same as the amount of possible hours of the day
if i != schedules.len() {
checker = false;
break;
}
}
checker
}
// Data builded in the timetable webpage
type T = (
// Schedules
Vec<String>,
// Timetable per days with the semester as the key
(usize, Vec<models::Day>),
);
// Data builded in the info webpage // Data builded in the info webpage
type D = HashMap< type D = HashMap<
// Semester // Semester
@ -228,13 +122,11 @@ type D = HashMap<
>; >;
/// Build the timetable /// Build the timetable
pub fn build(timetable: T, dates: D) -> Vec<models::Course> { pub fn build(timetable: models::Timetable, dates: D) -> Vec<models::Course> {
let mut schedules = Vec::new(); let mut schedules = Vec::new();
// h1 => heure de début | m1 => minute de début // h1 => heure de début | m1 => minute de début
// h2 => heure de fin | m2 => minute de fin // h2 => heure de fin | m2 => minute de fin
let re = let re = Regex::new(r"(?P<h1>\d{1,2})h(?P<m1>\d{2})-(?P<h2>\d{1,2})h(?P<m2>\d{2})").unwrap();
Regex::new(r"(?P<h1>\d{1,2})(h|:)(?P<m1>\d{1,2})?.(?P<h2>\d{1,2})(h|:)(?P<m2>\d{1,2})?")
.unwrap();
for hour in timetable.0 { for hour in timetable.0 {
let captures = re.captures(&hour).unwrap(); let captures = re.captures(&hour).unwrap();
@ -314,37 +206,6 @@ pub fn build(timetable: T, dates: D) -> Vec<models::Course> {
semester semester
} }
/// Get the current semester depending on the letter or the current date
fn get_semester(semester: Option<i8>, letter: Option<char>) -> i8 {
match semester {
// Force the asked semester
Some(n) => n,
// Find the potential semester
None => match letter {
// Based on letter (kinda accurate)
Some(c) => {
if c.to_ascii_uppercase() as i8 > 77 {
// If letter is N or after
2
} else {
// If letter is before N
1
}
}
// Based on the time (kinda less accurate)
None => {
if Utc::now().month() > 6 {
// From july to december
1
} else {
// from january to june
2
}
}
},
}
}
/// Display the timetable /// Display the timetable
pub fn display(timetable: (Vec<String>, (usize, Vec<models::Day>)), cell_length: usize) { pub fn display(timetable: (Vec<String>, (usize, Vec<models::Day>)), cell_length: usize) {
// Cell length for hours // Cell length for hours

View file

@ -1,5 +1,15 @@
#[derive(Clone)] #[derive(Clone, Debug, PartialEq)]
pub enum Type {
Cours,
TP,
TD,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Course { pub struct Course {
/// Type du cours
pub typee: Type,
/// Course's name /// Course's name
pub name: String, pub name: String,
@ -27,9 +37,18 @@ pub struct Course {
pub dtend: Option<chrono::DateTime<chrono::Utc>>, pub dtend: Option<chrono::DateTime<chrono::Utc>>,
} }
#[derive(Debug)]
pub struct Day { pub struct Day {
/// Day's name /// Day's name
pub name: String, pub name: String,
/// Ordered list of all the courses of the day /// Ordered list of all the courses of the day
pub courses: Vec<Option<Course>>, pub courses: Vec<Option<Course>>,
} }
// Data builded in the timetable webpage
pub type Timetable = (
// Schedules
Vec<String>,
// Timetable per days with the semester as the key
(usize, Vec<Day>),
);

View file

@ -1,21 +1,17 @@
use chrono::{Datelike, Utc};
use scraper::Html;
pub mod models; pub mod models;
/// Panic if an error happened /// Panic if an error happened
pub fn check_errors(html: &String, loc: &str) { pub fn check_errors(html: &String, loc: &str) {
let no_timetable = "Aucun créneau horaire affecté";
match html { match html {
t if t.contains(&err_code(429)) => panic!( t if t.contains(no_timetable) => panic!("URL: {}{}", loc, no_timetable),
"URL: {} • HTTP 429: Slow down - Rate limited (too many access attempts detected)",
loc
),
_ => (), _ => (),
} }
} }
/// Create String error code
fn err_code(code: i32) -> String {
format!("HTTP Code : {}", code)
}
/// Print a line for the table /// Print a line for the table
pub fn line_table( pub fn line_table(
cell_length_hours: usize, cell_length_hours: usize,
@ -112,11 +108,89 @@ pub fn etc_str(text: &str) -> String {
format!("{}...", split_half(text).0.trim()) format!("{}...", split_half(text).0.trim())
} }
// Capitalize string /// Get timetable webpage
pub fn capitalize(text: &mut str) -> String { pub async fn get_webpage(
if let Some(r) = text.get_mut(0..1) { level: i8,
semester: i8,
year: &str,
user_agent: &str,
) -> Result<Html, Box<dyn std::error::Error>> {
let url = format!("https://silice.informatique.univ-paris-diderot.fr/ufr/U{}/EDT/visualiserEmploiDuTemps.php?quoi=M{},{}", year, level, semester);
// Use custom User-Agent
let client = reqwest::Client::builder().user_agent(user_agent).build()?;
let html = client.get(&url).send().await?.text().await?;
// Panic on error
crate::utils::check_errors(&html, &url);
// Parse document
let document = Html::parse_document(&html);
Ok(document)
}
/// Get the current semester depending on the current date
pub fn get_semester(semester: Option<i8>) -> i8 {
match semester {
// Force the asked semester
Some(n) => n,
// Find the potential semester
None => {
if Utc::now().month() > 6 {
// From july to december
1
} else {
// from january to june
2
}
}
}
}
/// Get the current year depending on the current date
pub fn get_year(year: Option<i32>, semester: i8) -> String {
let wanted_year = match year {
// Force the asked semester
Some(n) => n,
// Find the potential semester
None => Utc::now().year(),
};
if semester == 1 {
format!("{}-{}", wanted_year, wanted_year + 1)
} else {
format!("{}-{}", wanted_year - 1, wanted_year)
}
}
pub trait Capitalize {
/// Capitalize string
fn capitalize(&self) -> String;
}
impl Capitalize for str {
fn capitalize(&self) -> String {
let mut string = self.to_owned();
if let Some(r) = string.get_mut(0..1) {
r.make_ascii_uppercase(); r.make_ascii_uppercase();
} }
text.to_string() string
}
}
pub fn fill_hours(hours: &mut Vec<String>) {
for hour in 8..=20 {
for minute in &[0, 15, 30, 45] {
let hour_str = format!("{}h{:02}", hour, minute);
if let Some(last_hour) = hours.pop() {
hours.push(format!("{}-{}", last_hour, hour_str));
}
hours.push(hour_str);
}
}
for _ in 0..4 {
hours.pop();
}
} }

View file

@ -47,5 +47,5 @@ impl TabChar {
pub enum Position { pub enum Position {
Top, Top,
Middle, Middle,
Bottom Bottom,
} }