Skip to content

Commit

Permalink
Add CSGOFast pricing support (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvegter authored and andrewda committed Feb 28, 2017
1 parent 56b4ada commit 5d138e4
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 8 deletions.
2 changes: 1 addition & 1 deletion models/History.js
Expand Up @@ -4,4 +4,4 @@ module.exports = mongoose.model('History', {
item: String,
current_price: String,
time: String
});
});
2 changes: 1 addition & 1 deletion models/Prices.js
Expand Up @@ -6,4 +6,4 @@ module.exports = mongoose.model('Prices', {
avg_week_price: String,
avg_month_price: String,
lastupdate: String
});
});
5 changes: 2 additions & 3 deletions package.json
Expand Up @@ -4,11 +4,10 @@
"description": "A free API to get the most accurate CSGO prices for high tier and low tier items.",
"main": "server.js",
"dependencies": {
"body-parser": "^1.13.3",
"express": "^4.14.1",
"fs": "0.0.1-security",
"mongodb": "^2.2.24",
"mongoose": "^4.8.5",
"body-parser": "^1.13.3",
"express": "^4.14.1",
"request": "^2.53.0"
},
"engines": {
Expand Down
52 changes: 51 additions & 1 deletion server.js
Expand Up @@ -121,7 +121,57 @@ router.get('/', function(req, res) {
lastupdate: current
});
} else {
res.json({ success: false, error: options.errors.unknown_item });
console.log('Attempting to use CSGOFAST-API for '+ query.item);

request('https://api.csgofast.com/price/all', function(error, response, body) {
var json = '';

try {
json = JSON.parse(body);
} catch (e) {
res.json({ success: false, error: options.errors.unknown_item });
return;
}

var current = Math.floor(Date.now() / 1000);
if (!error && response.statusCode === 200 && (query.item in json)) {
const a = new Prices({
"item": query.item,
"current_price": json[query.item].toString().replace('$', ''),
"avg_week_price": json[query.item].toString().replace('$', ''),
"avg_month_price": json[query.item].toString().replace('$', ''),
"lastupdate": current
});

a.save((err, response) => {
if (err) {
throw err;
}
})

const b = new History({
item: query.item,
current_price: json[query.item].toString().replace('$', ''),
time: current
});

b.save((err, response) => {
if (err) {
throw err;
}
})

res.json({
success: true,
current_price: json[query.item].toString().replace('$', ''),
avg_week_price: json[query.item].toString().replace('$', ''),
avg_month_price: json[query.item].toString().replace('$', ''),
lastupdate: current
});
} else {
res.json({ success: false, error: options.errors.unknown_item });
}
});
}
});
}
Expand Down
39 changes: 37 additions & 2 deletions updater.js
Expand Up @@ -37,7 +37,6 @@ function refreshPrices() {
if(prices.length > 0) {
var time = Math.floor(Date.now() / 1000);

console.log(prices);
prices.forEach(function(item) {
Prices.update( {item: item.item}, {$set: {lastupdate: current}}, (err, response) => {
if(err) {
Expand Down Expand Up @@ -79,7 +78,43 @@ function refreshPrices() {
}
});
} else {
console.log('An error occured receiving price for item: ' + item.item);
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() {
Expand Down

0 comments on commit 5d138e4

Please sign in to comment.