Did a lot of things.

This commit is contained in:
2019-02-06 17:08:45 +01:00
parent 8b1cc276d6
commit 20e41d8f65
14 changed files with 1118 additions and 77 deletions

View File

@@ -1,6 +1,5 @@
use scraper::{Html, Selector};
use crate::context::Context;
use crate::probe::{Entry, Probe};
fn from_html(document: &str) -> Result<Entry, ()> {
@@ -44,23 +43,23 @@ fn from_html(document: &str) -> Result<Entry, ()> {
pub struct Eniro;
impl Probe for Eniro {
fn provider(&self) -> &'static str {
"eniro.se"
}
fn uri(&self, number: &str) -> String {
format!("https://gulasidorna.eniro.se/hitta:{}", number)
}
fn search(&mut self, ctx: &mut Context, number: &str) -> Result<Entry, ()> {
let body = if let Some(cache) = ctx.cache_get("eniro", &number) {
String::from_utf8(cache.data).unwrap()
} else {
let body = reqwest::get(&self.uri(number)).unwrap().text().unwrap();
fn fetch(&self, number: &str) -> Result<String, ()> {
reqwest::get(&self.uri(number))
.map_err(|_| ())?
.text()
.map_err(|_| ())
}
ctx.cache_set("eniro", &number, body.as_bytes())
.expect("wut?! why not?!");
body
};
from_html(&body)
fn parse(&self, data: &str) -> Result<Entry, ()> {
from_html(&data)
}
}
@@ -146,4 +145,15 @@ mod tests {
- 303 denna vecka och 304 totalt.
comments: []"###);
}
#[test]
fn test_0701807618() {
let document = include_str!("../../fixtures/eniro/0701807618.html");
assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: []
history:
- 0 denna vecka och 1 totalt.
comments: []"###);
}
}