Compare commits
70 commits
Author | SHA1 | Date | |
---|---|---|---|
8e18f7d2e5 | |||
61a85ca1f1 | |||
586fb4ba28 | |||
1cab8ef246 | |||
b621436483 | |||
090a16433d | |||
8e7fdaf3c8 | |||
fabcd6dac9 | |||
b6a2fec3cb | |||
c81b07c277 | |||
181ebf53f4 | |||
07068a0150 | |||
0c26ed24de | |||
9269ff12db | |||
52648ac61a | |||
ae14925d9e | |||
71a47096b1 | |||
dfcf1226d0 | |||
e444d29a15 | |||
4d0f14d045 | |||
6c9ccff574 | |||
28f2c2b192 | |||
4791463150 | |||
aafd734728 | |||
6bc3a5643c | |||
e0afc9414f | |||
d237ff3098 | |||
8fdebbe4a1 | |||
51284fb5ae | |||
3db016519f | |||
8abec03d0b | |||
23892ad245 | |||
1eaa257dbf | |||
3a43782bd0 | |||
5c922a530e | |||
64ce6e478b | |||
862c02b3e7 | |||
9ce116f620 | |||
9ad0de6222 | |||
77c2444de1 | |||
220f46c5de | |||
83563529be | |||
f97d6c41e8 | |||
82ae498a93 | |||
1d9389e41b | |||
be6670b02f | |||
e65534d8f9 | |||
e679602b76 | |||
9e5dd7009d | |||
54317e2356 | |||
6e13936980 | |||
041b8625f1 | |||
384dd9eaa9 | |||
75832958d8 | |||
2ec3455d16 | |||
5fc7d9209c | |||
dc3a5c6d8e | |||
be636f552a | |||
c671587b7a | |||
bbee1d58be | |||
189b77cc4f | |||
b3fec12292 | |||
9ba185b247 | |||
1dbfe12566 | |||
e50475f0a7 | |||
ff95bf09b5 | |||
16a9bba6dd | |||
ade74b4e4c | |||
58399ca1e9 | |||
3d51a4b743 |
11 changed files with 1221 additions and 564 deletions
616
Cargo.lock
generated
616
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
30
Cargo.toml
30
Cargo.toml
|
@ -1,21 +1,25 @@
|
||||||
[package]
|
[package]
|
||||||
name = "cal8tor"
|
name = "cal7tor"
|
||||||
version = "0.4.5"
|
version = "0.10.0"
|
||||||
authors = ["Mylloon"]
|
authors = ["Mylloon"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "Extractor of the calendar of the IT degree of university Paris 8"
|
description = "Timetable extractor for the Paris Cité master's degree in IT"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
repository = "https://git.kennel.ml/Anri/cal8tor"
|
repository = "https://git.mylloon.fr/Anri/cal7tor"
|
||||||
keywords = ["scrape", "calendar"]
|
keywords = ["scrape", "calendar", "paris diderot", "paris cité"]
|
||||||
publish = false
|
publish = false
|
||||||
license = "AGPL-3.0-or-later"
|
license = "AGPL-3.0-or-later"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
reqwest = { version = "0.11" }
|
reqwest = { version = "0.12" }
|
||||||
tokio = { version = "1.32", features = ["full"] }
|
tokio = { version = "1.40", features = ["full"] }
|
||||||
scraper = "0.17"
|
scraper = "0.20"
|
||||||
regex = "1.9"
|
regex = "1.10"
|
||||||
chrono = "0.4.28"
|
chrono = "0.4.38"
|
||||||
ics = "0.5"
|
ics = { version = "0.5", default-features = false }
|
||||||
uuid = { version = "1.4", features = ["v4", "fast-rng"] }
|
uuid = { version = "1.10", features = ["v4", "fast-rng"] }
|
||||||
clap = { version = "4.4", features = ["derive"] }
|
clap = { version = "4.5", features = ["derive"] }
|
||||||
|
dialoguer = "0.11"
|
||||||
|
|
||||||
|
[lints.clippy]
|
||||||
|
pedantic = "warn"
|
||||||
|
|
33
README.md
33
README.md
|
@ -1,16 +1,13 @@
|
||||||
# cal8tor • *cal*endar P*8* extrac*tor*
|
# cal7tor • *cal*endar P*7* extrac*tor*
|
||||||
|
|
||||||
Extracteur d'emploi du temps pour la licence d'informatique de Paris 8
|
> Fork de [cal8tor](https://git.mylloon.fr/Anri/cal8tor)
|
||||||
|
|
||||||
[![dependency status](https://deps.rs/repo/gitea/git.mylloon.fr/Anri/cal8tor/status.svg)](https://deps.rs/repo/gitea/git.mylloon.fr/Anri/cal8tor)
|
Extracteur d'emploi du temps pour les masters d'informatique de Paris Cité (Diderot)
|
||||||
|
|
||||||
|
[![dependency status](https://deps.rs/repo/gitea/git.mylloon.fr/Anri/cal7tor/status.svg)](https://deps.rs/repo/gitea/git.mylloon.fr/Anri/cal7tor)
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
### Arch
|
|
||||||
|
|
||||||
cal8tor est disponible sur le AUR : [`cal8tor`](https://aur.archlinux.org/packages/cal8tor)
|
|
||||||
et [`cal8tor-git`](https://aur.archlinux.org/packages/cal8tor-git).
|
|
||||||
|
|
||||||
### Manuellement
|
### Manuellement
|
||||||
|
|
||||||
Cf. [Compilation et installation](#compilation-et-installation).
|
Cf. [Compilation et installation](#compilation-et-installation).
|
||||||
|
@ -20,26 +17,28 @@ Cf. [Compilation et installation](#compilation-et-installation).
|
||||||
Pour afficher la page d'aide
|
Pour afficher la page d'aide
|
||||||
|
|
||||||
```
|
```
|
||||||
$ cal8tor --help
|
$ cal7tor --help
|
||||||
```
|
```
|
||||||
|
|
||||||
## Voir le calendrier dans le terminal
|
<!-- ## Voir le calendrier dans le terminal
|
||||||
|
|
||||||
Pour les L2-X 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
|
||||||
$ cal8tor l2-X
|
$ 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 L1-A par exemple, lance :
|
Pour les M1 par exemple, lance :
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ cal8tor L1A --export calendar.ics
|
$ cal7tor 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
|
||||||
|
@ -58,7 +57,7 @@ Vous aurez besoin de Rust pour compiler le programme.
|
||||||
1. Clone le dépôt et s'y rendre
|
1. Clone le dépôt et s'y rendre
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ git clone https://git.mylloon.fr/Anri/cal8tor.git && cd cal8tor
|
$ git clone https://git.mylloon.fr/Anri/cal7tor.git && cd cal7tor
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Compiler et installer l'application
|
2. Compiler et installer l'application
|
||||||
|
@ -67,7 +66,7 @@ $ git clone https://git.mylloon.fr/Anri/cal8tor.git && cd cal8tor
|
||||||
$ cargo install --path .
|
$ cargo install --path .
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Tu peux maintenant supprimer le dossier `cal8tor` !
|
3. Tu peux maintenant supprimer le dossier `cal7tor` !
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
172
src/filter.rs
Normal file
172
src/filter.rs
Normal file
|
@ -0,0 +1,172 @@
|
||||||
|
use dialoguer::MultiSelect;
|
||||||
|
|
||||||
|
use crate::timetable::models::Category;
|
||||||
|
use crate::timetable::models::Timetable;
|
||||||
|
use crate::utils::get_count;
|
||||||
|
use crate::utils::get_entry;
|
||||||
|
use crate::utils::get_entry_nocat;
|
||||||
|
use crate::utils::get_selection;
|
||||||
|
|
||||||
|
const DISCLAIMER: &str = "(selection avec ESPACE, ENTRER pour valider)";
|
||||||
|
|
||||||
|
/// Filter the timetable
|
||||||
|
pub fn timetable(timetable: Timetable, merge_td_tp: bool) -> Timetable {
|
||||||
|
let mut my_timetable = timetable;
|
||||||
|
|
||||||
|
/* Note on Cours/TD:
|
||||||
|
* We use the "as long as x interests us, we accept" approach.
|
||||||
|
*
|
||||||
|
* Because when a course and its TD are on the same slot,
|
||||||
|
* it's probably because there's an alternation between course
|
||||||
|
* and TD and no other choice is possible. */
|
||||||
|
|
||||||
|
choice(&mut my_timetable);
|
||||||
|
courses(&mut my_timetable);
|
||||||
|
tdtp(&mut my_timetable, merge_td_tp);
|
||||||
|
|
||||||
|
my_timetable
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Exclude some courses
|
||||||
|
fn choice(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.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
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 multiple courses
|
||||||
|
fn courses(timetable: &mut Timetable) {
|
||||||
|
let entry_getter = get_entry;
|
||||||
|
|
||||||
|
// List of courses and Counter of how much they appears
|
||||||
|
// to know if multiples slots are available
|
||||||
|
let (mut courses, counts) = get_count(timetable, &[Category::Cours], entry_getter);
|
||||||
|
|
||||||
|
// Keep only elements who have multiples slots
|
||||||
|
courses.retain(|course| *counts.get(&entry_getter(course.0)).unwrap() > 1);
|
||||||
|
|
||||||
|
let mut multiselected: Vec<String> = courses.iter().map(get_selection).collect();
|
||||||
|
multiselected.sort();
|
||||||
|
|
||||||
|
let mut selections = vec![];
|
||||||
|
if !multiselected.is_empty() {
|
||||||
|
let defaults = vec![false; multiselected.len()];
|
||||||
|
selections = MultiSelect::new()
|
||||||
|
.with_prompt(format!("Choisis tes horaires de Cours {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 TD/TP
|
||||||
|
if course.category.contains(&Category::TD)
|
||||||
|
|| course.category.contains(&Category::TP)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep if only one slot is available
|
||||||
|
if *counts.get(&entry_getter(course)).unwrap() == 1 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep only chosen courses if multiple was available
|
||||||
|
for i in &selections {
|
||||||
|
if get_selection(&(course, day.name.clone())) == multiselected[*i] {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Filter the multiples TD/TP
|
||||||
|
fn tdtp(timetable: &mut Timetable, merge: bool) {
|
||||||
|
// If we differentiate TD from TP
|
||||||
|
let entry_getter = if merge { get_entry_nocat } else { get_entry };
|
||||||
|
|
||||||
|
// List of TP/TD and Counter of how much they appears
|
||||||
|
// to know if multiples slots are available
|
||||||
|
let (mut td_or_tp, counts) = get_count(timetable, &[Category::TD, Category::TP], entry_getter);
|
||||||
|
|
||||||
|
// Keep only elements who have multiples TD/TP
|
||||||
|
td_or_tp.retain(|course| *counts.get(&entry_getter(course.0)).unwrap() > 1);
|
||||||
|
|
||||||
|
let mut multiselected: Vec<String> = td_or_tp.iter().map(get_selection).collect();
|
||||||
|
multiselected.sort();
|
||||||
|
|
||||||
|
let mut selections = vec![];
|
||||||
|
if !multiselected.is_empty() {
|
||||||
|
let defaults = vec![false; multiselected.len()];
|
||||||
|
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.category.contains(&Category::Cours) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep if only one slot is available of the TD/TP
|
||||||
|
if *counts.get(&entry_getter(course)).unwrap() == 1 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep only chosen TD/TP if multiple was available
|
||||||
|
for i in &selections {
|
||||||
|
if get_selection(&(course, day.name.clone())) == multiselected[*i] {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
47
src/ics.rs
47
src/ics.rs
|
@ -1,12 +1,16 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use chrono::TimeZone;
|
use chrono::TimeZone;
|
||||||
use ics::{
|
use ics::{
|
||||||
parameters,
|
parameters::{Language, PartStat, Role, TzIDParam, CN},
|
||||||
properties::{Class, Description, DtEnd, DtStart, Location, Summary, Transp},
|
properties::{
|
||||||
|
Attendee, Categories, Class, Description, DtEnd, DtStart, Location, Summary, Transp,
|
||||||
|
},
|
||||||
Event, ICalendar, Standard,
|
Event, ICalendar, Standard,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn export(courses: Vec<crate::timetable::models::Course>, filename: &mut String) {
|
pub fn export(courses: Vec<crate::timetable::models::Course>, filename: &mut String) {
|
||||||
let mut calendar = ICalendar::new("2.0", "cal8tor");
|
let mut calendar = ICalendar::new("2.0", "cal7tor");
|
||||||
|
|
||||||
// Add Europe/Paris timezone
|
// Add Europe/Paris timezone
|
||||||
let timezone_name = "Europe/Paris";
|
let timezone_name = "Europe/Paris";
|
||||||
|
@ -36,34 +40,57 @@ pub fn export(courses: Vec<crate::timetable::models::Course>, filename: &mut Str
|
||||||
|
|
||||||
// Professor's name
|
// Professor's name
|
||||||
if course.professor.is_some() {
|
if course.professor.is_some() {
|
||||||
event.push(Description::new(course.professor.unwrap()));
|
let name = course.professor.unwrap();
|
||||||
|
let mut contact = Attendee::new("mailto:place@holder.com");
|
||||||
|
contact.add(CN::new(name));
|
||||||
|
contact.add(PartStat::ACCEPTED);
|
||||||
|
contact.add(Role::CHAIR);
|
||||||
|
event.push(contact);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start time of the course
|
// Start time of the course
|
||||||
let mut date_start = DtStart::new(dt_ical(course.dtstart.unwrap()));
|
let mut date_start = DtStart::new(dt_ical(course.dtstart.unwrap()));
|
||||||
date_start.append(parameters!("TZID" => timezone_name));
|
date_start.add(TzIDParam::new(timezone_name));
|
||||||
event.push(date_start);
|
event.push(date_start);
|
||||||
|
|
||||||
// End time of the course
|
// End time of the course
|
||||||
let mut date_end = DtEnd::new(dt_ical(course.dtend.unwrap()));
|
let mut date_end = DtEnd::new(dt_ical(course.dtend.unwrap()));
|
||||||
date_end.append(parameters!("TZID" => timezone_name));
|
date_end.add(TzIDParam::new(timezone_name));
|
||||||
event.push(date_end);
|
event.push(date_end);
|
||||||
|
|
||||||
// Room location
|
// Room location
|
||||||
event.push(Location::new(course.room));
|
event.push(Location::new(course.room));
|
||||||
|
|
||||||
|
let categories = course
|
||||||
|
.category
|
||||||
|
.iter()
|
||||||
|
.map(std::string::ToString::to_string)
|
||||||
|
.collect::<Arc<[String]>>()
|
||||||
|
.join("/");
|
||||||
|
|
||||||
// Course's name
|
// Course's name
|
||||||
let mut course_name = Summary::new(course.name);
|
let mut course_name = Summary::new(format!("{} - {}", categories, course.name));
|
||||||
course_name.append(parameters!("LANGUAGE" => "fr"));
|
course_name.add(Language::new("fr"));
|
||||||
event.push(course_name);
|
event.push(course_name);
|
||||||
|
|
||||||
|
// Course's category
|
||||||
|
event.push(Categories::new(categories));
|
||||||
|
|
||||||
|
// Course extra data
|
||||||
|
if course.data.is_some() {
|
||||||
|
event.push(Description::new(course.data.unwrap()));
|
||||||
|
}
|
||||||
|
|
||||||
// Add the course to the calendar
|
// Add the course to the calendar
|
||||||
calendar.add_event(event);
|
calendar.add_event(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the extension if needed
|
// Add the extension if needed
|
||||||
if !filename.ends_with(".ics") {
|
if !std::path::Path::new(filename)
|
||||||
*filename = format!("{}.ics", filename)
|
.extension()
|
||||||
|
.map_or(false, |ext| ext.eq_ignore_ascii_case("ics"))
|
||||||
|
{
|
||||||
|
*filename = format!("{filename}.ics");
|
||||||
};
|
};
|
||||||
|
|
||||||
calendar.save_file(filename).unwrap();
|
calendar.save_file(filename).unwrap();
|
||||||
|
|
146
src/info.rs
146
src/info.rs
|
@ -1,76 +1,100 @@
|
||||||
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, sync::Arc};
|
||||||
|
|
||||||
pub async fn info(user_agent: &str) -> HashMap<usize, Vec<(DateTime<Utc>, i64)>> {
|
use crate::utils::{
|
||||||
let document = get_webpage(user_agent)
|
get_semester, get_webpage, get_year,
|
||||||
|
models::{Info, InfoList, InfoType},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub async fn get_start_date(
|
||||||
|
level: i8,
|
||||||
|
semester_opt: Option<i8>,
|
||||||
|
year_opt: Option<i32>,
|
||||||
|
user_agent: &str,
|
||||||
|
) -> String {
|
||||||
|
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 re = Regex::new(r"\d{1,2} (septembre|octobre)").unwrap();
|
||||||
|
|
||||||
|
re.captures(&raw_data)
|
||||||
|
.and_then(|caps| caps.get(0))
|
||||||
|
.map_or("1 septembre".to_owned(), |m| m.as_str().to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut data = HashMap::new();
|
pub fn info(semester_opt: Option<i8>, year_opt: Option<i32>, date: &str, skip_week: bool) -> Info {
|
||||||
// d => date
|
let semester = get_semester(semester_opt);
|
||||||
// r => repetition
|
let year = get_year(year_opt, semester);
|
||||||
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());
|
// 1st semester
|
||||||
|
let weeks_s1_1 = 6; // Weeks before break
|
||||||
|
let weeks_s1_2 = 7; // Weeks after break
|
||||||
|
let date_s1_1 = get_date(&format!("{} {}", date, year.split_once('-').unwrap().0)); // Get first week of school
|
||||||
|
let date_s1_2 = date_s1_1 + Duration::weeks(weeks_s1_1 + 1); // Back-to-school week - add week of holidays
|
||||||
|
|
||||||
let rep: i64 = captures.name("r").unwrap().as_str().parse().unwrap();
|
// 2nd semester
|
||||||
|
let weeks_s2_1 = 11; // Weeks before break
|
||||||
|
let weeks_s2_2 = 1; // Weeks after break
|
||||||
|
let date_s2_1 = date_s1_2 + Duration::weeks(weeks_s1_2 + 4); // Get first week - add week of 'christmas/new year holidays'
|
||||||
|
let date_s2_2 = date_s2_1 + Duration::weeks(weeks_s2_1 + 2); // Back-to-school week - add week of holidays
|
||||||
|
|
||||||
data.insert(i + 1, vec![(start_date, rep)]);
|
// Group courses values and derive it for TD/TP
|
||||||
}
|
let cours_s1 = vec![(date_s1_1, weeks_s1_1), (date_s1_2, weeks_s1_2)];
|
||||||
e if e.starts_with("Reprise") => {
|
let cours_s2 = vec![(date_s2_1, weeks_s2_1), (date_s2_2, weeks_s2_2)];
|
||||||
let captures = re.captures(&e).unwrap();
|
|
||||||
captures.name("g");
|
|
||||||
|
|
||||||
let start_date = get_date(captures.name("d").unwrap().as_str());
|
let delta = i64::from(skip_week);
|
||||||
|
|
||||||
let rep: i64 = captures.name("r").unwrap().as_str().parse().unwrap();
|
let tdtp_s1 = derive_from_cours(&cours_s1, delta);
|
||||||
|
let tdtp_s2 = derive_from_cours(&cours_s2, delta);
|
||||||
|
|
||||||
let it = i + 1;
|
HashMap::from([
|
||||||
|
(
|
||||||
let mut vec = data.get(&it).unwrap().to_owned();
|
1_usize,
|
||||||
vec.push((start_date, rep));
|
InfoType {
|
||||||
|
course: cours_s1,
|
||||||
data.insert(it, vec);
|
td_tp: tdtp_s1,
|
||||||
}
|
},
|
||||||
_ => (),
|
),
|
||||||
}
|
(
|
||||||
}
|
2_usize,
|
||||||
|
InfoType {
|
||||||
|
course: cours_s2,
|
||||||
|
td_tp: tdtp_s2,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
data
|
/// Find TD/TP dates, based on the ones from courses
|
||||||
}
|
fn derive_from_cours(courses: &InfoList, delta: i64) -> Vec<(DateTime<Utc>, i64)> {
|
||||||
|
// TD/TP start one week after courses
|
||||||
/// Get info webpage
|
let before_break = courses.first().unwrap();
|
||||||
async fn get_webpage(user_agent: &str) -> Result<Html, Box<dyn std::error::Error>> {
|
let after_break = courses.last().unwrap();
|
||||||
let url = "https://informatique.up8.edu/licence-iv/edt";
|
vec![
|
||||||
|
(
|
||||||
// Use custom User-Agent
|
before_break.0 + Duration::weeks(delta),
|
||||||
let client = reqwest::Client::builder().user_agent(user_agent).build()?;
|
before_break.1 - delta,
|
||||||
let html = client.get(url).send().await?.text().await?;
|
),
|
||||||
|
(after_break.0, after_break.1 + delta),
|
||||||
// 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
|
||||||
|
@ -93,7 +117,7 @@ fn anglophonization(date: &str) -> String {
|
||||||
// New regex of all the french month
|
// New regex of all the french month
|
||||||
let re = Regex::new(&format!(
|
let re = Regex::new(&format!(
|
||||||
"({})",
|
"({})",
|
||||||
dico.keys().cloned().collect::<Vec<_>>().join("|")
|
dico.keys().copied().collect::<Arc<[_]>>().join("|")
|
||||||
))
|
))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
@ -101,19 +125,19 @@ fn anglophonization(date: &str) -> String {
|
||||||
// Use 12:00 and UTC TZ for chrono parser
|
// Use 12:00 and UTC TZ for chrono parser
|
||||||
"{} 12:00 +0000",
|
"{} 12:00 +0000",
|
||||||
// Replace french by english month
|
// Replace french by english month
|
||||||
re.replace_all(date, |cap: &Captures| match &cap[0] {
|
re.replace_all(&date.to_lowercase(), |cap: &Captures| match &cap[0] {
|
||||||
month if dico.contains_key(month) => dico.get(month).unwrap(),
|
month if dico.contains_key(month) => dico.get(month).unwrap(),
|
||||||
month => {
|
month => {
|
||||||
panic!("Unknown month: {}", month)
|
panic!("Unknown month: {month}")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Turn a string to a DateTime
|
/// Turn a string to a `DateTime`
|
||||||
fn get_date(date: &str) -> DateTime<Utc> {
|
fn get_date(date: &str) -> DateTime<Utc> {
|
||||||
// Use and keep UTC time, we have the hour set to 12h and
|
// Use and keep UTC time, we have the hour set to 12h and
|
||||||
// Paris 8 is in France so there is no problems
|
// Paris 7 is in France so there is no problems
|
||||||
DateTime::parse_from_str(&anglophonization(date), "%e %B %Y %H:%M %z")
|
DateTime::parse_from_str(&anglophonization(date), "%e %B %Y %H:%M %z")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into()
|
.into()
|
||||||
|
|
70
src/main.rs
70
src/main.rs
|
@ -1,6 +1,8 @@
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use dialoguer::Input;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
|
mod filter;
|
||||||
mod ics;
|
mod ics;
|
||||||
mod info;
|
mod info;
|
||||||
mod timetable;
|
mod timetable;
|
||||||
|
@ -9,73 +11,87 @@ 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
|
||||||
#[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), default to current semester
|
||||||
#[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 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>,
|
||||||
|
|
||||||
/// Size of cell of the timetable (irrelevant when exporting the timetable)
|
/// Size of cell of the timetable (irrelevant when exporting)
|
||||||
#[clap(short, long, value_name = "CELL LENGTH", default_value_t = 35)]
|
#[clap(short, long, value_name = "CELL LENGTH", default_value_t = 35)]
|
||||||
cl: usize,
|
cl: usize,
|
||||||
|
|
||||||
|
/// Doesn't distinguish TD from TP
|
||||||
|
#[clap(short, long)]
|
||||||
|
td_are_tp: bool,
|
||||||
|
|
||||||
|
/// First day of your year
|
||||||
|
#[clap(short, long)]
|
||||||
|
first_day: Option<String>,
|
||||||
|
|
||||||
|
/// If TD/TP start a week after courses
|
||||||
|
#[clap(short, long)]
|
||||||
|
week_skip: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
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(_) => "-",
|
println!("Récupération de l'emploi du temps des M{level}...");
|
||||||
None => "",
|
let mut timetable = timetable::timetable(level, args.semester, args.year, &user_agent).await;
|
||||||
|
|
||||||
|
timetable = filter::timetable(timetable, args.td_are_tp);
|
||||||
|
|
||||||
|
let date = match args.first_day {
|
||||||
|
None => Input::new()
|
||||||
|
.with_prompt("Début des cours de l'année")
|
||||||
|
.default(info::get_start_date(level, args.semester, args.year, &user_agent).await)
|
||||||
|
.interact_text()
|
||||||
|
.unwrap(),
|
||||||
|
Some(day) => day,
|
||||||
};
|
};
|
||||||
|
|
||||||
let user_agent = format!("cal8tor/{}", env!("CARGO_PKG_VERSION"));
|
|
||||||
|
|
||||||
println!(
|
|
||||||
"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(args.semester, args.year, &date, args.week_skip);
|
||||||
|
|
||||||
if args.export.is_some() {
|
if args.export.is_some() {
|
||||||
// Export the calendar
|
// Export the calendar
|
||||||
let mut filename = args.export.unwrap();
|
let mut filename = args.export.unwrap();
|
||||||
|
|
||||||
let builded_timetable = timetable::build(timetable, info);
|
let builded_timetable = timetable::build(&timetable, &info);
|
||||||
ics::export(builded_timetable, &mut filename);
|
ics::export(builded_timetable, &mut filename);
|
||||||
|
|
||||||
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);
|
||||||
println!("Vous devrez peut-être mettre votre terminal en plein écran si ce n'est pas déjà le cas.");
|
println!("Vous devrez peut-être mettre votre terminal en plein écran si ce n'est pas déjà le cas.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
405
src/timetable.rs
405
src/timetable.rs
|
@ -1,242 +1,152 @@
|
||||||
|
#![allow(clippy::cast_sign_loss)]
|
||||||
|
|
||||||
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, sync::Arc};
|
||||||
|
|
||||||
use crate::utils::{
|
use crate::utils::{
|
||||||
self, capitalize,
|
self, get_hours, get_semester, get_webpage, get_year,
|
||||||
models::{Position, TabChar},
|
models::{Info, InfoList, Position, TabChar},
|
||||||
|
Capitalize,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use self::models::Day;
|
||||||
|
|
||||||
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>)) {
|
) -> models::Timetable {
|
||||||
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();
|
||||||
|
let sel_span = Selector::parse("span").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 schedules = get_hours();
|
||||||
let raw_schedules = raw_timetable.select(&sel_tr).next().unwrap();
|
|
||||||
|
|
||||||
// Find availables schedules
|
let mut timetable: Vec<models::Day> = Vec::new();
|
||||||
let mut schedules = Vec::new();
|
|
||||||
for time in raw_schedules.select(&sel_th) {
|
|
||||||
schedules.push(time.inner_html());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find the timetable values
|
raw_timetable
|
||||||
let raw_timetable_values = raw_timetable.select(&sel_tbody).next().unwrap();
|
.select(&sel_tbody)
|
||||||
|
|
||||||
// For each days
|
|
||||||
let mut timetable = Vec::new();
|
|
||||||
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 extra_data = i.select(&sel_span).next().map(|span|
|
||||||
Some(data) => {
|
span.inner_html().replace("<br>", "").trim().to_owned()
|
||||||
if data.contains("</strong>") {
|
);
|
||||||
// This is the room, so there is no professor assigned
|
|
||||||
// to this courses yet
|
/* TODO: Instead of searching *_M2, just find any TD_* and TP_* */
|
||||||
None
|
let matches =
|
||||||
} else {
|
Regex::new(
|
||||||
Some(data.to_string())
|
r"(?P<type>COURS|COURS_TD|TD|TD_M2|TP|TP_M2)? (?P<name>.*) : (?P<day>(lundi|mardi|mercredi|jeudi|vendredi)) (?P<startime>.*) \(durée : (?P<duration>.*)\)")
|
||||||
}
|
.unwrap()
|
||||||
}
|
.captures(i.value().attr("title").unwrap())
|
||||||
None => None,
|
.unwrap();
|
||||||
|
|
||||||
|
let day = matches
|
||||||
|
.name("day")
|
||||||
|
.unwrap()
|
||||||
|
.as_str()
|
||||||
|
.capitalize();
|
||||||
|
|
||||||
|
let startime = matches
|
||||||
|
.name("startime")
|
||||||
|
.unwrap()
|
||||||
|
.as_str();
|
||||||
|
|
||||||
|
let binding = i.select(&sel_b).last().unwrap().inner_html();
|
||||||
|
let course = models::Course{
|
||||||
|
category: match matches
|
||||||
|
.name("type")
|
||||||
|
.map_or("", |m| m.as_str()) {
|
||||||
|
/* TODO: Instead of searching *_M2, just find any TD_* and TP_* */
|
||||||
|
"COURS" => [models::Category::Cours].into(),
|
||||||
|
"TP" | "TP_M2" => [models::Category::TP].into(),
|
||||||
|
"TD" | "TD_M2" => [models::Category::TD].into(),
|
||||||
|
"COURS_TD" => [models::Category::Cours, models::Category::TD].into(),
|
||||||
|
_ => {
|
||||||
|
println!("Unknown type of course, falling back to 'COURS': {}", i.value().attr("title").unwrap());
|
||||||
|
[models::Category::Cours].into()
|
||||||
},
|
},
|
||||||
room: capitalize(&mut match course.select(&sel_strong).next() {
|
|
||||||
Some(el) => el.inner_html().replace("<br>", ""),
|
|
||||||
// Error in the site, silently passing... (the room is probably at the professor member)
|
|
||||||
None => String::new(),
|
|
||||||
}),
|
|
||||||
start: location_tracker,
|
|
||||||
size: match course.value().attr("colspan") {
|
|
||||||
Some(i) => i.parse().unwrap(),
|
|
||||||
None => 1,
|
|
||||||
},
|
},
|
||||||
|
name: Regex::new(r"[ -][ML][1-3]$").unwrap().replace(
|
||||||
|
matches
|
||||||
|
.name("name")
|
||||||
|
.unwrap()
|
||||||
|
.as_str(),
|
||||||
|
""
|
||||||
|
).to_string(),
|
||||||
|
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,
|
||||||
}));
|
data: extra_data,
|
||||||
|
};
|
||||||
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.clone(),
|
||||||
courses: courses_vec,
|
courses: vec![Some(course)],
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if !check_consistency(&schedules, &timetable) {
|
// Sort by days
|
||||||
panic!("Error when building the timetable.");
|
let day_positions = ["Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi"]
|
||||||
}
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, &day)| (day.to_owned(), i))
|
||||||
|
.collect::<HashMap<String, usize>>();
|
||||||
|
timetable.sort_by(|a, b| day_positions[&a.name].cmp(&day_positions[&b.name]));
|
||||||
|
|
||||||
(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
|
|
||||||
type D = HashMap<
|
|
||||||
// Semester
|
|
||||||
usize,
|
|
||||||
// List of start and repetition of course weeks
|
|
||||||
Vec<(chrono::DateTime<Utc>, i64)>,
|
|
||||||
>;
|
|
||||||
|
|
||||||
/// Build the timetable
|
/// Build the timetable
|
||||||
pub fn build(timetable: T, dates: D) -> Vec<models::Course> {
|
pub fn build(timetable: &models::Timetable, dates: &Info) -> 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})?")
|
for hour in timetable.0.iter() {
|
||||||
.unwrap();
|
let captures = re.captures(hour).unwrap();
|
||||||
for hour in timetable.0 {
|
|
||||||
let captures = re.captures(&hour).unwrap();
|
|
||||||
|
|
||||||
let h1 = match captures.name("h1") {
|
let h1 = match captures.name("h1") {
|
||||||
Some(h) => h.as_str().parse().unwrap(),
|
Some(h) => h.as_str().parse().unwrap(),
|
||||||
|
@ -262,19 +172,67 @@ pub fn build(timetable: T, dates: D) -> Vec<models::Course> {
|
||||||
|
|
||||||
// Start date of the back-to-school week
|
// Start date of the back-to-school week
|
||||||
let datetimes = dates.get(&timetable.1 .0).unwrap();
|
let datetimes = dates.get(&timetable.1 .0).unwrap();
|
||||||
let before_break = datetimes.get(0).unwrap();
|
add_courses(
|
||||||
|
&mut semester,
|
||||||
|
&schedules,
|
||||||
|
&timetable.1 .1,
|
||||||
|
&datetimes.course,
|
||||||
|
&Some(vec![models::Category::Cours]),
|
||||||
|
&None,
|
||||||
|
);
|
||||||
|
add_courses(
|
||||||
|
&mut semester,
|
||||||
|
&schedules,
|
||||||
|
&timetable.1 .1,
|
||||||
|
&datetimes.td_tp,
|
||||||
|
&None,
|
||||||
|
&Some(vec![models::Category::Cours]),
|
||||||
|
);
|
||||||
|
|
||||||
|
semester
|
||||||
|
}
|
||||||
|
|
||||||
|
type Schedule = [((u32, u32), (u32, u32))];
|
||||||
|
|
||||||
|
/// Add a course to the semester list
|
||||||
|
fn add_courses(
|
||||||
|
// Accumulator of courses of semester
|
||||||
|
semester: &mut Vec<models::Course>,
|
||||||
|
// Hours used
|
||||||
|
schedules: &Schedule,
|
||||||
|
// List of days
|
||||||
|
days: &Vec<Day>,
|
||||||
|
// Current courses list
|
||||||
|
info: &InfoList,
|
||||||
|
// List of category allowed
|
||||||
|
keep: &Option<Vec<models::Category>>,
|
||||||
|
// List of category excluded
|
||||||
|
exclude: &Option<Vec<models::Category>>,
|
||||||
|
) {
|
||||||
|
let before_break = info.first().unwrap();
|
||||||
let mut date = before_break.0;
|
let mut date = before_break.0;
|
||||||
let mut rep = before_break.1;
|
let mut rep = before_break.1;
|
||||||
// For each weeks
|
// For each weeks
|
||||||
for _ in 0..2 {
|
for _ in 0..2 {
|
||||||
for _ in 0..rep {
|
for _ in 0..rep {
|
||||||
for day in &timetable.1 .1 {
|
for day in days {
|
||||||
for mut course in day.courses.clone().into_iter().flatten() {
|
for mut course in day.courses.iter().flatten().cloned() {
|
||||||
// Get the hours
|
// Get the hours
|
||||||
let start = schedules.get(course.start).unwrap().0;
|
let start = schedules.get(course.start).unwrap().0;
|
||||||
// -1 because we only add when the size is > 1
|
// -1 because we only add when the size is > 1
|
||||||
let end = schedules.get(course.start + course.size - 1).unwrap().1;
|
let end = schedules.get(course.start + course.size - 1).unwrap().1;
|
||||||
|
|
||||||
|
// Check keep and exclude filters
|
||||||
|
if keep
|
||||||
|
.clone()
|
||||||
|
.is_some_and(|list| !course.category.iter().any(|item| list.contains(item)))
|
||||||
|
|| exclude.clone().is_some_and(|list| {
|
||||||
|
course.category.iter().any(|item| list.contains(item))
|
||||||
|
})
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Add the changed datetimes
|
// Add the changed datetimes
|
||||||
course.dtstart = Some(
|
course.dtstart = Some(
|
||||||
Utc.with_ymd_and_hms(
|
Utc.with_ymd_and_hms(
|
||||||
|
@ -306,47 +264,14 @@ pub fn build(timetable: T, dates: D) -> Vec<models::Course> {
|
||||||
// From friday to monday
|
// From friday to monday
|
||||||
date += Duration::days(2);
|
date += Duration::days(2);
|
||||||
}
|
}
|
||||||
let after_break = datetimes.get(1).unwrap();
|
let after_break = info.last().unwrap();
|
||||||
date = after_break.0;
|
date = after_break.0;
|
||||||
rep = after_break.1;
|
rep = after_break.1;
|
||||||
}
|
}
|
||||||
|
|
||||||
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: &(Arc<[String]>, (usize, Vec<models::Day>)), cell_length: usize) {
|
||||||
// Cell length for hours
|
// Cell length for hours
|
||||||
let clh = 11;
|
let clh = 11;
|
||||||
// Cell number
|
// Cell number
|
||||||
|
@ -357,7 +282,7 @@ pub fn display(timetable: (Vec<String>, (usize, Vec<models::Day>)), cell_length:
|
||||||
let sep = TabChar::Bv.val();
|
let sep = TabChar::Bv.val();
|
||||||
|
|
||||||
// Top of the tab
|
// Top of the tab
|
||||||
utils::line_table(clh, cell_length, cn, Position::Top, HashMap::new());
|
utils::line_table(clh, cell_length, cn, &Position::Top, &HashMap::new());
|
||||||
|
|
||||||
// First empty case
|
// First empty case
|
||||||
print!("{}{:^clh$}{}", sep, "", sep);
|
print!("{}{:^clh$}{}", sep, "", sep);
|
||||||
|
@ -372,15 +297,15 @@ pub fn display(timetable: (Vec<String>, (usize, Vec<models::Day>)), cell_length:
|
||||||
// Store the data of the course for utils::line_table
|
// Store the data of the course for utils::line_table
|
||||||
let mut next_skip = HashMap::new();
|
let mut next_skip = HashMap::new();
|
||||||
// For each hours -- i the hour's number
|
// For each hours -- i the hour's number
|
||||||
for (i, hour) in timetable.0.into_iter().enumerate() {
|
for (i, hour) in timetable.0.iter().enumerate() {
|
||||||
// Draw separator line
|
// Draw separator line
|
||||||
utils::line_table(clh, cell_length, cn, Position::Middle, next_skip);
|
utils::line_table(clh, cell_length, cn, &Position::Middle, &next_skip);
|
||||||
|
|
||||||
// Reset
|
// Reset
|
||||||
next_skip = HashMap::new();
|
next_skip = HashMap::new();
|
||||||
|
|
||||||
// Print hour
|
// Print hour
|
||||||
print!("{}{:^clh$}", sep, hour);
|
print!("{sep}{hour:^clh$}");
|
||||||
|
|
||||||
// For all the days - `j` the day's number
|
// For all the days - `j` the day's number
|
||||||
for (j, day) in timetable.1 .1.iter().enumerate() {
|
for (j, day) in timetable.1 .1.iter().enumerate() {
|
||||||
|
@ -407,7 +332,8 @@ pub fn display(timetable: (Vec<String>, (usize, Vec<models::Day>)), cell_length:
|
||||||
}
|
}
|
||||||
info_slot = true;
|
info_slot = true;
|
||||||
break;
|
break;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
// Else simply print the course
|
// Else simply print the course
|
||||||
// If the data is too long
|
// If the data is too long
|
||||||
if course.name.len() > quarter {
|
if course.name.len() > quarter {
|
||||||
|
@ -419,7 +345,6 @@ pub fn display(timetable: (Vec<String>, (usize, Vec<models::Day>)), cell_length:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// If no course was found
|
// If no course was found
|
||||||
None => {
|
None => {
|
||||||
// Verify the "no course" is in the correct day and hour
|
// Verify the "no course" is in the correct day and hour
|
||||||
|
@ -439,8 +364,8 @@ pub fn display(timetable: (Vec<String>, (usize, Vec<models::Day>)), cell_length:
|
||||||
print!("{}{:^cell_length$}", sep, "");
|
print!("{}{:^cell_length$}", sep, "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
print!("{}", sep);
|
print!("{sep}");
|
||||||
}
|
}
|
||||||
// Bottom of the table
|
// Bottom of the table
|
||||||
utils::line_table(clh, cell_length, cn, Position::Bottom, HashMap::new());
|
utils::line_table(clh, cell_length, cn, &Position::Bottom, &HashMap::new());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,23 @@
|
||||||
#[derive(Clone)]
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub enum Category {
|
||||||
|
Cours,
|
||||||
|
TP,
|
||||||
|
TD,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for Category {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
std::fmt::Debug::fmt(self, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct Course {
|
pub struct Course {
|
||||||
|
/// Type du cours
|
||||||
|
pub category: Arc<[Category]>,
|
||||||
|
|
||||||
/// Course's name
|
/// Course's name
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
|
||||||
|
@ -25,11 +43,23 @@ pub struct Course {
|
||||||
/// Datetime when the course end
|
/// Datetime when the course end
|
||||||
/// Filled only when building for the ICS
|
/// Filled only when building for the ICS
|
||||||
pub dtend: Option<chrono::DateTime<chrono::Utc>>,
|
pub dtend: Option<chrono::DateTime<chrono::Utc>>,
|
||||||
|
|
||||||
|
/// Extra data
|
||||||
|
pub data: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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
|
||||||
|
Arc<[String]>,
|
||||||
|
// Timetable per days with the semester as the key
|
||||||
|
(usize, Vec<Day>),
|
||||||
|
);
|
||||||
|
|
183
src/utils.rs
183
src/utils.rs
|
@ -1,28 +1,28 @@
|
||||||
|
use std::{collections::HashMap, sync::Arc};
|
||||||
|
|
||||||
|
use chrono::{Datelike, Utc};
|
||||||
|
use scraper::Html;
|
||||||
|
|
||||||
|
use crate::timetable::models::{Category, Course, Timetable};
|
||||||
|
|
||||||
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,
|
||||||
cell_length: usize,
|
cell_length: usize,
|
||||||
number_cell: usize,
|
number_cell: usize,
|
||||||
pos: models::Position,
|
pos: &models::Position,
|
||||||
skip_with: std::collections::HashMap<usize, &str>,
|
skip_with: &std::collections::HashMap<usize, &str>,
|
||||||
) {
|
) {
|
||||||
// Left side
|
// Left side
|
||||||
let ls = match pos {
|
let ls = match pos {
|
||||||
|
@ -60,8 +60,8 @@ pub fn line_table(
|
||||||
|
|
||||||
// Hours column
|
// Hours column
|
||||||
match skip_with.get(&0) {
|
match skip_with.get(&0) {
|
||||||
Some(_) => print!("\n{}{}{}", ls, line_h, rs_bbc),
|
Some(_) => print!("\n{ls}{line_h}{rs_bbc}"),
|
||||||
None => print!("\n{}{}{}", ls, line_h, ms),
|
None => print!("\n{ls}{line_h}{ms}"),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Courses columns
|
// Courses columns
|
||||||
|
@ -72,26 +72,26 @@ pub fn line_table(
|
||||||
if i == range - 1 {
|
if i == range - 1 {
|
||||||
// Friday only
|
// Friday only
|
||||||
if let Some(text) = skip_with.get(&i) {
|
if let Some(text) = skip_with.get(&i) {
|
||||||
println!("{:^cell_length$}{}", text, rsbc_bbc);
|
println!("{text:^cell_length$}{rsbc_bbc}");
|
||||||
last_day = true;
|
last_day = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
match skip_with.get(&i) {
|
match skip_with.get(&i) {
|
||||||
Some(text) => match skip_with.get(&(i + 1)) {
|
Some(text) => match skip_with.get(&(i + 1)) {
|
||||||
// Match check if the next cell will be big
|
// Match check if the next cell will be big
|
||||||
Some(_) => print!("{:^cell_length$}{}", text, rsbc_bbc),
|
Some(_) => print!("{text:^cell_length$}{rsbc_bbc}"),
|
||||||
None => print!("{:^cell_length$}{}", text, rsbc),
|
None => print!("{text:^cell_length$}{rsbc}"),
|
||||||
},
|
},
|
||||||
None => match skip_with.get(&(i + 1)) {
|
None => match skip_with.get(&(i + 1)) {
|
||||||
// Match check if the next cell will be big
|
// Match check if the next cell will be big
|
||||||
Some(_) => print!("{}{}", line, rs_bbc),
|
Some(_) => print!("{line}{rs_bbc}"),
|
||||||
None => print!("{}{}", line, ms),
|
None => print!("{line}{ms}"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !last_day {
|
if !last_day {
|
||||||
println!("{}{}", line, rs);
|
println!("{line}{rs}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,11 +112,148 @@ 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{year}/EDT/visualiserEmploiDuTemps.php?quoi=M{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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get all hours used the source, from 08:00 to at least 20:00
|
||||||
|
pub fn get_hours() -> Arc<[String]> {
|
||||||
|
let mut hours = vec![];
|
||||||
|
for hour in 8..=20 {
|
||||||
|
for minute in &[0, 15, 30, 45] {
|
||||||
|
let hour_str = format!("{hour}h{minute:02}");
|
||||||
|
if let Some(last_hour) = hours.pop() {
|
||||||
|
hours.push(format!("{last_hour}-{hour_str}"));
|
||||||
|
}
|
||||||
|
hours.push(hour_str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _ in 0..4 {
|
||||||
|
hours.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
hours.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Names showed to the users
|
||||||
|
pub fn get_selection(data: &(&Course, String)) -> String {
|
||||||
|
let hours = get_hours();
|
||||||
|
|
||||||
|
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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Entry's name used for finding duplicates
|
||||||
|
pub fn get_entry(course: &Course) -> String {
|
||||||
|
format!("{} - {:?}", course.name, course.category)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Entry's name used for finding duplicates, ignoring categories
|
||||||
|
pub fn get_entry_nocat(course: &Course) -> String {
|
||||||
|
course.name.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a couple of (list of courses) and (a hashmap of how much they appears in the vector)
|
||||||
|
pub fn get_count<'a>(
|
||||||
|
timetable: &'a mut Timetable,
|
||||||
|
allowed_list: &'a [Category],
|
||||||
|
getter: fn(&Course) -> String,
|
||||||
|
) -> (Vec<(&'a Course, String)>, HashMap<String, i32>) {
|
||||||
|
// List of courses who will be courses
|
||||||
|
let mut courses = vec![];
|
||||||
|
|
||||||
|
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 {
|
||||||
|
if course
|
||||||
|
.category
|
||||||
|
.iter()
|
||||||
|
.any(|category| allowed_list.contains(category))
|
||||||
|
{
|
||||||
|
courses.push((course, day.name.clone()));
|
||||||
|
let count = counts.entry(getter(course)).or_insert(0);
|
||||||
|
*count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
(courses, counts)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use chrono::Utc;
|
||||||
|
|
||||||
/// Collection of char for the table
|
/// Collection of char for the table
|
||||||
pub enum TabChar {
|
pub enum TabChar {
|
||||||
/// Vertical bar
|
/// Vertical bar
|
||||||
|
@ -47,5 +51,20 @@ impl TabChar {
|
||||||
pub enum Position {
|
pub enum Position {
|
||||||
Top,
|
Top,
|
||||||
Middle,
|
Middle,
|
||||||
Bottom
|
Bottom,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub type InfoList = Vec<(chrono::DateTime<Utc>, i64)>;
|
||||||
|
|
||||||
|
pub struct InfoType {
|
||||||
|
pub course: InfoList,
|
||||||
|
pub td_tp: InfoList,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info who old the start and end of courses
|
||||||
|
pub type Info = HashMap<
|
||||||
|
// Semester
|
||||||
|
usize,
|
||||||
|
// List of start and repetition of course and TD/TP weeks
|
||||||
|
InfoType,
|
||||||
|
>;
|
||||||
|
|
Reference in a new issue