steampy. 始终获得太多请求。

huangapple go评论70阅读模式
英文:

steampy. Always getting TooManyRequests

问题

今天我开始制作Discord机器人,它将跟踪我的CS:GO贴纸的市场价格。问题是,当我尝试使用steampy获取市场价格时,我总是收到"TooManyRequests"错误。

这是代码的一部分。

@client.command()
async def cena(ctx):   
    global steam_client
    f = open('items.json', "r")
    data = json.load(f)
    item_dict = data['items_info']
    for x in item_dict:
        item = x['item']
        quantity = x['quantity']
        cost = x['cost']
        market = steam_client.market.fetch_price(item, game=GameOptions.CS, currency=Currency.EURO)
        print(market)
        if market == None or market['success'] == False:
            await ctx.send("error") 
            break
        else:      
            lowest = market['lowest_price'].replace("€", "").replace(",", ".")
            ans = f"{item} : **{lowest}€** ||{cost}€ ({quantity})||"
            await ctx.send(ans)
    f.close()
    time.sleep(6)

这是items.json

{
	"items_info" : [
	{ 
		"item" : "Sticker | Spirit (Holo) | 2020 RMR",
		"quantity" : 1,
		"cost" : 0.50,
		"sellat" : 0.65
	},
	{
		"item" : "Sticker | Virtus.pro (Holo) | 2020 RMR",
		"quantity" : 1,
		"cost" : 0.61,
		"sellat" : 0.90
	},
	{ 
		"item" : "Sticker | FURIA (Holo) | 2020 RMR",
		"quantity" : 2,
		"cost" : 0.48,
		"sellat" : 0.60
	},
	{ 
		"item" : "Sticker | Renegades (Holo) | 2020 RMR",
		"quantity" : 5,
		"cost" : 0.20,
		"sellat" : 0.30
	}
   ]
}

我尝试撤销我的Steam API密钥,但没有帮助。我还尝试了其他包,如steammarket,但也没有成功。这甚至在第一次获取时都发生了。

编辑:今天我尝试运行代码,第一次它获取了前两个贴纸的成本,但在第三个贴纸中完全停止工作。

英文:

Today i started making Discord bot that will track market price of my CS:GO Stickers. Problem is when I trying to fetch market price using steampy I always getting TooManyRequests error.

This fragment of a code.

@client.command()
async def cena(ctx):   
    global steam_client
    f = open('items.json', "r")
    data = json.load(f)
    item_dict = data['items_info']
    for x in item_dict:
        item = x['item']
        quantity = x['quantity']
        cost = x['cost']
        market = steam_client.market.fetch_price(item, game=GameOptions.CS, currency=Currency.EURO)
        print(market)
        if  market == None or market['success'] == False:
            await ctx.send("error") 
            break
        else:      
            lowest = market['lowest_price'].replace("€", "").replace(",", ".")
            ans = f"{item} : **{lowest}€** ||{cost}€ ({quantity})||"
            await ctx.send(ans)
    f.close()
    time.sleep(6)

Here is items.json

{
		"items_info" : [
		{ 
			"item" : "Sticker | Spirit (Holo) | 2020 RMR",
			"quantity" : 1,
			"cost" : 0.50,
			"sellat" : 0.65
		},
		{
			"item" : "Sticker | Virtus.pro (Holo) | 2020 RMR",
			"quantity" : 1,
			"cost" : 0.61,
			"sellat" : 0.90
		},
		{ 
			"item" : "Sticker | FURIA (Holo) | 2020 RMR",
			"quantity" : 2,
			"cost" : 0.48,
			"sellat" : 0.60
		},
		{ 
			"item" : "Sticker | Renegades (Holo) | 2020 RMR",
			"quantity" : 5,
			"cost" : 0.20,
			"sellat" : 0.30
		}
       ]
}

I tried to revoking my Steam API Key it didn't help. I've also tried with another packages like steammarket but it didn't work too. It happends even in first fetch.

EDIT I tried run code today and first time it fetched cost of first two sticker but in sticker three it stopped working at all.

答案1

得分: 1

你超过了Steam API设置的速率限制,你需要在发出请求之间添加延迟。最好使用asyncio.sleep来添加延迟。

import asyncio

@client.command()
async def cena(ctx):
    global steam_client
    f = open('items.json', "r")
    data = json.load(f)
    item_dict = data['items_info']
    for x in item_dict:
        item = x['item']
        quantity = x['quantity']
        cost = x['cost']
        market = steam_client.market.fetch_price(item, game=GameOptions.CS, currency=Currency.EURO)
        print(market)
        if market == None or market['success'] == False:
            await ctx.send("error")
            break
        else:
            lowest = market['lowest_price'].replace("€", "").replace(",", ".")
            ans = f"{item} : **{lowest}€** ||{cost}€ ({quantity})||"
            await ctx.send(ans)

        # 在请求之间添加延迟
        await asyncio.sleep(5)

    f.close()
英文:

You exceed the rate limit imposed by the Steam API, you gonna have to add a delay in between the request you make.
The best would be to use asyncio.sleep to add delay.

import asyncio

@client.command()
async def cena(ctx):   
    global steam_client
    f = open('items.json', "r")
    data = json.load(f)
    item_dict = data['items_info']
    for x in item_dict:
        item = x['item']
        quantity = x['quantity']
        cost = x['cost']
        market = steam_client.market.fetch_price(item, game=GameOptions.CS, currency=Currency.EURO)
        print(market)
        if  market == None or market['success'] == False:
            await ctx.send("error") 
            break
        else:      
            lowest = market['lowest_price'].replace("€", "").replace(",", ".")
            ans = f"{item} : **{lowest}€** ||{cost}€ ({quantity})||"
            await ctx.send(ans)
        
        # Add a delay between requests
        await asyncio.sleep(5)

    f.close()

huangapple
  • 本文由 发表于 2023年4月10日 21:34:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定