Skip to content

Commit

Permalink
fix day dropdown selection
Browse files Browse the repository at this point in the history
  • Loading branch information
henninggross committed Jan 25, 2024
1 parent 8053fab commit 49ba5dd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
17 changes: 15 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,20 @@ pub fn main() -> PyResult<()> {
}
let origin = renfe.filter_station(matches.opt_str("f").unwrap_or("".to_owned()))?;
let destination = renfe.filter_station(matches.opt_str("t").unwrap_or("".to_owned()))?;
let day = matches.opt_str("d").unwrap_or(now.day().to_string());
let day = enrich_day(matches.opt_str("d").unwrap_or(now.day().to_string()));
let month = matches.opt_str("m").unwrap_or(now.month().to_string());
let year = matches.opt_str("y").unwrap_or(now.year().to_string());
let wait = matches
.opt_str("w")
.unwrap_or(2.to_string())
.parse::<u64>()?;

println!("Today is: {}-{}-{}", now.year(), now.month(), now.day());
println!(
"Today is: {}-{}-{}",
now.year(),
now.month(),
enrich_day(now.day().to_string())
);
println!("Searching timetable for date: {}-{}-{}", year, month, day);

let timetable = search_timetable(origin, destination, day, month, year, wait)?;
Expand All @@ -46,6 +51,14 @@ pub fn main() -> PyResult<()> {
Ok(())
}

fn enrich_day(day: String) -> String {
if day.len() == 1 {
"0".to_owned() + &day
} else {
day
}
}

fn print_usage(program: &str, opts: Options) {
let brief = format!("Usage: {} [options]", program);
print!("{}", opts.usage(&brief));
Expand Down
43 changes: 20 additions & 23 deletions src/timetable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ fn make_selector(selector: &str) -> Selector {
Selector::parse(selector).unwrap()
}

fn to_renfe_day(day: String) -> String {
let first_digit: u8 = day.parse::<u8>().unwrap() / 10;
let last_digit: u8 = day.parse::<u8>().unwrap() % 10;
let mut renfe_day: String = String::new();
if day.starts_with('0') {
day
} else {
for _ in 0..last_digit + 1 {
renfe_day += &first_digit.to_string();
}
renfe_day
}
}

fn to_renfe_month(month: String) -> String {
let months: HashMap<&str, &str> = HashMap::from([
("1", "Ene"),
Expand Down Expand Up @@ -95,55 +109,38 @@ pub fn search_timetable(
println!("adding origin station");
tab.find_element_by_xpath(r#"//*[@id="O"]"#)
.unwrap()
.click()
.type_into(&origin)
.unwrap();
let elem = tab.type_str(&origin).unwrap();
elem.press_key("Tab").unwrap();

println!("adding destination station");
tab.find_element_by_xpath(r#"//*[@id="D"]"#)
.unwrap()
.click()
.type_into(&destination)
.unwrap();
let elem = tab.type_str(&destination).unwrap();
elem.press_key("Tab").unwrap();

println!("adding day");
tab.find_element_by_xpath(r#"//*[@id="DF"]"#)
.unwrap()
.click()
.type_into(&to_renfe_day(day))
.unwrap();
let elem = tab.type_str(&day).unwrap();
elem.press_key("Tab").unwrap();

println!("adding month");
tab.find_element_by_xpath(r#"//*[@id="MF"]"#)
.unwrap()
.click()
.type_into(&to_renfe_month(month))
.unwrap();
tab.type_str(&to_renfe_month(month)).unwrap();
elem.press_key("Tab").unwrap();

println!("adding year");
tab.find_element_by_xpath(r#"//*[@id="AF"]"#)
.unwrap()
.click()
.type_into(&year)
.unwrap();
let elem = tab.type_str(&year).unwrap();
elem.press_key("Tab").unwrap();

println!("searching timetable");

elem.press_key("Enter").unwrap();
tab.press_key("Enter").unwrap();

sleep(Duration::from_secs(wait));

// wait on navigating to search result page
tab.wait_until_navigated()
.unwrap()
.wait_for_elements_by_xpath(r#"//*[@id="contenedor"]"#)
.unwrap();

println!("got timetable page");
let html = tab
.find_element_by_xpath(r#"//*[@id="contenedor"]"#)
Expand Down

0 comments on commit 49ba5dd

Please sign in to comment.