-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtoot_screenshot.js
More file actions
executable file
·104 lines (93 loc) · 4 KB
/
toot_screenshot.js
File metadata and controls
executable file
·104 lines (93 loc) · 4 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
104
const puppeteer = require('puppeteer');
const fs = require("fs");
const fsPromises = require('fs').promises;
const { htmlToText } = require('html-to-text');
let tooturl = process.argv[2];
if (!tooturl) tooturl = "mastodon.social/@johnmu/109292050061122141";
if (!tooturl.startsWith("https://")) tooturl = "https://" + tooturl;
let outfile = process.argv[3];
if (!outfile) outfile = "screenshot.png";
let filenames = [], filesizes = [];
for (let i=0; i<5; i++) { filenames.push("image-" + i + ".png")};
(async() => {
let browser;
try {
browser = await puppeteer.launch();
const page = await browser.newPage();
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
await page.setViewport({
width: 1024,
height: 2048,
deviceScaleFactor: 1.0,
});
page
.on('console', message => console.log(`${message.type().substr(0, 3).toUpperCase()} ${message.text()}`))
.on('pageerror', ({ message }) => console.log(message))
.on('requestfailed', request => console.log(`${request.failure().errorText} ${request.url()}`))
//.on('response', response =>
// console.log(`${response.status()} ${response.url()}`))
var contentHtml = fs.readFileSync('toot_local_embed.html', 'utf8');
contentHtml = contentHtml.replace("https://mastodon.social/@johnmu/109292050061122141", tooturl)
contentHtml = contentHtml.replace('%%TOOTID%%', tooturl)
await page.setContent(contentHtml);
try {
await page.waitForSelector('#content_ready' , { timeout: 20000 });
} catch (err) {
console.log("Timed out waiting; continuing");
}
const elem = await page.$('#bounding');
const boundingBox = await elem.boundingBox();
await page.setViewport({
width: parseInt(boundingBox.width + boundingBox.x),
height: parseInt(boundingBox.height + boundingBox.y),
deviceScaleFactor: 1.0,
});
// get maximum sized screenshot -- they fail randomly
for (const fn of filenames) {
console.log("Saving ", fn);
await page.screenshot({path: fn});
const stats = await fsPromises.stat(fn);
filesizes.push(stats.size);
};
let i = filesizes.indexOf(Math.max(...filesizes));
await fsPromises.copyFile(filenames[i], outfile);
console.log("Saved to ", outfile);
for (const fn of filenames) {
console.log("Removing ", fn);
await fsPromises.unlink(fn);
}
// now fetch contents
const url_parts = tooturl.split("/");
const just_id = url_parts[url_parts.length-1];
const api_url = [url_parts[0], url_parts[1], url_parts[2]].join("/") + "/api/v1/statuses/" + just_id;
const res = await fetch(api_url);
const res_json = await res.json();
if (res_json["id"]) {
const user_name = res_json["account"]["display_name"];
const post_html = res_json["content"];
const post_text = htmlToText(post_html, {
wordwrap: false,
selectors: [ { selector: 'a', options: { ignoreHref: true } },
{ selector: 'img', format: 'skip' }] });
let text_version = user_name + " posted: " + post_text;
if (text_version.length>204) text_version = text_version.substring(0, 200) + " ...";
text_version = text_version.replaceAll('"', "'"); // since we wrap it in alt
text_version = text_version.replaceAll('<', " "); // to be safe
text_version = text_version.replaceAll('&', " "); // to be safe
text_version = text_version.replaceAll('%', " "); // to be paranoid
await fsPromises.writeFile(outfile + ".txt", text_version);
console.log("Wrote caption to text file: " + text_version);
} else {
let text_version = "Unknown post.";
await fsPromises.writeFile(outfile + ".txt", text_version);
console.log("Wrote empty caption to text file...");
}
} catch (error) {
console.error("Error generating screenshot:", error.message);
process.exit(1);
} finally {
if (browser) {
await browser.close();
}
}
})();