Answer the question
In order to leave comments, you need to log in
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¤cy=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())
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
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question