-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbot.js
113 lines (88 loc) · 2.52 KB
/
bot.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
var _ = require('lodash');
var fs = require('fs');
var moment = require('moment');
var conf = JSON.parse(fs.readFileSync('conf.json'));
const Masto = require('masto');
var pages = require('./pages.js');
var renderer = require('./renderer.js');
const DRY_RUN = (process.env.DRY_RUN === 'true');
// send the page to mastodon
var tootPage = async function(p, dest) {
if ( DRY_RUN ) {
console.log("Dry run, exiting tootPage!");
return;
}
const masto = await Masto.login({
url: 'https://botsin.space',
accessToken: conf.mastodon.access_token,
});
var url = "https://web.archive.org/web/" + p.tstamp + "/" + p.url;
var title = p.title;
var date = moment(p.tstamp, "YYYYMMDDHHmmss").format("MMM YYYY");
var tweetText = title + "\n" + date + "\n";
if ( typeof(p.generator) !== "undefined" &&
p.generator !== "" )
{
tweetText = tweetText + p.generator + "\n";
}
tweetText = tweetText + url;
var oldweb_url = "http://oldweb.today/random/" + p.tstamp + "/" + p.url;
tweetText = tweetText + "\n" + oldweb_url;
const attachment = await masto.mediaAttachments.create({
file: fs.createReadStream(dest),
description: `A screengrab of ${p.url} from ${date}`,
focus: {
x: 0.0,
y: 1.0
}
});
const status = await masto.statuses.create({
status: tweetText,
visibility: 'unlisted',
mediaIds: [attachment.id],
});
console.log(status);
};
var renderPage = async function(p) {
try {
// hacky fun to pass along an option which will wrap the output in a frame
p.wrap = true;
const dest = await renderer.render(p);
if ( typeof(dest) === "undefined" ) {
console.log("well, better luck next time i guess");
}
else {
try {
await Promise.all([
tootPage(p, dest),
]);
}
catch(e) {
console.log("*****", e);
}
process.exit();
}
} catch(error) {
console.log(error);
}
};
// you can test functionality with a little snippet like this
(async () => {
await renderPage({
id: 1318,
url: 'http://www.insurance-life.com/',
tstamp: '19961221011029',
// url: 'http://www.mcs.com/~nr706/home.html',
// tstamp: '19970521165416',
title: 'Thomas Keith & Associates',
generator: '',
score: 84,
created_at: "Wed Oct 14 2015 22:24:43 GMT+0000 (UTC)",
posted_at: null
}, true);
})();
// // make sure we exit at some point
// setTimeout(process.exit, 120 * 1000);
// (async () => {
// pages.getAndMarkRandom(renderPage);
// })();