2

我正在使用 Django 环境,我想从 Steam 中提取统计信息。然而,Steam 的 API 非常愚蠢,因为它们有大约 20 个不同的 URL,我想从中获取信息的主要两个是:

我正在使用 Python 请求库来GET处理来自 Steam 的数据。

import requests
import json
from xml.dom.minidom import parseString

STEAM_API_URL = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002'
STEAM_API_KEY = 'XXXXX'
STEAM_USERNAME = 'niteshade'

# Make request to steamcommunity.com with the username to get the 64-bit Steam ID
username_r = requests.get('http://steamcommunity.com/id/{0}/games?tab=all&xml=1'.format(STEAM_USERNAME))
steamid = str(parseString(username_r.text.encode('utf-8')).getElementsByTagName('steamID64')[0].firstChild.wholeText)
totalgames = parseString(username_r.text.encode('utf-8')).getElementsByTagName('game').length

data = {
    'key': STEAM_API_KEY,
    'steamids': steamid,
}

user_r = requests.get(STEAM_API_URL, params=data)
#user_r.json['response']['players'][0].update({'totalgames'.encode('utf-8'): totalgames})

当我查询时,steamcommunity.com我得到了类似的东西:

<gamesList>
    <steamID64>12345</steamID64>
    <steamID>aSteamID</steamID>
    <games>
        <game>
            <appID>201790</appID>
            <name>Orcs Must Die! 2</name>
            <logo>http://media.steampowered.com/steamcommunity/public/images/apps/201790/c345d9b205f349f0e7f4e6cdf8af4d0b7d242505.jpg</logo>
            <storeLink>http://steamcommunity.com/app/201790</storeLink>
            <hoursLast2Weeks>2.2</hoursLast2Weeks><hoursOnRecord>14.3</hoursOnRecord>
            <statsLink>http://steamcommunity.com/id/niteshade/stats/201790</statsLink>
            <globalStatsLink>http://steamcommunity.com/stats/201790/achievements/</globalStatsLink>
        </game>
        <game>
            <appID>113200</appID>
            <name>The Binding of Isaac</name>
            <logo>http://media.steampowered.com/steamcommunity/public/images/apps/113200/d9a7ee7e07dffed1700cb8b3b9482105b88cc5b5.jpg</logo>
            <storeLink>http://steamcommunity.com/app/113200</storeLink>
            <hoursLast2Weeks>0.2</hoursLast2Weeks>
            <hoursOnRecord>22.8</hoursOnRecord>
            <statsLink>http://steamcommunity.com/id/niteshade/stats/BindingOfIsaac</statsLink>
            <globalStatsLink>http://steamcommunity.com/stats/BindingOfIsaac/achievements/</globalStatsLink>
        </game>
        <game>
            <appID>19680</appID>
            <name>Alice: Madness Returns</name>
            <logo>http://media.steampowered.com/steamcommunity/public/images/apps/19680/16eb0cc15cde07377c0cb3bffa6d92bbc6dd72b2.jpg</logo>
            <storeLink>http://steamcommunity.com/app/19680</storeLink>
        </game>
    </games>

api.steampowered.com我得到这个:

{
    "response": {
        "players": [
            {
                "steamid": "12345",
                "communityvisibilitystate": 3,
                "profilestate": 1,
                "personaname": "aSteamID",
                "lastlogoff": 1351676021,
                "profileurl": "http:\/\/steamcommunity.com\/id\/aSteamID\/",
                "avatar": "http:\/\/media.steampowered.com\/steamcommunity\/public\/images\/avatars\/b2\/b261f66a17bfa6c95b24f8b4c6b58bb3776d57e4.jpg",
                "avatarmedium": "http:\/\/media.steampowered.com\/steamcommunity\/public\/images\/avatars\/b2\/b261f66a17bfa6c95b24f8b4c6b58bb3776d57e4_medium.jpg",
                "avatarfull": "http:\/\/media.steampowered.com\/steamcommunity\/public\/images\/avatars\/b2\/b261f66a17bfa6c95b24f8b4c6b58bb3776d57e4_full.jpg",
                "personastate": 4,
                "realname": "Real Name",
                "primaryclanid": "103582791429705688",
                "timecreated": 1250966723,
                "loccountrycode": "GB"
            }
        ]       
    }
}

基本上,为了能够从中获得任何东西api.steampowered.com,我需要一个 Steam ID。为此,我调用了steamcommunity.com网站,该网站返回一个带有 Steam ID 的 XML 文件,等等。我需要的信息如下(使用 XML/JSON 响应中的名称):

播放器

  • 蒸汽机
  • 人名
  • 个人资料网址
  • 朋友(我相信需要另一个 API 调用)
  • 人格化
  • 游戏总数
  • 阿凡达全

游戏

  • 应用ID
  • 姓名
  • 标识
  • hoursLast2Weeks

对于游戏总数,我想我只需<game>计算steamcommunity.com. 它起作用了,因为我能够计算它们,但我似乎无法将totalgames字段添加到来自api.steampowered.com. 其次,并非所有<game>节点都有<hoursLast2Weeks>子节点,我只想要那些有子节点的。第三,我只想得到玩家的朋友总数。我不是在开玩笑,我知道我必须使用另一个GET,但问题是将它添加到 JSON 中。

我的主要问题是尝试添加到 JSON 响应中,我查看了 SO 上的其他示例,尝试关注它们,但我不确定我在哪里出错了。任何帮助将不胜感激。

4

2 回答 2

5

更改 json 数据非常简单:

jsondata = json.loads(json_input_string)
jsondata['newkey'] = new_value
json_output_string = json.dumps(jsondata)
于 2012-11-01T15:39:26.500 回答
1

为了满足添加totalgamesplayers响应中的每个元素:

import json
response = '{"response": {"players": [{"steamid": "12345","communityvisibilitystate": 3,"profilestate": 1,"personaname": "aSteamID","lastlogoff": 1351676021,"profileurl": "http:\/\/steamcommunity.com\/id\/aSteamID\/","avatar": "http:\/\/media.steampowered.com\/steamcommunity\/public\/images\/avatars\/b2\/b261f66a17bfa6c95b24f8b4c6b58bb3776d57e4.jpg","avatarmedium": "http:\/\/media.steampowered.com\/steamcommunity\/public\/images\/avatars\/b2\/b261f66a17bfa6c95b24f8b4c6b58bb3776d57e4_medium.jpg","avatarfull": "http:\/\/media.steampowered.com\/steamcommunity\/public\/images\/avatars\/b2\/b261f66a17bfa6c95b24f8b4c6b58bb3776d57e4_full.jpg","personastate": 4,"realname": "Real Name","primaryclanid": "103582791429705688","timecreated": 1250966723,"loccountrycode": "GB"}]}}'
data = json.loads(response)
for player in data["response"]["players"]:
    # add code to get game count by player here                  
    # set to zero for this example
    totalgames = 0
    player["totalgames"] = totalgames

print json.dumps(data, sort_keys=True, indent=4)
于 2012-11-01T16:39:50.800 回答