From 5c04f59ff5c3309c740a988f64a3e0d0854b7526 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Fri, 31 Jul 2015 20:32:42 +0200 Subject: [PATCH] more commands and stuff --- client.go | 139 ++++++++++++++++++++++++++++++++++++++++++ cmd/steam/commands.go | 64 +++++++++++++++++-- cmd/steam/main.go | 2 +- 3 files changed, 198 insertions(+), 7 deletions(-) diff --git a/client.go b/client.go index 5dfe61e..a5727e4 100644 --- a/client.go +++ b/client.go @@ -3,6 +3,8 @@ package steam import ( "fmt" "net/http" + "strconv" + "strings" ) // a steam API client, not tied to any particular game @@ -18,3 +20,140 @@ func (c *Client) Get(iface, method, version string) (*http.Response, error) { url := fmt.Sprintf("https://api.steampowered.com/%s/%s/%s/?key=%s", iface, method, version, c.key) return http.Get(url) } + +func (c *Client) GetFriendList(userid uint64) (*http.Response, error) { + url := fmt.Sprintf("https://api.steampowered.com/ISteamUser/GetFriendList/v1/?key=%s&steamid=%d", c.key, userid) + return http.Get(url) +} + +func (c *Client) ResolveVanityUrl(vanity string) (*http.Response, error) { + url := fmt.Sprintf("https://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=%s&vanityurl=%s", c.key, vanity) + return http.Get(url) +} + +func (c *Client) GetPlayerSummaries(steamids ...uint64) (*http.Response, error) { + if len(steamids) > 100 { + return nil, fmt.Errorf("GetPlayerSummaries accepts a max of 100 ids, saw %d", len(steamids)) + } + ids_s := make([]string, len(steamids)) + for i := range steamids { + ids_s[i] = strconv.FormatUint(steamids[i], 10) + } + ids := strings.Join(ids_s, ",") + url := fmt.Sprintf("https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s", c.key, ids) + return http.Get(url) +} + +/* + "name": "ISteamUser", + "methods": [ + { + "name": "GetPlayerBans", + "version": 1, + "httpmethod": "GET", + "parameters": [ + { + "name": "key", + "type": "string", + "optional": false, + "description": "access key" + }, + { + "name": "steamids", + "type": "string", + "optional": false, + "description": "Comma-delimited list of SteamIDs" + } + ] + + }, + { + "name": "GetPlayerSummaries", + "version": 1, + "httpmethod": "GET", + "parameters": [ + { + "name": "key", + "type": "string", + "optional": false, + "description": "access key" + }, + { + "name": "steamids", + "type": "string", + "optional": false, + "description": "Comma-delimited list of SteamIDs" + } + ] + + }, + { + "name": "GetPlayerSummaries", + "version": 2, + "httpmethod": "GET", + "parameters": [ + { + "name": "key", + "type": "string", + "optional": false, + "description": "access key" + }, + { + "name": "steamids", + "type": "string", + "optional": false, + "description": "Comma-delimited list of SteamIDs (max: 100)" + } + ] + + }, + { + "name": "GetUserGroupList", + "version": 1, + "httpmethod": "GET", + "parameters": [ + { + "name": "key", + "type": "string", + "optional": false, + "description": "access key" + }, + { + "name": "steamid", + "type": "uint64", + "optional": false, + "description": "SteamID of user" + } + ] + + }, + { + "name": "ResolveVanityURL", + "version": 1, + "httpmethod": "GET", + "parameters": [ + { + "name": "key", + "type": "string", + "optional": false, + "description": "access key" + }, + { + "name": "vanityurl", + "type": "string", + "optional": false, + "description": "The vanity URL to get a SteamID for" + }, + { + "name": "url_type", + "type": "int32", + "optional": true, + "description": "The type of vanity URL. 1 (default): Individual profile, 2: Group, 3: Official game group" + } + ] + + } + ] + + }, +*/ diff --git a/cmd/steam/commands.go b/cmd/steam/commands.go index 2b8da3f..bfec4ef 100644 --- a/cmd/steam/commands.go +++ b/cmd/steam/commands.go @@ -1,22 +1,74 @@ package main import ( + "fmt" "github.com/jordanorelli/steam" "io" "net/http" "os" + "sort" + "strconv" ) -var commands = map[string]command{ - "api-list": command{ - handler: func(c *steam.Client) { - dump(c.Get("ISteamWebAPIUtil", "GetSupportedAPIList", "v0001")) +var commands map[string]command + +func init() { + commands = map[string]command{ + "api-list": command{ + handler: func(c *steam.Client, args ...string) { + dump(c.Get("ISteamWebAPIUtil", "GetSupportedAPIList", "v0001")) + }, + }, + "user-friends": command{ + handler: func(c *steam.Client, args ...string) { + if len(args) < 1 { + bail(1, "please provide a user id") + } + userid, err := strconv.ParseUint(args[0], 10, 64) + if err != nil { + bail(1, "bad user id: %s", err) + } + dump(c.GetFriendList(userid)) + }, + }, + "user-id": command{ + handler: func(c *steam.Client, args ...string) { + dump(c.ResolveVanityUrl(args[0])) + }, }, - }, + "user-details": command{ + handler: func(c *steam.Client, args ...string) { + if len(args) < 1 { + bail(1, "please provide a user id") + } + ids := make([]uint64, 0, len(args)) + for _, arg := range args { + userid, err := strconv.ParseUint(arg, 10, 64) + if err != nil { + bail(1, "bad user id: %s", err) + } + ids = append(ids, userid) + } + dump(c.GetPlayerSummaries(ids...)) + }, + }, + "commands": command{ + handler: func(c *steam.Client, args ...string) { + keys := make([]string, 0, len(commands)) + for name, _ := range commands { + keys = append(keys, name) + } + sort.Strings(keys) + for _, key := range keys { + fmt.Println(key) + } + }, + }, + } } type command struct { - handler func(*steam.Client) + handler func(*steam.Client, ...string) } func dump(r *http.Response, e error) { diff --git a/cmd/steam/main.go b/cmd/steam/main.go index 72dce71..2a0da30 100644 --- a/cmd/steam/main.go +++ b/cmd/steam/main.go @@ -32,5 +32,5 @@ func main() { if !ok { bail(1, "no such subcommand %s", os.Args[1]) } - cmd.handler(client) + cmd.handler(client, os.Args[2:]...) }