-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
132 lines (110 loc) · 4.06 KB
/
index.mjs
File metadata and controls
132 lines (110 loc) · 4.06 KB
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
import converter from 'json-2-csv';
import { USER_AGENT, BASE_URL_2, PSE_HOME, PSE_STOCK } from './lib/constants';
import { makeCookieJar, getPage, getJson, formatDate, zero, enq, deq } from './lib/util';
import companyData from './companies';
const cookieJar = makeCookieJar();
const commonHeadersPSE = {
Accept: 'application/json',
'User-Agent': USER_AGENT,
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Cache-Control': 'no-cache',
'Origin': 'https://www.pse.com.ph',
'Pragma': 'no-cache',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
};
const commonOpts = {
credentials: 'include',
referrerPolicy: 'no-referrer-when-downgrade',
mode: 'cors',
};
const getHomePage = async () => {
let url;
let referrer;
let headers;
let opts;
let content;
url = BASE_URL_2;
headers = commonHeadersPSE;
opts = { ...commonOpts, headers };
content = await getPage(url, cookieJar, opts);
if (!content) return null;
url = `${BASE_URL_2}/${PSE_HOME}`;
referrer = BASE_URL_2;
headers = { ...commonHeadersPSE, Referer: referrer };
opts = { ...commonOpts, headers };
content = await getPage(url, cookieJar, opts);
if (!content) return null;
return content;
};
(async () => {
let url;
let referrer;
let content;
let headers;
let opts;
let asOfDate;
const queue = [];
const outDict = {};
try {
console.warn('Starting...');
content = await getHomePage();
if (!content) return;
console.warn('Getting company data...');
const companyKeyList = Object.keys(companyData);
companyKeyList.sort();
for (let i = 0; i < companyKeyList.length; i++) {
enq(queue, companyKeyList[i]);
}
while (queue.length > 0) {
let companyKey = deq(queue);
if (companyKey === '_') continue;
try {
console.warn('Processing: ', companyKey);
url = `${BASE_URL_2}/${PSE_STOCK}?id=${companyData[companyKey].cmpyId}&security=${companyData[companyKey].securityId}&tab=0`;
referrer = `${BASE_URL_2}/${PSE_HOME}`;
headers = { ...commonHeadersPSE, Referer: referrer };
opts = { ...commonOpts, headers };
content = await getPage(url, cookieJar, opts, async () => {
console.warn('Trying later.');
enq(queue, companyKey);
});
if (!content) continue;
asOfDate = '';
const match = content.match(/<div id="comTopInfo">\s*As of ([a-zA-Z]+\s+[0-9]+\s*,\s*[0-9]+)/);
if (match) {
asOfDate = match[1];
}
url = `${BASE_URL_2}/${PSE_STOCK}?method=fetchHeaderData&ajax=true`;
referrer = `${BASE_URL_2}/${PSE_STOCK}?id=${companyData[companyKey].cmpyId}&security=${companyData[companyKey].securityId}&tab=0`;
headers = { ...commonHeadersPSE, Referer: referrer };
opts = { ...commonOpts, method: 'POST', body: `company=${companyData[companyKey].cmpyId}&security=${companyData[companyKey].securityId}`, headers };
content = await getJson(url, cookieJar, opts, async () => {
console.warn('Trying later.');
enq(queue, companyKey);
});
if (!content) continue;
if (!content.records || !content.records[0].lastTradedDate) {
console.warn(`Unable to fetch the record for "${companyKey}": `, content);
} else {
outDict[companyKey] = {
Symbol: companyKey,
Date: formatDate(asOfDate),
'Stock Price': !zero(content.records[0].headerLastTradePrice) ? content.records[0].headerLastTradePrice : content.records[0].headerSqPrevious,
'P/E Ratio': content.records[0].headerCurrentPe,
'52-Week High': content.records[0].headerFiftyTwoWeekHigh,
'52-Week Low': content.records[0].headerFiftyTwoWeekLow,
};
}
} catch (itemEx) {
console.error(`Error while fetching ${companyKey}: `, itemEx);
}
}
const list = companyKeyList.map((key) => outDict[key]);
const csv = await converter.json2csvAsync(list);
console.log(csv);
// console.log(list);
} catch (ex) {
console.error(`Error: `, ex);
}
})();