Create example

This commit is contained in:
Sakuhl 2018-03-02 14:22:08 +01:00
parent b71c3d1af9
commit 9ece9e2da5
4 changed files with 38 additions and 33 deletions

View File

@ -1,5 +1,5 @@
[package]
authors = ["Sakuhl <2012collector@gmail.com>"]
authors = ["FliegendeWurst <2012gdwu@web.de>", "Sakuhl <2012collector@gmail.com>"]
name = "wynncraft"
version = "0.1.0"
@ -9,3 +9,6 @@ reqwest = "0.8.2"
serde = "1.0.27"
serde_derive = "1.0.27"
serde_json = "1.0.9"
[dev-dependencies]
chrono = "0.4.0"

34
examples/last-join.rs Normal file
View File

@ -0,0 +1,34 @@
extern crate wynncraft;
extern crate chrono;
use chrono::prelude::*;
use std::env;
fn main() {
let args = env::args().collect::<Vec<_>>();
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);
}
},
Some("guild-player-lastjoin") if args.len() > 2 => {
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",
name,
player.last_join_friendly,
now.signed_duration_since(joined).num_days()
);
}
}
},
_ => {} // TODO
}
}

View File

@ -49,18 +49,6 @@ pub fn guild_list() -> Result<Vec<String>, Error> {
}
}
pub fn guild_by_prefix(prefix: &str) -> Result<Option<GuildEntry>, Error> {
let top_100 = guild_leaderboard()?;
for guild in top_100.data.into_iter() {
if guild.prefix.to_lowercase() == prefix.to_lowercase() {
return Ok(Some(guild));
}
}
Ok(None)
}
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()?;
@ -238,9 +226,4 @@ mod tests {
fn guild_list() {
::guild_list().unwrap();
}
#[test]
fn guild_prefix() {
::guild_by_prefix("Two").unwrap();
}
}

View File

@ -1,15 +0,0 @@
extern crate wynncraft;
use std::env;
fn main() {
let args = env::args().collect::<Vec<_>>();
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);
}
},
_ => {} // TODO
}
}