-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.js
168 lines (160 loc) · 4.81 KB
/
main.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
158
159
160
161
162
163
164
165
166
167
168
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({
accessToken: process.env.DROPBOX_ACCESS_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_ACCESS_TOKEN likely expired. Error: ${error}`);
process.exit(1);
}
}
function getWSJC(date) {
return new Promise((resolve, reject) => {
const req = https.request({
protocol: 'https:',
host: 's.wsj.net',
path: `/public/resources/documents/${moment(date).format('[XWD]MMDDYYYY')}.pdf`,
method: 'GET',
}, (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 wsjc(date) {
date.setDate(date.getDate() + 1);
console.log(`Downloading ${moment(date).format('YYYY-MM-DD')}'s crossword.`);
data = undefined;
try {
data = await getWSJC(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_WSJC_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_WSJC_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_ACCESS_TOKEN likely expired. Error: ${error}`);
process.exit(1);
}
}
async function download(date) {
console.log(`NYTC Block`);
await nytc(new Date(date.getTime()));
console.log(`WSJC Block`);
await wsjc(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));