This commit is contained in:
2018-12-18 14:43:13 +01:00
parent 9f9955a848
commit 2dfd8abfc1
6 changed files with 36 additions and 28 deletions

View File

@@ -2,7 +2,7 @@ use std::fs;
use std::io;
use directories::ProjectDirs;
use serde_derive::{Serialize, Deserialize};
use serde_derive::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Cache {
@@ -27,8 +27,9 @@ impl Context {
if cache.exists() {
fs::File::open(cache)
.and_then(|file| {
bincode::deserialize_from(&file)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "failed to deserialize cache entry"))
bincode::deserialize_from(&file).map_err(|_| {
io::Error::new(io::ErrorKind::Other, "failed to deserialize cache entry")
})
})
.ok()
} else {
@@ -59,8 +60,9 @@ impl Context {
.truncate(true)
.open(cache)
.and_then(|mut file| {
bincode::serialize_into(&mut file, &entry)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "failed to serialize cache entry"))
bincode::serialize_into(&mut file, &entry).map_err(|_| {
io::Error::new(io::ErrorKind::Other, "failed to serialize cache entry")
})
})
}
}

View File

@@ -7,8 +7,7 @@ pub struct Eniro;
impl Probe for Eniro {
fn search(&mut self, ctx: &mut Context, number: &str) {
let body = if let Some(cache) = ctx.cache_get("eniro", &number) {
String::from_utf8(cache.data)
.unwrap()
String::from_utf8(cache.data).unwrap()
} else {
reqwest::get(&format!("https://gulasidorna.eniro.se/hitta:{}", number))
.unwrap()
@@ -16,6 +15,7 @@ impl Probe for Eniro {
.unwrap()
};
ctx.cache_set("eniro", &number, body.as_bytes()).expect("wut?! why not?!");
ctx.cache_set("eniro", &number, body.as_bytes())
.expect("wut?! why not?!");
}
}

View File

@@ -9,8 +9,7 @@ pub struct Hitta;
impl Probe for Hitta {
fn search(&mut self, ctx: &mut Context, number: &str) {
let body = if let Some(cache) = ctx.cache_get("hitta", &number) {
String::from_utf8(cache.data)
.unwrap()
String::from_utf8(cache.data).unwrap()
} else {
reqwest::get(&format!("https://www.hitta.se/vem-ringde/{}", number))
.unwrap()
@@ -18,7 +17,8 @@ impl Probe for Hitta {
.unwrap()
};
ctx.cache_set("hitta", &number, body.as_bytes()).expect("wut?! why not?!");
ctx.cache_set("hitta", &number, body.as_bytes())
.expect("wut?! why not?!");
let document = Html::parse_document(&body);

View File

@@ -7,15 +7,18 @@ pub struct KonsumentInfo;
impl Probe for KonsumentInfo {
fn search(&mut self, ctx: &mut Context, number: &str) {
let body = if let Some(cache) = ctx.cache_get("konsument_info", &number) {
String::from_utf8(cache.data)
.unwrap()
String::from_utf8(cache.data).unwrap()
} else {
reqwest::get(&format!("http://konsumentinfo.se/telefonnummer/sverige/{}", number))
reqwest::get(&format!(
"http://konsumentinfo.se/telefonnummer/sverige/{}",
number
))
.unwrap()
.text()
.unwrap()
};
ctx.cache_set("konsument_info", &number, body.as_bytes()).expect("wut?! why not?!");
ctx.cache_set("konsument_info", &number, body.as_bytes())
.expect("wut?! why not?!");
}
}

View File

@@ -7,15 +7,18 @@ pub struct Telefonforsaljare;
impl Probe for Telefonforsaljare {
fn search(&mut self, ctx: &mut Context, number: &str) {
let body = if let Some(cache) = ctx.cache_get("telefonforsaljare", &number) {
String::from_utf8(cache.data)
.unwrap()
String::from_utf8(cache.data).unwrap()
} else {
reqwest::get(&format!("http://www.telefonforsaljare.nu/telefonnummer/{}/", number))
reqwest::get(&format!(
"http://www.telefonforsaljare.nu/telefonnummer/{}/",
number
))
.unwrap()
.text()
.unwrap()
};
ctx.cache_set("telefonforsaljare", &number, body.as_bytes()).expect("wut?! why not?!");
ctx.cache_set("telefonforsaljare", &number, body.as_bytes())
.expect("wut?! why not?!");
}
}

View File

@@ -7,8 +7,7 @@ pub struct VemRingde;
impl Probe for VemRingde {
fn search(&mut self, ctx: &mut Context, number: &str) {
let body = if let Some(cache) = ctx.cache_get("vem_ringde", &number) {
String::from_utf8(cache.data)
.unwrap()
String::from_utf8(cache.data).unwrap()
} else {
reqwest::get(&format!("http://vemringde.se/?q={}", number))
.unwrap()
@@ -16,6 +15,7 @@ impl Probe for VemRingde {
.unwrap()
};
ctx.cache_set("vem_ringde", &number, body.as_bytes()).expect("wut?! why not?!");
ctx.cache_set("vem_ringde", &number, body.as_bytes())
.expect("wut?! why not?!");
}
}