use indexmap::IndexMap; use regex::Regex; use select::document::Document; use select::predicate::{Class, Name}; use serde_json::Value; use lazy_static::{lazy_static}; use crate::data_source::DataSource; pub struct CGGDataSource; impl DataSource for CGGDataSource { fn get_champs_with_positions_and_patch( &self, client: &reqwest::Client, ) -> (IndexMap>, 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) } fn get_champ_data(&self, id: &str, position: &str, client: &reqwest::Client) -> Option { 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 } } }