Add specials items, check some errors

This commit is contained in:
nyyu 2018-06-09 00:06:30 +02:00
parent bb490b3fcf
commit 1e72fd6cc0

View file

@ -8,7 +8,7 @@ extern crate select;
extern crate regex; extern crate regex;
#[macro_use] extern crate lazy_static; #[macro_use] extern crate lazy_static;
use std::collections::HashMap; use std::collections::BTreeMap;
use std::{fs, thread, time}; use std::{fs, thread, time};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use reqwest::header::{Headers, UserAgent}; use reqwest::header::{Headers, UserAgent};
@ -16,6 +16,7 @@ use select::document::Document;
use select::predicate::{Class, Name}; use select::predicate::{Class, Name};
use regex::Regex; use regex::Regex;
use serde_json::Value; use serde_json::Value;
use time::Duration;
#[derive(Deserialize)] #[derive(Deserialize)]
struct Realm { struct Realm {
@ -24,12 +25,12 @@ struct Realm {
#[derive(Deserialize)] #[derive(Deserialize)]
struct Champion { struct Champion {
data: HashMap<String, ChampInfo> data: BTreeMap<String, ChampInfo>
} }
#[derive(Deserialize)] #[derive(Deserialize)]
struct ChampInfo { struct ChampInfo {
id: String /*id: String*/
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
@ -46,14 +47,16 @@ struct ItemSet {
const USER_AGENT: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0"; const USER_AGENT: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0";
const LOL_CHAMPS_DIR: &str = ".\\champs"; const LOL_CHAMPS_DIR: &str = ".\\champs";
const CONSUMABLES: [u32; 9] = [2003, 2004, 2055, 2031, 2032, 2033, 2138, 2140, 2139];
const TRINKETS: [u32; 3] = [3340, 3364, 3363];
lazy_static! { lazy_static! {
static ref ITEM_TYPES: HashMap<&'static str, [&'static str; 2]> = { static ref ITEM_TYPES: BTreeMap<&'static str, [&'static str; 2]> = {
let mut m = HashMap::new(); let mut m = BTreeMap::new();
m.insert("Most Frequent Starters", ["firstItems", "mostGames"]); m.insert("1. Most Frequent Starters", ["firstItems", "mostGames"]);
m.insert("Highest Win % Starters", ["firstItems", "highestWinPercent"]); m.insert("2. Highest Win % Starters", ["firstItems", "highestWinPercent"]);
m.insert("Most Frequent Core Build", ["items", "mostGames"]); m.insert("3. Most Frequent Core Build", ["items", "mostGames"]);
m.insert("Highest Win % Core Build", ["firstItems", "highestWinPercent"]); m.insert("4. Highest Win % Core Build", ["firstItems", "highestWinPercent"]);
m m
}; };
} }
@ -66,7 +69,10 @@ fn main() {
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.set(UserAgent::new(USER_AGENT)); headers.set(UserAgent::new(USER_AGENT));
let client = reqwest::Client::builder().default_headers(headers).build().unwrap(); let client = reqwest::Client::builder()
.default_headers(headers)
.timeout(Duration::from_secs(10))
.build().unwrap();
let realm: Realm = client.get("https://ddragon.leagueoflegends.com/realms/euw.json").send().unwrap().json().unwrap(); let realm: Realm = client.get("https://ddragon.leagueoflegends.com/realms/euw.json").send().unwrap().json().unwrap();
info!("LoL version: {}", realm.v); info!("LoL version: {}", realm.v);
@ -85,7 +91,7 @@ fn main() {
fs::create_dir_all(&path).unwrap(); fs::create_dir_all(&path).unwrap();
for pos in positions { for pos in positions {
write_item_set(&id, &pos, &patch, &path, &client); write_item_set(&id, &pos, &patch, &path, &client);
thread::sleep(time::Duration::from_millis(300)); thread::sleep(Duration::from_millis(300));
} }
} else { } else {
info!("{} not found in LoL champs", &id); info!("{} not found in LoL champs", &id);
@ -93,13 +99,13 @@ fn main() {
} }
} }
fn get_champs_with_positions_and_patch(client: &reqwest::Client) -> (HashMap<String, Vec<String>>, String) { fn get_champs_with_positions_and_patch(client: &reqwest::Client) -> (BTreeMap<String, Vec<String>>, String) {
let page = client.get("https://champion.gg").send().unwrap().text().unwrap(); let page = client.get("https://champion.gg").send().unwrap().text().unwrap();
let document = Document::from(&*page); let document = Document::from(&*page);
let patch = document.find(Class("analysis-holder")).next().unwrap().find(Name("strong")).next().unwrap().text(); let patch = document.find(Class("analysis-holder")).next().unwrap().find(Name("strong")).next().unwrap().text();
let mut champions = HashMap::new(); let mut champions = BTreeMap::new();
for node in document.find(Class("champ-height")) { for node in document.find(Class("champ-height")) {
let id = node.find(Class("home-champion")).next().unwrap().attr("class").unwrap().split(' ').last().unwrap().to_string(); let id = node.find(Class("home-champion")).next().unwrap().attr("class").unwrap().split(' ').last().unwrap().to_string();
let positions = node.find(Name("a")) let positions = node.find(Name("a"))
@ -112,9 +118,13 @@ fn get_champs_with_positions_and_patch(client: &reqwest::Client) -> (HashMap<Str
(champions, patch) (champions, patch)
} }
fn get_champ_data(id: &str, position: &str, client: &reqwest::Client) -> Value { fn get_champ_data(id: &str, position: &str, client: &reqwest::Client) -> Option<Value> {
let page = client.get(&format!("https://champion.gg/champion/{}/{}?league=", id, position)).send().unwrap().text().unwrap(); let mut req = client.get(&format!("https://champion.gg/champion/{}/{}?league=", id, position)).send().unwrap();
serde_json::from_str(&find_champ_data(&page)).unwrap() if req.status() == reqwest::StatusCode::Ok {
serde_json::from_str(&find_champ_data(&req.text().unwrap())).unwrap()
} else {
None
}
} }
fn make_item_set(data: &Value, label: &str) -> Value { fn make_item_set(data: &Value, label: &str) -> Value {
@ -124,26 +134,45 @@ fn make_item_set(data: &Value, label: &str) -> Value {
}) })
} }
fn make_item_set_from_list(list: Vec<u32>, label: &str, key: &str, data: &Value) -> Value {
json!({
"items": list.iter().map(|x| json!({"id": x.to_string(), "count": 1})).collect::<Vec<Value>>(),
"type": format!("{} {}", label, data["skills"][key]["order"].as_array().unwrap().iter().map(|x| data["skills"]["skillInfo"].as_array().unwrap()[x.as_str().unwrap().parse::<usize>().unwrap()-1]["key"].as_str().unwrap()).collect::<Vec<&str>>().join("."))
})
}
fn write_item_set(id: &str, pos: &str, ver: &str, path: &PathBuf, client: &reqwest::Client) { fn write_item_set(id: &str, pos: &str, ver: &str, path: &PathBuf, client: &reqwest::Client) {
info!("Retrieving data for {} at {}", id, pos); info!("Retrieving data for {} at {}", id, pos);
let data = get_champ_data(id, pos, client); let data = get_champ_data(id, pos, client);
let mut item_set = ItemSet { match data {
title: format!("CGG {} {} - {:.2}%", pos, ver, data["stats"]["winRate"].as_f64().unwrap() * 100.), Some(data) => {
type_: "custom".to_string(), let mut item_set = ItemSet {
map: "any".to_string(), title: format!("CGG {} {} - {:.2}%", pos, ver, data["stats"]["winRate"].as_f64().unwrap() * 100.),
mode: "any".to_string(), type_: "custom".to_string(),
priority: false, map: "any".to_string(),
sortrank: 0, mode: "any".to_string(),
blocks: vec![] priority: false,
}; sortrank: 0,
blocks: vec![]
};
for (label, path) in ITEM_TYPES.iter() { for (label, path) in ITEM_TYPES.iter() {
item_set.blocks.push(make_item_set(&data[&path[0]][&path[1]], label)); if !data[&path[0]].get(&path[1]).is_none() {
item_set.blocks.push(make_item_set(&data[&path[0]][&path[1]], label));
}
}
item_set.blocks.push(make_item_set_from_list(CONSUMABLES.to_vec(), "Consumables | Frequent:", "mostGames", &data));
item_set.blocks.push(make_item_set_from_list(TRINKETS.to_vec(), "Trinkets | Wins:", "highestWinPercent", &data));
info!("Writing item set for {} at {}", id, pos);
fs::write(path.join(format!("CGG_{}_{}.json", id, pos)), serde_json::to_string_pretty(&item_set).unwrap()).unwrap();
},
None => {
error!("Can't get data for {} at {}", id, pos);
}
} }
info!("Writing item set for {} at {}", id, pos);
fs::write(path.join(format!("CGG_{}_{}.json", id, pos)), serde_json::to_string_pretty(&item_set).unwrap()).unwrap();
} }
fn find_champ_data(text: &str) -> String { fn find_champ_data(text: &str) -> String {