-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.js
More file actions
54 lines (47 loc) · 1.46 KB
/
fetch.js
File metadata and controls
54 lines (47 loc) · 1.46 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
module.exports = fetchSked;
const https = require("node:https");
const fs = require("node:fs");
function fetchSked(date) {
return new Promise((resolve, reject) => {
//reject("fail");
const urlprefix = "https://www.cnatra.navy.mil/scheds/";
const urlwing = "TW5";
const urlsquadron = "SQ-HT-8";
//const filedate = date.getFullYear().toString().padStart(2, "0")+"-"+(date.getMonth()+1).toString().padStart(2, "0")+"-"+(date.getDate()+0).toString().padStart(2, "0");
const filedate = date;
const filesquadron = "HT-8";
const filename = "Frontpage";
let url = new URL(urlprefix+urlwing+"/"+urlsquadron+"/"+"!"+filedate+"!"+filesquadron+"!"+filename+".pdf");
//console.log(url);
const urloptions = {
hostname: url.host,
path: url.pathname,
method: "GET",
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0"
}
};
let data = Buffer.from([]);
const urlreq = https.request(urloptions, (res) => {
res.on("data", (chunk) => {
if(res.statusCode == 200) {
data = Buffer.concat([data, chunk]);
}
else {
let e = "Fetch Error ["+res.statusCode+"] - "+res.statusMessage;
//console.log(e);
reject(e);
}
});
res.on("end", () => {
//fs.createWriteStream("test1.pdf", {flags: "w"}).end(data);
resolve(data);
});
});
urlreq.on("error", (e) => {
console.error("problem with request: ${e.message}");
reject(e);
});
urlreq.end();
});
}