-
Notifications
You must be signed in to change notification settings - Fork 0
/
equippedProfileItems.go
216 lines (200 loc) · 7.96 KB
/
equippedProfileItems.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
"strconv"
"strings"
"time"
)
type itemData struct {
ItemName string `json:"item_name"`
ItemTitle string `json:"item_title"`
ItemDescription string `json:"item_description"`
ItemImageLarge string `json:"item_image_large"`
ItemImageSmall string `json:"item_image_small"`
ItemMovieWebm string `json:"item_movie_webm"`
ItemMovieMp4 string `json:"item_movie_mp4"`
ItemMovieWebmSmall string `json:"item_movie_webm_small"`
ItemMovieMp4Small string `json:"item_movie_mp4_small"`
Animated bool `json:"animated"`
}
type itemDefinition struct {
Appid int `json:"appid"`
Defid int `json:"defid"`
Type int `json:"type"`
CommunityItemClass int `json:"community_item_class"`
CommunityItemType int `json:"community_item_type"`
PointCost string `json:"point_cost"`
TimestampCreated int `json:"timestamp_created"`
TimestampUpdated int `json:"timestamp_updated"`
TimestampAvailable int `json:"timestamp_available"`
TimestampAvailableEnd int `json:"timestamp_available_end"`
Quantity string `json:"quantity"`
InternalDescription string `json:"internal_description"`
Active bool `json:"active"`
CommunityItemData itemData `json:"community_item_data"`
UsableDuration int `json:"usable_duration"`
BundleDiscount int `json:"bundle_discount"`
BundleDefids []int `json:"bundle_defids"`
}
type equippedItemsResponse struct {
ActiveDefinitions []itemDefinition `json:"active_definitions"`
InactiveDefinitions []itemDefinition `json:"inactive_definitions"`
}
type equippedItemsGlobalResponse struct {
Response equippedItemsResponse `json:"response"`
}
type EquippedItem struct {
Appid int `json:"appid"`
CommunityItemClass int `json:"community_item_class"`
ItemName string `json:"item_name"`
ItemTitle string `json:"item_title"`
PointCost string `json:"point_cost"`
ItemDescription string `json:"item_description"`
Active bool `json:"active"`
InternalDescription string `json:"internal_description"`
Animated bool `json:"animated"`
IsActiveDefinition bool `json:"is_active_definition"`
ItemImageURI string `json:"item_image_uri"`
ItemPointsURI string `json:"item_points_uri"`
ItemMarketURI string `json:"item_market_uri"`
ItemMarketID int `json:"item_market_id"`
ItemMarketPrice string `json:"item_market_price"`
}
func GetEquippedItems(steam64ID string, language string) ([]EquippedItem, error) {
res, err := http.Get(fmt.Sprintf("https://api.steampowered.com/ILoyaltyRewardsService/GetEquippedProfileItems/v1?steamid=%s&language=%s", steam64ID, language))
if err != nil {
return nil, err
}
defer res.Body.Close()
var response equippedItemsGlobalResponse
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
activeItems := make([]EquippedItem, len(response.Response.ActiveDefinitions))
inactiveItems := make([]EquippedItem, len(response.Response.InactiveDefinitions))
eqItemCh := make(chan EquippedItem, len(activeItems)+len(inactiveItems))
defer close(eqItemCh)
for _, item := range response.Response.ActiveDefinitions {
go func(item itemDefinition, isActive bool) {
eqItemCh <- createEquippedItemFromDefinition(item, true)
}(item, true)
}
for _, item := range response.Response.InactiveDefinitions {
go func(item itemDefinition, isActive bool) {
eqItemCh <- createEquippedItemFromDefinition(item, false)
}(item, false)
}
equippedItems := make([]EquippedItem, 0, len(activeItems)+len(inactiveItems))
for i := 0; i < len(activeItems)+len(inactiveItems); i++ {
equippedItems = append(equippedItems, <-eqItemCh)
}
return equippedItems, nil
}
func createEquippedItemFromDefinition(item itemDefinition, isActive bool) EquippedItem {
itemImageURI := ""
if item.CommunityItemData.Animated {
if item.CommunityItemData.ItemMovieMp4 != "" {
itemImageURI = item.CommunityItemData.ItemMovieMp4
} else if item.CommunityItemData.ItemMovieWebm != "" {
itemImageURI = item.CommunityItemData.ItemMovieWebm
} else {
itemImageURI = item.CommunityItemData.ItemImageSmall
}
} else {
itemImageURI = item.CommunityItemData.ItemImageLarge
}
return EquippedItem{
Appid: item.Appid,
CommunityItemClass: item.CommunityItemClass,
ItemName: item.CommunityItemData.ItemName,
ItemTitle: item.CommunityItemData.ItemTitle,
PointCost: item.PointCost,
ItemDescription: item.CommunityItemData.ItemDescription,
Active: item.Active,
InternalDescription: item.InternalDescription,
Animated: item.CommunityItemData.Animated,
IsActiveDefinition: isActive,
ItemImageURI: itemImageURI,
ItemPointsURI: fmt.Sprintf("https://store.steampowered.com/points/shop/app/%d/reward/%d/", item.Appid, item.Defid),
ItemMarketURI: "",
ItemMarketID: 0,
}
}
func addMarketURIToEquippedItems(equippedItems []EquippedItem, currency int) []EquippedItem {
eqItemCh := make(chan EquippedItem, len(equippedItems))
for i := range equippedItems {
go func(item EquippedItem) {
if item.ItemMarketURI == "" {
itemMarketURI := fmt.Sprintf("https://steamcommunity.com/market/listings/753/%d-%s", item.Appid, strings.ReplaceAll(item.ItemName, " ", "%20"))
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Get(itemMarketURI)
if err != nil {
fmt.Printf("Error getting market URI for item %s: %v", item.ItemName, err)
item.ItemMarketURI = ""
} else {
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error reading response body for item %s: %v", item.ItemName, err)
item.ItemMarketURI = ""
} else {
bodyString := string(body)
re := regexp.MustCompile(`<div id="searchResultsTable"[\s\S]*?class="market_content_block market_home_listing_table market_home_main_listing_table market_listing_table">`)
if !re.MatchString(bodyString) {
item.ItemMarketURI = itemMarketURI
reNum := regexp.MustCompile(`Market_LoadOrderSpread\(\s*(\d+)\s*\);`)
match := reNum.FindStringSubmatch(bodyString)
if len(match) > 1 {
num, err := strconv.Atoi(match[1])
if err != nil {
fmt.Printf("Error parsing number from response body for item %s: %v", item.ItemName, err)
} else {
item.ItemMarketID = num
item = putMarketPriceToEquippedItem(item, currency)
}
}
} else {
item.ItemMarketURI = ""
}
}
}
}
eqItemCh <- item
}(equippedItems[i])
}
equippedItemsWithMarketURI := make([]EquippedItem, 0, len(equippedItems))
for i := 0; i < len(equippedItems); i++ {
item := <-eqItemCh
equippedItemsWithMarketURI = append(equippedItemsWithMarketURI, item)
}
return equippedItemsWithMarketURI
}
func putMarketPriceToEquippedItem(item EquippedItem, currency int) EquippedItem {
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Get(fmt.Sprintf("https://steamcommunity.com/market/itemordershistogram?language=english¤cy=%d&item_nameid=%d", currency, item.ItemMarketID))
if err != nil {
fmt.Printf("Error making a request to steam market API for item %s: %v", item.ItemName, err)
} else {
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error reading response body for item %s: %v", item.ItemName, err)
} else {
bodyString := string(body)
regex := regexp.MustCompile(`<span class=\\"market_commodity_orders_header_promote\\">([^<]*)<\\/span>",`)
match := regex.FindStringSubmatch(bodyString)
if len(match) > 0 {
extractedString := match[1]
item.ItemMarketPrice = extractedString
} else {
fmt.Printf("No match found: %s", bodyString)
}
}
}
return item
}