Skip to content

Instantly share code, notes, and snippets.

@Drowze
Last active October 5, 2017 02:06
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 Drowze/7b6aec63330d13ac91b778b4d2fc0a44 to your computer and use it in GitHub Desktop.
Save Drowze/7b6aec63330d13ac91b778b4d2fc0a44 to your computer and use it in GitHub Desktop.
run with "ruby main.rb GAME_ID" and build a simple webpage with all the not-obtained steam achievements for a game, ordered from the most popular to the less popular
require 'ostruct'
require 'erb'
STEAM_PROFILE_ID = 'YOUR_STEAM_PROFILE_ID' # https://steamid.io/
STEAM_API_KEY = 'YOUR_API_KEY' # http://steamcommunity.com/dev/apikey (enter '127.0.0.1' if you're unsure about your domain name)
OUTPUT_HTML = 'steam_achievements.html'
INPUT_ERB = 'steam_achievements.html.erb'
module Steam
require 'net/http'
require 'json'
STEAM_API_BASE_URI = 'api.steampowered.com'
GET_GLOBAL_ACHIEVEMENT_PERCENTAGE_FOR_APP_ENDPOINT = '/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/'
GET_PLAYER_ACHIEVEMENTS_ENDPOINT = '/ISteamUserStats/GetPlayerAchievements/v0001/'
GET_SCHEMA_FOR_GAME_ENDPOINT = '/ISteamUserStats/GetSchemaForGame/v2/'
class Client
def initialize(api_key:)
@api_key = api_key
end
def schema_for_game(game_id:)
get("#{GET_SCHEMA_FOR_GAME_ENDPOINT}?appid=#{game_id}&key=#{@api_key}&format=json&l=en")
end
def all_achievements_for_game(game_id:)
get("#{GET_GLOBAL_ACHIEVEMENT_PERCENTAGE_FOR_APP_ENDPOINT}?gameid=#{game_id}")
end
def player_achievements(game_id:, profile_id:)
get("#{GET_PLAYER_ACHIEVEMENTS_ENDPOINT}?appid=#{game_id}&key=#{@api_key}&steamid=#{profile_id}&l=en")
end
private
def get(endpoint)
response = Net::HTTP.get_response(STEAM_API_BASE_URI, endpoint)
if response.kind_of?(Net::HTTPSuccess)
JSON.parse(response.body, object_class: OpenStruct)
else
response.error!
end
end
end
end
game_id = ARGV[0]
profile_id = STEAM_PROFILE_ID
steam_client = Steam::Client.new(api_key: STEAM_API_KEY)
all_achievements = steam_client.all_achievements_for_game(game_id: game_id).achievementpercentages.achievements
own_achievements = steam_client.player_achievements(game_id: game_id, profile_id: profile_id).playerstats.achievements
achievements_icons = steam_client.schema_for_game(game_id: game_id).game.availableGameStats.achievements
@own_achievements_with_global_percentages_and_icons = own_achievements.map do |achievement|
if achievement.achieved == 0
achievement_icon = achievements_icons.find { |a| a.name == achievement.apiname }
OpenStruct.new(
id: achievement.apiname,
name: achievement.name,
description: achievement.description,
percentage: all_achievements.find { |a| a.name == achievement.apiname }.percent,
icon: achievement_icon.icon,
incomplete_icon: achievement_icon.icongray
)
end
end.compact.sort_by(&:percentage).reverse!
template = File.read(INPUT_ERB)
result = ERB.new(template).result(binding)
File.open(OUTPUT_HTML, 'w+') { |f| f.write(result) }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<div class="container">
<% @own_achievements_with_global_percentages_and_icons.each do |achievement| %>
<div class="card col-md-4">
<img class="card-img-top" src="<%= achievement.incomplete_icon %>" /><br />
<div class="card-block">
<h4 class="card-title"><%= achievement.name %><br /></h4>
<p class="card-text">Description: <%= achievement.description %><br />
<%= achievement.percentage.round(2) %> of all players</p>
</div>
</div>
<% end %>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment