-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
103 lines (76 loc) · 2.66 KB
/
Copy pathindex.js
File metadata and controls
103 lines (76 loc) · 2.66 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
103
require("dotenv").config();
const config = {
backupFolder: "backups"
};
const AWS = require("aws-sdk");
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_ACCESS_KEY_SECRET
});
const fs = require("fs");
const glob = require("glob");
const puppeteer = require("puppeteer");
const generateExport = async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
try {
await page._client.send("Page.setDownloadBehavior", {
behavior: "allow",
downloadPath: process.cwd()
});
await page.goto(process.env.SUBSTACK_URL);
console.log("Logging into Substack...");
await page.$eval(".login-button", el => el.click());
await page.waitFor(5000);
await page.$eval(".login-option a", el => el.click());
await page.focus('[name="email"]');
await page.keyboard.type(process.env.SUBSTACK_EMAIL);
await page.focus('[name="password"]');
await page.keyboard.type(process.env.SUBSTACK_PASSWORD);
await page.$eval("[type='submit']", el => el.click());
await page.waitFor(5000);
console.log("Logged in!");
await page.$eval("#subscribers", el => el.click());
// the Substack export link weirdly opens in a new tab,
// which breaks Puppeteers ability to download it directly
// so had to use this hack: https://github.com/puppeteer/puppeteer/issues/299#issuecomment-508545356
// sad to report it took me 3 hours to figure this out... -_-
await page.$eval('.stat-export', el => el.target = '');
await page.click('.stat-export');
console.log("Downloading subscribers...");
await page.waitFor(10000);
console.log("Done!")
await page.screenshot({ path: "success.png" });
} catch (err) {
console.error("Something went wrong!");
console.error(err);
await page.screenshot({ path: "error.png" });
}
await browser.close();
};
const uploadToS3 = async filename => {
try {
const fileContent = fs.readFileSync(filename);
const params = {
Bucket: process.env.AWS_BUCKET_NAME,
Key: `${config.backupFolder}/${filename}`,
Body: fileContent
};
const data = await s3.upload(params).promise();
console.log(`Successfully backed up Substack data to S3: ${data.Location}`);
} catch (err) {
console.error("Something went wrong while uploading to S3");
console.error(err);
}
};
const main = async function() {
await generateExport();
const files = glob.sync("*.csv");
const filename = files[0];
if (!filename) {
throw new Error("Couldn't find a file to upload, aborting");
}
console.log(`Uploading ${filename} to S3`);
await uploadToS3(filename);
};
main();