title: Parsing and mixing Steam API calls
body: <p>I'm using a Django environment, and I want to pull stats from Steam. However, Steam's API is annoyingly stupid in that they have about 20 different URLs, the main two I want information from are:</p>
  <ul>
  <li><a href="http://steamcommunity.com/id/STEAM_USERNAME/games?tab=all&amp;xml=1" rel="nofollow noreferrer">http://steamcommunity.com/id/STEAM_USERNAME/games?tab=all&amp;xml=1</a></li>
  <li><a href="http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=KEY&amp;steamids=STEAM_ID" rel="nofollow noreferrer">http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=KEY&amp;steamids=STEAM_ID</a></li>
  </ul>
  <p>I'm using the Python Requests library to <code>GET</code> the data from Steam.</p>
  <pre><code>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&amp;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})
  </code></pre>
  <p>When I query <code>steamcommunity.com</code> I get something like this back:</p>
  <pre><code>&lt;gamesList&gt;
      &lt;steamID64&gt;12345&lt;/steamID64&gt;
      &lt;steamID&gt;aSteamID&lt;/steamID&gt;
      &lt;games&gt;
          &lt;game&gt;
              &lt;appID&gt;201790&lt;/appID&gt;
              &lt;name&gt;Orcs Must Die! 2&lt;/name&gt;
              &lt;logo&gt;http://media.steampowered.com/steamcommunity/public/images/apps/201790/c345d9b205f349f0e7f4e6cdf8af4d0b7d242505.jpg&lt;/logo&gt;
              &lt;storeLink&gt;http://steamcommunity.com/app/201790&lt;/storeLink&gt;
              &lt;hoursLast2Weeks&gt;2.2&lt;/hoursLast2Weeks&gt;&lt;hoursOnRecord&gt;14.3&lt;/hoursOnRecord&gt;
              &lt;statsLink&gt;http://steamcommunity.com/id/niteshade/stats/201790&lt;/statsLink&gt;
              &lt;globalStatsLink&gt;http://steamcommunity.com/stats/201790/achievements/&lt;/globalStatsLink&gt;
          &lt;/game&gt;
          &lt;game&gt;
              &lt;appID&gt;113200&lt;/appID&gt;
              &lt;name&gt;The Binding of Isaac&lt;/name&gt;
              &lt;logo&gt;http://media.steampowered.com/steamcommunity/public/images/apps/113200/d9a7ee7e07dffed1700cb8b3b9482105b88cc5b5.jpg&lt;/logo&gt;
              &lt;storeLink&gt;http://steamcommunity.com/app/113200&lt;/storeLink&gt;
              &lt;hoursLast2Weeks&gt;0.2&lt;/hoursLast2Weeks&gt;
              &lt;hoursOnRecord&gt;22.8&lt;/hoursOnRecord&gt;
              &lt;statsLink&gt;http://steamcommunity.com/id/niteshade/stats/BindingOfIsaac&lt;/statsLink&gt;
              &lt;globalStatsLink&gt;http://steamcommunity.com/stats/BindingOfIsaac/achievements/&lt;/globalStatsLink&gt;
          &lt;/game&gt;
          &lt;game&gt;
              &lt;appID&gt;19680&lt;/appID&gt;
              &lt;name&gt;Alice: Madness Returns&lt;/name&gt;
              &lt;logo&gt;http://media.steampowered.com/steamcommunity/public/images/apps/19680/16eb0cc15cde07377c0cb3bffa6d92bbc6dd72b2.jpg&lt;/logo&gt;
              &lt;storeLink&gt;http://steamcommunity.com/app/19680&lt;/storeLink&gt;
          &lt;/game&gt;
      &lt;/games&gt;
  </code></pre>
  
  <p>And from <code>api.steampowered.com</code> I get this:</p>
  <pre><code>{
      &quot;response&quot;: {
          &quot;players&quot;: [
              {
                  &quot;steamid&quot;: &quot;12345&quot;,
                  &quot;communityvisibilitystate&quot;: 3,
                  &quot;profilestate&quot;: 1,
                  &quot;personaname&quot;: &quot;aSteamID&quot;,
                  &quot;lastlogoff&quot;: 1351676021,
                  &quot;profileurl&quot;: &quot;http:\/\/steamcommunity.com\/id\/aSteamID\/&quot;,
                  &quot;avatar&quot;: &quot;http:\/\/media.steampowered.com\/steamcommunity\/public\/images\/avatars\/b2\/b261f66a17bfa6c95b24f8b4c6b58bb3776d57e4.jpg&quot;,
                  &quot;avatarmedium&quot;: &quot;http:\/\/media.steampowered.com\/steamcommunity\/public\/images\/avatars\/b2\/b261f66a17bfa6c95b24f8b4c6b58bb3776d57e4_medium.jpg&quot;,
                  &quot;avatarfull&quot;: &quot;http:\/\/media.steampowered.com\/steamcommunity\/public\/images\/avatars\/b2\/b261f66a17bfa6c95b24f8b4c6b58bb3776d57e4_full.jpg&quot;,
                  &quot;personastate&quot;: 4,
                  &quot;realname&quot;: &quot;Real Name&quot;,
                  &quot;primaryclanid&quot;: &quot;103582791429705688&quot;,
                  &quot;timecreated&quot;: 1250966723,
                  &quot;loccountrycode&quot;: &quot;GB&quot;
              }
          ]       
      }
  }
  </code></pre>
  <p>Basically, to be able to get anything from <code>api.steampowered.com</code>, I need a Steam ID. To get that, I make the call to <code>steamcommunity.com</code> website which returns an XML file with the Steam ID, amongst other things. The information I need is as follows (using the names as in the XML/JSON responses):</p>
  <h3>Player</h3>
  <ul>
  <li>Steamid</li>
  <li>Personaname</li>
  <li>ProfileURL</li>
  <li>Friends (Requires another API call, I believe)</li>
  <li>Personastate</li>
  <li>Total number of games</li>
  <li>Avatarfull</li>
  </ul>
  <h3>Games</h3>
  <ul>
  <li>AppID</li>
  <li>Name</li>
  <li>Logo</li>
  <li>hoursLast2Weeks</li>
  </ul>
  <p>For total number of games, I thought I'd simply just count the number of <code>&lt;game&gt;</code> nodes in the XML file returned by <code>steamcommunity.com</code>. It worked, in that I was able to count them, but I can't seem to add a <code>totalgames</code> field to the JSON response from <code>api.steampowered.com</code>.
  Secondly, not all <code>&lt;game&gt;</code> nodes have <code>&lt;hoursLast2Weeks&gt;</code> child nodes, I only want the ones that do.
  Thirdly, I want to just get the player's friends total. I'm not kidding myself, I know I'll have to use another <code>GET</code>, but the problem is adding it to the JSON.</p>
  <p>My main issue is trying to add to the JSON response, I've looked at other examples on SO, tried to follow them but I'm not sure where I'm going wrong here. Any help would be greatly appreciated.</p>