The Xbox Connection – Part 2

Welcome back!

In the last article, I introduced you to the Xbox API that you can use to gather information on Xbox users (if you know the gamertag…hopefully, they are your friends) or your own profile information.  Today, I am going to continue showing you some more of the API that you can use and how Powershell can help you get the information.

This article will focus on the “friends” and “games” components of the API.  I will continue to use the XML format of return data, but you can easily switch to a JSON response, which is the default format if you don’t specify one in the API call.

The API calls will look very similar to what I used last time, but the difference will be that instead of “profile,” we will use the “friends” or “games” keywords.

$friends = [xml](Invoke-WebRequest -URI https://xboxapi.com/xml/friends/jth1205)
$games = [xml](Invoke-WebRequest -URI https://xboxapi.com/xml/games/jth1205)

In this code sample, I have executed both requests and stuffed these into respective variables so that we can play around with the data without fear of exceeding our API Call Limit for the hour (though, it would be difficult with what we are doing here since the limit is 250 calls per hour).  As you can see, we have the data now in the variables.  So far, it doesn’t seem to be much different than the way we started in the last article.

friends_games

To start, we will look at the friend data…

friendlistAs you can see there is a lot of data to look at.  You can get your friends’ Gamertag, Gamerscore, and their presence data.  There is a date field that can be converted to get the last time online for a friend, but I have not developed a good way to convert that number.  Perhaps that will be a project for another day.  For those interested, it is the Unix Epoch time.

Why would someone want to gather this information?  Well, you as an individual user may not, but perhaps you are running a competition among friends, and you need to pull a report of current gamer scores.  Powershell allows you to gather this information quickly and easily.  Watch the magic!

$friends.Data.Friends | select-object GamerTag, GamerScore | sort-object GamerScore -Descending | format-table -AutoSize

And the results:
gamerscore

Wait a minute!  I thought the scores were supposed to be sorted!  As it turns out, the data is read in as text, and the sorting is done against the text…not the data as numbers.  We will have to modify our command a bit to get the information the way we want it.

$friends.Data.Friends | select-object GamerTag, @{L="Score";E={$_.GamerScore -as [Int32]}} | sort-object Score -Descending | format-table -AutoSize

Using a technique called a “calculated property,” we have told Powershell to read the data as an Int32 data type, and now it can sort properly.

sortedscoreNow, what can we do with the games information?  Let’s take a look!

gameslistThere is a lot of information here, and even some more hidden information!  You can see the game ID, the name, the possible score and achievements, and additional properties to get your progress and achievements.  My favorite game now is Halo 4, so I will concentrate on that and it is also the first item, which will make getting the information a little easier.

Since most of these properties are collections, you can index into them.  For instance, the first item (index 0) is the last game I played, which of course is Halo 4.  If I want to see my current progress on this game, I can access it with the following command:

$games.Data.Games[0].Progress

firstgameSo you can see my current score for this game and the number of achievements that I have gathered.  I haven’t proven this, but I believe this score is the amount of your gamer score that comes from this particular game.

So, how could you use this information?  Well, let’s say that I want to see possible and current score and achievements for my last five games.  I could use the following command to gather that information:

$games.Data.Games | select-object -first 5 -property Name, PossibleScore, @{L="ActualScore";E={$_.Progress.Score}}, PossibleAchievements, @{L="ActualAchievements";E={$_.Progress.Achievements}} format-table -autosize

fivegameslistNow I have the information in a nice table so I can see what games I need to work on.

If you look back at the list of games, you can see another property, which is the AchievementInfo property.  This is a pre-built URL to get information on your specific achievements on a particular game.  I will present that information in the last article in this series.  You can cut & paste the information from this link and use it with the API calls until then, but I will show you how to extract this without having to cut & paste.

Advertisements
This entry was posted in Xbox 360, XML and tagged , , , , , , . Bookmark the permalink.

One Response to The Xbox Connection – Part 2

  1. Pingback: Building an Xbox Live Widget using PowerShell | Learn Powershell | Achieve More

Comments are closed.