Update wod to current rust, close vulnerabilities

This commit is contained in:
Orien Vandenbergh 2023-11-03 11:28:31 -04:00
parent 961a073e78
commit 63d2c226b5
3 changed files with 1207 additions and 763 deletions

1922
wod/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,12 @@
[package] [package]
edition = "2021"
name = "wod" name = "wod"
version = "0.1.0" version = "0.1.0"
authors = ["Orien Vandenbergh <orien.vandenbergh@defpoint.com>"] authors = ["Orien Vandenbergh <orien.vandenbergh@defpoint.com>"]
[dependencies] [dependencies]
reqwest = "0.8.2" reqwest = "0.11.22"
scraper = "0.4.0" futures = "0.3.0"
select = "0.4.2" scraper = "0.18.1"
select = "0.6.0"
tokio = { version = "1.12.0", features = ["full"] }

View File

@ -5,37 +5,62 @@ extern crate select;
use select::document::Document; use select::document::Document;
use select::predicate::{Class,Name,Predicate}; use select::predicate::{Class,Name,Predicate};
fn main() { #[tokio::main]
async fn main() {
if is_cached() { if is_cached() {
} else { } else {
wod("http://www.merriam-webster.com/word-of-the-day"); wod("http://www.merriam-webster.com/word-of-the-day").await;
} }
} }
fn is_cached() -> bool { fn is_cached() -> bool {
return false; return false;
} }
fn wod(url: &str) { /*
fn print_type_of<T>(_: &T) {
println!("{}", std::any::type_name::<T>())
}
*/
async fn wod(url: &str) {
let yellow = "\x1b[1;33m"; let yellow = "\x1b[1;33m";
let purple = "\x1b[0;35m"; let purple = "\x1b[0;35m";
let nc = "\x1b[0m"; let nc = "\x1b[0m";
let resp = reqwest::get(url).unwrap(); let resp = reqwest::get(url).await.unwrap();
assert!(resp.status().is_success()); assert!(resp.status().is_success());
let text = resp.text().await.unwrap();
let document = Document::from_read(resp).unwrap(); let document = Document::from(text.as_str());
let node = document.find(Class("word-header")).next().unwrap(); let node = document.find(Class("word-header")).next().unwrap();
let word = node.find(Name("h1")).next().unwrap().text(); let word = node.find(Name("h2")).next().unwrap().text();
// println!("word: {:?}",word);
let node = document.find(Class("word-attributes")).next().unwrap(); let node = document.find(Class("word-attributes")).next().unwrap();
let attr = node.find(Class("main-attr")).next().unwrap().text(); let attr = node.find(Class("main-attr")).next().unwrap().text();
let syllables = node.find(Class("word-syllables")).next().unwrap().text(); let syllables = node.find(Class("word-syllables")).next().unwrap().text();
// println!("attr: {:?}",attr);
// println!("syllables: {:?}",syllables);
println!("Word of the Day: {}{} : {} ({}){}", yellow, word, attr, syllables, nc); println!("Word of the Day: {}{} : {} ({}){}", yellow, word, attr, syllables, nc);
for definition in document.find(Class("wod-definition-container").child(Name("p"))) { let definitions = document.find(Class("wod-definition-container").child(Name("p")));
//print_type_of(&definitions);
for definition in definitions.filter(|d| d.children().nth(0).unwrap().text() != "See the entry >") {
let children: Vec<String> = definition.children().map(|n| n.text()).collect(); let children: Vec<String> = definition.children().map(|n| n.text()).collect();
let texts = children.join(""); let texts = children.join("");
println!(" definition: {}{}{}", purple, texts, nc); println!(" definition: {}{}{}", purple, texts, nc);
} }
/*
match resp.status() {
reqwest::StatusCode::OK => {
println!("Success! {:?}");
},
reqwest::StatusCode::UNAUTHORIZED => {
println!("Need to grab a new token");
},
_ => {
panic!("Un oh! Something unexpected happened.");
},
};
*/
} }