#[macro_use] extern crate serde_derive; #[macro_use] extern crate log; extern crate reqwest; extern crate serde; extern crate serde_json; extern crate simple_logger; extern crate select; use std::collections::HashMap; use std::fs; use std::path::{Path, PathBuf}; use reqwest::header::{Headers, UserAgent}; use select::document::Document; use select::predicate::{Class, Name}; #[derive(Deserialize)] struct Realm { v: String } #[derive(Deserialize)] struct Champion { data: HashMap } #[derive(Deserialize)] struct ChampInfo { id: String } #[derive(Serialize, Deserialize)] struct ItemSet { title: String, #[serde(rename = "type")] type_: String, map: String, mode: String, priority: bool, sortrank: i32, blocks: Vec } 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"; fn main() { simple_logger::init_with_level(log::Level::Info).unwrap(); info!("CGG Item Sets"); let mut headers = Headers::new(); headers.set(UserAgent::new(USER_AGENT)); let client = reqwest::Client::builder().default_headers(headers).build().unwrap(); let realm: Realm = client.get("https://ddragon.leagueoflegends.com/realms/euw.json").send().unwrap().json().unwrap(); info!("LoL version: {}", realm.v); let champion: Champion = client.get(&format!("https://ddragon.leagueoflegends.com/cdn/{}/data/en_US/champion.json", realm.v)).send().unwrap().json().unwrap(); info!("LoL numbers of champs: {}", champion.data.len()); let (champs, patch) = get_champs_with_positions_and_patch(&client); info!("CGG version: {}", patch); info!("CGG numbers of champs: {}", champs.len()); for (id, positions) in champs { if champion.data.contains_key(&id) { let path = Path::new(LOL_CHAMPS_DIR).join(&id).join("Recommended"); fs::create_dir_all(&path).unwrap(); for pos in positions { write_item_set(&id, pos, &patch, &path, &client) } } else { info!("{} not found in LoL champs", &id); } } } fn get_champs_with_positions_and_patch(client: &reqwest::Client) -> (HashMap>, String) { let page = client.get("https://champion.gg").send().unwrap().text().unwrap(); let document = Document::from(&*page); let patch = document.find(Class("analysis-holder")).next().unwrap().find(Name("strong")).next().unwrap().text(); let mut champions = HashMap::new(); 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 positions = node.find(Name("a")) .skip(1) .map(|x| x.attr("href").unwrap().split('/').last().unwrap().to_string()) .collect(); champions.insert(id, positions); } (champions, patch) } fn write_item_set(id: &String, pos: String, ver: &String, path: &PathBuf, client: &reqwest::Client) { info!("Retrieving data for {} at {}", id, pos); let item_set = ItemSet { title: format!("CGG {}", pos), type_: "custom".to_string(), map: "any".to_string(), mode: "any".to_string(), priority: false, sortrank: 0, blocks: vec![] }; 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(); }