Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use fantoccini::error::CmdError;
use fantoccini::{ClientBuilder, Locator};
#[tokio::main]
async fn main() {
let filters = vec!["RX 6800", "RX 6700"];
loop {
let products = get_html_products().await.unwrap();
let filtered: Vec<Product> = products
.into_iter()
.filter(|el| {
for s in &filters {
if el.name.contains(s) {
return el.available;
}
}
false
})
.collect();
if filtered.len() > 0 {
println!("The following products are available");
} else {
println!("Sorry, all products are out of stock. :/");
}
}
}
#[derive(Debug)]
pub struct Product {
name: String,
price: String,
available: bool,
}
async fn get_html_products() -> Result<Vec<Product>, CmdError> {
// navigate to amd-site
let mut webc = ClientBuilder::native()
.connect("http://localhost:4444")
.await
.expect("failed to connect to WebDriver");
webc.goto("https://www.amd.com/de/direct-buy/de")
.await
.expect("Could not connect to the amd Website");
// find all articles that are for sale
let html_elements = webc
.find_all(Locator::Css("article"))
.await
.expect("find_all error");
//Interate over all items in the shop and parse the relevant strings
// in the struct Product
let mut products: Vec<Product> = Vec::new();
for mut el in html_elements {
let name = el.find(Locator::Css(".shop-title")).await?.text().await?;
let price = el.find(Locator::Css(".shop-price")).await?.text().await?;
let avail = el.find(Locator::Css(".shop-links")).await?.text().await?;
let product = Product {
name,
price,
available: !avail.contains("ergriffen"),
};
products.push(product);
}
// we do not need the webbrowser anymore
webc.close()
.await
.expect("Failed to terminate webdriver sessions");
return Ok(products);
}