-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
147 lines (126 loc) · 4.3 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
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
import fs from "fs";
import fetch from "node-fetch";
import path from "path";
import * as config from "./config.js";
/**
* This tool downloads all PDFs in your ePost account to the `/downloads`
* directory (within this dir).
*/
const BaseHeaders = {
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0) Gecko/20100101 Firefox/98.0",
};
const downloadDir = path.join(process.cwd(), "downloads");
const buildQueryParams = (params) => {
const p = new URLSearchParams();
for (const key of Object.getOwnPropertyNames(params)) {
p.append(key, params[key]);
}
const result = p.toString();
if (result.length == 0) {
return "";
}
return `?${result}`;
};
const ensureDownloadsDirectory = () => {
if (!fs.existsSync(downloadDir)) {
fs.mkdirSync(path.join(downloadDir));
}
};
const listItems = async (offset = 0, limit = 15) => {
console.log("Listing items...");
const url =
"https://www.canadapost-postescanada.ca/inbox/rs/mailitem" +
buildQueryParams({
folderId: 0,
sortField: 1,
order: "D",
offset,
limit,
});
const res = await fetch(url, {
referrer: "https://www.canadapost-postescanada.ca/inbox/en",
compress: true,
redirect: "error",
headers: {
...BaseHeaders,
Accept: "application/json, text/plain, */*",
"Accept-Language": "en",
"Accept-Encoding": "gzip, deflate, br",
cookie: config.canadapostPostescanadaCookie,
"x-ibx-lang": "en",
csrf: config.canadapostPostescanadaCsrfToken,
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"Sec-GPC": "1",
},
});
// console.log(url);
// console.log(`${res.status} ${res.statusText}`);
return await res.json();
};
const fetchItem = async (num, mailItemInfo) => {
// convert all remotely unsafe chars to "_"
let filename =
mailItemInfo.shortDescription.replace(/[^a-z0-9]/gi, "_") + ".pdf";
const outputPath = path.join(downloadDir, filename).trim();
const url =
"https://www.epost.ca/service/displayMailStream.a" +
buildQueryParams({
importSummaryId: mailItemInfo.mailItemID,
lang: "en",
OWASP_CSRFTOKEN: config.epostCaOWASP_CSRFTOKEN,
});
const res = await fetch(url, {
compress: true,
redirect: "error",
referrer:
"https://www.epost.ca/service/displayEpostInboxMail.a?documentId=" +
mailItemInfo.mailItemID +
`&language=en&source=myinbox&OWASP_CSRF=${config.epostCaOWASP_CSRFTOKEN}`,
headers: {
...BaseHeaders,
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Accept-Language": "en-CA,en-US;q=0.7,en;q=0.3",
"Accept-Encoding": "gzip, deflate, br",
DNT: "1",
Cookie: config.epostCaCookie,
"Upgrade-Insecure-Requests": "1",
"Sec-Fetch-Dest": "iframe",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "same-origin",
"Sec-GPC": "1",
},
});
// console.log(url);
// console.log(`${res.status} ${res.statusText}`);
if (res.status === 200) {
const fileStream = fs.createWriteStream(outputPath);
await new Promise((resolve, reject) => {
res.body.pipe(fileStream);
res.body.on("error", reject);
fileStream.on("finish", resolve);
});
console.log(
`⤵️ [${num}] Fetched: ${mailItemInfo.mailItemID} - ${
mailItemInfo.shortDescription
} ==> ${path.relative(process.cwd(), outputPath)}`
);
} else {
console.error(
`!! Error fetching ${mailitemInfo.shortDescription} - ${res.status} ${res.statusText}`
);
}
};
const main = async () => {
ensureDownloadsDirectory();
const { mailitemInfos } = await listItems(0, 200);
console.log(`Found ${mailitemInfos.length} items to download...`);
let i = 0;
mailitemInfos.forEach(async (mailItem) => {
i++;
await fetchItem(i, mailItem);
});
};
main();