Added test, but broke stuff.
This commit is contained in:
@@ -1,21 +1,29 @@
|
||||
use unhtml::FromHtml;
|
||||
use unhtml_derive::FromHtml;
|
||||
use scraper::{Html, Selector};
|
||||
|
||||
use crate::context::Context;
|
||||
use crate::probe::Probe;
|
||||
use crate::probe::{Entry, Probe};
|
||||
|
||||
#[derive(Debug, FromHtml)]
|
||||
#[html(selector = ".panel-body")]
|
||||
struct Info {
|
||||
#[html(selector = "h4", attr = "inner")]
|
||||
message: String,
|
||||
}
|
||||
fn from_html(document: &str) -> Result<Entry, ()> {
|
||||
let html = Html::parse_document(document);
|
||||
|
||||
#[derive(Debug, FromHtml)]
|
||||
#[html(selector = ".body-content > .row")]
|
||||
struct Error {
|
||||
#[html(selector = ".col-md-12", attr = "inner")]
|
||||
message: String,
|
||||
let mut messages = Vec::new();
|
||||
let history = Vec::new();
|
||||
let comments = Vec::new();
|
||||
|
||||
let selector = Selector::parse(".panel-heading > h1:nth-child(3)").unwrap();
|
||||
|
||||
if let Some(element) = html.select(&selector).next() {
|
||||
let message = element.inner_html();
|
||||
let message = htmlescape::decode_html(&message).unwrap();
|
||||
|
||||
messages.push(message);
|
||||
}
|
||||
|
||||
Ok(Entry {
|
||||
messages,
|
||||
history,
|
||||
comments,
|
||||
})
|
||||
}
|
||||
|
||||
pub struct KonsumentInfo;
|
||||
@@ -37,16 +45,32 @@ impl Probe for KonsumentInfo {
|
||||
body
|
||||
};
|
||||
|
||||
println!("konsumentinfo.se:");
|
||||
match from_html(&body) {
|
||||
Ok(entry) => {
|
||||
println!("konsumentinfo.se:");
|
||||
print!("{}", entry);
|
||||
|
||||
if let Ok(info) = Info::from_html(&body) {
|
||||
println!(" {}", info.message);
|
||||
} else if let Ok(error) = Error::from_html(&body) {
|
||||
println!(" {}", error.message);
|
||||
} else {
|
||||
println!(" Failed to find any data");
|
||||
Ok(())
|
||||
}
|
||||
Err(_) => Err(()),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_0702269893() {
|
||||
let document = include_str!("../../fixtures/konsumentinfo/0702269893.html");
|
||||
|
||||
let expected = Entry {
|
||||
messages: vec!["Hydroscand AB".to_string()],
|
||||
history: vec![],
|
||||
comments: vec![],
|
||||
};
|
||||
|
||||
assert_eq!(from_html(&document), Ok(expected));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user