A
A
Alex Fedorov2021-10-18 11:27:03
Python
Alex Fedorov, 2021-10-18 11:27:03

Steam CS:GO inventory cost parsing?

link_inv = "https://steamcommunity.com/id/{}/inventory/json/730/2"
GUN_URL = "https://steamcommunity.com/market/priceoverview/?appid=730&currency=1&market_hash_name={}"



async def get_inventory(steam_id):
    r = requests.get(link_inv.format(steam_id)).json()
    x = r["rgDescriptions"]
    for item in x:
        if x[item]["marketable"] == 1 and x[item]["tradable"] == 1:
            task2 = asyncio.create_task(get_quantity(x[item]["classid"], r["rgInventory"]))
            quantity = await task2
            collection_data = [(x[item]["market_hash_name"], quantity) for item in x]
        else:
            continue
    return collection_data

async def get_quantity(classid, inventory):
    counter = 0
    for item in inventory:
        if inventory[item]["classid"] == classid:
            counter += 1
    return counter

async def get_price(skin):
    total_price = 0
    for item in skin:
        async with aiohttp.ClientSession() as session:
            async with session.get(GUN_URL.format(item[0])) as resp:
                r = await resp.json()

                if r is None:
                    continue
                elif r["success"] is False:
                    continue

                try:
                    if r["lowest_price"] and r["median_price"] is None:
                        continue
                except KeyError:
                    continue
                try:
                    lowest_price = str(r["lowest_price"])
                except KeyError:
                    lowest_price = str(r["median_price"])

                lowest_price = float(lowest_price.strip("$"))
                total_price = total_price + (lowest_price * (float(item[1])))
    return round(total_price, 2)
    


async def main():
    steam_id = input("Enter your SteamID: ")
    task = asyncio.create_task(get_inventory(steam_id))
    await asyncio.gather(task)
    xxx = task.result()
    task1 = asyncio.create_task(get_price(xxx))
    await asyncio.gather(task1)
    print('$'+str(task1.result()))

if __name__ == "__main__":
    asyncio.run(main())


Hello! At the university, they said to make an inventory price parser in Steam. Help, what's wrong with it? When I do debugging step by step, the desired result comes out (inventory price) , and when I run the code and enter stimide, the program shows the result, but also below it comes out like this

Enter your SteamID: silencers77
$0.51
Exception ignored in:
Traceback (most recent call last):
File "C:\Users\Ruslan\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 116, in __del__
self.close()
File "C:\Users\Ruslan\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close
self._loop.call_soon(self._call_connection_lost, None )
File "C:\Users\Ruslan\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 746, in call_soon
self._check_closed()
File "C:\Users\Ruslan\AppData\ Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 510, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed


Also, the code does not calculate the price of inventories, in which there are more than 100 items. For example, let's say the price is $45, but the output shows that it is $7..

Specify the errors, please. It can be code, it can be words. I will be grateful. All the best!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-10-19
@robben_55

Very strange code. Why are you even using create_task() and gather()?
create_task() makes sense if you want to call a coroutine so that it runs independently of the caller.
gather() makes sense if you have MULTIPLE coroutines and you want to execute them in parallel and then wait for them all to complete. And in your case, xxx = await get_inventory(steam_id)
should be enough . Further, why do you have get_quantity() called asynchronously, and even again through create_task()? It doesn't do any I/O, and it won't load the CPU too much, leave it synchronous and call it normally.
Next, you use synchronous requests and asynchronous aiohttp on the heap. Pick one? I would stop on aiohttp, since you are writing asynchronous code.
In general, it is easier to rewrite than to correct ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question