-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from gerardcl/feature/check-stations
check stations
- Loading branch information
Showing
4 changed files
with
101 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,61 @@ | ||
use pyo3::pyfunction; | ||
use pyo3::{ | ||
exceptions::{PyConnectionError, PyValueError}, | ||
pyclass, pymethods, PyResult, | ||
}; | ||
use scraper::{Html, Selector}; | ||
|
||
#[pyfunction] | ||
pub fn load_stations() -> Vec<String> { | ||
let response = match ureq::get("https://www.renfe.com/content/renfe/es/en/viajar/informacion-util/horarios/app-horarios.html").call() { | ||
Ok(response) => { response }, | ||
Err(_) => { panic!("something wrong") } | ||
}; | ||
#[pyclass] | ||
pub struct Renfe { | ||
stations: Vec<String>, | ||
} | ||
|
||
#[pymethods] | ||
impl Renfe { | ||
#[new] | ||
pub fn new() -> PyResult<Self> { | ||
println!("Loading stations from Renfe web"); | ||
let response = match ureq::get("https://www.renfe.com/content/renfe/es/en/viajar/informacion-util/horarios/app-horarios.html").call() { | ||
Ok(response) => { response }, | ||
Err(_) => { return Err(PyConnectionError::new_err("something wrong")) } | ||
}; | ||
|
||
let parsed_html = Html::parse_document(&response.into_string().unwrap()); | ||
|
||
let selector = &Selector::parse(r#"#O > option"#).unwrap(); | ||
|
||
let parsed_html = Html::parse_document(&response.into_string().unwrap()); | ||
let stations: Vec<String> = parsed_html | ||
.select(selector) | ||
.flat_map(|el| el.text()) | ||
.map(|t| t.to_string()) | ||
.collect(); | ||
|
||
let selector = &Selector::parse(r#"#O > option"#).unwrap(); | ||
Ok(Renfe { | ||
stations: stations[1..].to_vec(), | ||
}) | ||
} | ||
|
||
let stations: Vec<String> = parsed_html | ||
.select(selector) | ||
.flat_map(|el| el.text()) | ||
.map(|t| t.to_string()) | ||
.collect(); | ||
pub fn stations_match(&self, station: String) -> PyResult<Vec<&String>> { | ||
let found: Vec<&String> = self | ||
.stations | ||
.iter() | ||
.filter(|s| s.contains(&station)) | ||
.collect(); | ||
Ok(found) | ||
} | ||
|
||
stations[1..].to_vec() | ||
pub fn filter_station(&self, station: String) -> PyResult<String> { | ||
match self.stations_match(station.clone()) { | ||
Ok(v) if v.len() == 1 => { | ||
println!( | ||
"Provided input '{}' station matches with '{}'...continue", | ||
station, v[0] | ||
); | ||
Ok(v[0].to_owned()) | ||
} | ||
Ok(v) => Err(PyValueError::new_err(format!( | ||
"Provided input '{station}' station does not match one '{v:?}'" | ||
))), | ||
Err(e) => Err(e), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters