Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
102 lines (96 loc) · 2.96 KB
/
Copy pathmain.js
File metadata and controls
102 lines (96 loc) · 2.96 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
const dropbox = require('dropbox');
const https = require('https');
const moment = require('moment');
const path = require('path');
const process = require('process');
const dbx = new dropbox.Dropbox({
clientId: process.env.DROPBOX_APP_KEY,
clientSecret: process.env.DROPBOX_APP_SECRET,
refreshToken: process.env.DROPBOX_REFRESH_TOKEN,
});
function getNYTC(date) {
return new Promise((resolve, reject) => {
const req = https.request({
protocol: 'https:',
host: 'www.nytimes.com',
path: `/svc/crosswords/v2/puzzle/print/${moment(date).format('MMMDDYY')}.pdf`,
method: 'GET',
headers: {
Referer: 'https://www.nytimes.com/crosswords/archive/daily',
Cookie: process.env.NYT_COOKIE,
},
}, (res) => {
if (res.statusCode === 200) {
const data = [];
res.on('error', (err) => {
reject(err);
});
res.on('data', (chunk) => {
data.push(chunk);
});
res.on('end', () => {
resolve(Buffer.concat(data));
});
} else {
reject(res.statusCode);
}
});
req.on('error', (err) => {
reject(err);
});
req.end();
});
}
async function nytc(date) {
console.log(`Checking ${moment(date).format('YYYY-MM-DD')}'s crossword.`);
try {
await getNYTC(date);
console.log(`Successfully checked ${moment(date).format('YYYY-MM-DD')}'s crossword.`);
} catch (error) {
console.log(`NYT_COOKIE likely expired. Error: ${error}`);
process.exit(1);
}
date.setDate(date.getDate() + 1);
console.log(`Downloading ${moment(date).format('YYYY-MM-DD')}'s crossword.`);
data = undefined;
try {
data = await getNYTC(date);
console.log(`Successfully downloaded ${moment(date).format('YYYY-MM-DD')}'s crossword.`);
} catch (error) {
console.log(`${moment(date).format('YYYY-MM-DD')}'s crossword is not yet released.`);
return;
}
console.log(`Checking if file exists.`);
try {
await dbx.filesGetMetadata({
path: path.join(process.env.DROPBOX_NYTC_PATH, `${moment(date).format('YYYY-MM-DD-ddd')}-crossword.pdf`),
});
console.log(`File already uploaded.`);
return;
} catch (error) {
console.log(`File not yet uploaded.`);
}
console.log(`Uploading file.`);
try {
response = await dbx.filesUpload({
path: path.join(process.env.DROPBOX_NYTC_PATH, `${moment(date).format('YYYY-MM-DD-ddd')}-crossword.pdf`),
contents: data,
});
console.log(`Successfully uploaded ${response.result.content_hash}.`);
return;
} catch (error) {
console.log(`DROPBOX_REFRESH_TOKEN likely invalid. Error: ${error}`);
process.exit(1);
}
}
async function download(date) {
await nytc(new Date(date.getTime()));
}
async function main() {
const date = new Date((new Date()).toLocaleString('en-US', { timeZone: 'America/New_York' }));
for (let i = 0; i < 14; i++) {
await download(date);
date.setDate(date.getDate() - 1);
}
}
main().then(() => process.exit(0));