Python – Math operations from parsed API data

import requests

#Here is your new feature calculating the difference between two lowest prices for each item
def func(name, prices):
    prices.sort(reverse = True) #sort prices of an item descending 
    if len(prices) == 1:
        print (f"There is only one price for {name}")
    else:
        print (f"Name : {name}, Price difference between two lowest ones: {prices[-2] - prices[-1]}")

data = requests.get("https://api.hypixel.net/skyblock/auctions").json()
auctions = data["auctions"]
items = []
items1 = {} #Define items1 to group prices of an item in dictionay
for auction in auctions:
    try:
        if auction["bin"] and (str(auction["item_name"]).startswith("")) and auction["category"] == "weapon" and auction["tier"] == "EPIC":
            items.append((auction["item_name"], auction["starting_bid"], auction["tier"]))
            #Find and group all prices for an item
            if auction["item_name"] in list(items1.keys()):
                items1[auction["item_name"]].append(auction["starting_bid"])
            else:
                items1[auction["item_name"]] = [auction["starting_bid"]]
    except KeyError:
        continue
items.sort(key=lambda x: x[1])

#Apply feature for each item
for name,prices in items1.items():
    func(name, prices)

And here is the result:

There is only one price for Emerald Blade
There is only one price for Gentle Zombie Commander Whip
There is only one price for Reaper Falchion
Name : Treecapitator, Price difference between two lowest ones: 350000
Name : Heroic Ornate Zombie Sword, Price difference between two lowest ones: 0
There is only one price for Grand Sniper Bow
Name : Spicy Reaper Falchion, Price difference between two lowest ones: 1400000
There is only one price for Unreal Machine Gun Bow ✪✪✪✪✪
There is only one price for Spicy Leaping Sword
There is only one price for Hurricane Bow
There is only one price for Spicy Scorpion Foil
Name : Magma Bow, Price difference between two lowest ones: 535001
There is only one price for Heroic Ornate Zombie Sword ✪✪✪✪✪
There is only one price for Spicy Silk-Edge Sword

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top