-
Notifications
You must be signed in to change notification settings - Fork 9
/
amfi.js
163 lines (146 loc) · 4.24 KB
/
amfi.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var http = require('http');
var events = require('events');
var amfiHost = "portal.amfiindia.com"; //changed on 19 Oct 2013
var amfiPath = "/spages/NAVAll.txt";
var lineBuffer = "";
var headers = [];
var types = [];
var managers = [];
var funds = [];
var managerHash = {};
var typesHash = {};
var currTyp, currMgr;
var callback, refreshInterval;
var AMFINavs = function (options) {
events.EventEmitter.call(this);
var self = this;
if (options) {
if (options.callback)
callback = options.callback;
if (options.refreshInterval)
refreshInterval = options.refreshInterval * 1000;
}
acquireData.call(self);
if (refreshInterval) {
var int = setInterval(function () {
acquireData.call(self);
}, refreshInterval);
}
}
//inherit events.EventEmitter
AMFINavs.super_ = events.EventEmitter;
AMFINavs.prototype = Object.create(events.EventEmitter.prototype, {
constructor: {
value: AMFINavs,
enumerable: false
}
});
function acquireData () {
funds = [];
types = [];
managers = [];
var self = this;
self.status = 'Acquiring Data';
console.log(self.status);
http.request({host: amfiHost, path: amfiPath}, function(response) {
var str = '';
response.on('error', function (chunk) {
var str;
str += chunk;
console.log(str);
});
response.on('data', function (chunk) {
var str;
str += chunk;
processData(chunk.toString());
});
response.on('end', function () {
//sort the lists
managers.sort();
types.sort();
//set the data
self.funds = funds;
self.fundManagers = managers;
self.fundTypes = types;
self.updateDate = new Date();
self.status = 'Data Ready';
console.log(self.status);
//data for callback
self.data = {funds: self.funds,
fundTypes: self.fundTypes,
fundManagers: self.fundManagers,
updateDate : self.updateDate}
//emit event
self.emit('dataready', self.data);
//if there is a callback, then do callback
if (callback)
callback(self.data);
});
}).end();
}
function processData(stringChunk) {
lineBuffer = lineBuffer + stringChunk; //add chunk to previous incomplete line
if (stringChunk.lastIndexOf('\r\n') > 0) { //there are multiple lines
lines = splitTextIntoLines(lineBuffer);
if (lines) {
if (lines[lines.length-1])
lineBuffer = lines[lines.length-1]; //assign last line to buffer
else
lineBuffer = "";
lines.forEach(processLine);
}
}
}
function processLine(line, idx, lines) {
if (!line) //if line is null
return;
if (idx+1 == lines.length) //if last line
return;
//console.log(line);
if (headers.length==0) { //if there are no headers yet, then treat the line as a header
headers = [];
processHeader(line);
}
else if (line.indexOf(';') > 0) //if the line has a semi-colon, then this line has func info
processFund(line);
else if (line.indexOf('Ended') > 0) //if the line has a fund type, then it will have 'Ended' text
processFundType(line);
else if (line.length> 2) //if line is not any of above but has text, then it is a Fund manager name
processMgr(line);
}
//process line with column headers
function processHeader(line) {
headers = line.split(';');
}
//process line with fund type
function processFundType(line) {
currTyp = line; //next few funds will be of this fund type, so set it as current value
if (!typesHash[currTyp]) { //making sure types are not duplicated
types.push(currTyp);
typesHash[currTyp] = currTyp;
}
}
//process line with fund manager name
function processMgr(line) {
currMgr = line; //next few funds will be of this fund manager, so set it as current value
if (!managerHash[currMgr]) { //making sure fund managers are not duplicated
managers.push(currMgr);
managerHash[currMgr] = currMgr;
}
}
//process line with fund information
function processFund(line) {
fundVals = line.split(';');
var fund = {};
for (i=0; i < fundVals.length; i++)
fund[headers[i]] = fundVals[i];
fund.Type = currTyp; //add type to fund information
fund.Manager = currMgr; //add manager to fund information
funds.push(fund);
}
function splitTextIntoLines(txt) {
if (txt && txt.length > 0)
lineArr = txt.split('\r\n');
return lineArr;
}
module.exports = AMFINavs;