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

View File

@@ -7,8 +7,7 @@ pub struct Eniro;
impl Probe for Eniro { impl Probe for Eniro {
fn search(&mut self, ctx: &mut Context, number: &str) { fn search(&mut self, ctx: &mut Context, number: &str) {
let body = if let Some(cache) = ctx.cache_get("eniro", &number) { let body = if let Some(cache) = ctx.cache_get("eniro", &number) {
String::from_utf8(cache.data) String::from_utf8(cache.data).unwrap()
.unwrap()
} else { } else {
reqwest::get(&format!("https://gulasidorna.eniro.se/hitta:{}", number)) reqwest::get(&format!("https://gulasidorna.eniro.se/hitta:{}", number))
.unwrap() .unwrap()
@@ -16,6 +15,7 @@ impl Probe for Eniro {
.unwrap() .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 { impl Probe for Hitta {
fn search(&mut self, ctx: &mut Context, number: &str) { fn search(&mut self, ctx: &mut Context, number: &str) {
let body = if let Some(cache) = ctx.cache_get("hitta", &number) { let body = if let Some(cache) = ctx.cache_get("hitta", &number) {
String::from_utf8(cache.data) String::from_utf8(cache.data).unwrap()
.unwrap()
} else { } else {
reqwest::get(&format!("https://www.hitta.se/vem-ringde/{}", number)) reqwest::get(&format!("https://www.hitta.se/vem-ringde/{}", number))
.unwrap() .unwrap()
@@ -18,7 +17,8 @@ impl Probe for Hitta {
.unwrap() .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); let document = Html::parse_document(&body);

View File

@@ -7,15 +7,18 @@ pub struct KonsumentInfo;
impl Probe for KonsumentInfo { impl Probe for KonsumentInfo {
fn search(&mut self, ctx: &mut Context, number: &str) { fn search(&mut self, ctx: &mut Context, number: &str) {
let body = if let Some(cache) = ctx.cache_get("konsument_info", &number) { let body = if let Some(cache) = ctx.cache_get("konsument_info", &number) {
String::from_utf8(cache.data) String::from_utf8(cache.data).unwrap()
.unwrap()
} else { } else {
reqwest::get(&format!("http://konsumentinfo.se/telefonnummer/sverige/{}", number)) reqwest::get(&format!(
"http://konsumentinfo.se/telefonnummer/sverige/{}",
number
))
.unwrap() .unwrap()
.text() .text()
.unwrap() .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 { impl Probe for Telefonforsaljare {
fn search(&mut self, ctx: &mut Context, number: &str) { fn search(&mut self, ctx: &mut Context, number: &str) {
let body = if let Some(cache) = ctx.cache_get("telefonforsaljare", &number) { let body = if let Some(cache) = ctx.cache_get("telefonforsaljare", &number) {
String::from_utf8(cache.data) String::from_utf8(cache.data).unwrap()
.unwrap()
} else { } else {
reqwest::get(&format!("http://www.telefonforsaljare.nu/telefonnummer/{}/", number)) reqwest::get(&format!(
"http://www.telefonforsaljare.nu/telefonnummer/{}/",
number
))
.unwrap() .unwrap()
.text() .text()
.unwrap() .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 { impl Probe for VemRingde {
fn search(&mut self, ctx: &mut Context, number: &str) { fn search(&mut self, ctx: &mut Context, number: &str) {
let body = if let Some(cache) = ctx.cache_get("vem_ringde", &number) { let body = if let Some(cache) = ctx.cache_get("vem_ringde", &number) {
String::from_utf8(cache.data) String::from_utf8(cache.data).unwrap()
.unwrap()
} else { } else {
reqwest::get(&format!("http://vemringde.se/?q={}", number)) reqwest::get(&format!("http://vemringde.se/?q={}", number))
.unwrap() .unwrap()
@@ -16,6 +15,7 @@ impl Probe for VemRingde {
.unwrap() .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?!");
} }
} }