Remove regex for size
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
nyyu 2021-03-14 16:43:29 +01:00
parent 9f6170b234
commit 7bdcf52c47
4 changed files with 23 additions and 59 deletions

View file

@ -1,6 +1,5 @@
use indexmap::IndexMap;
use lazy_static::lazy_static;
use regex::Regex;
use serde_derive::Deserialize;
use serde_json::{json, Value};
use std::collections::HashMap;
@ -90,6 +89,13 @@ impl CGGDataSource {
}
}
fn extract_json(pattern: &str, page: &str) -> String {
let json = page[page.find(pattern).unwrap()+pattern.len()..].to_owned();
json[..json.find("};").unwrap()+1].replace("undefined", "null").to_owned()
}
impl DataSource for CGGDataSource {
fn get_alias(&self) -> &str {
"CGG"
@ -104,23 +110,14 @@ impl DataSource for CGGDataSource {
client: &ureq::Agent,
) -> (IndexMap<String, Vec<String>>, String) {
let req = client.get("https://champion.gg").call().unwrap();
lazy_static! {
static ref RE: Regex =
Regex::new(r"(?m)^\s+window.__PRELOADED_STATE__ = (.*);$").unwrap();
static ref RE2: Regex =
Regex::new(r"(?m)^\s+window.__FLASH_CMS_APOLLO_STATE__ = (.*);$").unwrap();
}
let page = &req.into_string().unwrap();
let datas: BuildResponse =
serde_json::from_str(&RE.captures(&page).unwrap()[1].replace("undefined", "null"))
.unwrap();
serde_json::from_str(&extract_json("window.__PRELOADED_STATE__ = ", &page)).unwrap();
let champs_stats: HashMap<String, ChampStat> =
serde_json::from_str(&RE2.captures(&page).unwrap()[1].replace("undefined", "null"))
.unwrap();
serde_json::from_str(&extract_json("window.__FLASH_CMS_APOLLO_STATE__ = ", &page)).unwrap();
for entry in champs_stats.iter() {
CHAMPIONS_STATS
.lock()

View file

@ -3,7 +3,6 @@ use crate::data_source::{Build, DataSource, Item, Stat};
use crate::time::Duration;
use crate::ChampInfo;
use indexmap::IndexMap;
use regex::Regex;
use serde_derive::Deserialize;
use serde_json::{json, Value};
@ -102,27 +101,29 @@ impl KBDataSource {
}
// It will be better to use Result...
fn get_auth_token(&self, client: &ureq::Agent) -> Option<String> {
let bundle = match client.get("https://koreanbuilds.net/bundle.js").call() {
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 regex = match Regex::new(r##"Authorization:\s*"(\w+)""##) {
Ok(reg) => reg,
Err(_) => return None,
};
let result = match regex.captures(&bundle) {
Some(res) => res,
let auth_position = match bundle.find("Authorization") {
Some(position) => position,
None => return None,
};
return match result.get(1) {
Some(token) => Some(token.as_str().to_string()),
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();
return match bundle.find("\"") {
Some(position) => Some((&bundle[..position]).to_string()),
None => None,
};
}
}
fn get_classname_mapping(&self, client: &ureq::Agent) -> IndexMap<String, String> {
let mut mapping = IndexMap::new();