use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use reqwest::Client;
use scraper::{Html, Selector};

type StdResult = Result<(), Box<dyn std::error::Error>>;

// from: https://mp.weixin.qq.com/s/5kwVXFobW1OJ5lU1nn9Vkw
#[tokio::main]
async fn main() -> StdResult {
    let res = Client::new()
        .get(String::from_utf8(STANDARD.decode("aHR0cDovL2Jvb2tzLnRvc2NyYXBlLmNvbS8=")?)?)
        .send().await?;

    let body = res.text().await?;
    let document = Html::parse_document(&body);

    let book_title_selector = Selector::parse("h3 > a")?;

    for book_title in document.select(&book_title_selector) {
        let title = book_title.text().collect::<Vec<_>>();
        println!("Title: {}", title[0]);
    }

    let book_price_selector = Selector::parse(".price_color")?;

    for book_price in document.select(&book_price_selector) {
        let price = book_price.text().collect::<Vec<_>>();
        println!("Price: {}", price[0]);
    }

    Ok(())
}