MetaSrc: Add datasource with champs list

This commit is contained in:
nyyu 2021-04-08 13:55:10 +02:00
parent c341827ec7
commit e43f6396b0
6 changed files with 86 additions and 9 deletions

View file

@ -6,6 +6,7 @@ use std::collections::HashMap;
use std::sync::Mutex; use std::sync::Mutex;
use crate::data_source::{DataSource, Stat}; use crate::data_source::{DataSource, Stat};
use crate::Champion;
use crate::ChampInfo; use crate::ChampInfo;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
@ -103,7 +104,7 @@ impl DataSource for CGGDataSource {
0 0
} }
fn get_champs_with_positions(&self, client: &ureq::Agent) -> IndexMap<u32, Vec<String>> { fn get_champs_with_positions(&self, client: &ureq::Agent, _champion: &Champion) -> IndexMap<u32, Vec<String>> {
let req = client.get("https://champion.gg").call().unwrap(); let req = client.get("https://champion.gg").call().unwrap();
let page = &req.into_string().unwrap(); let page = &req.into_string().unwrap();

View file

@ -1,4 +1,5 @@
use crate::ChampInfo; use crate::ChampInfo;
use crate::Champion;
use indexmap::IndexMap; use indexmap::IndexMap;
use log::{error, info}; use log::{error, info};
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
@ -43,7 +44,7 @@ pub trait DataSource {
fn get_timeout(&self) -> u64; fn get_timeout(&self) -> u64;
fn get_champs_with_positions(&self, client: &ureq::Agent) -> IndexMap<u32, Vec<String>>; fn get_champs_with_positions(&self, client: &ureq::Agent, champion: &Champion) -> IndexMap<u32, Vec<String>>;
fn get_champ_data_with_win_pourcentage( fn get_champ_data_with_win_pourcentage(
&self, &self,

View file

@ -1,4 +1,5 @@
use crate::data_source::{Build, DataSource, Item, Stat}; use crate::data_source::{Build, DataSource, Item, Stat};
use crate::Champion as ChampionLoL;
use crate::ChampInfo; use crate::ChampInfo;
use crate::USER_AGENT_VALUE; use crate::USER_AGENT_VALUE;
use indexmap::IndexMap; use indexmap::IndexMap;
@ -296,7 +297,7 @@ impl DataSource for KBDataSource {
300 300
} }
fn get_champs_with_positions(&self, client: &ureq::Agent) -> IndexMap<u32, Vec<String>> { fn get_champs_with_positions(&self, client: &ureq::Agent, _champion: &ChampionLoL) -> IndexMap<u32, Vec<String>> {
let mut champions = IndexMap::new(); let mut champions = IndexMap::new();
let data: ChampionResponse = match self.get_champion_response(client) { let data: ChampionResponse = match self.get_champion_response(client) {
Some(val) => val, Some(val) => val,
@ -376,7 +377,10 @@ mod tests {
.timeout(Duration::from_secs(10)) .timeout(Duration::from_secs(10))
.build(); .build();
let datasource = KBDataSource::new(); 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); assert!(champs_with_positions.len() > 0);
} }

View file

@ -22,11 +22,13 @@ mod cgg_data_source;
mod data_source; mod data_source;
mod kb_data_source; mod kb_data_source;
mod pb_data_source; mod pb_data_source;
mod ms_data_source;
use cgg_data_source::CGGDataSource; use cgg_data_source::CGGDataSource;
use data_source::DataSource; use data_source::DataSource;
use kb_data_source::KBDataSource; use kb_data_source::KBDataSource;
use pb_data_source::PBDataSource; use pb_data_source::PBDataSource;
use ms_data_source::MSDataSource;
#[derive(Deserialize)] #[derive(Deserialize)]
struct Realm { struct Realm {
@ -34,7 +36,7 @@ struct Realm {
} }
#[derive(Deserialize)] #[derive(Deserialize)]
struct Champion { pub struct Champion {
data: IndexMap<String, ChampInfo>, data: IndexMap<String, ChampInfo>,
} }
@ -99,8 +101,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.into_json()?; .into_json()?;
info!("LoL numbers of champs: {}", champion.data.len()); info!("LoL numbers of champs: {}", champion.data.len());
let data_sources: [&'static (dyn DataSource + Sync + Send); 3] = let data_sources: [&'static (dyn DataSource + Sync + Send); 4] =
[&PBDataSource, &CGGDataSource, KBDataSource::new()]; [&PBDataSource, &CGGDataSource, KBDataSource::new(), &MSDataSource];
data_sources.par_iter().for_each(|data_source| { data_sources.par_iter().for_each(|data_source| {
let init = Instant::now(); let init = Instant::now();
execute_data_source(*data_source, &client, &champion, &lol_champs_dir); execute_data_source(*data_source, &client, &champion, &lol_champs_dir);
@ -128,7 +130,7 @@ fn execute_data_source(
champion: &Champion, champion: &Champion,
lol_champs_dir: &PathBuf, lol_champs_dir: &PathBuf,
) { ) {
let champs = data_source.get_champs_with_positions(&client); let champs = data_source.get_champs_with_positions(&client, &champion);
info!( info!(
"{} numbers of champs: {}", "{} numbers of champs: {}",

68
src/ms_data_source.rs Normal file
View file

@ -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<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(" 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::<u32>().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<Value>, Stat)> {
let mut builds = vec![];
builds
}
}

View file

@ -3,6 +3,7 @@ use serde_derive::Deserialize;
use serde_json::{json, Value}; use serde_json::{json, Value};
use crate::data_source::{DataSource, Stat}; use crate::data_source::{DataSource, Stat};
use crate::Champion as ChampionLoL;
use crate::ChampInfo; use crate::ChampInfo;
pub struct PBDataSource; pub struct PBDataSource;
@ -27,7 +28,7 @@ impl DataSource for PBDataSource {
300 300
} }
fn get_champs_with_positions(&self, client: &ureq::Agent) -> IndexMap<u32, Vec<String>> { fn get_champs_with_positions(&self, client: &ureq::Agent, _champion: &ChampionLoL) -> IndexMap<u32, Vec<String>> {
let mut champs = IndexMap::new(); let mut champs = IndexMap::new();
let page = client let page = client