forked from xasos/Coins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
143 lines (114 loc) · 3.85 KB
/
app.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
'use strict';
var express = require('express');
var bodyParser = require('body-parser');
var cheerio = require('cheerio');
var request = require('request');
var port = process.env.PORT || 1337;
var router = express.Router();
var app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
var coinList = [];
var target = 'http://coinmarketcap.com/';
router.use(function(req, res, next) {
next();
});
router.get('/', function(req, res) {
res.json({
message: 'Welcome to coins-api! Documentation for querying the API is available here: https://github.com/xasos/Coins.'
});
});
router.route('/coins/:ticker?')
.get(function(req, res) {
var coinTicker = req.params.ticker;
request(target, function(err, resp, body) {
if (!err && resp.statusCode == 200) {
var $ = cheerio.load(body);
var currentTime = Date.now();
$('tr').each(function(i) {
if ($(this).attr("id")) {
var coinName = $(this).attr("id").slice(3);
}
var pos = $(this).find('td').eq(0).text().trim();
var marketCap = $(this).find('td').eq(2).text().trim();
marketCap = marketCap.slice(2).replace(/,/g, "");
var price = $(this).find('td').eq(3).text().trim();
price = price.slice(2).replace(/,/g, "");
var ticker = $(this).find('td').eq(4).text().trim();
ticker = ticker.split('\n').slice(1, 2).join('\n').trim();
var volume = $(this).find('td').eq(5).text().trim();
volume = volume.slice(2).replace(/,/g, "");
var delta24hr = $(this).find('td').eq(6).text().trim();
delta24hr = delta24hr.slice(0, -2);
var coins = {
name: coinName,
position: pos,
price: price,
currency: "usd",
marketCap: marketCap,
ticker: ticker,
volume: volume,
delta24hr: delta24hr,
timestamp: currentTime
};
if (coinTicker) { // Check if user supplied a specified ticker in URL
if (coins.name === coinTicker) {
var individualCoin = key;
coinList.push(key);
}
}
else if (coinName) { // Else check if coinName is valid
coinList.push(coins);
}
});
}
});
res.json(coinList);
});
router.route('/coins/:ticker?/price/:currency')
.get(function(req, res) {
var ticker = req.params.ticker;
var currency = req.params.currency;
var coins;
var requestURL = 'http://freecurrencyconverterapi.com/api/v3/convert?q=USD_' + currency + '&compact=ultra';
request(target, function(err, resp, body) {
if (!err && resp.statusCode == 200) {
var $ = cheerio.load(body);
$('tr').each(function(i) {
if ($(this).attr("id")) {
coinName = $(this).attr("id").slice(3);
}
price = $(this).find('td').eq(3).text().trim();
price = price.slice(2).replace(/,/g, "");
if (coinName == ticker) {
coins = {
name: coinName,
price: price
}
};
});
request(requestURL, function(error, response, body) {
if (!error && response.statusCode == 200) {
// Get first property from returned data
var firstProp;
for (var key in body) {
if (body.hasOwnProperty(key)) {
firstProp = body[key];
break;
}
};
// Get new price and return
var newPrice = coins.price * body.firstProp;
res.send({
price: newPrice
});
}
});
}
});
});
app.use('/', router);
app.listen(port);
console.log('Listening on port ' + port);