-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
74 lines (67 loc) · 2.1 KB
/
index.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
//const url = "ftp://pubftp.spp.org/Markets/DA/BINDING_CONSTRAINTS/2017/01/By_Day
const url = "ftp://pubftp.spp.org/Markets/DA/BINDING_CONSTRAINTS/";
const { spawn } = require("child_process");
const folders = [];
const data_folder = './data'; // the folder path to where you want to downalod the files to.
// choose the range dates you want to download your files.
// recommended to stay within a year from start to current.
// example;
// fromYear: 2016
// fromMonth: 1
// currentYear: 2016
// currentMonth: 12
// and so on.
// You may download up to 2 years of data at a time, however this will take some
// time depending on your computer resources and internet speed.
const searchBase = {
fromYear: 2017,
fromMonth: 1,
currentYear: 2018,
currentMonth: 5
}
// scan the base folder by providing the search settings and base url
const parseFtpFolders = (base, url) => {
const unit = 12;
let month = base.fromMonth;
let year = base.fromYear;
let gate = true;
const urls = [];
while (gate) {
if (year == base.currentYear && month == base.currentMonth) {
gate = false;
}
if (unit < month) {
year++
month = 1;
}
urls.push(`${url}${year}/${month < 10 ? '0' + month: month}/By_Day/`);
month++;
}
downloadFiles(urls);
}
// map thru the urls received from parse folder and Q the files and download it
// async each file.
const downloadFiles = (dUrl) =>
{
for (let i = 0; i < dUrl.length; i++) {
const ls = spawn('curl', [dUrl[i]]);
console.log("urls --> ", dUrl[i]);
ls.stdout.on('data', (data) => {
console.log(data.toString());
folders.push(...data.toString().split('\n'));
});
ls.on('close', data => {
for (let x = 0; x < folders.length; x++) {
let temp = folders[x].split(' ');
console.log(temp);
if (temp[temp.length - 1].split('.')[1] == 'csv') {
const fUrl = `${dUrl[i]}${temp[temp.length - 1]}`;
console.log("-------->>>> ", fUrl);
const download = spawn('curl', [fUrl, '--output', `${data_folder}/${temp[temp.length - 1]}`]);
download.on('close', data => console.log("File Download from -> ", fUrl));
}
}
})
}
}
parseFtpFolders(searchBase, url);