StackScraper/src/main.rs

46 lines
1.3 KiB
Rust

use fantoccini::{Client, ClientBuilder};
pub mod collector;
use clap::Parser;
use collector::*;
#[derive(Debug, Parser, Clone)]
pub struct Args {
#[clap(default_value_t = 5)]
pages: u16,
}
#[tokio::main]
async fn main() {
let args = Args::parse();
let start = std::time::Instant::now();
println!("Spawning client");
let c: Client = ClientBuilder::native()
.connect("http://localhost:4444")
.await
.expect("failed to connect to WebDriver");
println!("Getting links");
let links = get_top_links(&c, args.pages)
.await
.expect("Failed to get links. Exiting");
println!("Got {} links. Expected {}", links.len(), args.pages * 15);
println!("Getting answers");
let mut answers = vec![];
for (i, link) in links.iter().enumerate() {
answers.append(
&mut get_answers(&c, format!("https://stackoverflow.com{}", link).as_str(), i)
.await
.unwrap_or_default(),
);
}
println!(
"Got {} answers in {} sec",
answers.len(),
start.elapsed().as_secs_f32()
);
c.close().await.unwrap();
println!("Writing answers to answers.json");
std::fs::write("answers.json", serde_json::to_string(&answers).unwrap());
}