-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathindex.js
43 lines (37 loc) · 1.38 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
const moment = require("moment");
const fs = require("fs");
const { scrapeCompaniesData, scrapeMarketData, fetchData, groupMarketDataByCompany } = require("./scrapper_v2");
const { lastMarketDay } = require("./helpers");
// FIX: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' issue with API call
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
async function runScript() {
try {
// fetch last 7 market-days data
let date = moment();
for (let i = 0; i <= 7; i++) {
try {
let dateStr = lastMarketDay(date.subtract(1, 'days').format("YYYY-MM-DD"));
if (fs.existsSync(`./data/date/${dateStr}.json`)) continue;
const data = await fetchData(dateStr);
scrapeCompaniesData(data);
scrapeMarketData(data, dateStr);
groupMarketDataByCompany(data, dateStr);
console.log("scraped data for", dateStr);
} catch (e) {
console.log(e);
continue;
}
}
// update info.json
fs.writeFileSync("./data/info.json", JSON.stringify({
name: "Nepse API",
source: "https://nepalstock.com/",
lastUpdatedAt: moment().format("YYYY-MM-DD HH:mm:ss"),
}));
console.log("SUCCESS");
} catch (e) {
console.log(e);
console.log("FAIL");
}
}
runScript();