Skip to content

Instantly share code, notes, and snippets.

@cheshire137
Created October 19, 2014 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cheshire137/ff52a940eb8a5930030f to your computer and use it in GitHub Desktop.
Save cheshire137/ff52a940eb8a5930030f to your computer and use it in GitHub Desktop.
Get Steam player details, recently played games
#!/usr/bin/env ruby
# encoding: utf-8
require 'rubygems'
require 'json'
require 'open-uri'
class SteamAPI
attr_reader :api_key, :user_id, :api_url
def initialize api_key, user_id, api_url=nil
@api_key = api_key
@user_id = user_id
@api_url = api_url || 'http://api.steampowered.com'
end
# https://developer.valvesoftware.com/wiki/Steam_Web_API#GetPlayerSummaries_.28v0002.29
def get_player_summary
url = "#@api_url/ISteamUser/GetPlayerSummaries/v0002/" +
"?key=#@api_key&steamids=#@user_id"
response = open(url).read
json = JSON.parse(response)
json['response']['players'].first
end
# https://developer.valvesoftware.com/wiki/Steam_Web_API#GetRecentlyPlayedGames_.28v0001.29
def get_recently_played_games
url = "#@api_url/IPlayerService/GetRecentlyPlayedGames/" +
"v0001/?key=#@api_key&steamid=#@user_id&format=json"
response = open(url).read
json = JSON.parse(response)
games = json['response']['games']
games.map {|game|
app_id = game['appid']
icon_url = game['img_icon_url']
if icon_url
game['img_icon_url'] = 'http://media.steampowered.com/' +
'steamcommunity/public/images/' +
"apps/#{app_id}/#{icon_url}.jpg"
end
logo_url = game['img_logo_url']
if logo_url
game['img_logo_url'] = 'http://media.steampowered.com/' +
'steamcommunity/public/images/' +
"apps/#{app_id}/#{logo_url}.jpg"
end
game['url'] = "http://store.steampowered.com/app/#{app_id}"
game
}
end
end
def print_json_object obj
print JSON.generate(obj) + "\r\n"
end
# See http://steamcommunity.com/dev/registerkey
steam_web_api_key = 'your_steam_web_api_key_here'
# Your 64-bit Steam ID--see http://steamidconverter.com/
steam64_id = '76561198025023644' # cheshire137
print "Content-type: application/json\r\n\r\n"
begin
steam = SteamAPI.new(steam_web_api_key, steam64_id)
print_json_object({
player: steam.get_player_summary,
recent_games: steam.get_recently_played_games
})
rescue => e
print_json_object({error: e.message})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment