-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
157 lines (127 loc) · 4.63 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
148
149
150
151
152
153
154
155
156
157
import { createWriteStream, statSync } from "node:fs";
import { dirname, join } from "node:path";
import { Readable } from "node:stream";
import { finished } from "node:stream/promises";
import { fileURLToPath } from "node:url";
import { fileList, login, uploadFile } from "@colingourlay/supernote-cloud-api";
const email = process.env.SUPERNOTE_CLOUD_EMAIL;
const password = process.env.SUPERNOTE_CLOUD_PASSWORD;
if (!email || !password) {
console.error("Missing email or password environment variables");
process.exit(1);
}
const padTwoDigits = (num) => String(num).padStart(2, "0");
const getWeekday = (date) => date.getDay();
const getYYYYMMDD = (date) =>
[
date.getFullYear(),
padTwoDigits(date.getMonth() + 1),
padTwoDigits(date.getDate()),
].join("-");
const __dirname = dirname(fileURLToPath(import.meta.url));
const nowUTC = new Date();
const weekdayUTC = getWeekday(nowUTC);
const yyyymmddUTC = getYYYYMMDD(nowUTC);
// const nowETLocaleString = nowUTC.toLocaleString("en-US", {
// timeZone: "America/New_York",
// });
// const nowET = new Date(nowETLocaleString);
// const weekdayET = getWeekday(nowET);
// const yyyymmddET = getYYYYMMDD(nowET);
const getUploadFolderId = async (folderPath, token) => {
const pathSegments = folderPath.split("/");
if (pathSegments[0] !== "Document") {
pathSegments.unshift("Document");
}
let folderId = undefined;
for (let pathSegment of pathSegments) {
const items = await fileList(token, folderId);
const [folder] = items.filter(
({ fileName, isFolder }) => isFolder === "Y" && fileName === pathSegment
);
folderId = folder.id;
}
return folderId;
};
const FILE_NAME_MAPPINGS = [
[/(\d{4})-(\d{2})-(\d{2})-guardian-cryptic/, "gdn.cryptic.$1$2$3"],
[/(\d{4})-(\d{2})-(\d{2})-guardian-quick/, "gdn.quick.$1$2$3"],
[/(\d{4})-(\d{2})-(\d{2})-wsj-number/, "WSJ_$2$3"],
[/(\d{4})-(\d{2})-(\d{2})-wsj-standard/, "XWD$2$3$1"],
[/(\d{4})-(\d{2})-(\d{2})-wsj-variety/, "SatPuz$2$3$1"],
];
const URL_BASE_MAPPINGS = {
guardian: "https://crosswords-static.guim.co.uk/",
wsj: "https://s.wsj.net/public/resources/documents/",
};
const getFileURLAndPath = (fileName) => {
const base = URL_BASE_MAPPINGS[fileName.match(/\d{4}-\d{2}-\d{2}-(\w+)/)[1]];
const [pattern, replacement] = FILE_NAME_MAPPINGS.find(([pattern]) =>
pattern.test(fileName)
);
const mappedFileName = fileName.replace(pattern, replacement);
return [`${base}${mappedFileName}`, join(__dirname, fileName)];
};
const downloadFile = async (fileURL, filePath) =>
finished(
Readable.fromWeb((await fetch(fileURL)).body).pipe(
createWriteStream(filePath)
)
);
const deliverFile = async (fileName, folderId, token) => {
const [fileURL, filePath] = getFileURLAndPath(fileName);
const items = await fileList(token, folderId);
const isFileAlreadyDelivered = items.some(
(item) => item.fileName === fileName
);
if (isFileAlreadyDelivered) {
console.log(`${fileName} has already been delivered.`);
return Promise.resolve();
}
await downloadFile(fileURL, filePath);
const { size } = statSync(filePath);
if (size < 4096) {
console.log(
`${fileURL} is not available yet, or the URL prediction has failed for today.`
);
return Promise.resolve();
}
await uploadFile(token, filePath, folderId);
};
(async () => {
const token = await login(email, password);
const folderId = await getUploadFolderId("Document/Crosswords", token);
const deliveries = [];
if (weekdayUTC !== 0) {
console.log(`Delivering today's Guardian cryptic puzzle.`);
deliveries.push(
deliverFile(`${yyyymmddUTC}-guardian-cryptic.pdf`, folderId, token)
);
console.log(`Delivering today's Guardian quick puzzle.`);
deliveries.push(
deliverFile(`${yyyymmddUTC}-guardian-quick.pdf`, folderId, token)
);
}
// if (weekdayET !== 0) {
if (weekdayUTC !== 0) {
console.log(`Delivering today's WSJ standard puzzle.`);
deliveries.push(
// deliverFile(`${yyyymmddET}-wsj-standard.pdf`, folderId, token)
deliverFile(`${yyyymmddUTC}-wsj-standard.pdf`, folderId, token)
);
// if (weekdayET === 6) {
if (weekdayUTC === 6) {
console.log(`Delivering today's WSJ number puzzle.`);
deliveries.push(
// deliverFile(`${yyyymmddET}-wsj-number.pdf`, folderId, token)
deliverFile(`${yyyymmddUTC}-wsj-number.pdf`, folderId, token)
);
console.log(`Delivering today's WSJ variety puzzle.`);
deliveries.push(
// deliverFile(`${yyyymmddET}-wsj-variety.pdf`, folderId, token)
deliverFile(`${yyyymmddUTC}-wsj-variety.pdf`, folderId, token)
);
}
}
await Promise.all(deliveries);
})();