CGGItemSets/src/cgg_data_source.rs

82 lines
2.3 KiB
Rust
Raw Normal View History

2018-06-16 10:48:46 +02:00
use indexmap::IndexMap;
use regex::Regex;
use select::document::Document;
use select::predicate::{Class, Name};
use serde_json::Value;
2019-09-26 22:39:25 +02:00
use lazy_static::{lazy_static};
2018-06-16 10:48:46 +02:00
2019-09-26 22:39:25 +02:00
use crate::data_source::DataSource;
2018-06-16 10:48:46 +02:00
pub struct CGGDataSource;
impl DataSource for CGGDataSource {
fn get_champs_with_positions_and_patch(
2018-06-16 12:48:14 +02:00
&self,
2018-06-16 10:48:46 +02:00
client: &reqwest::Client,
) -> (IndexMap<String, Vec<String>>, String) {
let page = client
.get("https://champion.gg")
.send()
.unwrap()
.text()
.unwrap();
let document = Document::from(&*page);
let patch = document
.find(Class("analysis-holder"))
.next()
.unwrap()
.find(Name("strong"))
.next()
.unwrap()
.text();
let mut champions = IndexMap::new();
for node in document.find(Class("champ-height")) {
let id = node
.find(Class("home-champion"))
.next()
.unwrap()
.attr("class")
.unwrap()
.split(' ')
.last()
.unwrap()
.to_string();
let positions = node
.find(Name("a"))
.skip(1)
.map(|x| {
x.attr("href")
.unwrap()
.split('/')
.last()
.unwrap()
.to_string()
})
.collect();
champions.insert(id, positions);
}
(champions, patch)
}
2018-06-16 18:13:24 +02:00
fn get_champ_data(&self, id: &str, position: &str, client: &reqwest::Client) -> Option<Value> {
2018-06-16 10:48:46 +02:00
let mut req = client
.get(&format!(
"https://champion.gg/champion/{}/{}?league=",
id, position
))
.send()
.unwrap();
if req.status().is_success() {
lazy_static! {
static ref RE: Regex =
Regex::new(r"(?m)^\s+matchupData\.championData = (.*)$").unwrap();
}
serde_json::from_str(&RE.captures(&req.text().unwrap())?[1]).unwrap()
} else {
None
}
}
}