Added test, but broke stuff.
This commit is contained in:
@@ -3,7 +3,7 @@ use regex::Regex;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::context::Context;
|
||||
use crate::probe::Probe;
|
||||
use crate::probe::{self, Entry, Probe};
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
@@ -41,6 +41,46 @@ struct Comment {
|
||||
timestamp: u64,
|
||||
}
|
||||
|
||||
fn from_html(document: &str) -> Result<Entry, ()> {
|
||||
let re = Regex::new(r#"<script>__NEXT_DATA__ = (.*?);__NEXT_LOADED_PAGES__"#).unwrap();
|
||||
|
||||
let result = re.captures(&document).ok_or_else(|| {
|
||||
debug!("Hitta: failed to find __NEXT_DATA__");
|
||||
})?;
|
||||
|
||||
let json = result.get(1).unwrap().as_str();
|
||||
|
||||
if let Ok(data) = serde_json::from_str::<Data>(&json) {
|
||||
let messages = Vec::new();
|
||||
let mut history = Vec::new();
|
||||
let mut comments = Vec::new();
|
||||
|
||||
if let Some(phone_data) = data.props.page_props.phone_data {
|
||||
history.push(phone_data.statistics_text);
|
||||
|
||||
for comment in phone_data.comments {
|
||||
comments.push(probe::Comment {
|
||||
datetime: "".to_string(),
|
||||
title: "".to_string(),
|
||||
message: comment.comment,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Entry {
|
||||
messages,
|
||||
history,
|
||||
comments,
|
||||
})
|
||||
} else {
|
||||
if let Err(error) = serde_json::from_str::<Data>(&json) {
|
||||
debug!("Hitta: failed to deserialize data: {:#?}", error);
|
||||
}
|
||||
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Hitta;
|
||||
|
||||
impl Probe for Hitta {
|
||||
@@ -60,36 +100,32 @@ impl Probe for Hitta {
|
||||
body
|
||||
};
|
||||
|
||||
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();
|
||||
|
||||
if let Ok(data) = serde_json::from_str::<Data>(&json) {
|
||||
match from_html(&body) {
|
||||
Ok(entry) => {
|
||||
println!("hitta.se:");
|
||||
|
||||
if let Some(phone_data) = data.props.page_props.phone_data {
|
||||
println!(" {}", phone_data.statistics_text);
|
||||
|
||||
for comment in &phone_data.comments {
|
||||
println!(" * {}", comment.comment);
|
||||
}
|
||||
} else {
|
||||
println!(" Vi hittar det mesta, men inte just den här sidan.");
|
||||
}
|
||||
print!("{}", entry);
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
if let Err(error) = serde_json::from_str::<Data>(&json) {
|
||||
debug!("Hitta: failed to deserialize data: {:#?}", error);
|
||||
}
|
||||
|
||||
Err(())
|
||||
}
|
||||
} else {
|
||||
debug!("Hitta: failed to find __NEXT_DATA__");
|
||||
|
||||
Err(())
|
||||
Err(_) => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_0702269893() {
|
||||
let document = include_str!("../../fixtures/hitta/0702269893.html");
|
||||
|
||||
let expected = Entry {
|
||||
messages: vec![],
|
||||
history: vec!["Tre andra har också sökt på detta nummer".to_string()],
|
||||
comments: vec![],
|
||||
};
|
||||
|
||||
assert_eq!(from_html(&document), Ok(expected));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user