remove init data source
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
a29054520d
commit
9d7eb9f875
5 changed files with 49 additions and 58 deletions
|
@ -95,8 +95,6 @@ fn extract_json(pattern: &str, page: &str) -> String {
|
|||
}
|
||||
|
||||
impl DataSource for CGGDataSource {
|
||||
fn init(&self, _client: &ureq::Agent) {}
|
||||
|
||||
fn get_alias(&self) -> &str {
|
||||
"CGG"
|
||||
}
|
||||
|
|
|
@ -39,8 +39,6 @@ pub struct Stat {
|
|||
}
|
||||
|
||||
pub trait DataSource {
|
||||
fn init(&self, client: &ureq::Agent);
|
||||
|
||||
fn get_alias(&self) -> &str;
|
||||
|
||||
fn get_timeout(&self) -> u64;
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
use crate::data_source::{Build, DataSource, Item, Stat};
|
||||
#[cfg(test)]
|
||||
use crate::time::Duration;
|
||||
use crate::ChampInfo;
|
||||
use indexmap::IndexMap;
|
||||
use lazy_static::lazy_static;
|
||||
use serde_derive::Deserialize;
|
||||
use serde_json::{json, Value};
|
||||
use std::sync::Mutex;
|
||||
use std::time::Duration;
|
||||
use crate::USER_AGENT_VALUE;
|
||||
|
||||
pub struct KBDataSource;
|
||||
pub struct KBDataSource {
|
||||
token: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct ChampionResponse {
|
||||
|
@ -90,38 +91,47 @@ struct Summoner {
|
|||
name: String,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref TOKEN: Mutex<Option<String>> = Mutex::new(None);
|
||||
// It will be better to use Result...
|
||||
fn get_auth_token() -> Option<String> {
|
||||
let client = ureq::AgentBuilder::new()
|
||||
.user_agent(USER_AGENT_VALUE)
|
||||
.timeout(Duration::from_secs(10))
|
||||
.build();
|
||||
let mut bundle = match client.get("https://koreanbuilds.net/bundle.js").call() {
|
||||
Ok(resp) => match resp.into_string() {
|
||||
Ok(val) => val,
|
||||
Err(_) => return None,
|
||||
},
|
||||
Err(_) => return None,
|
||||
};
|
||||
let auth_position = match bundle.find("Authorization") {
|
||||
Some(position) => position,
|
||||
None => return None,
|
||||
};
|
||||
bundle = (&bundle[(auth_position + 13)..]).to_string();
|
||||
let q_position = match bundle.find('"') {
|
||||
Some(position) => position,
|
||||
None => return None,
|
||||
};
|
||||
bundle = (&bundle[(q_position + 1)..]).to_string();
|
||||
match bundle.find('"') {
|
||||
Some(position) => Some((&bundle[..position]).to_string()),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
impl KBDataSource {
|
||||
// It will be better to use Result...
|
||||
fn get_auth_token(&self, client: &ureq::Agent) -> Option<String> {
|
||||
let mut bundle = match client.get("https://koreanbuilds.net/bundle.js").call() {
|
||||
Ok(resp) => match resp.into_string() {
|
||||
Ok(val) => val,
|
||||
Err(_) => return None,
|
||||
},
|
||||
Err(_) => return None,
|
||||
};
|
||||
let auth_position = match bundle.find("Authorization") {
|
||||
Some(position) => position,
|
||||
None => return None,
|
||||
};
|
||||
bundle = (&bundle[(auth_position + 13)..]).to_string();
|
||||
let q_position = match bundle.find('"') {
|
||||
Some(position) => position,
|
||||
None => return None,
|
||||
};
|
||||
bundle = (&bundle[(q_position + 1)..]).to_string();
|
||||
match bundle.find('"') {
|
||||
Some(position) => Some((&bundle[..position]).to_string()),
|
||||
None => None,
|
||||
pub fn new() -> &'static KBDataSource {
|
||||
lazy_static! {
|
||||
static ref DATASOURCE: KBDataSource = KBDataSource {
|
||||
token: get_auth_token(),
|
||||
};
|
||||
}
|
||||
&DATASOURCE
|
||||
}
|
||||
|
||||
fn get_champion_response(&self, client: &ureq::Agent) -> Option<ChampionResponse> {
|
||||
if let Some(token) = TOKEN.lock().unwrap().as_ref() {
|
||||
if let Some(token) = &self.token {
|
||||
return match client
|
||||
.get("https://api.koreanbuilds.net/champions?patchid=-1")
|
||||
.set("Accept", "application/json")
|
||||
|
@ -278,12 +288,6 @@ impl KBDataSource {
|
|||
}
|
||||
|
||||
impl DataSource for KBDataSource {
|
||||
fn init(&self, client: &ureq::Agent) {
|
||||
if let Some(t) = self.get_auth_token(client) {
|
||||
TOKEN.lock().unwrap().replace(t);
|
||||
}
|
||||
}
|
||||
|
||||
fn get_alias(&self) -> &str {
|
||||
"KB"
|
||||
}
|
||||
|
@ -313,7 +317,7 @@ impl DataSource for KBDataSource {
|
|||
client: &ureq::Agent,
|
||||
) -> Vec<(String, Vec<Value>, Stat)> {
|
||||
let mut champ_data = vec![];
|
||||
if let Some(token) = TOKEN.lock().unwrap().as_ref() {
|
||||
if let Some(token) = &self.token {
|
||||
let data: BuildResponse = match client
|
||||
.get(&format!(
|
||||
"https://api.koreanbuilds.net/builds?chmpname={}&patchid=-2&position=COMPOSITE",
|
||||
|
@ -359,11 +363,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_get_auth_token() {
|
||||
let datasource = KBDataSource;
|
||||
let client = ureq::AgentBuilder::new()
|
||||
.timeout(Duration::from_secs(10))
|
||||
.build();
|
||||
match datasource.get_auth_token(&client) {
|
||||
match get_auth_token() {
|
||||
Some(token) => assert!(token.len() > 0),
|
||||
None => assert!(false),
|
||||
};
|
||||
|
@ -372,22 +372,21 @@ mod tests {
|
|||
#[test]
|
||||
fn test_get_champs_with_positions_and_patch() {
|
||||
let client = ureq::AgentBuilder::new()
|
||||
.user_agent(USER_AGENT_VALUE)
|
||||
.timeout(Duration::from_secs(10))
|
||||
.build();
|
||||
let datasource = KBDataSource;
|
||||
datasource.init(&client);
|
||||
let champs_with_positions =
|
||||
datasource.get_champs_with_positions(&client);
|
||||
let datasource = KBDataSource::new();
|
||||
let champs_with_positions = datasource.get_champs_with_positions(&client);
|
||||
assert!(champs_with_positions.len() > 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_champ_data_with_win_pourcentage() {
|
||||
let client = ureq::AgentBuilder::new()
|
||||
.user_agent(USER_AGENT_VALUE)
|
||||
.timeout(Duration::from_secs(10))
|
||||
.build();
|
||||
let datasource = KBDataSource;
|
||||
datasource.init(&client);
|
||||
let datasource = KBDataSource::new();
|
||||
let champ = ChampInfo {
|
||||
id: String::from("Annie"),
|
||||
name: String::from("Annie"),
|
||||
|
|
|
@ -100,12 +100,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
.into_json()?;
|
||||
info!("LoL numbers of champs: {}", champion.data.len());
|
||||
|
||||
static DATA_SOURCES: [&'static (dyn DataSource + Sync + Send); 3] = [
|
||||
let data_sources: [&'static (dyn DataSource + Sync + Send); 3] = [
|
||||
&PBDataSource,
|
||||
&CGGDataSource,
|
||||
&KBDataSource,
|
||||
KBDataSource::new(),
|
||||
];
|
||||
DATA_SOURCES.par_iter().for_each(|data_source| {
|
||||
data_sources.par_iter().for_each(|data_source| {
|
||||
|
||||
let init = Instant::now();
|
||||
execute_data_source(*data_source, &client, &champion, &lol_champs_dir);
|
||||
|
@ -133,8 +133,6 @@ fn execute_data_source(
|
|||
champion: &Champion,
|
||||
lol_champs_dir: &PathBuf,
|
||||
) {
|
||||
data_source.init(client);
|
||||
|
||||
let champs = data_source.get_champs_with_positions(&client);
|
||||
|
||||
info!(
|
||||
|
|
|
@ -19,8 +19,6 @@ struct Champion {
|
|||
}
|
||||
|
||||
impl DataSource for PBDataSource {
|
||||
fn init(&self, _client: &ureq::Agent) {}
|
||||
|
||||
fn get_alias(&self) -> &str {
|
||||
"PB"
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue