diff --git a/src/cgg_data_source.rs b/src/cgg_data_source.rs index b3d3fcc..9340d4b 100644 --- a/src/cgg_data_source.rs +++ b/src/cgg_data_source.rs @@ -6,6 +6,7 @@ use std::collections::HashMap; use std::sync::Mutex; use crate::data_source::{DataSource, Stat}; +use crate::Champion; use crate::ChampInfo; #[derive(Deserialize, Debug)] @@ -103,7 +104,7 @@ impl DataSource for CGGDataSource { 0 } - fn get_champs_with_positions(&self, client: &ureq::Agent) -> IndexMap> { + fn get_champs_with_positions(&self, client: &ureq::Agent, _champion: &Champion) -> IndexMap> { let req = client.get("https://champion.gg").call().unwrap(); let page = &req.into_string().unwrap(); diff --git a/src/data_source.rs b/src/data_source.rs index 4ad3ed4..9b9d35a 100644 --- a/src/data_source.rs +++ b/src/data_source.rs @@ -1,4 +1,5 @@ use crate::ChampInfo; +use crate::Champion; use indexmap::IndexMap; use log::{error, info}; use serde_derive::{Deserialize, Serialize}; @@ -43,7 +44,7 @@ pub trait DataSource { fn get_timeout(&self) -> u64; - fn get_champs_with_positions(&self, client: &ureq::Agent) -> IndexMap>; + fn get_champs_with_positions(&self, client: &ureq::Agent, champion: &Champion) -> IndexMap>; fn get_champ_data_with_win_pourcentage( &self, diff --git a/src/kb_data_source.rs b/src/kb_data_source.rs index ce23c3d..a04d342 100644 --- a/src/kb_data_source.rs +++ b/src/kb_data_source.rs @@ -1,4 +1,5 @@ use crate::data_source::{Build, DataSource, Item, Stat}; +use crate::Champion as ChampionLoL; use crate::ChampInfo; use crate::USER_AGENT_VALUE; use indexmap::IndexMap; @@ -296,7 +297,7 @@ impl DataSource for KBDataSource { 300 } - fn get_champs_with_positions(&self, client: &ureq::Agent) -> IndexMap> { + fn get_champs_with_positions(&self, client: &ureq::Agent, _champion: &ChampionLoL) -> IndexMap> { let mut champions = IndexMap::new(); let data: ChampionResponse = match self.get_champion_response(client) { Some(val) => val, @@ -376,7 +377,10 @@ mod tests { .timeout(Duration::from_secs(10)) .build(); let datasource = KBDataSource::new(); - let champs_with_positions = datasource.get_champs_with_positions(&client); + let champion = ChampionLoL { + data: IndexMap::new() + }; + let champs_with_positions = datasource.get_champs_with_positions(&client, &champion); assert!(champs_with_positions.len() > 0); } diff --git a/src/main.rs b/src/main.rs index 7e666ef..6d63348 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,11 +22,13 @@ mod cgg_data_source; mod data_source; mod kb_data_source; mod pb_data_source; +mod ms_data_source; use cgg_data_source::CGGDataSource; use data_source::DataSource; use kb_data_source::KBDataSource; use pb_data_source::PBDataSource; +use ms_data_source::MSDataSource; #[derive(Deserialize)] struct Realm { @@ -34,7 +36,7 @@ struct Realm { } #[derive(Deserialize)] -struct Champion { +pub struct Champion { data: IndexMap, } @@ -99,8 +101,8 @@ fn main() -> Result<(), Box> { .into_json()?; info!("LoL numbers of champs: {}", champion.data.len()); - let data_sources: [&'static (dyn DataSource + Sync + Send); 3] = - [&PBDataSource, &CGGDataSource, KBDataSource::new()]; + let data_sources: [&'static (dyn DataSource + Sync + Send); 4] = + [&PBDataSource, &CGGDataSource, KBDataSource::new(), &MSDataSource]; data_sources.par_iter().for_each(|data_source| { let init = Instant::now(); execute_data_source(*data_source, &client, &champion, &lol_champs_dir); @@ -128,7 +130,7 @@ fn execute_data_source( champion: &Champion, lol_champs_dir: &PathBuf, ) { - let champs = data_source.get_champs_with_positions(&client); + let champs = data_source.get_champs_with_positions(&client, &champion); info!( "{} numbers of champs: {}", diff --git a/src/ms_data_source.rs b/src/ms_data_source.rs new file mode 100644 index 0000000..10acd1a --- /dev/null +++ b/src/ms_data_source.rs @@ -0,0 +1,68 @@ +use indexmap::IndexMap; +use serde_json::{Value}; + +use crate::data_source::{DataSource, Stat}; +use crate::ChampInfo; +use crate::Champion; + +pub struct MSDataSource; + +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(" href=\"/5v5/champion/"); + while let Some(mut p) = pos { + p += 21; + 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_string()]); + + let next = page[p..].find(" href=\"/5v5/champion/"); + 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![]; + + builds + } +} diff --git a/src/pb_data_source.rs b/src/pb_data_source.rs index 8e2c639..abfc45b 100644 --- a/src/pb_data_source.rs +++ b/src/pb_data_source.rs @@ -3,6 +3,7 @@ use serde_derive::Deserialize; use serde_json::{json, Value}; use crate::data_source::{DataSource, Stat}; +use crate::Champion as ChampionLoL; use crate::ChampInfo; pub struct PBDataSource; @@ -27,7 +28,7 @@ impl DataSource for PBDataSource { 300 } - fn get_champs_with_positions(&self, client: &ureq::Agent) -> IndexMap> { + fn get_champs_with_positions(&self, client: &ureq::Agent, _champion: &ChampionLoL) -> IndexMap> { let mut champs = IndexMap::new(); let page = client