refactor main
This commit is contained in:
+35
-25
@@ -1,6 +1,6 @@
|
|||||||
mod extractor;
|
mod extractor;
|
||||||
|
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, bail, Result};
|
||||||
use scraper::{Html, Selector};
|
use scraper::{Html, Selector};
|
||||||
|
|
||||||
use extractor::Extractor;
|
use extractor::Extractor;
|
||||||
@@ -14,35 +14,45 @@ fn main() -> Result<()> {
|
|||||||
|
|
||||||
let extractor = Extractor::new();
|
let extractor = Extractor::new();
|
||||||
|
|
||||||
for departure_block in html.select(&extractor.departure) {
|
for departure in get_departures(&html, &extractor) {
|
||||||
let val = departure_block.value();
|
println!("{}", get_transport_line(departure));
|
||||||
println!("{}", val.attr("data-filter").unwrap().trim());
|
println!("{}", get_times(departure, &extractor)?);
|
||||||
|
|
||||||
let late = extract_delay(departure_block, &extractor.delay);
|
|
||||||
for time_block in departure_block
|
|
||||||
.select(&extractor.time)
|
|
||||||
.next()
|
|
||||||
.unwrap()
|
|
||||||
.children()
|
|
||||||
{
|
|
||||||
let value = time_block.value();
|
|
||||||
|
|
||||||
if value.is_text() {
|
|
||||||
let text = value.as_text().unwrap().trim();
|
|
||||||
if !text.is_empty() {
|
|
||||||
print!("Abfahrt: {text}");
|
|
||||||
if let Some(late_text) = &late {
|
|
||||||
print!(" | Verspätet: {late_text}");
|
|
||||||
}
|
|
||||||
println!();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
println!();
|
println!();
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_times(departure: scraper::ElementRef, extractor: &Extractor) -> Result<String> {
|
||||||
|
let delay = extract_delay(departure, &extractor.delay);
|
||||||
|
for time_block in departure.select(&extractor.time).next().unwrap().children() {
|
||||||
|
let value = time_block.value();
|
||||||
|
|
||||||
|
if value.is_text() {
|
||||||
|
let scheduled_time = value.as_text().unwrap().trim();
|
||||||
|
if !scheduled_time.is_empty() {
|
||||||
|
let mut result = "Abfahrt: ".to_owned();
|
||||||
|
result.push_str(scheduled_time);
|
||||||
|
|
||||||
|
if let Some(late_text) = &delay {
|
||||||
|
result.push_str(" | Verspätet: ");
|
||||||
|
result.push_str(late_text);
|
||||||
|
}
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bail!("could not get the times")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_departures<'a>(html: &'a Html, extractor: &'a Extractor) -> scraper::html::Select<'a, 'a> {
|
||||||
|
html.select(&extractor.departure)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_transport_line(departure_block: scraper::ElementRef) -> &str {
|
||||||
|
let val = departure_block.value();
|
||||||
|
val.attr("data-filter").unwrap().trim()
|
||||||
|
}
|
||||||
|
|
||||||
fn query_server() -> Result<String> {
|
fn query_server() -> Result<String> {
|
||||||
match ureq::get(SIEDLUNG).call() {
|
match ureq::get(SIEDLUNG).call() {
|
||||||
Ok(response) => Ok(response.into_string().map_err(|e| anyhow!(e))?),
|
Ok(response) => Ok(response.into_string().map_err(|e| anyhow!(e))?),
|
||||||
|
|||||||
Reference in New Issue
Block a user