CGGItemSets/src/ms_data_source.rs

122 lines
3.3 KiB
Rust
Raw Normal View History

use indexmap::IndexMap;
2021-10-31 10:12:53 +01:00
use serde_json::Value;
use crate::data_source::{DataSource, Stat};
use crate::ChampInfo;
use crate::Champion;
pub struct MSDataSource;
2021-10-30 11:32:54 +02:00
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<u32, Vec<String>> {
let mut champs = IndexMap::new();
let page = client
.get("https://www.metasrc.com/5v5")
.call()
.unwrap()
.into_string()
.unwrap();
2021-10-30 11:32:54 +02:00
let mut pos: Option<usize> = page.find(CHAMP_PATTERN);
while let Some(mut p) = pos {
2021-10-30 11:32:54 +02:00
p += CHAMP_PATTERN.len();
let role =
&page[p + page[p..].find('/').unwrap() + 1..p + page[p..].find('"').unwrap()];
2021-05-10 10:40:27 +02:00
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::<u32>().unwrap();
2021-10-31 10:12:53 +01:00
champs.insert(id, vec![role.to_uppercase()]);
2021-10-30 11:32:54 +02:00
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<Value>, Stat)> {
let mut builds = vec![];
2021-10-31 10:12:53 +01:00
let rep = client
2021-04-08 14:07:23 +02:00
.get(
format!(
"https://www.metasrc.com/5v5/champion/{}/{}",
champ.id.to_lowercase(),
2021-10-31 10:12:53 +01:00
positions[0].to_lowercase()
2021-04-08 14:07:23 +02:00
)
.as_str(),
)
2021-10-31 10:12:53 +01:00
.call();
if let Ok(p) = rep {
let page = p.into_string().unwrap();
2021-04-08 14:07:23 +02:00
2021-10-31 10:12:53 +01:00
let mut pos = page.find("Patch ").unwrap();
let patch = &page[pos + 6..pos + 11];
2021-04-08 14:07:23 +02:00
2021-10-31 10:12:53 +01:00
pos = page.find("Win Rate:").unwrap();
2021-11-01 08:35:19 +01:00
let win_rate: f32 = (&page[pos + 26..pos + 31]).parse().unwrap();
2021-04-08 14:07:23 +02:00
2021-10-31 10:12:53 +01:00
pos = page.find("KDA:").unwrap();
2021-11-01 08:35:19 +01:00
let kda: f32 = (&page[pos + 21..pos + 25]).parse().unwrap();
2021-04-08 14:07:23 +02:00
2021-10-31 10:12:53 +01:00
let mut items = vec![];
let mut pos: Option<usize> = 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;
}
2021-04-08 14:07:23 +02:00
}
2021-10-31 10:12:53 +01:00
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(),
},
));
}
2021-04-08 14:07:23 +02:00
builds
}
}