Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kenkoooo/061b09f0f61c8c5bcc2f8f150a352341 to your computer and use it in GitHub Desktop.
Save kenkoooo/061b09f0f61c8c5bcc2f8f150a352341 to your computer and use it in GitHub Desktop.
use anyhow::Result;
use reqwest::ClientBuilder;
use scraper::{Html, Selector};
use serde::Deserialize;
use std::collections::{HashMap, HashSet};
#[tokio::main]
async fn main() -> Result<()> {
let client = ClientBuilder::new().gzip(true).build()?;
let cookie = "...";
let table_selector = Selector::parse("#problems_table").unwrap();
let a_selector = Selector::parse("a").unwrap();
let difficulty_selector = Selector::parse("span.tooltiptext_narrow").unwrap();
let mut problems = vec![];
for i in 1..3 {
let url = format!(r"https://projecteuler.net/archives;page={}", i);
let html = client
.get(url)
.header("Cookie", cookie)
.send()
.await?
.text()
.await?;
let document = Html::parse_document(&html);
let table = document.select(&table_selector).next().unwrap();
for row in table.select(&Selector::parse("tr").unwrap()) {
let links = row.select(&a_selector).collect::<Vec<_>>();
if links.len() != 2 {
continue;
}
let id = links[0]
.value()
.attr("href")
.unwrap()
.replace("problem=", "")
.parse::<u32>()?;
let difficulty = row
.select(&difficulty_selector)
.next()
.unwrap()
.text()
.next()
.unwrap()
.replace("Difficulty rating: ", "")
.replace("%", "")
.parse::<u32>()?;
problems.push((id, difficulty));
}
}
print_table(&problems, 20);
Ok(())
}
fn print_table(problems: &[(u32, u32)], size: usize) {
for _ in 0..size {
print!("|");
}
println!("|");
for _ in 0..size {
print!("|:-----");
}
println!("|");
for chunk in problems.chunks(size) {
for problem in chunk {
print!(
"|[{} {}%](https://projecteuler.net/problem={})",
problem.0, problem.1, problem.0
);
}
let last = size - chunk.len();
for _ in 0..last {
print!("|");
}
println!("|");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment