start date chooser (#8)

This commit is contained in:
Mylloon 2024-09-13 19:00:18 +02:00
parent b6a2fec3cb
commit fabcd6dac9
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 27 additions and 12 deletions

View file

@ -8,12 +8,12 @@ use crate::utils::{
models::{Info, InfoList, InfoType}, models::{Info, InfoList, InfoType},
}; };
pub async fn info( pub async fn get_start_date(
level: i8, level: i8,
semester_opt: Option<i8>, semester_opt: Option<i8>,
year_opt: Option<i32>, year_opt: Option<i32>,
user_agent: &str, user_agent: &str,
) -> Info { ) -> String {
let semester = get_semester(semester_opt); let semester = get_semester(semester_opt);
let year = get_year(year_opt, semester); let year = get_year(year_opt, semester);
@ -34,14 +34,15 @@ pub async fn info(
.inner_html(); .inner_html();
let re = Regex::new(r"\d{1,2} (septembre|octobre)").unwrap(); let re = Regex::new(r"\d{1,2} (septembre|octobre)").unwrap();
let date = re.captures(&raw_data).and_then(|caps| caps.get(0)).map_or(
{ re.captures(&raw_data)
let default = "1 septembre"; .and_then(|caps| caps.get(0))
println!("Can't find the first week of school, default to : {default}"); .map_or("1 septembre".to_owned(), |m| m.as_str().to_owned())
default }
},
|m| m.as_str(), pub fn info(semester_opt: Option<i8>, year_opt: Option<i32>, date: &str) -> Info {
); let semester = get_semester(semester_opt);
let year = get_year(year_opt, semester);
// 1st semester // 1st semester
let weeks_s1_1 = 6; // Weeks before break let weeks_s1_1 = 6; // Weeks before break
@ -119,7 +120,7 @@ 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}")

View file

@ -1,4 +1,5 @@
use clap::Parser; use clap::Parser;
use dialoguer::Input;
use regex::Regex; use regex::Regex;
mod filter; mod filter;
@ -33,6 +34,10 @@ struct Args {
/// Doesn't distinguish TD from TP /// Doesn't distinguish TD from TP
#[clap(short, long)] #[clap(short, long)]
td_are_tp: bool, td_are_tp: bool,
/// First day of your year
#[clap(short, long)]
first_day: Option<String>,
} }
#[tokio::main] #[tokio::main]
@ -58,8 +63,17 @@ async fn main() {
timetable = filter::timetable(timetable, args.td_are_tp); 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,
};
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(level, args.semester, args.year, &user_agent).await; let info = info::info(args.semester, args.year, &date);
if args.export.is_some() { if args.export.is_some() {
// Export the calendar // Export the calendar