-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·257 lines (235 loc) · 5.87 KB
/
index.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
// requires
var request = require('request');
var mkdirp = require('mkdirp');
var fs = require('fs');
// constructor
function tfprices(options) {
p.key = options.key;
if(options.currency !== undefined) {
p.currency = options.currency;
}
p.assetsDir = __dirname + "/assets/";
mkdirp(p.assetsDir, function(err) {
// path was created unless there was error
});
}
// prototype
var p = tfprices.prototype;
p.init = function(callback) {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDate();
var hour = now.getHours();
p.pricesFile = p.assetsDir + "prices_" + year + "-" + month + "-" + day + "-" + hour + ".json";
if(typeof(callback) == "function") {
callback();
}
}
/**
* Get the backpack.tf price list and build a local cache file to handle API failures. A new cache file is created every
* hour. Read from the cache file if a one exists for the current hour.
*
* @param callback
*/
p.fetchPrices = function(callback) {
if(!fs.existsSync(p.pricesFile)) {
var j = request.jar();
request = request.defaults({jar:j});
request.get({
uri: "http://backpack.tf/api/IGetPrices/v3/?names=1&format=json&key=" + p.key,
json: true
}, function(error, response, data) {
if (error) {
console.log("ERROR: " + error);
return;
}
if(JSON.stringify(data.response).length > 0 && data.response.success == 1) {
fs.writeFile(p.pricesFile, JSON.stringify(data.response), function (err) {
if (err) throw err;
p.getPricesFromFile(function(price_list) {
if(typeof(callback) == "function") {
callback(price_list);
}
});
});
} else {
p.getNewestFile(function() {
p.getPricesFromFile(function(price_list) {
if(typeof(callback) == "function") {
callback(price_list);
}
});
});
}
});
} else {
p.getPricesFromFile(function(price_list) {
if(typeof(callback) == "function") {
callback(price_list);
}
});
}
}
/**
* Get the price of a given item
*
* @param item tf2 item object - needs to have at least the defindex
* @param callback
*/
p.getItemPriceObject = function(item, callback) {
if(item.quality === undefined) {
item.quality = 6;
}
p.init(function() {
p.fetchPrices(function(price_list) {
var price_object = undefined;
if(price_list !== undefined) {
if(price_list.prices !== undefined) {
if(price_list.prices[item.defindex] !== undefined) {
price_object = price_list.prices[item.defindex];
}
if(typeof(callback) == "function") {
callback(price_object);
}
} else {
console.log(item.name + " price_list.prices is undefined");
}
} else {
console.log(item.name + " price_list is undefined");
}
});
});
}
/**
* Get the high price for item if it exists
*
* @param item
* @param callback
*/
p.getItemPriceHigh = function(item, callback) {
p.getItemPriceObject(item, function(price_object) {
var ret = undefined;
if(price_object !== undefined) {
ret = {};
if(price_object[item.quality][0].current.value_high) {
ret.price = price_object[item.quality][0].current.value_high;
} else {
ret.price = price_object[item.quality][0].current.value;
}
ret.currency = price_object[item.quality][0].current.currency;
}
if(typeof(callback) == "function") {
p.convertCurrency(ret, function(result) {
callback(result);
});
}
});
}
/**
* Get the low price for item if it exists
*
* @param item
* @param callback
*/
p.getItemPriceLow = function(item, callback) {
p.getItemPriceObject(item, function(price_object) {
var ret = undefined;
if(price_object !== undefined) {
ret = {
price: price_object[item.quality][0].current.value,
currency: price_object[item.quality][0].current.currency
}
}
if(typeof(callback) == "function") {
p.convertCurrency(ret, function(result) {
callback(result);
});
}
});
}
/**
* Get prices list from cache file
*
* @param callback
*/
p.getPricesFromFile = function(callback) {
var price_list;
if(fs.existsSync(p.pricesFile)) {
var data = fs.readFileSync(p.pricesFile, 'utf8');
if(data !== undefined && data != null) {
price_list = JSON.parse(data);
}
}
if(typeof(callback) == "function") {
callback(price_list);
}
}
p.getNewestFile = function(callback) {
var files = fs.readdirSync(p.assetsDir);
var stats;
var filetime;
var ts;
var newest = 0;
for(var i in files) {
stats = fs.statSync(p.assetsDir + files[i]);
filetime = new Date(stats.mtime);
ts = filetime.getTime();
if (ts > newest) {
newest = ts;
p.pricesFile = p.assetsDir + files[i];
}
}
if(i >= files.length - 1) {
if(typeof(callback) == "function") {
callback();
}
}
}
/**
* Get the high end value for a key (defindex 5021)
*
* @param callback
*/
p.getKeyMetal = function(callback) {
var item = {defindex: 5021};
p.getItemPriceObject(item, function(price_object) {
var ret = undefined;
if(price_object !== undefined) {
if(price_object[item.quality][0].current.value_high) {
ret = price_object[item.quality][0].current.value_high;
} else {
ret = price_object[item.quality][0].current.value;
}
}
if(typeof(callback) == "function") {
callback(ret);
}
});
}
/**
* Convert from one currency type to another. Possible currency types are:
* metal - defindex 5002
* keys - defindex 5021
* usd - will not convert to this one
*
* @param price_obj
* @param callback
*/
p.convertCurrency = function(price_obj, callback) {
if(p.currency !== undefined && price_obj.currency !== p.currency) {
//console.log("Need to convert from " + price_obj.currency + " to " + p.currency);
p.getKeyMetal(function(key_price) {
price_obj.price = price_obj.price * key_price;
price_obj.currency = p.currency;
if(typeof(callback) == "function") {
callback(price_obj);
}
});
} else {
if(typeof(callback) == "function") {
callback(price_obj);
}
}
}
module.exports = tfprices;