Did a lot of things.
This commit is contained in:
5
src/lib.rs
Normal file
5
src/lib.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
mod context;
|
||||
mod probe;
|
||||
|
||||
pub use crate::context::Context;
|
||||
pub use crate::probe::*;
|
||||
32
src/main.rs
32
src/main.rs
@@ -2,12 +2,7 @@ use std::process::Command;
|
||||
|
||||
use fern::colors::{Color, ColoredLevelConfig};
|
||||
use structopt::StructOpt;
|
||||
|
||||
mod context;
|
||||
mod probe;
|
||||
|
||||
use crate::context::Context;
|
||||
use crate::probe::*;
|
||||
use whoareyou::*;
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
#[structopt(name = "whoareyou", about = "Search for swedish phone numbers.")]
|
||||
@@ -57,7 +52,7 @@ fn main() {
|
||||
|
||||
config.apply().expect("failed to init fern");
|
||||
|
||||
let mut probes: Vec<Box<Probe>> = vec![
|
||||
let mut probes: Vec<Box<dyn Probe>> = vec![
|
||||
Box::new(Eniro),
|
||||
Box::new(Hitta),
|
||||
Box::new(KonsumentInfo),
|
||||
@@ -77,9 +72,28 @@ fn main() {
|
||||
} else {
|
||||
let mut ctx = Context::new();
|
||||
|
||||
let mut first = true;
|
||||
|
||||
for probe in &mut probes {
|
||||
if let Ok(entry) = probe.search(&mut ctx, &opt.number) {
|
||||
print!("{}", entry);
|
||||
let data = if let Some(cache) = ctx.cache_get(probe.provider(), &opt.number) {
|
||||
String::from_utf8(cache.data).unwrap()
|
||||
} else if let Ok(data) = probe.fetch(&opt.number) {
|
||||
ctx.cache_set(probe.provider(), &opt.number, data.as_bytes())
|
||||
.expect("wut?! why not?!");
|
||||
|
||||
data
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
|
||||
if let Ok(entry) = probe.parse(&data) {
|
||||
if first {
|
||||
print!("{}\n{}", probe.provider(), entry);
|
||||
|
||||
first = false;
|
||||
} else {
|
||||
print!("\n{}\n{}", probe.provider(), entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,6 @@ mod konsument_info;
|
||||
mod telefonforsaljare;
|
||||
mod vem_ringde;
|
||||
|
||||
use crate::context::Context;
|
||||
|
||||
pub use self::eniro::Eniro;
|
||||
pub use self::hitta::Hitta;
|
||||
pub use self::konsument_info::KonsumentInfo;
|
||||
@@ -79,6 +77,9 @@ impl fmt::Display for Comment {
|
||||
}
|
||||
|
||||
pub trait Probe {
|
||||
fn provider(&self) -> &'static str;
|
||||
fn uri(&self, _: &str) -> String;
|
||||
fn search(&mut self, _: &mut Context, _: &str) -> Result<Entry, ()>;
|
||||
|
||||
fn fetch(&self, _: &str) -> Result<String, ()>;
|
||||
fn parse(&self, _: &str) -> Result<Entry, ()>;
|
||||
}
|
||||
|
||||
@@ -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: []"###);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ use log::debug;
|
||||
use regex::Regex;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::context::Context;
|
||||
use crate::probe::{self, Entry, Probe};
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
@@ -97,23 +96,23 @@ fn from_html(document: &str) -> Result<Entry, ()> {
|
||||
pub struct Hitta;
|
||||
|
||||
impl Probe for Hitta {
|
||||
fn provider(&self) -> &'static str {
|
||||
"hitta.se"
|
||||
}
|
||||
|
||||
fn uri(&self, number: &str) -> String {
|
||||
format!("https://www.hitta.se/vem-ringde/{}", number)
|
||||
}
|
||||
|
||||
fn search(&mut self, ctx: &mut Context, number: &str) -> Result<Entry, ()> {
|
||||
let body = if let Some(cache) = ctx.cache_get("hitta", &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("hitta", &number, body.as_bytes())
|
||||
.expect("wut?! why not?!");
|
||||
|
||||
body
|
||||
};
|
||||
|
||||
from_html(&body)
|
||||
fn parse(&self, data: &str) -> Result<Entry, ()> {
|
||||
from_html(&data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -282,4 +281,11 @@ mod tests {
|
||||
history: []
|
||||
comments: []"###);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_0701807618() {
|
||||
let document = include_str!("../../fixtures/hitta/0701807618.html");
|
||||
|
||||
assert_yaml_snapshot_matches!(from_html(&document), @"Err: ~");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use scraper::{Html, Selector};
|
||||
|
||||
use crate::context::Context;
|
||||
use crate::probe::{Entry, Probe};
|
||||
|
||||
fn from_html(document: &str) -> Result<Entry, ()> {
|
||||
@@ -33,23 +32,23 @@ fn from_html(document: &str) -> Result<Entry, ()> {
|
||||
pub struct KonsumentInfo;
|
||||
|
||||
impl Probe for KonsumentInfo {
|
||||
fn provider(&self) -> &'static str {
|
||||
"konsumentinfo.se"
|
||||
}
|
||||
|
||||
fn uri(&self, number: &str) -> String {
|
||||
format!("http://konsumentinfo.se/telefonnummer/sverige/{}", number)
|
||||
}
|
||||
|
||||
fn search(&mut self, ctx: &mut Context, number: &str) -> Result<Entry, ()> {
|
||||
let body = if let Some(cache) = ctx.cache_get("konsument_info", &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("konsument_info", &number, body.as_bytes())
|
||||
.expect("wut?! why not?!");
|
||||
|
||||
body
|
||||
};
|
||||
|
||||
from_html(&body)
|
||||
fn parse(&self, data: &str) -> Result<Entry, ()> {
|
||||
from_html(&data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,4 +110,11 @@ mod tests {
|
||||
|
||||
assert_yaml_snapshot_matches!(from_html(&document), @"Err: ~");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_0701807618() {
|
||||
let document = include_str!("../../fixtures/konsumentinfo/0701807618.html");
|
||||
|
||||
assert_yaml_snapshot_matches!(from_html(&document), @"Err: ~");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ use chrono::{DateTime, NaiveDateTime, TimeZone, Utc};
|
||||
use chrono_tz::Europe::Stockholm;
|
||||
use scraper::{Html, Selector};
|
||||
|
||||
use crate::context::Context;
|
||||
use crate::probe::{Comment, Entry, Probe};
|
||||
|
||||
fn stockholm_to_utc(s: &str, fmt: &str) -> Result<DateTime<Utc>, ()> {
|
||||
@@ -93,23 +92,23 @@ fn from_html(document: &str) -> Result<Entry, ()> {
|
||||
pub struct Telefonforsaljare;
|
||||
|
||||
impl Probe for Telefonforsaljare {
|
||||
fn provider(&self) -> &'static str {
|
||||
"telefonforsaljare.nu"
|
||||
}
|
||||
|
||||
fn uri(&self, number: &str) -> String {
|
||||
format!("http://www.telefonforsaljare.nu/telefonnummer/{}/", number)
|
||||
}
|
||||
|
||||
fn search(&mut self, ctx: &mut Context, number: &str) -> Result<Entry, ()> {
|
||||
let body = if let Some(cache) = ctx.cache_get("telefonforsaljare", &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("telefonforsaljare", &number, body.as_bytes())
|
||||
.expect("wut?! why not?!");
|
||||
|
||||
body
|
||||
};
|
||||
|
||||
from_html(&body)
|
||||
fn parse(&self, data: &str) -> Result<Entry, ()> {
|
||||
from_html(&data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,4 +221,15 @@ mod tests {
|
||||
- Du är den första de senaste 24 timmarna som söker efter detta nummer. Det tyder på att numret inte används av telefonförsäljare. Totalt har minst 22 personer sökt efter numret.
|
||||
comments: []"###);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_0701807618() {
|
||||
let document = include_str!("../../fixtures/telefonforsaljare/0701807618.html");
|
||||
|
||||
assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
|
||||
messages: []
|
||||
history:
|
||||
- De senaste 24 timmarna har 1 personer sökt efter numret 0701807618. Det kan tyda på att numret används av telefonförsäljare. Totalt har minst 2 personer sökt efter numret.
|
||||
comments: []"###);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ use std::str;
|
||||
// use log::debug;
|
||||
use scraper::{Html, Selector};
|
||||
|
||||
use crate::context::Context;
|
||||
use crate::probe::{Entry, Probe};
|
||||
|
||||
fn from_html(document: &str) -> Result<Entry, ()> {
|
||||
@@ -36,23 +35,23 @@ fn from_html(document: &str) -> Result<Entry, ()> {
|
||||
pub struct VemRingde;
|
||||
|
||||
impl Probe for VemRingde {
|
||||
fn provider(&self) -> &'static str {
|
||||
"vemringde.se"
|
||||
}
|
||||
|
||||
fn uri(&self, number: &str) -> String {
|
||||
format!("http://vemringde.se/?q={}", number)
|
||||
}
|
||||
|
||||
fn search(&mut self, ctx: &mut Context, number: &str) -> Result<Entry, ()> {
|
||||
let body = if let Some(cache) = ctx.cache_get("vem_ringde", &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("vem_ringde", &number, body.as_bytes())
|
||||
.expect("wut?! why not?!");
|
||||
|
||||
body
|
||||
};
|
||||
|
||||
from_html(&body)
|
||||
fn parse(&self, data: &str) -> Result<Entry, ()> {
|
||||
from_html(&data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,4 +131,14 @@ mod tests {
|
||||
history: []
|
||||
comments: []"###);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_0701807618() {
|
||||
let document = include_str!("../../fixtures/vemringde/0701807618.html");
|
||||
|
||||
assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
|
||||
messages: []
|
||||
history: []
|
||||
comments: []"###);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user