hypixel-sdk 0.2.0

Async client for the Hypixel Public API with typed models and SkyBlock helpers
Documentation
use std::error::Error;

use hypixel::HypixelClient;
use hypixel::util::{nbt, networth};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    dotenvy::dotenv().ok();
    let key = std::env::var("HYPIXEL_API_KEY").expect("set HYPIXEL_API_KEY");
    let uuid = std::env::args()
        .nth(1)
        .unwrap_or_else(|| "be5c42d4ea454601a57b944c6413f3cd".to_string());
    let member_id = uuid.replace('-', "");
    let client = HypixelClient::new(key);

    let profiles = client.skyblock_profiles(&uuid).await?;
    let profile = profiles
        .iter()
        .find(|p| p.selected)
        .or_else(|| profiles.first())
        .ok_or("player has no SkyBlock profiles")?;
    let member = profile
        .members
        .get(&member_id)
        .ok_or("uuid is not a member of this profile")?;

    let inv_contents = member
        .inventory
        .as_ref()
        .and_then(|i| i.get("inv_contents"))
        .ok_or("inventory API is disabled for this profile")?;

    let decoded = nbt::decode_inventory(inv_contents)?;
    let stacks = networth::extract_item_stacks(&decoded);

    let bazaar = client.skyblock_bazaar().await?;
    let prices = networth::PriceBook::new().with_bazaar(&bazaar);
    let estimate = networth::value_items(&stacks, &prices);

    println!("Inventory items: {}", stacks.len());
    println!("Bazaar-priced value: {:.0} coins", estimate.total);
    println!("\nTop priced items:");
    let mut items = estimate.items.clone();
    items.sort_by(|a, b| b.total.partial_cmp(&a.total).unwrap());
    for item in items.into_iter().take(10) {
        println!(
            "  {:<28} x{:<3} = {:>14.0}",
            item.item_id, item.count, item.total
        );
    }
    println!(
        "\n({} items had no bazaar price; gear is typically valued via auctions)",
        estimate.unpriced.len()
    );
    Ok(())
}