-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
69 lines (62 loc) · 2.82 KB
/
index.ts
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
import { appendFile, existsSync, mkdir, readFile } from 'node:fs';
import type { CustomersType } from './config/customersType.ts';
import defaultConfiguration from './config/default.json' assert { type: 'json' };
const generatedCustomsFolder = 'generated-cn23';
readFile('./config/customers.json', { encoding: 'utf8' }, (error, data) => {
if (error) { console.error('\x1b[31m Error while reading file: ', error) };
try {
if (!existsSync(generatedCustomsFolder)) {
mkdir(generatedCustomsFolder, (error) => error && console.error('\x1b[31m Error while creating folder: ', error));
}
} catch (error) {
console.error('\x1b[31m Error: ', error);
};
const customers: CustomersType = JSON.parse(data);
customers.forEach(
(customer, index) => {
const documentData = { ...customer, ...defaultConfiguration };
setTimeout(
async () => {
try {
console.log(`\x1b[32m [n°${index + 1}] - fetching for ${customer.receiver.userFirstName}-${customer.receiver.userLastName}... \x1b[0m`);
const request = await fetch('https://cn23.laposte.fr/form?lang=fr', {
'credentials': 'include',
'headers': {
'accept': 'application/json, text/plain, */*',
'accept-language': 'en-US,en;q=0.9,fr-FR;q=0.8,fr;q=0.7',
'cache-control': 'no-cache',
'content-type': 'application/json',
'pragma': 'no-cache',
'sec-ch-ua': '\"Google Chrome\";v=\"129\", \"Not=A?Brand\";v=\"8\", \"Chromium\";v=\"129\"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '\"macOS\"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'Referer': 'https://cn23.laposte.fr/parcours/recapitulatif',
'Referrer-Policy': 'strict-origin-when-cross-origin',
},
'referrer': 'https://cn23.laposte.fr/parcours/recapitulatif',
'body': JSON.stringify(documentData),
'method': 'POST',
'mode': 'cors',
});
const response = await request.json();
const documentBuffer = JSON.parse(response.pae.buffer);
appendFile(
`./${generatedCustomsFolder}/${customer.invoice}_${customer.receiver.userFirstName}-${customer.receiver.userLastName}_${response.pae.fileName}`,
//@ts-ignore
Buffer.from(documentBuffer.data),
(error: any) => error && console.error('\x1b[31m Error while creating the file: ', error),
);
console.log(`\x1b[36m ✔ [n°${index + 1}] - "${customer.invoice}_${customer.receiver.userFirstName}-${customer.receiver.userLastName}_${response.pae.fileName}" successfully created! \x1b[0m`);
(index === (customers.length - 1)) && console.log(`\x1b[32m ✔✔ done! \x1b[0m`);
} catch (error) {
console.error(`\x1b[31m [n°${index + 1}] - Error while trying to request the API: `, error);
};
},
5000 * index,
);
}
);
});