Python - Math operations from parsed API data

Viewed 61

I have written a python script that pulls data from an API ( https://api.hypixel.net/skyblock/auctions ) and it works pretty well for what I want it to do, however, I wanted to add a feature which would find the exact same item twice, then compare the lowest 2 prices for it (subtraction) then shows me the difference between the lowest price and 2nd lowest price. Example: Item X Lowest Price: 100,000 Item X 2nd Lowest Price: 300,000 if the difference is =< 200,000, it shows me the value with the exact difference between the item. Outputs: <item_name>, <price difference between 2 lowest prices>,

This is the code I have right now.

import requests

data = requests.get("https://api.hypixel.net/skyblock/auctions").json()
auctions = data["auctions"]
items = []
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"]])
    except KeyError:
        continue
items.sort(key=lambda x: x[1])
for item in items:
    print(item)
1 Answers
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
Related