-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfund-holdings.js
187 lines (162 loc) · 5.07 KB
/
fund-holdings.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const https = require("https");
const fs = require("fs");
const csv = require("fast-csv");
var dayjs = require('dayjs')
const AWS = require("aws-sdk");
const path = require("path");
const { Client } = require("pg");
const currency = require("currency.js");
require("dotenv").config();
const argv = require("yargs/yargs")(process.argv.slice(2)).argv;
/*
usage: node fund-holdings.js --https ${HTTPS_URL}
*/
if (argv.https) {
downloadFile(argv.https);
}
// download file from url to ./tmp/
function downloadFile(url) {
const fileName = path.parse(url).base;
const options = {
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0",
Accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
Connection: "keep-alive",
"Upgrade-Insecure-Requests": "1",
"Cache-Control": "max-age=0",
},
};
https.get(url, options, function (response) {
!fs.existsSync(`tmp`) && fs.mkdirSync(`tmp`);
const file = fs.createWriteStream(`tmp/${fileName}`);
response.pipe(file).on("finish", () => {
console.log(`${fileName} is downloaded to tmp/${fileName}`);
relocateFile("tmp", fileName);
});
});
}
// move the file from ./tmp/ to ./data/{data_date}/raw/
function relocateFile(srcDir, fileName) {
var date;
fs.createReadStream(`${srcDir}/${fileName}`)
.pipe(csv.parse({ headers: true, maxRows: 1 }))
.on("error", (err) => {
throw err;
})
.on("data", (row) => {
if (isNaN(new Date(row.date).getTime())) {
throw new Error(`${row.date} is an invalid date`);
}
date = dayjs(row.date).format("YYYY-MM-DD");
})
.on("end", () => {
console.log(`${srcDir}/${fileName} contains ${date} data`);
const dstDir = `data/${date}/raw`;
!fs.existsSync(dstDir) && fs.mkdirSync(dstDir, { recursive: true });
fs.rename(`${srcDir}/${fileName}`, `${dstDir}/${fileName}`, (err) => {
if (err) throw err;
console.log(`${srcDir}/${fileName} is moved to ${dstDir}/${fileName}`);
});
processFile(dstDir, `${dstDir}/..`, fileName);
});
}
// remove invalid rows from file and save the result to ./data/{data_date}/
function processFile(srcDir, dstDir, fileName) {
const csvStream = csv.format({
headers: true,
});
const ws = fs.createWriteStream(`${dstDir}/${fileName}`);
csvStream.pipe(ws);
const weightRegexp = "([0-9]+[.][0-9]{2})";
fs.createReadStream(`${srcDir}/${fileName}`)
.pipe(csv.parse({ headers: true, ignoreEmpty: true }))
.on("error", (err) => {
throw err;
})
.on("data", (row) => {
if (!isNaN(new Date(row.date))) {
const processed = {
date: row.date,
fund: row.fund,
company: row.company,
ticker: row.ticker,
cusip: row.cusip,
"market value($)": currency(row["market value ($)"], {
separator: "",
pattern: "#",
}).format(),
"weight(%)": row["weight (%)"].match(weightRegexp)[1],
};
csvStream.write(processed);
}
})
.on("end", () => {
csvStream.end();
console.log(`${fileName} is processed and saved to ${dstDir}`);
uploadToS3(
`${dstDir}/${fileName}`,
path.relative(`./data`, path.resolve(`${dstDir}/${fileName}`))
);
});
}
function uploadToS3(srcPath, dstPath) {
AWS.config.update({ region: "us-east-1" });
// Create S3 service object
s3 = new AWS.S3({ apiVersion: "2006-03-01" });
// call S3 to retrieve upload file to specified bucket
const uploadParams = { Bucket: process.env.AWS_S3_BUCKET, Key: "", Body: "" };
const file = srcPath;
// Configure the file stream and obtain the upload parameters
const fileStream = fs.createReadStream(file);
fileStream.on("error", function (err) {
throw err;
});
uploadParams.Body = fileStream;
uploadParams.Key = dstPath;
// call S3 to retrieve upload file to specified bucket
s3.upload(uploadParams, function (err, data) {
if (err) {
throw err;
}
if (data) {
console.log(`Uploaded ${data.key} to S3`);
copyToPostgres(data.key);
}
});
}
function copyToPostgres(location) {
const pgclient = new Client({
ssl: {
rejectUnauthorized: true,
ca: fs.readFileSync("rds-combined-ca-bundle.pem").toString(),
},
});
pgclient.connect();
const copy = `
SELECT aws_s3.table_import_from_s3(
'fund_holdings',
'date,fund,company,ticker,cusip,market_value,weight',
'(format csv, header true)',
'${process.env.AWS_S3_BUCKET}',
'${location}',
'us-east-1',
'${process.env.AWS_ACCESS_KEY_ID}',
'${process.env.AWS_SECRET_ACCESS_KEY}'
);
`;
pgclient.query(copy, (err, res) => {
pgclient.end();
if (err) {
if (err.code === "23505") {
console.log(err.detail);
} else {
throw err;
}
}
console.log(`${location} uploaded to Postgres`);
});
}