CGGItemSets/src/ms_data_source.rs
Slany ace57d30fc
All checks were successful
ci/woodpecker/push/linux Pipeline was successful
ci/woodpecker/push/mingw Pipeline was successful
feat: enhancement
2023-01-03 20:48:54 +01:00

121 lines
3.3 KiB
Rust

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<u32, Vec<String>> {
let mut champs = IndexMap::new();
let page = client
.get("https://www.metasrc.com/5v5")
.call()
.unwrap()
.into_string()
.unwrap();
let mut pos: Option<usize> = 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() + 23;
let pipe = k + page[k..].find('|').unwrap() + 1;
let key = &page[pipe..pipe + page[pipe..].find(' ').unwrap()].replace('\"', "");
let id = champion.data.get(key).unwrap().key.parse::<u32>().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<Value>, 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: f32 = page[pos + 26..pos + 31].parse().unwrap();
pos = page.find("KDA:").unwrap();
let kda: f32 = page[pos + 21..pos + 25].parse().unwrap();
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;
}
}
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
}
}