使用Steam Web API获取玩游戏的总时间

时间:2013-10-29 08:50:32

标签: python steam-web-api

我正在尝试访问使用Steam API播放的TF2时间量。我目前正在使用: -

http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=440&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&steamid=xxxxxxxxxxxxxxxxx&format=xml

然后过滤XML并提取与每个类相关的播放时间(例如pyro(Pyro.accum.iPlayTime)等)。这工作正常,但我认为错过了MVM类使我的最终值不正确(我的代码,在Python中,当在线网站超过1600小时时返回977)。添加MVM类(加上可能还有其他类)可能会提供错误的结果,但它会让代码长时间啰嗦。

所以我想知道Steam Web API中是否有一个调用只会给我一个总时间而不必去完所有的提取和添加?

我查看了Steam Web API Developer页面,但找不到任何我所追求的内容。

添加了代码:

if __name__ == '__main__':
    import urllib2
    import xml.etree.ElementTree as ET
    import datetime

    timeKISA = 0
    playerStatsKISA = urllib2.urlopen('http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=440&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&steamid=xxxxxxxxxxxxxxxxx&format=xml')
    statsKISA = playerStatsKISA.read()
    theStatsKISA = ET.fromstring(statsKISA)
    for stat in theStatsKISA.findall("./stats/stat"):
        if stat.find('name').text.startswith('Scout.accum.iPlayTime') or \
             stat.find('name').text.startswith('Soldier.accum.iPlayTime') or \
             stat.find('name').text.startswith('Engineer.accum.iPlayTime') or \
             stat.find('name').text.startswith('Medic.accum.iPlayTime') or \
             stat.find('name').text.startswith('Spy.accum.iPlayTime') or \
             stat.find('name').text.startswith('Sniper.accum.iPlayTime') or \
             stat.find('name').text.startswith('Demoman.accum.iPlayTime') or \
             stat.find('name').text.startswith('Heavy.accum.iPlayTime') or \
             stat.find('name').text.startswith('Pyro.accum.iPlayTime'):

             timeKISA = timeKISA + int(stat.find('value').text) 
        finalTimeKISA = timeKISA / 60 / 60
        KISATime = ('KISA_Time=' + str(finalTimeKISA) + ' hours')
        print KISATime

谢谢。

马库斯

1 个答案:

答案 0 :(得分:1)

将我的评论拉回答案,

据我了解,无论游戏模式或地图如何,*.accum.iPlayTime字段都会累计为您的地点时间。根据我自己的统计数据(以及我朋友列表中的其他一些内容),这与Steam社区报告的播放时间完全一致。此外,它报告您的游戏时间与TF2成就页面上的这些字段匹配。

一对夫妇注意到:

  • 玩家个人资料的摘要页面似乎与成就页面显示的实际统计数据不匹配。我不确定这是Steam社区问题还是其他字段的摘要。但是,成就页面具有按类别,最长寿命等详细分解播放时间,使用与GetUserStatsForGame API调用相同的数据。
  • 您的代码中存在非常小的格式问题。最后一个print KISATime缩进一次或多次,因此多次打印KISA_Time =行。如果您将其拉出for循环,则只能获得一次打印行。
  • 如果您将finalTimeKISA = timeKISA / 60 / 60更改为小数60.0,您将获得小数答案。否则,按原样,您只会收到整数答案。