forked from charredvenge/node-backpacktf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.js
More file actions
182 lines (168 loc) · 4.89 KB
/
queries.js
File metadata and controls
182 lines (168 loc) · 4.89 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
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
var http = require("http");
var request = require("request");
//var fs = require("fs");
//var IDs = require("./values.js");
/*
backpacktf.queryAPI()
Queries the backpack.tf API with a given method and parameters
Parameters:
method: the method you are calling the API with
v: version of the method. i.e. v4
key: backpack.tf api key
format: format of the file, this should really always be json
unless debugging.
adds: any additional parameters in the method
callback: called when API responds.
Callback arguments:
data: an Object containing the response
Not included in module.exports.
*/
function queryAPI(method, v, key, format, adds, callback) {
var urltouse = "http://backpack.tf/api/" + method + "/" + v + "/?key=" + key + "&format=" + format + adds;
http.get(urltouse, function(res) {
var body = "";
res.on("data", function(chunk) {
body += chunk;
});
res.on("end", function() {
callback(JSON.parse(body));
});
});
}
/*
backpacktf.getMarketPrices()
Uses the backpack.tf api to get SCM data.
Parameters:
key: backpack.tf api key
appid: steam"s numeric identifier for the game
you want data from
callback: called when market prices are retrieved.
Callback arguments:
err: an Error object, null on success
data: an Object containing the response
*/
function getMarketPrices(key, appid, callback) {
queryAPI("IGetMarketPrices", "v4", key, "json", "&appid=" + appid, function(data) {
if (data.response.success === 0) {
callback(new Error(data.response.message));
} else {
callback(null, data);
}
});
}
/*
backpacktf.getBPPrices()
Retrieves backpack.tf price data for specified appid.
Parameters:
key: backpack.tf api key
appid: game id
callback: called when prices are recieved
Callback arguments:
err: an Error object with the reason for
failure, null on success
data: an Object containing the response
*/
function getBPPrices(key, appid, callback) {
queryAPI("IGetPrices", "v4", key, "json", "&appid=" + appid, function(data) {
if (data.response.success === 0) {
callback(new Error(data.response.message));
} else {
callback(null, data);
}
});
}
/*
backpacktf.getUser()
Retrieves backpack.tf price data for specified appid.
Parameters:
key: backpack.tf api key
steamids: list of users to retrieve data on, delimited by commas
callback: called when backpacks are retrieved
Callback Arguments:
err: an Error object with the reason for failure,
null on success
data: an Object containing the response
*/
function getUser(key, steamids, callback) {
queryAPI("IGetUsers", "v3", key, "json", "&steamids=" + steamids, function(data) {
if (data.response.success === 0) {
callback(new Error(data.response.message));
} else {
callback(null, data);
}
});
}
function getCurrencies(key, appid, callback) {
queryAPI("IGetCurrencies", "v1", key, "json", "&appid=" + appid, function(data) {
if (data.response.success === 0) {
callback(new Error(data.response.message));
} else {
callback(null, data);
}
});
}
/*
backpacktf.startAutomatic()
Tells backpack.tf that your SteamID should have the lightning icon. Cool.
Parameters:
*/
function startAutomatic(steamid, token, callback) {
var requestParams = {
uri: "http://backpack.tf/api/IAutomatic/IHeartBeat/",
form: {
method: "alive",
version: "1.0.0",
steamid,
token
},
json: true,
method: "POST"
};
request(requestParams, function(err, response, body) {
//uh yeah. probably should return a status code or something
if (err) {
callback(err);
} else if (response.statusCode !== 200) {
callback(new Error("Connection Error: HTTP Status code " + response.statusCode));
} else {
if (body.success) {
callback();
} else {
callback(new Error("Invalid Token"));
}
}
});
}
function offerAccepted(steamid, token, callback) {
var requestParams = {
uri: "http://backpack.tf/api/IAutomatic/IOfferDetails/",
form: {
method: "completed",
steamid,
version: "1.0.0",
token,
offer: 943526664, //hard-coded in, doesn't actually matter i think
message: "Yes" //same ^^
},
method: "POST"
};
request(requestParams, function(err, response, body) {
//uh yeah. probably should return a status code or something
if (err) {
callback(err);
} else if (response.statusCode !== 200) {
callback(new Error("Connection Error: HTTP Status code " + response.statusCode));
} else {
//success!
callback();
}
});
}
module.exports = {
getUser,
getCurrencies,
getBPPrices,
getMarketPrices,
startAutomatic,
offerAccepted
};