xchange_rs/
lib.rs

1//importing required deps
2use scraper::{Html, Selector};
3
4#[tokio::main]
5
6//The "main" function !!! This is Option because of clap!
7pub async fn xchangeit(amount: Option<&str>, from: Option<&str>, to: Option<&str>) -> Result<(), Box<dyn std::error::Error>> {
8
9    //formatting the url to actually get where the user wants to get!
10    let url_final = format!("https://www.xe.com/currencyconverter/convert/?Amount={:?}&From={:?}&To={:?}", amount.unwrap_or_default(), from.unwrap_or_default(), to.unwrap_or_default());
11
12    let url = url_final.replace("\"", "");
13
14    //this line is for testing!
15    //println!("{}", url);
16
17    let req = reqwest::get(url).await?;
18
19    //this line is for testing!
20    //println!("{:?}", req);
21
22    //fragment
23    let fragment = Html::parse_fragment(&req.text().await?);
24
25    //the "filter" for the element
26    let selector = Selector::parse(".iGrAod").unwrap();
27
28    //actually looking for the element!
29    for elem in fragment.select(&selector) {
30        println!("{}", elem.text().collect::<String>().trim());
31    }
32
33    Ok(())
34
35}
36