-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsteam.go
More file actions
152 lines (120 loc) · 3.83 KB
/
steam.go
File metadata and controls
152 lines (120 loc) · 3.83 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
// Steam API Documentation: https://lab.xpaw.me/steam_api_documentation.html#ISteamRemoteStorage_GetCollectionDetails_v1
const CollectionUrl string = "https://api.steampowered.com/ISteamRemoteStorage/GetCollectionDetails/v1/"
const FileDetailsUrl string = "https://api.steampowered.com/ISteamRemoteStorage/GetPublishedFileDetails/v1/"
type WorkshopFile struct {
Id int
Name string
}
func requestSteam(data url.Values, url string) (*http.Response, error) {
data.Set("format", "json")
response, err := http.PostForm(url, data)
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("invalid http status: %v", response.StatusCode)
} else {
return nil, fmt.Errorf("invalid http status: %v\n body: %v", response.StatusCode, string(body))
}
}
return response, nil
}
func requestCollection(apiKey string, collectionId int) ([]int, error) {
data := url.Values{}
data.Set("key", apiKey)
data.Set("collectioncount", "1")
data.Set("publishedfileids[0]", strconv.Itoa(collectionId))
response, err := requestSteam(data, CollectionUrl)
if err != nil {
return nil, err
}
return parseCollections(&response.Body, collectionId)
}
func requestFileDetails(apiKey string, fileIds []int) ([]WorkshopFile, error) {
data := url.Values{}
data.Set("key", apiKey)
data.Set("itemcount", strconv.Itoa(len(fileIds)))
for key, fileId := range fileIds {
data.Set("publishedfileids["+strconv.Itoa(key)+"]", strconv.Itoa(fileId))
}
response, err := requestSteam(data, FileDetailsUrl)
if err != nil {
return nil, err
}
return parseFileDetails(&response.Body)
}
func parseSteamResp(stream *io.ReadCloser, category string) ([]interface{}, error) {
jsonDec := json.NewDecoder(*stream)
jsonCon := new(map[string]interface{})
err := jsonDec.Decode(&jsonCon)
if err != nil {
return nil, err
}
response := (*jsonCon)["response"].(map[string]interface{})
resultCount := response["resultcount"].(float64)
if resultCount <= 0 {
return nil, fmt.Errorf("zero collection count")
}
details := response[category+"details"].([]interface{})
return details, nil
}
func parseCollections(stream *io.ReadCloser, wantedCollectionId int) ([]int, error) {
collections, err := parseSteamResp(stream, "collection")
if err != nil {
return nil, err
}
collection := collections[0].(map[string]interface{})
collectionId, err := strconv.Atoi(collection["publishedfileid"].(string))
if err != nil {
return nil, fmt.Errorf("collection id isn't a int: %v", collection["publishedfileid"])
} else if collectionId != wantedCollectionId {
return nil, fmt.Errorf("wanted and given collections ids don't match: %v / %v", wantedCollectionId, collectionId)
}
children := collection["children"].([]interface{})
fileIds := make([]int, len(children))
for key, child := range children {
childMap := child.(map[string]interface{})
fileIdStr := childMap["publishedfileid"].(string)
i, err := strconv.Atoi(fileIdStr)
if err != nil {
fmt.Printf("error while reading: %v is not a int", fileIdStr)
i = -1
}
fileIds[key] = i
}
return fileIds, nil
}
func parseFileDetails(stream *io.ReadCloser) ([]WorkshopFile, error) {
publishedFiles, err := parseSteamResp(stream, "publishedfile")
if err != nil {
return nil, err
}
workFiles := make([]WorkshopFile, len(publishedFiles))
for key, pubFile := range publishedFiles {
pubMap := pubFile.(map[string]interface{})
fileIdStr := pubMap["publishedfileid"].(string)
title := pubMap["title"].(string)
fid, err := strconv.Atoi(fileIdStr)
if err != nil {
fmt.Printf("error while reading: %v is not a int", fileIdStr)
fid = -1
}
workFiles[key] = WorkshopFile{
Id: fid,
Name: title,
}
}
return workFiles, nil
}