Steam API获取价格清单

时间:2015-04-12 15:47:08

标签: python

我试图获取价格清单。到目前为止,我的代码是:

def steamlibrarypull(steamID, key):
    #Pulls out a CSV of Steam appids.
    steaminfo = {
        'key': key,
        'steamid': steamID,
        'format':'JSON',
        'include_appinfo':'1'
    }
    r = requests.get('http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/', params=steaminfo)
    d = json.loads(r.content)
    I = d['response']['games']
    B = {}
    for games in I:
        B[games['name'].encode('utf8')] = games['appid']
    with open('games.csv', 'w') as f:
        for key, value in B.items():
            f.write("%s,%s\r\n" % (key, value))
    return B

但我希望能够做一个请求。我会拿这本字典并输出一份价格清单。 https://wiki.teamfortress.com/wiki/User:RJackson/StorefrontAPI似乎需要CSV列表但是真的有必要吗?

1 个答案:

答案 0 :(得分:1)

这是一种非正式的蒸汽api,意思是蒸汽会在他们认为合适时进行修改。目前不支持多个appid ,如上所述here 用它来获得你要去的游戏的价格

http://store.steampowered.com/api/appdetails/?appids=237110&cc=us&filters=price_overview

根据您上面的代码工作,您将需要知道如何迭代字典并在获得它后更新商店价格。

def steamlibrarypull(steamID, key):
#Pulls out a CSV of Steam appids.
    steaminfo = {
        'key': key,
        'steamid': steamID,
        'format':'JSON',
        'include_appinfo':'1'
    }
    r = requests.get('http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/', params=steaminfo)
    d = json.loads(r.content)
    response = d['response']['games']
    games = {}
    for game in response:
        getprice = requests.get('http://store.steampowered.com/api/appdetails/?appids=%d&filters=price_overview&cc=us' % game['appid'])
        if getprice.status_code == 200:
            rjson = json.loads(getprice.text)
            # use the appid to fetch the value and convert to decimal
            # appid is numeric, cast to string to lookup the price
            try:
                price = rjson[str(game['appid'])]['data']['price_overview']['initial'] * .01
            except:
                price = 0
            games[game['name']] = {'price': price, 'appid': game['appid']}

这将返回以下字典:

{u'Half-Life 2: Episode Two': {'price': 7.99, 'appid': 420}

通过appid而不是名称导航会更容易,但根据您的请求和原始结构,这是应该如何完成的。然后,它会为您提供可以进一步处理或写入文件的名称,appid和价格。 请注意,这不包括睡眠计时器,如果你的游戏列表很长,你应该在你的api调用之前再睡2秒,或者api会阻止你,并且不会返回数据,这会导致你在python中出错解析价格。