Update to latest Wynncraft API

This commit is contained in:
Sakuhl 2020-03-14 14:56:33 +01:00
parent af6f4a80cf
commit bb39f6593e
3 changed files with 57 additions and 53 deletions

View File

@ -8,7 +8,7 @@ repository = "https://gitlab.com/Sakuhl/wynncraft"
[dependencies] [dependencies]
failure = "0.1.1" failure = "0.1.1"
reqwest = "0.8.2" reqwest = { version = "0.10.4", features = ["blocking"] }
serde = "1.0.27" serde = "1.0.27"
serde_derive = "1.0.27" serde_derive = "1.0.27"
serde_json = "1.0.9" serde_json = "1.0.9"

View File

@ -10,24 +10,28 @@ fn main() {
match args.get(1).map(|x| &**x) { match args.get(1).map(|x| &**x) {
Some("player-lastjoin") if args.len() > 2 => { Some("player-lastjoin") if args.len() > 2 => {
for name in &args[2..] { for name in &args[2..] {
println!("{}: {:?}", name, wynncraft::player(name).unwrap().last_join_friendly); println!("{}: {:?}", name, wynncraft::player(name).unwrap().meta.lastJoin);
} }
}, },
Some("guild-player-lastjoin") if args.len() > 2 => { Some("guild-player-lastjoin") if args.len() > 2 => {
let mut inactive = 0;
for guild_name in &args[2..] { for guild_name in &args[2..] {
for name in wynncraft::guild(guild_name).unwrap().unwrap().members.iter().map(|x| &x.name) { for name in wynncraft::guild(guild_name).unwrap().unwrap().members.iter().map(|x| &x.name) {
let player = wynncraft::player(name).unwrap(); let days = wynncraft::player(name)
let time = player.last_join.replace(' ', "T") + "+00:00"; .map(|x| x.meta.lastJoin)
//println!("parsing {}", time); .map(|x| x.parse::<DateTime<FixedOffset>>().unwrap())
let joined: DateTime<FixedOffset> = time.parse().unwrap(); // TODO: timezone? .map(|x| Local::now().signed_duration_since(x).num_days() as i64)
let now = Local::now(); .unwrap_or(-1);
println!("{}: {:?} -> {:?} days ago", println!("{}: {:?} days ago",
name, name,
player.last_join_friendly, days
now.signed_duration_since(joined).num_days()
); );
if days > 14 {
inactive += 1;
}
} }
} }
println!("--> {} inactive", inactive);
}, },
_ => {} // TODO _ => {} // TODO
} }

View File

@ -1,4 +1,5 @@
extern crate reqwest; extern crate reqwest;
use reqwest::blocking::get;
extern crate serde; extern crate serde;
extern crate serde_json; extern crate serde_json;
@ -10,22 +11,18 @@ extern crate serde_derive;
extern crate failure; extern crate failure;
use failure::Error; use failure::Error;
use std::collections::HashMap;
pub fn player(name: &str) -> Result<Player, Error> { pub fn player(name: &str) -> Result<Player, Error> {
let resp = reqwest::get(&format!("https://api.wynncraft.com/public_api.php?action=playerStats&command={}", name))? let resp = get(&format!("https://api.wynncraft.com/v2/player/{}/stats", name))?
.error_for_status()? .error_for_status()?
.text().unwrap(); .text().unwrap();
if let Ok(error) = serde_json::from_str::<APIError>(&resp) { let x: Value = serde_json::from_str(&resp)?;
bail!("API error ({})", error.error); //println!("{:?}", x);
} else { Ok(serde_json::from_value(x["data"][0].clone())?)
Ok(serde_json::from_str(&resp)?)
}
} }
pub fn guild(name: &str) -> Result<Option<Guild>, Error> { pub fn guild(name: &str) -> Result<Option<Guild>, Error> {
let resp = reqwest::get(&format!("https://api.wynncraft.com/public_api.php?action=guildStats&command={}", name))? let resp = get(&format!("https://api.wynncraft.com/public_api.php?action=guildStats&command={}", name))?
.error_for_status()? .error_for_status()?
.text().unwrap(); .text().unwrap();
@ -37,7 +34,7 @@ pub fn guild(name: &str) -> Result<Option<Guild>, Error> {
} }
pub fn guild_list() -> Result<Vec<String>, Error> { pub fn guild_list() -> Result<Vec<String>, Error> {
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=guildList")? let resp = get("https://api.wynncraft.com/public_api.php?action=guildList")?
.error_for_status()? .error_for_status()?
.text().unwrap(); .text().unwrap();
@ -50,7 +47,7 @@ pub fn guild_list() -> Result<Vec<String>, Error> {
} }
pub fn guild_leaderboard() -> Result<Top100Guilds, Error> { pub fn guild_leaderboard() -> Result<Top100Guilds, Error> {
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=statsLeaderboard&type=guild&timeframe=alltime")?.error_for_status()?.text()?; let resp = get("https://api.wynncraft.com/public_api.php?action=statsLeaderboard&type=guild&timeframe=alltime")?.error_for_status()?.text()?;
if let Ok(error) = serde_json::from_str::<APIError2>(&resp) { if let Ok(error) = serde_json::from_str::<APIError2>(&resp) {
bail!("API error ({})", error.message); bail!("API error ({})", error.message);
@ -131,21 +128,24 @@ pub enum Rank {
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub struct Player { pub struct Player {
pub username: String, pub username: String,
pub rank: String, pub uuid: String,
pub tag: String, pub rank: String,
pub displayTag: bool, pub meta: PlayerMetadata,
pub veteran: bool, pub classes: Vec<Class>,
pub playtime: u64, pub guild: PlayerGuildInfo,
pub first_join: String, pub global: Value,
pub last_join: String, pub ranking: Value
pub first_join_friendly: String, }
pub last_join_friendly: String,
pub current_server: String, #[derive(Debug, Deserialize)]
pub global: GlobalPlayerInfo, #[allow(non_snake_case)]
pub classes: HashMap<String, Class>, pub struct PlayerMetadata {
pub wizard_fortress: WFPlayerInfo, pub firstJoin: String,
pub rankings: Option<PlayerRankings>, pub lastJoin: String,
pub guild: PlayerGuildInfo pub location: Value,
pub playtime: u64,
pub tag: Value,
pub veteran: bool
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -164,24 +164,24 @@ pub struct GlobalPlayerInfo {
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub struct Class { pub struct Class {
pub name: String,
pub level: u64, pub level: u64,
pub xp: f64,
pub dungeons: Value, pub dungeons: Value,
pub quests: Vec<String>, pub quests: Value,
pub dungeonsAmount: String, pub itemsIdentified: u64,
pub questsAmount: String, pub mobsKilled: u64,
pub skills: Value, pub pvp: Value,
pub items_identified: u64, pub chestsFound: u64,
pub mobs_killed: u64, pub blocksWalked: u64,
pub pvp_kills: u64,
pub pvp_deaths: u64,
pub chests_found: u64,
pub blocks_walked: u64,
pub logins: u64, pub logins: u64,
pub deaths: u64, pub deaths: u64,
pub events_won: u64, pub playtime: u64,
/// in minutes pub gamemode: Value,
pub playtime: u64 pub skills: Value,
pub professions: Value,
pub discoveries: u64,
pub eventsWon: u64,
pub preEconomyUpdate: bool
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -201,8 +201,8 @@ pub struct PlayerRankings {
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct PlayerGuildInfo { pub struct PlayerGuildInfo {
pub name: String, pub name: Option<String>,
pub rank: String pub rank: Option<String>
} }
#[cfg(test)] #[cfg(test)]