Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
v1.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
yashko333 committed Jan 15, 2017
1 parent 9889878 commit a2290d1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
7 changes: 6 additions & 1 deletion example.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ market.on('connected', function() {

market.on('newitems_go', function (item) {
console.log(item);
});
});

market.api.call('GetMoney', function (err, balance) {
if (err) return console.error(err);
console.log('Account balance: ' + (balance.money/100) + ' RUB')
})
41 changes: 35 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,57 @@
const websocket = require('ws');
const request = require('request');
const queue = require('apiqueue');
const empty = x => true;

class CSGOTM {
constructor(opts) {
if (typeof opts === 'string') return this.apikey = opts;
if (!opts.apikey) throw new Error('Specify your API KEY');
this.apikey = opts.apikey;
if (typeof opts === 'string') this.apikey = opts; else this.apikey = opts.apikey;
if (!opts && !opts.apikey) throw new Error('Specify your API KEY');
this.q = new queue({interval: 250, name: "csgo.tm api calls"});
}

get api() {
let self = this;
return {
call: function(data, callback = empty) {
let f = function () {
request(self.api.url.build(data), function (err, response, body) {
if (err) return callback(err);
if (response.statusCode != 200) return callback(response.statusCode);
let data = JSON.parse(body);
if (data.error) return callback(data.error);
callback(null, data);
})
};
self.q.addTask(f);
},
url: {
base: 'https://market.csgo.com/api/',
build (method) {
return this.base + method + '/?key=' + self.apikey;
}
}
}
}

get socket() {
let self = this;
return {
connect: function () {
self.ws = new websocket('wss://wsn.dota2.net/wsn/');
self.ws.onopen = x => self.emit('connected');
self.ws.onopen = function() {
self.emit('connected');
self.api.call('PingPong');
}
self.ws.on('message', function (message) {
try {
message = JSON.parse(message);
self.emit(message.type, message.data);
self.emit(message.type, JSON.parse(message.data));
} catch (e) {
console.error('Cant parse JSON from message: ' + message);
}
});
setInterval(this.ping, 60 * 1000);
//setInterval(this.ping, 60 * 1000);
},
auth: function (callback) {
request.post({url: 'https://market.csgo.com/api/GetWSAuth/?key=' + self.apikey, json: true}, function (err, res, body) {
Expand Down

0 comments on commit a2290d1

Please sign in to comment.