add first working code

This commit is contained in:
Dr. Matthias Ratajczak
2022-12-15 15:31:15 +01:00
commit 5d2bb48a52
4 changed files with 1055 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
use anyhow::{anyhow, Result};
use scraper::{Html, Selector};
const SIEDLUNG: &str =
"https://www.vvo-online.de/de/fahrplan/aktuelle-abfahrten-ankuenfte/abfahrten?stopid=33006764";
fn main() -> Result<()> {
let message = query_server()?;
let html = Html::parse_document(&message);
let departure_selector = get_departure_selector();
let time_selector = get_time_selector();
let late_selector = get_late_selector();
for departure_block in html.select(&departure_selector) {
let val = departure_block.value();
println!("{}", val.attr("data-filter").unwrap().trim());
let late = extract_delay(departure_block, &late_selector);
for time_block in departure_block
.select(&time_selector)
.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!();
}
Ok(())
}
fn get_late_selector() -> Selector {
Selector::parse("span.late").unwrap()
}
fn query_server() -> Result<String> {
match ureq::get(SIEDLUNG).call() {
Ok(response) => Ok(response.into_string().map_err(|e| anyhow!(e))?),
Err(e) => Err(anyhow!(e)),
}
}
fn extract_delay(departure_block: scraper::ElementRef, late_selector: &Selector) -> Option<String> {
departure_block
.select(late_selector)
.next()
.map(|content| content.inner_html())
}
fn get_time_selector() -> Selector {
Selector::parse("div.col.c2of12.tour p").unwrap()
}
fn get_departure_selector() -> Selector {
Selector::parse("li[data-filter][data-departure-id][data-departure-time]").unwrap()
}