Skip to content

Commit e424e5a

Browse files
added auto url parser
1 parent 83e629b commit e424e5a

File tree

1 file changed

+66
-16
lines changed

1 file changed

+66
-16
lines changed

index.js

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,74 @@
1-
//const url = "ftp://ftp.uconn.edu/48_hour/";
2-
const url = "ftp://pubftp.spp.org/Markets/DA/BINDING_CONSTRAINTS/2018/05/By_Day/";
1+
//const url = "ftp://pubftp.spp.org/Markets/DA/BINDING_CONSTRAINTS/2017/01/By_Day
2+
3+
const url = "ftp://pubftp.spp.org/Markets/DA/BINDING_CONSTRAINTS/";
34
const { spawn } = require("child_process");
45
const folders = [];
5-
const ls = spawn('curl', [url]);
66
const data_folder = './data'; // the folder path to where you want to downalod the files to.
7-
ls.stdout.on('data', (data) => {
8-
folders.push(...data.toString().split('\n'));
9-
});
10-
117

12-
ls.on('close', data => {
8+
// choose the range dates you want to download your files.
9+
// recommended to stay within a year from start to current.
10+
// example;
11+
// fromYear: 2016
12+
// fromMonth: 1
13+
// currentYear: 2016
14+
// currentMonth: 12
15+
// and so on.
16+
// You may download up to 2 years of data at a time, however this will take some
17+
// time depending on your computer resources and internet speed.
18+
const searchBase = {
19+
fromYear: 2015,
20+
fromMonth: 1,
21+
currentYear: 2016,
22+
currentMonth: 11
23+
}
1324

14-
for (let i = 0; i < folders.length; i++) {
15-
let temp = folders[i].split(' ');
16-
if (temp[temp.length - 1].split('.')[1] == 'csv') {
17-
const fUrl = `${url}${temp[temp.length - 1]}`;
18-
console.log(fUrl);
19-
const download = spawn('curl', [fUrl, '--output', `${data_folder}/${temp[temp.length - 1]}`]);
20-
download.on('close', data => console.log("File Download -> ", temp[temp.length -1]));
25+
// scan the base folder by providing the search settings and base url
26+
const parseFtpFolders = (base, url) => {
27+
const unit = 12;
28+
let month = base.fromMonth;
29+
let year = base.fromYear;
30+
let gate = true;
31+
const urls = [];
32+
while (gate) {
33+
34+
if (year == base.currentYear && month == base.currentMonth) {
35+
gate = false;
36+
}
37+
if (unit < month) {
38+
year++
39+
month = 1;
2140
}
41+
urls.push(`${url}${year}/${month < 10 ? '0' + month: month}/By_Day/`);
42+
month++;
43+
}
44+
downloadFiles(urls);
45+
}
46+
47+
// map thru the urls received from parse folder and Q the files and download it
48+
// async each file.
49+
const downloadFiles = (dUrl) =>
50+
{
51+
for (let i = 0; i < dUrl.length; i++) {
52+
const ls = spawn('curl', [dUrl[i]]);
53+
console.log("urls --> ", dUrl[i]);
54+
55+
ls.stdout.on('data', (data) => {
56+
console.log(data.toString());
57+
folders.push(...data.toString().split('\n'));
58+
});
59+
ls.on('close', data => {
60+
for (let x = 0; x < folders.length; x++) {
61+
let temp = folders[x].split(' ');
62+
console.log(temp);
63+
if (temp[temp.length - 1].split('.')[1] == 'csv') {
64+
const fUrl = `${dUrl[i]}${temp[temp.length - 1]}`;
65+
console.log("-------->>>> ", fUrl);
66+
const download = spawn('curl', [fUrl, '--output', `${data_folder}/${temp[temp.length - 1]}`]);
67+
download.on('close', data => console.log("File Download from -> ", fUrl));
68+
}
69+
}
70+
})
2271
}
23-
})
72+
}
2473

74+
parseFtpFolders(searchBase, url);

0 commit comments

Comments
 (0)