diff --git a/Cargo.lock b/Cargo.lock index 8beb0b9..b45a7b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -210,9 +210,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.105" +version = "0.2.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "869d572136620d55835903746bcb5cdc54cb2851fd0aeec53220b4bb65ef3013" +checksum = "a60553f9a9e039a333b4e9b20573b9e9b9c0bb3a11e201ccc48ef4283456d673" [[package]] name = "log" diff --git a/src/data_source.rs b/src/data_source.rs index 676f54f..50bc829 100644 --- a/src/data_source.rs +++ b/src/data_source.rs @@ -3,7 +3,7 @@ use crate::Champion; use indexmap::IndexMap; use log::{error, info}; use serde_derive::{Deserialize, Serialize}; -use serde_json::Value; +use serde_json::{json, Value}; use std::fs; use std::path::Path; @@ -19,14 +19,14 @@ struct ItemSet { blocks: Vec, } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize)] pub struct Build { #[serde(rename = "type")] pub type_: String, pub items: Vec, } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize)] pub struct Item { pub id: String, pub count: u8, @@ -50,6 +50,13 @@ pub trait DataSource { champion: &Champion, ) -> IndexMap>; + fn make_item_set(&self, items: Vec<&str>, label: String) -> Value { + json!({ + "items": items.iter().map(|x| json!({"id": x.to_string(), "count": 1})).collect::>(), + "type": label + }) + } + fn get_champ_data_with_win_pourcentage( &self, champ: &ChampInfo, diff --git a/src/ms_data_source.rs b/src/ms_data_source.rs index 8883272..691b2ed 100644 --- a/src/ms_data_source.rs +++ b/src/ms_data_source.rs @@ -1,5 +1,5 @@ use indexmap::IndexMap; -use serde_json::{json, Value}; +use serde_json::Value; use crate::data_source::{DataSource, Stat}; use crate::ChampInfo; @@ -44,7 +44,7 @@ impl DataSource for MSDataSource { let id = champion.data.get(key).unwrap().key.parse::().unwrap(); - champs.insert(id, vec![role.to_string()]); + champs.insert(id, vec![role.to_uppercase()]); let next = page[p..].find(CHAMP_PATTERN); if let Some(n) = next { @@ -65,41 +65,56 @@ impl DataSource for MSDataSource { ) -> Vec<(String, Vec, Stat)> { let mut builds = vec![]; - let page = client + let rep = client .get( format!( "https://www.metasrc.com/5v5/champion/{}/{}", champ.id.to_lowercase(), - positions[0] + positions[0].to_lowercase() ) .as_str(), ) - .call() - .unwrap() - .into_string() - .unwrap(); + .call(); + if let Ok(p) = rep { + let page = p.into_string().unwrap(); - let mut items = vec![]; + let mut pos = page.find("Patch ").unwrap(); + let patch = &page[pos + 6..pos + 11]; - let mut pos: Option = page.find("/item/"); - while let Some(mut p) = pos { - p += 6; - let i = &page[p..p + page[p..].find('.').unwrap()]; + pos = page.find("Win Rate:").unwrap(); + let win_rate: f64 = (&page[pos + 26..pos + 31]).parse().unwrap(); - items.push(i); + pos = page.find("KDA:").unwrap(); + let kda: f64 = (&page[pos + 21..pos + 25]).parse().unwrap(); - let next = page[p..].find("/item/"); - if let Some(n) = next { - pos = Some(p + n); - } else { - pos = None; + 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![json!({ - "items": items.iter().map(|x| json!({"id": x.to_string(), "count": 1})).collect::>(), - "type": "Set" - })], Stat{win_rate:0.0,games:1,kda:0.0,patch:"".to_string()})); + 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 } diff --git a/src/pb_data_source.rs b/src/pb_data_source.rs index ccc04d0..1e2d03e 100644 --- a/src/pb_data_source.rs +++ b/src/pb_data_source.rs @@ -1,6 +1,6 @@ use indexmap::IndexMap; use serde_derive::Deserialize; -use serde_json::{json, Value}; +use serde_json::Value; use crate::data_source::{DataSource, Stat}; use crate::ChampInfo; @@ -73,7 +73,7 @@ impl DataSource for PBDataSource { for build in rep { let pos = build.find("/players").unwrap(); let tmp = &build[pos..]; - let name = &tmp[tmp.find(">").unwrap() + 1..tmp.find("<").unwrap()]; + let name = &tmp[tmp.find('>').unwrap() + 1..tmp.find('<').unwrap()]; let mut items = vec![]; let mut build = &build[build.find("'items'").unwrap()..]; @@ -85,9 +85,7 @@ impl DataSource for PBDataSource { build = &build[end + 20..]; } - blocks.push(json!({ - "items": items.iter().map(|x| json!({"id": x.to_string(), "count": 1})).collect::>(), - "type": format!("{}", name)})); + blocks.push(self.make_item_set(items, name.to_string())); } builds.push(( positions[0].to_owned(),