-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_financials.js
131 lines (117 loc) · 5.4 KB
/
update_financials.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
//
// Scrape the stock financial information from web
// Data is stored into dynamodb
//
var when = require('when');
var parseArgs = require('minimist');
var logger = require('./utility').logger;
var stocks = require('./stock');
var finanicals = require('./financial');
var scraper = require('./scraper');
var config = require('./config');
var local = config.local;
var AWS = require('aws-sdk');
AWS.config.region = 'us-west-1';
//
// Get a handle of Dynamodb
//
var db, docClient;
if (local) {
db = new AWS.DynamoDB({endpoint: new AWS.Endpoint('http://localhost:8000')});
docClient = new AWS.DynamoDB.DocumentClient({endpoint: new AWS.Endpoint('http://localhost:8000')});
} else {
db = new AWS.DynamoDB();
docClient = new AWS.DynamoDB.DocumentClient();
}
//
// Get exchange name (Nasdaq or NYSE)
//
var argv = parseArgs(process.argv.slice(2));
var exchange = (argv.t || 'nasdaq').toLowerCase();
//
// Go through each stock in database and scrape its financial data
//
var delay = local ? 0 : 1000;
when.resolve(null)
.then(function() { return finanicals.initFinancialTables(db); })
.then(function() {
stocks.forEachStock(docClient, exchange, delay, function(stockInfo, isFinal) {
return when.promise(function(resolve, reject) {
try {
logger.info('Processing ' + stockInfo.Symbol);
when.resolve(null)
.then(function () {
return scraper.scrape(stockInfo.Symbol, false);
})
.then(function (financialRecords) {
if (!financialRecords || financialRecords.length == 0) {
// Empty records, skip the rest
return when.reject();
}
var startIndex = 0;
//if (stockInfo.hasOwnProperty('LastUpdatedYear')) {
// for (startIndex = 0; startIndex < financialRecords.length; startIndex++) {
// if (financialRecords[startIndex].Year == stockInfo.LastUpdatedYear) {
// startIndex++;
// break;
// }
// }
//}
stockInfo.LastUpdatedYear = financialRecords[financialRecords.length-1].Year;
if (startIndex < financialRecords.length) {
return finanicals.addFinancialRecords(docClient,
financialRecords.slice(startIndex), false);
} else {
return true;
}
})
.then(function () {
return scraper.scrape(stockInfo.Symbol, true);
})
.then(function (financialRecords) {
if (!financialRecords || financialRecords.length == 0) {
// Empty records, skip
return when.reject();
}
var startIndex = 0;
//if (stockInfo.hasOwnProperty('LastUpdatedQuarter')) {
// for (startIndex = financialRecords.length - 1; startIndex >= 0; startIndex--) {
// if (financialRecords[startIndex].Quarter == stockInfo.LastUpdatedQuarter) {
// startIndex++;
// break;
// }
// }
//}
stockInfo.LastUpdatedQuarter = financialRecords[financialRecords.length-1].Quarter;
if (startIndex < financialRecords.length) {
return finanicals.addFinancialRecords(docClient,
financialRecords.slice(startIndex), true);
} else {
return true;
}
})
.then(function() {
// Update stock timestamps
return stocks.updateStock(docClient, stockInfo, exchange);
})
.then(function() {
resolve(false); // true means stop scanning the table
return true;
})
.catch(when.TimeoutError, function() {
logger.error('Promise timeout on processing !' + stockInfo.Symbol);
resolve(false); // true means stop scanning the table
return false;
})
.catch(function() {
logger.info('Skipping ' + stockInfo.Symbol);
resolve(false); // true means stop scanning the table
return false;
});
} catch (exception) {
logger.warn(exception);
reject(null);
}
});
});
});