-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpredictIt.js
59 lines (50 loc) · 1.48 KB
/
predictIt.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
const rp = require('request-promise');
const Promise = require('bluebird');
const base = 'https://www.predictit.org/api/marketdata/';
const apiCall = function(uri) {
return rp({uri, json: true})
.then(d => {
if (d === null) { throw new Error('BADARG'); }
return d;
})
.catch(err => {
if (err.message === 'BADARG') {
console.log('ERROR: BADARG: You likely entered in bad arguments.');
} else if (err.error.errno === 'ENOTFOUND') {
console.log('ERROR: ENOTFOUND: Couldn\'t access the url you were trying to query.');
console.log('Your internet connection could be bad.');
}
return err;
});
};
exports.contract = function(targetContract) {
return new Promise(resolve => {
exports.market(targetContract)
.then(market => {
market.Contracts.forEach((contract, index) => {
if (contract.TickerSymbol === targetContract) {
resolve(contract);
}
});
});
});
};
exports.market = function(market) {
const url = base + 'markets/' + market;
return apiCall(url);
};
exports.group = function(group) {
const url = base + 'group/' + group.toString();
return apiCall(url)
.then(d => d.Markets);
};
exports.category = function(category) {
const url = base + 'category/' + category.toString();
return apiCall(url)
.then(d => d.Markets);
};
exports.all = function() {
const url = base + 'all';
return apiCall(url)
.then(data => data.Markets);
};