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]
failure = "0.1.1"
reqwest = "0.8.2"
reqwest = { version = "0.10.4", features = ["blocking"] }
serde = "1.0.27"
serde_derive = "1.0.27"
serde_json = "1.0.9"

View File

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

View File

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