Steam API Website Integration Using a Plone External Method

How to get information from your Steam profile and display it on your website using a Plone External Method, some Python and TAL

If you take a look at the home page of this site you will see the last paragraph shows you if I am online on Steam or not, contains a link to my profile and also the last game I played. This information is automatically updated should I change my profile link, play other games or go offline, so if you want to do it yourself on a Plone site... Keep reading. 

Get a Steam API Key

For this you will need a Steam account, as  you're reading this I will assume you already own one (NOTE: your privacy settings must be set to 'Public' for things you want to share otherwise the URL mentioned below will not return that information).

To get your API Key go to the Steam Community Developer site and click the 'Go To Registration Page' link at the bottom of the page and log in.

Request an API Key and take note of it once you have it, you will also have to enter the domain you are requesting the key for.

Build the URL

We are going to send a request to a Steam community URL which will return JSON data to us, we will parse the JSON and display it on the site, first we need to build the URL. The URLs tend to look a bit different depending on what information you're looking to obtain but the Steam Web API documentation has examples you can work from, just swap out the XXXXXXX's for your API Key and replace the steamid value for yours.

Get your 64 Bit Steam ID

You need to get a 64 bit version of your Steam ID to use with the API key in order to pull down the JSON. You can get this quite easily by appending ?xml=1 to the end of your Steam Community profile URL.

http://steamcommunity.com/id/plenum/?xml=1

You will see a page of XML, one of the first elements will be <steamID64>, copy the number in between those tags.

GetPlayerSummaries

To get your basic summary information (In game name, avatar, online or offline etc) we should use the following URL:

http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=XXX&steamids=XXX

Replace the 'XXX' after 'key=' with your API Key. Replace the 'XXX' after 'steamids=' with your 64 bit Steam ID

If you got to the above URL in a web browser you will get a JSON response back. We're going to go through this with Python and display the result on our Plone website.

IPlayerService

For the latest game played information (although it differs from what is displayed on your Steam community profile, other people are complaining about this). You'll need to put in this URL:

http://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v0001/?key=XXXX&steamid=XXXX

As you can see it's fairly simple to format the URLs, using the documentation you should be able to format them quite easily.

Set up the Directories for External Methods

Firstly we need to create an Extensions directory (if one doesn't exist) already in the correct folder on our Plone site.

My Plone installation is at /usr/local/plone (the default), if yours is different modify my instructions accordingly.

Go to the right place:

cd /usr/local/plone/zeocluster/parts/client1/

Make the folder

mkdir Extensions

Now if you have a zeo installation (multiple clients) you'll need to create an Extensions folder in each of them, so that we only have to update one script it is better to create symbolic links instead. So for each client you have (I only have two) you need to do the following:

cd ../client2

Make the symbolic link

ln -s ../client1/Extensions Extensions

Just update "client2" for each client number you are making the links for until you have links in all of the clients folders pointing to ../client1/Extensions.

Create the Script

We're now going to create the steam_info.py script (you may call it what you want, just remember to modify the future instructions to allow for whatever name you give it).

Now you should be in the: /usr/local/plone/zeocluster/parts/client1/Extensions directory for this next part.

Load up a new script with your favourite editor, I use joe so:

joe steam_info.py

Put this in the script (I warn you it probably isn't the most efficient method but it works):

#!/usr/bin/python
# -*- encoding: utf-8 -*-

import json
import urllib2 as ul

def steamInfo():
  steam_url = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=XXXX&steamids=XXXX'
  game_info_url = 'http://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v0001/?key=XXXX&steamid=XXXX'

  profile_info = ul.urlopen(steam_url)
  data = json.load(profile_info)

  player_summary = data['response']['players'][0]
  personastate = player_summary['personastate']

  if personastate != 0:
    personastate = 'online'
  else:
    personastate = 'offline'

  game_info = ul.urlopen(game_info_url)
  latest_games = json.load(game_info)
  latest_game = latest_games['response']['games'][0]
  latest_game_name = latest_game['name']

  profile_link = "<a href='%s' rel='nofollow' class='external-link' title='My Steam Profile'>Steam</a>" % player_summary['profileurl']
  image_link = 'http://media.steampowered.com/steamcommunity/public/images/apps/%s/%s.jpg' % (latest_game['appid'], latest_game['img_icon_url'])
  image_tag = '<img src="%s" alt="%s Icon" />' % (image_link, latest_game_name)
  wholeline = "I'm currently %s on %s, the last game I played was %s%s!" % (personastate, profile_link, image_tag, latest_game_name)

  return wholeline

Create the External Method

Now go to yoursite.com/portal_skins/custom/manage_main.

Using the drop down menu at the top right hand side of the Zope Management Interface select "External Method" and click "Add"

Give it the following details:

  • ID: steam
  • Title: Obtain Steam Info (optional)
  • Module Name: steam_info (update this to whatever you called your script, dropping the .py at the end)
  • Function Name: steamInfo 

And press 'Save'.

Render it in a Page Template

The line will read as "I'm currently online/offline on Steam, the game I played was _GAME_!" but as it includes HTML we need to tell the template to use structure instead of rendering the HTML literally in the page as a string, so add this line to your template:

<p tal:content="structure context/steam" />

And that's it!

You can modify the above script to include more or less information obviously. Either way the above information will give you exactly what you see on my home page.

There will be an update to this article soon as there is a way to show your current game playing.

If you have any questions about the steps above, feel free to contact me.