feat: migrate from reqwest to ureq
All checks were successful
continuous-integration/drone/pr Build is passing
All checks were successful
continuous-integration/drone/pr Build is passing
This commit is contained in:
parent
7e7bb8aaad
commit
ef56f2c049
869
Cargo.lock
generated
869
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -10,14 +10,14 @@ simple_logger = "1.11"
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
serde_json = { version = "1.0", features = ["preserve_order"] }
|
||||
reqwest = { version = "0.10", features = ["blocking", "json"] }
|
||||
select = "0.5"
|
||||
regex = "1.4"
|
||||
lazy_static = "1.4"
|
||||
indexmap = { version = "1.6", features = ["serde-1"] }
|
||||
ureq = { version = "2.0", features = ["json", "charset"] }
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
winreg = "0.7"
|
||||
winreg = "0.8"
|
||||
|
||||
[profile.release]
|
||||
opt-level = 'z'
|
||||
|
@ -65,13 +65,13 @@ impl DataSource for CGGDataSource {
|
||||
|
||||
fn get_champs_with_positions_and_patch(
|
||||
&self,
|
||||
client: &reqwest::blocking::Client,
|
||||
client: &ureq::Agent,
|
||||
) -> (IndexMap<String, Vec<String>>, String) {
|
||||
let page = client
|
||||
.get("https://champion.gg")
|
||||
.send()
|
||||
.call()
|
||||
.unwrap()
|
||||
.text()
|
||||
.into_string()
|
||||
.unwrap();
|
||||
let document = Document::from(&*page);
|
||||
|
||||
@ -118,21 +118,23 @@ impl DataSource for CGGDataSource {
|
||||
&self,
|
||||
id: &str,
|
||||
position: &str,
|
||||
client: &reqwest::blocking::Client,
|
||||
client: &ureq::Agent,
|
||||
) -> Option<(Vec<Value>, f64)> {
|
||||
let req = client
|
||||
.get(&format!(
|
||||
"https://champion.gg/champion/{}/{}?league=",
|
||||
id, position
|
||||
))
|
||||
.send()
|
||||
.call()
|
||||
.unwrap();
|
||||
if req.status().is_success() {
|
||||
let response_status = req.status();
|
||||
if 300 > response_status && response_status >= 200 {
|
||||
lazy_static! {
|
||||
static ref RE: Regex =
|
||||
Regex::new(r"(?m)^\s+matchupData\.championData = (.*)$").unwrap();
|
||||
}
|
||||
let data: Value = serde_json::from_str(&RE.captures(&req.text().unwrap())?[1]).unwrap();
|
||||
let data: Value =
|
||||
serde_json::from_str(&RE.captures(&req.into_string().unwrap())?[1]).unwrap();
|
||||
let mut blocks = vec![];
|
||||
for (label, path) in ITEM_TYPES.iter() {
|
||||
if !data[&path[0]].get(&path[1]).is_none() {
|
||||
|
@ -35,14 +35,14 @@ pub trait DataSource {
|
||||
|
||||
fn get_champs_with_positions_and_patch(
|
||||
&self,
|
||||
client: &reqwest::blocking::Client,
|
||||
client: &ureq::Agent,
|
||||
) -> (IndexMap<String, Vec<String>>, String);
|
||||
|
||||
fn get_champ_data_with_win_pourcentage(
|
||||
&self,
|
||||
id: &str,
|
||||
position: &str,
|
||||
client: &reqwest::blocking::Client,
|
||||
client: &ureq::Agent,
|
||||
) -> Option<(Vec<Value>, f64)>;
|
||||
|
||||
fn write_item_set(
|
||||
@ -52,7 +52,7 @@ pub trait DataSource {
|
||||
pos: &str,
|
||||
ver: &str,
|
||||
path: &PathBuf,
|
||||
client: &reqwest::blocking::Client,
|
||||
client: &ureq::Agent,
|
||||
) {
|
||||
info!("Retrieving data for {} at {}", name, pos);
|
||||
let data = self.get_champ_data_with_win_pourcentage(id, pos, client);
|
||||
|
@ -1,4 +1,6 @@
|
||||
use crate::data_source::{Build, DataSource, Item};
|
||||
#[cfg(test)]
|
||||
use crate::time::Duration;
|
||||
use indexmap::IndexMap;
|
||||
use regex::Regex;
|
||||
use serde_derive::Deserialize;
|
||||
@ -88,7 +90,7 @@ struct Summoner {
|
||||
}
|
||||
|
||||
impl KBDataSource {
|
||||
pub fn new(client: &reqwest::blocking::Client) -> KBDataSource {
|
||||
pub fn new(client: &ureq::Agent) -> KBDataSource {
|
||||
let mut datasource = KBDataSource {
|
||||
token: None,
|
||||
internal_classname_mapping: IndexMap::new(),
|
||||
@ -99,9 +101,9 @@ impl KBDataSource {
|
||||
}
|
||||
|
||||
// It will be better to use Result...
|
||||
fn get_auth_token(&self, client: &reqwest::blocking::Client) -> Option<String> {
|
||||
let bundle = match client.get("https://koreanbuilds.net/bundle.js").send() {
|
||||
Ok(resp) => match resp.text() {
|
||||
fn get_auth_token(&self, client: &ureq::Agent) -> Option<String> {
|
||||
let bundle = match client.get("https://koreanbuilds.net/bundle.js").call() {
|
||||
Ok(resp) => match resp.into_string() {
|
||||
Ok(val) => val,
|
||||
Err(_) => return None,
|
||||
},
|
||||
@ -121,7 +123,7 @@ impl KBDataSource {
|
||||
};
|
||||
}
|
||||
|
||||
fn get_classname_mapping(&self, client: &reqwest::blocking::Client) -> IndexMap<String, String> {
|
||||
fn get_classname_mapping(&self, client: &ureq::Agent) -> IndexMap<String, String> {
|
||||
let mut mapping = IndexMap::new();
|
||||
match self.get_champion_response(client) {
|
||||
Some(data) => {
|
||||
@ -134,18 +136,18 @@ impl KBDataSource {
|
||||
mapping
|
||||
}
|
||||
|
||||
fn get_champion_response(&self, client: &reqwest::blocking::Client) -> Option<ChampionResponse> {
|
||||
fn get_champion_response(&self, client: &ureq::Agent) -> Option<ChampionResponse> {
|
||||
let token = match self.token.clone() {
|
||||
Some(t) => t,
|
||||
None => return None,
|
||||
};
|
||||
match client
|
||||
.get("https://api.koreanbuilds.net/champions?patchid=-1")
|
||||
.header("Accept", "application/json")
|
||||
.header("Authorization", token)
|
||||
.send()
|
||||
.set("Accept", "application/json")
|
||||
.set("Authorization", token.as_str())
|
||||
.call()
|
||||
{
|
||||
Ok(resp) => match resp.json() {
|
||||
Ok(resp) => match resp.into_json() {
|
||||
Ok(val) => return val,
|
||||
Err(_) => return None,
|
||||
},
|
||||
@ -186,7 +188,7 @@ impl DataSource for KBDataSource {
|
||||
|
||||
fn get_champs_with_positions_and_patch(
|
||||
&self,
|
||||
client: &reqwest::blocking::Client,
|
||||
client: &ureq::Agent,
|
||||
) -> (IndexMap<String, Vec<String>>, String) {
|
||||
let mut champions = IndexMap::new();
|
||||
let data: ChampionResponse = match self.get_champion_response(client) {
|
||||
@ -209,7 +211,7 @@ impl DataSource for KBDataSource {
|
||||
&self,
|
||||
id: &str,
|
||||
position: &str,
|
||||
client: &reqwest::blocking::Client,
|
||||
client: &ureq::Agent,
|
||||
) -> Option<(Vec<Value>, f64)> {
|
||||
let token = match self.token.clone() {
|
||||
Some(t) => t,
|
||||
@ -224,11 +226,11 @@ impl DataSource for KBDataSource {
|
||||
"https://api.koreanbuilds.net/builds?chmpname={}&patchid=-2&position={}",
|
||||
map_id, position
|
||||
))
|
||||
.header("Accept", "application/json")
|
||||
.header("Authorization", token)
|
||||
.send()
|
||||
.set("Accept", "application/json")
|
||||
.set("Authorization", token.as_str())
|
||||
.call()
|
||||
{
|
||||
Ok(resp) => match resp.json() {
|
||||
Ok(resp) => match resp.into_json() {
|
||||
Ok(val) => val,
|
||||
Err(_) => {
|
||||
return None;
|
||||
@ -349,10 +351,9 @@ mod tests {
|
||||
token: None,
|
||||
internal_classname_mapping: IndexMap::new(),
|
||||
};
|
||||
let client = reqwest::blocking::Client::builder()
|
||||
let client = ureq::AgentBuilder::new()
|
||||
.timeout(Duration::from_secs(10))
|
||||
.build()
|
||||
.unwrap();
|
||||
.build();
|
||||
match datasource.get_auth_token(&client) {
|
||||
Some(token) => assert!(token.len() > 0),
|
||||
None => assert!(false),
|
||||
@ -361,10 +362,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_get_champs_with_positions_and_patch() {
|
||||
let client = reqwest::blocking::Client::builder()
|
||||
let client = ureq::AgentBuilder::new()
|
||||
.timeout(Duration::from_secs(10))
|
||||
.build()
|
||||
.unwrap();
|
||||
.build();
|
||||
let datasource = KBDataSource::new(&client);
|
||||
let champs_with_positions_and_patch =
|
||||
datasource.get_champs_with_positions_and_patch(&client);
|
||||
@ -373,10 +373,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_get_champ_data_with_win_pourcentage() {
|
||||
let client = reqwest::blocking::Client::builder()
|
||||
let client = ureq::AgentBuilder::new()
|
||||
.timeout(Duration::from_secs(10))
|
||||
.build()
|
||||
.unwrap();
|
||||
.build();
|
||||
let datasource = KBDataSource::new(&client);
|
||||
let result = datasource.get_champ_data_with_win_pourcentage("Aatrox", "TOP", &client);
|
||||
assert!(result.is_some());
|
||||
|
31
src/main.rs
31
src/main.rs
@ -1,9 +1,10 @@
|
||||
use indexmap::IndexMap;
|
||||
use log::{error, info, LevelFilter};
|
||||
use reqwest::header;
|
||||
use serde_derive::Deserialize;
|
||||
use simple_logger::SimpleLogger;
|
||||
use std::io::{Error, ErrorKind};
|
||||
use std::io::Error;
|
||||
#[cfg(target_os = "windows")]
|
||||
use std::io::ErrorKind;
|
||||
use std::path::PathBuf;
|
||||
use std::{fs, thread, time};
|
||||
use time::Duration;
|
||||
@ -35,13 +36,15 @@ struct ChampInfo {
|
||||
name: String,
|
||||
}
|
||||
|
||||
const USER_AGENT: &str =
|
||||
const USER_AGENT_KEY: &str = "User-Agent";
|
||||
const USER_AGENT_VALUE: &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() {
|
||||
SimpleLogger::new()
|
||||
.with_level(LevelFilter::Info)
|
||||
.with_module_level("ureq", LevelFilter::Error)
|
||||
.init()
|
||||
.unwrap();
|
||||
|
||||
@ -53,23 +56,16 @@ fn main() {
|
||||
};
|
||||
info!("LoL Champs Folder: {}", lol_champs_dir.to_str().unwrap());
|
||||
|
||||
let mut headers = header::HeaderMap::new();
|
||||
headers.insert(
|
||||
header::USER_AGENT,
|
||||
header::HeaderValue::from_static(USER_AGENT),
|
||||
);
|
||||
|
||||
let client = reqwest::blocking::Client::builder()
|
||||
.default_headers(headers)
|
||||
let client = ureq::AgentBuilder::new()
|
||||
.timeout(Duration::from_secs(10))
|
||||
.build()
|
||||
.unwrap();
|
||||
.build();
|
||||
|
||||
let realm: Realm = client
|
||||
.get("https://ddragon.leagueoflegends.com/realms/euw.json")
|
||||
.send()
|
||||
.set(USER_AGENT_KEY, USER_AGENT_VALUE)
|
||||
.call()
|
||||
.unwrap()
|
||||
.json()
|
||||
.into_json()
|
||||
.unwrap();
|
||||
info!("LoL version: {}", realm.v);
|
||||
|
||||
@ -78,9 +74,10 @@ fn main() {
|
||||
"https://ddragon.leagueoflegends.com/cdn/{}/data/en_US/champion.json",
|
||||
realm.v
|
||||
))
|
||||
.send()
|
||||
.set(USER_AGENT_KEY, USER_AGENT_VALUE)
|
||||
.call()
|
||||
.unwrap()
|
||||
.json()
|
||||
.into_json()
|
||||
.unwrap();
|
||||
info!("LoL numbers of champs: {}", champion.data.len());
|
||||
|
||||
|
@ -11,7 +11,7 @@ impl DataSource for PBDataSource {
|
||||
|
||||
fn get_champs_with_positions_and_patch(
|
||||
&self,
|
||||
_client: &reqwest::blocking::Client,
|
||||
_client: &ureq::Agent,
|
||||
) -> (IndexMap<String, Vec<String>>, String) {
|
||||
(IndexMap::new(), String::new())
|
||||
}
|
||||
@ -20,7 +20,7 @@ impl DataSource for PBDataSource {
|
||||
&self,
|
||||
_id: &str,
|
||||
_position: &str,
|
||||
_client: &reqwest::blocking::Client,
|
||||
_client: &ureq::Agent,
|
||||
) -> Option<(Vec<Value>, f64)> {
|
||||
None
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user