-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch-api.js
316 lines (273 loc) · 8.99 KB
/
fetch-api.js
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
const axios = require('axios')
const { JSDOM } = require('jsdom')
const { getGuild } = require('./db/guilds')
require('dotenv').config()
// TODO: Add support for different country prices & languages
async function getApp(appID, guildID) {
/*const guild = await getGuild(guildID)
let language = "english"
if (guild) {
language = guild.settings.language
}*/
// TODO: Get rid of this function with getApp as replacement
const headers = {
method: 'GET',
// TODO: Get detailed_description on api instead of short_description for different languages
// Or use translator package
url: `https://store.steampowered.com/api/appdetails?appids=${appID}`
}
try {
const response = await axios.request(headers);
if (response.data[appID].success) {
return response.data[appID].data
} else {
console.log("Doesn't exist")
}
} catch (error) {
console.error(error);
}
}
async function userReviews(appID) {
const headers = {
method: 'GET',
url: `https://store.steampowered.com/appreviews/${appID}?cursor=*&json=1&filter=summary`
}
try {
const response = await axios.request(headers);
if (response.data.success) {
if (response.data.query_summary.review_score_desc == "No user reviews") {
return false
}
return response.data.reviews
}
return false
} catch (error) {
console.error(error);
}
}
async function getUser(steamID) {
const headers = {
method: 'GET',
url: `https://steamcommunity.com/actions/ajaxresolveusers?steamids=${steamID}`
}
try {
const response = await axios.request(headers);
if (response.data) {
return response.data[0]
} else {
console.log("Doesn't exist")
return false
}
} catch (error) {
console.error(error);
}
}
async function searchGame(searchQuery) {
const headers = {
method: 'GET',
url: `https://steamcommunity.com/actions/SearchApps/${searchQuery}`
}
try {
const response = await axios.request(headers);
if (response.data.length > 0) {
return response.data
} else {
console.log("Search came up with no results")
return false
}
} catch (error) {
console.error(error);
}
}
async function getGameNews(appID) {
const headers = {
method: 'GET',
url: `https://api.steampowered.com/ISteamNews/GetNewsForApp/v2/?appid=${appID}&feeds=steam_community_announcements`
}
try {
const response = await axios.request(headers);
if (response.data) {
return response.data.appnews.newsitems
} else {
console.log("Search came up with no results")
}
} catch (error) {
console.error(error);
}
}
async function getPlayerCount(appID) {
const headers = {
method: 'GET',
url: `https://api.steampowered.com/ISteamUserStats/GetNumberOfCurrentPlayers/v1/?key=${process.env.STEAM_TOKEN}&appid=${appID}`
}
try {
const response = await axios.request(headers)
if (response.data) {
return response.data.response.player_count
}
} catch (error) {
console.error(error)
return false
}
}
async function getUserBadges(steamID) {
const headers = {
method: 'GET',
url: `https://api.steampowered.com/IPlayerService/GetBadges/v1/?key=${process.env.STEAM_TOKEN}&steamid=${steamID}`
}
try {
const response = await axios.request(headers)
if (response.data.response) {
return response.data.response
} else {
return false
}
} catch (error) {
console.error(error)
return false
}
}
async function resolveVanityURL(vanity) {
const headers = {
method: 'GET',
url: `https://api.steampowered.com/ISteamUser/ResolveVanityURL/v1/?key=${process.env.STEAM_TOKEN}&vanityurl=${vanity}`
}
try {
const response = await axios.request(headers)
if (response.data) {
return response.data.response.steamid
}
} catch (error) {
console.error(error)
}
}
async function searchUser(username) {
// Get a session ID to make AJAX request later
const headers = {
url: 'https://steamcommunity.com/'
}
let sessionID = null
try {
const response = await axios.request(headers)
// TODO: Find better way to get sessionID from headers
sessionID = response.headers['set-cookie'][0].split('=').join(";").split(';')[1]
} catch (error) {
console.error(error)
}
if (sessionID) {
const headers = {
method: 'GET',
url: `https://steamcommunity.com/search/SearchCommunityAjax?text=${username}&filter=users&sessionid=${sessionID}&page=1`,
headers: {
// Set sessionid cookie to authenticate to steam
Cookie: `sessionid=${sessionID}`
}
}
try {
let ids = []
const response = await axios.request(headers)
if (response.data.success) {
// Create HTML parser to get users from response HTML
const parser = new JSDOM(response.data.html).window.document
for (let i = 0; i < (Math.min(response.data.search_result_count, 20)); i++) {
let url = parser.getElementsByClassName("searchPersonaName").item(i).getAttribute("href").split('/').slice(-2)
if (url[0] == "id") {
url[1] = await resolveVanityURL(url[1])
}
ids.push(url[1])
}
return ids
}
} catch (error) {
console.log(error)
}
}
}
function sortGames(property) {
return function(a,b) {
if(a[property] > b[property]) {
return 1
} else if (a[property] < b[property]) {
return -1
}
return 0
}
}
async function getUserGames(steamID) {
const headers = {
method: 'GET',
url: `https://api.steampowered.com/IPlayerService/GetOwnedGames/v1/?key=${process.env.STEAM_TOKEN}&steamid=${steamID}&include_appinfo=true&include_played_free_games=true`
}
try {
const response = await axios.request(headers)
if (response.data.response.game_count) {
response.data.response.games = response.data.response.games.sort(sortGames("playtime_forever"))
return response.data.response
} else {
console.log("User profile private")
return false
}
} catch (error) {
console.error(error)
}
}
async function getAppOverviews(appIDs) {
// TODO: Find api limits for amount of appids that can be queried at once
const query = appIDs.join(',')
const headers = {
method: 'GET',
url: `https://store.steampowered.com/api/appdetails/?appids=${query}&filters=price_overview`
}
try {
const response = await axios.request(headers)
if (response.data) {
return response.data
} else {
console.log("No results")
return false
}
} catch (error) {
console.error(error)
}
}
async function getTopConcurrentPlayers() {
const headers = {
method: 'GET',
url: `https://api.steampowered.com/ISteamChartsService/GetGamesByConcurrentPlayers/v1/?key=${process.env.STEAM_TOKEN}`
}
try {
const response = await axios.request(headers)
if (response.data) {
return response.data.response
} else {
return false
}
} catch (error) {
console.error(error)
}
}
async function getAppList(appIDs) {
const headers = {
method: 'GET',
// TODO: Look into what price_change_number is (could prove to be useful for finding sales)
url: `https://api.steampowered.com/IStoreService/GetAppList/v1/?key=${process.env.STEAM_TOKEN}&max_results=50000&include_games=true&include_software=true`
}
try {
const result = []
while (true) {
const response = await axios.request(headers)
if (response.data) {
const data = response.data.response
result.push(data.apps)
if (!data.have_more_results) {
break
}
headers.url = `https://api.steampowered.com/IStoreService/GetAppList/v1/?key=${process.env.STEAM_TOKEN}&max_results=50000&last_appid=${data.last_appid}&include_games=true&include_software=true`
}
}
return result
} catch(error) {
console.error(error)
}
}
module.exports = { getApp, userReviews, getUser, searchGame, getGameNews, getPlayerCount, getUserGames, searchUser, getUserBadges, getAppOverviews, getTopConcurrentPlayers, getAppList }