use indexmap::IndexMap; use serde_json::Value; use crate::data_source::{DataSource, Stat}; use crate::ChampInfo; use crate::Champion; pub struct MSDataSource; const CHAMP_PATTERN: &str = " href=\"https://www.metasrc.com/5v5/champion/"; impl DataSource for MSDataSource { fn get_alias(&self) -> &str { "MS" } fn get_timeout(&self) -> u64 { 300 } fn get_champs_with_positions( &self, client: &ureq::Agent, champion: &Champion, ) -> IndexMap> { let mut champs = IndexMap::new(); let page = client .get("https://www.metasrc.com/5v5") .call() .unwrap() .into_string() .unwrap(); let mut pos: Option = page.find(CHAMP_PATTERN); while let Some(mut p) = pos { p += CHAMP_PATTERN.len(); let role = &page[p + page[p..].find('/').unwrap() + 1..p + page[p..].find('"').unwrap()]; let k = p + page[p..].find("data-search-terms-like=\"").unwrap() + 24; let pipe = k + page[k..].find('|').unwrap() + 1; let key = &page[pipe..pipe + page[pipe..].find('"').unwrap()]; let id = champion.data.get(key).unwrap().key.parse::().unwrap(); champs.insert(id, vec![role.to_uppercase()]); let next = page[p..].find(CHAMP_PATTERN); if let Some(n) = next { pos = Some(p + n); } else { pos = None; } } champs } fn get_champ_data_with_win_pourcentage( &self, champ: &ChampInfo, positions: &[String], client: &ureq::Agent, ) -> Vec<(String, Vec, Stat)> { let mut builds = vec![]; let rep = client .get( format!( "https://www.metasrc.com/5v5/champion/{}/{}", champ.id.to_lowercase(), positions[0].to_lowercase() ) .as_str(), ) .call(); if let Ok(p) = rep { let page = p.into_string().unwrap(); let mut pos = page.find("Patch ").unwrap(); let patch = &page[pos + 6..pos + 11]; pos = page.find("Win Rate:").unwrap(); let win_rate: f64 = (&page[pos + 26..pos + 31]).parse().unwrap(); pos = page.find("KDA:").unwrap(); let kda: f64 = (&page[pos + 21..pos + 25]).parse().unwrap(); let mut items = vec![]; let mut pos: Option = page.find("/item/"); while let Some(mut p) = pos { p += 6; let i = &page[p..p + page[p..].find('.').unwrap()]; items.push(i); let next = page[p..].find("/item/"); if let Some(n) = next { pos = Some(p + n); } else { pos = None; } } builds.push(( positions[0].to_owned(), vec![self.make_item_set(items, "Set".to_owned())], Stat { win_rate, games: 1, kda, patch: patch.to_owned(), }, )); } builds } }