This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
steam.go
80 lines (73 loc) · 1.64 KB
/
steam.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Package steam provides interface to some parts of Steam Web API.
package steam
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
)
const (
scheme = "http" // https or http
host = "api.steampowered.com"
)
// GetUserCount returns current number of users for a specified application.
func GetUserCount(appID int) (int, error) {
url := scheme + "://" + host + "/ISteamUserStats/GetNumberOfCurrentPlayers/v1/?appid=" + strconv.Itoa(appID)
resp, err := http.Get(url)
if err != nil {
return 0, err
}
if resp.StatusCode != 200 {
return 0, errors.New(fmt.Sprintf("Request to %s failed (%s)!", url, resp.Status))
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return 0, err
}
// Types for parsing
type response struct {
Result int
Player_count int
}
type jason struct {
Response response
}
respParsed := jason{}
err = json.Unmarshal(body, &respParsed)
if err != nil {
return 0, err
}
return respParsed.Response.Player_count, nil
}
type App struct {
ID int `json:"appid"`
Name string `json:"name"`
}
// GetApps returns slice of all application that are available on Steam platform.
func GetApps() ([]App, error) {
resp, err := http.Get(scheme + "://" + host + "/ISteamApps/GetAppList/v2/")
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
// Types for parsing
type appList struct {
Apps []App
}
type jason struct {
Applist appList
}
respParsed := jason{}
err = json.Unmarshal(body, &respParsed)
if err != nil {
return nil, err
}
return respParsed.Applist.Apps, nil
}