refactor main
This commit is contained in:
+55
-1
@@ -1,4 +1,5 @@
|
||||
use scraper::Selector;
|
||||
use anyhow::{bail, Context, Result};
|
||||
use scraper::{Html, Selector};
|
||||
|
||||
pub(crate) struct Extractor {
|
||||
pub time: Selector,
|
||||
@@ -26,4 +27,57 @@ impl Extractor {
|
||||
fn get_late_selector() -> Selector {
|
||||
Selector::parse("span.late").unwrap()
|
||||
}
|
||||
|
||||
pub(crate) fn get_departures<'a>(&'a self, html: &'a Html) -> scraper::html::Select<'a, 'a> {
|
||||
html.select(&self.departure)
|
||||
}
|
||||
|
||||
pub(crate) fn get_transport_line(departure_block: scraper::ElementRef) -> Result<&str> {
|
||||
let val = departure_block.value();
|
||||
let result = val
|
||||
.attr("data-filter")
|
||||
.context("accessing 'data-filter'")?
|
||||
.trim();
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub(crate) fn extract_delay(
|
||||
departure_block: scraper::ElementRef,
|
||||
late_selector: &Selector,
|
||||
) -> Option<String> {
|
||||
departure_block
|
||||
.select(late_selector)
|
||||
.next()
|
||||
.map(|content| content.inner_html())
|
||||
}
|
||||
|
||||
pub(crate) fn get_times(&self, departure: scraper::ElementRef) -> Result<String> {
|
||||
let delay = Self::extract_delay(departure, &self.delay);
|
||||
for time_block in departure
|
||||
.select(&self.time)
|
||||
.next()
|
||||
.context("extracting time block")?
|
||||
.children()
|
||||
{
|
||||
let value = time_block.value();
|
||||
|
||||
if value.is_text() {
|
||||
let scheduled_time = value
|
||||
.as_text()
|
||||
.context("extracting text of scheduled time")?
|
||||
.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")
|
||||
}
|
||||
}
|
||||
|
||||
+5
-54
@@ -1,7 +1,7 @@
|
||||
mod extractor;
|
||||
|
||||
use anyhow::{anyhow, bail, Context, Result};
|
||||
use scraper::{Html, Selector};
|
||||
use anyhow::{anyhow, Result};
|
||||
use scraper::Html;
|
||||
|
||||
use extractor::Extractor;
|
||||
|
||||
@@ -14,66 +14,17 @@ fn main() -> Result<()> {
|
||||
|
||||
let extractor = Extractor::new();
|
||||
|
||||
for departure in get_departures(&html, &extractor) {
|
||||
println!("{}", get_transport_line(departure)?);
|
||||
println!("{}", get_times(departure, &extractor)?);
|
||||
for departure in extractor.get_departures(&html) {
|
||||
println!("{}", Extractor::get_transport_line(departure)?);
|
||||
println!("{}", extractor.get_times(departure)?);
|
||||
println!();
|
||||
}
|
||||
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()
|
||||
.context("extracting time block")?
|
||||
.children()
|
||||
{
|
||||
let value = time_block.value();
|
||||
|
||||
if value.is_text() {
|
||||
let scheduled_time = value
|
||||
.as_text()
|
||||
.context("extracting text of scheduled time")?
|
||||
.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) -> Result<&str> {
|
||||
let val = departure_block.value();
|
||||
Ok(val
|
||||
.attr("data-filter")
|
||||
.context("accessing 'data-filter'")?
|
||||
.trim())
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user