78 lines
1.9 KiB
Rust
78 lines
1.9 KiB
Rust
use regex::Regex;
|
|
use serde_derive::Deserialize;
|
|
|
|
use crate::context::Context;
|
|
use crate::probe::Probe;
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct Data {
|
|
props: Props,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct Props {
|
|
page_props: PageProps,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct PageProps {
|
|
phone_data: PhoneData,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct PhoneData {
|
|
alternative_formats: Vec<String>,
|
|
clean_number: String,
|
|
#[serde(default)]
|
|
comments: Vec<Comment>,
|
|
statistics_text: String,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct Comment {
|
|
comment: String,
|
|
timestamp: u64,
|
|
}
|
|
|
|
// https://www.hitta.se/vem-ringde/{}
|
|
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()
|
|
} else {
|
|
reqwest::get(&format!("https://www.hitta.se/vem-ringde/{}", number))
|
|
.unwrap()
|
|
.text()
|
|
.unwrap()
|
|
};
|
|
|
|
ctx.cache_set("hitta", &number, body.as_bytes())
|
|
.expect("wut?! why not?!");
|
|
|
|
let re = Regex::new(r#"<script>__NEXT_DATA__ = (.*?);__NEXT_LOADED_PAGES__"#).unwrap();
|
|
|
|
if let Some(result) = re.captures(&body) {
|
|
let json = result.get(1).unwrap().as_str();
|
|
|
|
println!("hitta.se:");
|
|
|
|
if let Ok(data) = serde_json::from_str::<Data>(&json) {
|
|
println!(" {}", data.props.page_props.phone_data.statistics_text);
|
|
|
|
for comment in &data.props.page_props.phone_data.comments {
|
|
println!(" * {}", comment.comment);
|
|
}
|
|
} else {
|
|
println!(" Failed to find any data");
|
|
}
|
|
}
|
|
}
|
|
}
|