use std::io::{BufRead, Write};
use std::time::Instant;
use pagesift_content::{Options, extract_metadata};
fn main() {
let list = std::env::args().nth(1).expect("usage: meta_eval <list>");
let out = std::io::stdout();
let mut out = out.lock();
for line in std::io::BufReader::new(std::fs::File::open(list).unwrap()).lines() {
let line = line.unwrap();
let (path, url) = match line.split_once('\t') {
Some((p, u)) => (p.to_string(), Some(u.to_string())),
None => (line.clone(), None),
};
let bytes = std::fs::read(&path).unwrap();
let html = pagesift_net::decode(&bytes, None, None).text;
let opts = Options {
base_url: url.as_deref().and_then(|u| url::Url::parse(u).ok()),
..Options::default()
};
let t = Instant::now();
let m = extract_metadata(&html, &opts);
let ms = t.elapsed().as_secs_f64() * 1000.0;
let rec = serde_json::json!({
"file": path,
"published": m.published_time,
"modified": m.modified_time,
"title": m.title,
"author": m.author,
"language": m.language,
"ms": ms,
});
writeln!(out, "{rec}").unwrap();
}
}