-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathupdater.js
More file actions
198 lines (165 loc) · 5.01 KB
/
updater.js
File metadata and controls
198 lines (165 loc) · 5.01 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
var request = require('request');
var fs = require('fs');
// define constants
var WEEK_SECONDS = 604800;
var MONTH_SECONDS = 2592000;
var options = {};
const mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/itemdb');
const Prices = require('./models/Prices');
const Keys = require('./models/Keys');
const History = require('./models/History');
// get the options from `options.json`
try {
options = JSON.parse(fs.readFileSync('options.json'));
} catch (err) {
throw err;
}
////////////////////////////////////////////////////
// Update the prices of all items in the database //
////////////////////////////////////////////////////
function refreshPrices() {
var current = Math.floor(Date.now() / 1000);
Prices.find({
lastupdate: {$lt:(parseInt(current-options.update_time))}
}, (err, prices) => {
if(err) {
throw err;
}
if(prices.length > 0) {
var time = Math.floor(Date.now() / 1000);
prices.forEach(function(item) {
Prices.update( {item: item.item}, {$set: {lastupdate: current}}, (err, response) => {
if(err) {
throw err;
}
});
request('http://steamcommunity.com/market/priceoverview/?country=US¤cy=1&appid=730&market_hash_name=' + encodeURIComponent(item.item), function (error, response, body) {
if(error) {
throw error;
}
var json = null;
if (body.indexOf("success") > 0) {
json = JSON.parse(body);
}
if (!error && response.statusCode === 200 && json.lowest_price !== undefined) {
time = Math.floor(Date.now() / 1000);
Prices.update( {item:item.item}, {$set: {current_price:parseFloat(json.lowest_price.replace('$', '')).toString()} }, (err, response) => {
if(err) {
throw err;
}
});
const a = new History({
item: item.item,
current_price: parseFloat(json.lowest_price.replace('$', '')).toString(),
time: time.toString()
});
a.save((err, response) => {
if (err) {
throw err;
} else {
console.log('Succesfully updated ' + item.item + ' w/ ' + json.lowest_price);
}
});
} else {
console.log('Attempting to use CSGOFAST-API for '+ item.item);
request('https://api.csgofast.com/price/all', function(error, response, body) {
var json = '';
try {
json = JSON.parse(body);
} catch (e) {
console.log(item.item + " not found in the Steam Market");
return;
}
var current = Math.floor(Date.now() / 1000);
if (!error && response.statusCode === 200 && (item.item in json)) {
Prices.update( {item:item.item}, {$set: {current_price:json[item.item].toString().replace('$', '')} }, (err, response) => {
if(err) {
throw err;
}
});
const a = new History({
item: item.item,
current_price: json[item.item].toString().replace('$', ''),
time: time.toString()
});
a.save((err, response) => {
if (err) {
throw err;
} else {
console.log('Succesfully updated ' + item.item + ' w/ ' + json[item.item].toString().replace('$', ''));
}
});
} else {
console.log('An error occured receiving price for item: ' + item.item);
}
});
}
});
setTimeout(function() {
time = Math.floor(Date.now() / 1000);
// Update Weekly Price
History.find({
item: item.item,
time: {$lt:(time - WEEK_SECONDS)}
}, (err, his) => {
if(err) {
throw err;
}
var total = 0;
var num = 0;
his.forEach(function(item) {
total += parseFloat(item.price);
num++;
});
if (!isNaN(total/num) && num !== 0) {
Prices.update( {item: item}, {$set: {avg_week_price: (total/num).toFixed(2).toString()} }, (err, response) => {
if(err) {
throw err;
}
});
}
});
// Update Monthly Price
History.find({
item: item.item,
time: {$lt:(time - MONTH_SECONDS)}
}, (err, his) => {
if(err) {
throw err;
}
var total = 0;
var num = 0;
his.forEach(function(item) {
total += parseFloat(item.price);
num++;
});
if (!isNaN(total/num) && num !== 0) {
Prices.update( {item: item}, {$set: {avg_month_price: (total/num).toFixed(2).toString()} }, (err, response) => {
if(err) {
throw err;
}
});
}
});
}, 10000);
});
}
});
}
///////////////////////////////////////////////////////////////
// Delete all rows from `price_history` older than one month //
///////////////////////////////////////////////////////////////
function deleteOld() {
var time = Math.floor(Date.now() / 1000);
History.remove({
"time":{$lte:(time - MONTH_SECONDS).toString()}
}, (err, response) => {
if(err) {
throw err;
}
});
}
setTimeout(refreshPrices, 1000);
setInterval(refreshPrices, options.refresh_interval);
setInterval(deleteOld, options.delete_old_interval);