-
Notifications
You must be signed in to change notification settings - Fork 22
/
live_quote.js
82 lines (67 loc) · 2.22 KB
/
live_quote.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
/**
* Nifty Application that can be used for creating any virtual stock market apps that needs live NSE Stock rates
*
* @author Ashwanth Kumar <[email protected]>
* @date 01/01/2012
* @link http://blog.ashwanthkumar.in/2012/01/live-nse-stock-rates.html
*/
var express = require('express');
var app = express.createServer(express.logger());
// Only possible part that needs to be executed
app.get('/', function(request, response) {
if(request.query.symbol && request.query.symbol.length > 0) {
console.log("Symbol Requested - " + request.query.symbol);
var http = require('http');
var symbol = request.query.symbol;
var options = {
host: 'www.nseindia.com',
method: 'GET',
path: '/live_market/dynaContent/live_watch/get_quote/ajaxGetQuoteJSON.jsp?symbol=' + symbol,
headers: {
"User-Agent": "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)",
"Referer": "http://www.nseindia.com/",
"Accept": '*/*'
}
};
var resp = ""; // Hold the servers response
var req = http.request(options, function(res) {
res.on('error', function(e) {
console.log("Got error: " + e.message);
});
res.on('data', function(data) {
resp += data;
});
res.on('end', function() {
resp = resp.trim();
data = JSON.parse(resp);
if(data.data.length < 1) {
// Invalid Symbol
response.send('<h1>Invalid Symbol</h1><p>Invalid Symbol given. Refer valid list of Symbols at <a href="http://blog.ashwanthkumar.in/2012/01/nse-valid-symbols.html">here</a></p>');
return;
}
// Cleaning up the feeds
delete data.otherSeries;
delete data.optLink;
delete data.futLink;
// Now send the data
response.send(data);
// @TODO Can still filter to provide more precise data
});
});
req.end();
} else {
// Invalid request
response.send(invalidRequest(), 400);
}
});
// Map all other request
app.get('/**', function(req, res) {
res.send(invalidRequest(), 400);
});
var port = process.env.PORT || 3000;
app.listen(port, function() {
console.log("Listening on " + port);
});
function invalidRequest() {
return "<h1>INVALID REQUEST</h1><p>Request must be of the form:\r\n <strong>http://live-nse.herokuapp.com/?symbol=INFY</strong></p>\r\n";
}