Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort timetable #190

Merged
merged 6 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Changelog

## v4.3.0 (2024-02-10)

* New feature for sorting timetables [#189](https://github.com/gerardcl/renfe-cli/issues/189)

## v4.2.1 (2024-02-02)

* Fix navigation with proper date handling [#184](https://github.com/gerardcl/renfe-cli/issues/187)
* Fix navigation with proper date handling [#187](https://github.com/gerardcl/renfe-cli/issues/187)

## v4.2.0 (2023-11-26)

Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "renfe-cli"
version = "4.2.1"
version = "4.3.0"
edition = "2021"
license = "BSD-3-Clause"
description = "CLI for searching Renfe train timetables in the Spanish country"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The navigation through the site happens in the following steps:
4. Writes down and selects the month to search for
5. Writes down and selects the year to search for
6. Clicks on search button
7. Parses the HTML data and prints the timetable
7. Parses the HTML data, optionally sorts the connections and prints the timetable

```bash
$ renfe-cli -h
Expand All @@ -45,6 +45,7 @@ Options:
-m, --month MONTH Set Month to search timetable for (default: today's month)
-y, --year YEAR Set Year to search timetable for (default: today's year)
-w, --wait SECONDS Set Wait time in seconds for Renfe search result page (default: 2)
-s, --sort Option to sort the timetable by Duration
-h, --help Print this help menu
```

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ maintainers = [
description = "Get faster Renfe Spanish Trains timetables in your terminal."
readme = "README.md"
license = {file = "LICENSE"}
keywords = ["timetables", "trains", "renfe", "cli", "rust"]
keywords = ["timetables", "trains", "renfe", "cli", "rust", "pyo3", "maturin"]
classifiers = [
"Programming Language :: Rust",
"Programming Language :: Python :: Implementation :: CPython",
Expand Down
4 changes: 3 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub fn main() -> PyResult<()> {
.opt_str("w")
.unwrap_or(2.to_string())
.parse::<u64>()?;
let sorted: bool = matches.opt_present("s");

println!(
"Today is: {}-{}-{}",
Expand All @@ -45,7 +46,7 @@ pub fn main() -> PyResult<()> {
);
println!("Searching timetable for date: {}-{}-{}", year, month, day);

let timetable = search_timetable(origin, destination, day, month, year, wait)?;
let timetable = search_timetable(origin, destination, day, month, year, wait, sorted)?;

print_timetable(timetable);
Ok(())
Expand Down Expand Up @@ -92,6 +93,7 @@ fn set_opts() -> Options {
"Set Wait time in seconds for Renfe search result page (default: 2)",
"SECONDS",
);
opts.optflag("s", "sort", "Option to sort the timetable by Duration");
opts.optflag("h", "help", "Print this help menu");

opts
Expand Down
20 changes: 20 additions & 0 deletions src/timetable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ fn to_renfe_month(month: String) -> String {
months[month.as_str()].to_owned()
}

fn get_duration_from_renfe_string(s: &str) -> u16 {
let splits: Vec<&str> = s.split(' ').collect();
if s.contains('h') {
let hours = splits[0].parse::<u16>().unwrap();
let minutes = splits[2].parse::<u16>().unwrap();
hours * 60 + minutes
} else {
splits[0].parse::<u16>().unwrap()
}
}

#[pyfunction]
pub fn search_timetable(
origin: String,
Expand All @@ -63,6 +74,7 @@ pub fn search_timetable(
month: String,
year: String,
wait: u64,
sorted: bool,
) -> PyResult<Vec<Vec<String>>> {
println!("loading headless chrome browser");
let browser = Browser::new(LaunchOptions {
Expand Down Expand Up @@ -188,6 +200,13 @@ pub fn search_timetable(
tracks.push(row);
}

if sorted {
println!("sorting timetable");
tracks.sort_by(|a, b| {
get_duration_from_renfe_string(&a[3]).cmp(&get_duration_from_renfe_string(&b[3]))
});
}

Ok(tracks)
}

Expand Down Expand Up @@ -225,6 +244,7 @@ mod tests {
now.month().to_string(),
now.year().to_string(),
15,
false,
)?);

Ok(())
Expand Down
Loading