-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathcrosspost.mjs
More file actions
executable file
·459 lines (398 loc) · 12.2 KB
/
crosspost.mjs
File metadata and controls
executable file
·459 lines (398 loc) · 12.2 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#!/usr/bin/env node
/* eslint-disable no-console */
/* global AbortSignal, fetch, process */
import {existsSync, mkdirSync, readFileSync, writeFileSync} from 'node:fs';
import {createInterface} from 'node:readline/promises';
import {basename, resolve} from 'node:path';
import {
Client,
BlueskyStrategy,
DiscordWebhookStrategy,
MastodonStrategy,
} from '@humanwhocodes/crosspost';
import * as p from '@clack/prompts';
import {parse, stringify} from 'yaml';
const URL_RE = /https?:\/\/[^\s<>)"']+/g;
function extractUrls(message) {
return [...new Set(message.match(URL_RE) ?? [])];
}
async function fetchOgData(url) {
const res = await fetch(url, {
headers: {'User-Agent': 'crosspost-bot/1.0'},
redirect: 'follow',
signal: AbortSignal.timeout(10000),
});
const html = await res.text();
const og = name => {
const m =
html.match(
new RegExp(`<meta[^>]+property=["']og:${name}["'][^>]+content=["']([^"']+)["']`, 'i')
) ??
html.match(
new RegExp(`<meta[^>]+content=["']([^"']+)["'][^>]+property=["']og:${name}["']`, 'i')
);
return m?.[1] ?? null;
};
return {
uri: url,
title: og('title') ?? '',
description: og('description') ?? '',
thumb_url: og('image') ?? null,
};
}
const dir = process.argv[2] || '.crosspost';
const baseDir = resolve(dir);
const postsPath = resolve(baseDir, 'posts.yaml');
function loadPosts() {
if (!existsSync(postsPath)) return [];
const raw = readFileSync(postsPath, 'utf-8');
const data = parse(raw);
return Array.isArray(data) ? data : [];
}
function savePosts(posts) {
mkdirSync(baseDir, {recursive: true});
writeFileSync(postsPath, stringify(posts));
}
// --- post command ---
async function runPost() {
const posts = loadPosts();
if (posts.length === 0) {
console.log('No posts found.');
return;
}
const allStrategies = [];
if (process.env.BLUESKY_IDENTIFIER && process.env.BLUESKY_PASSWORD) {
allStrategies.push(
new BlueskyStrategy({
identifier: process.env.BLUESKY_IDENTIFIER,
password: process.env.BLUESKY_PASSWORD,
host: process.env.BLUESKY_HOST || 'bsky.social',
})
);
}
if (process.env.DISCORD_WEBHOOK_URL) {
allStrategies.push(
new DiscordWebhookStrategy({
webhookUrl: process.env.DISCORD_WEBHOOK_URL,
})
);
}
if (process.env.MASTODON_ACCESS_TOKEN && process.env.MASTODON_HOST) {
allStrategies.push(
new MastodonStrategy({
accessToken: process.env.MASTODON_ACCESS_TOKEN,
host: process.env.MASTODON_HOST,
})
);
}
if (allStrategies.length === 0) {
console.error('No services configured. Set env vars for Bluesky and/or Mastodon.');
return;
}
const serviceIds = allStrategies.map(s => s.id);
function needsPosting(post) {
if (!post.posted) return true;
return serviceIds.some(id => !post.posted[id]);
}
const pending = posts.filter(needsPosting);
if (pending.length === 0) {
console.log('Nothing to post.');
return;
}
let modified = false;
for (const post of pending) {
const alreadyPosted = post.posted ?? {};
const strategies = allStrategies.filter(s => !alreadyPosted[s.id]);
const client = new Client({strategies});
console.log(`Posting: ${post.message.slice(0, 60)}...`);
try {
const opts = {};
if (post.image) {
if (!existsSync(post.image)) {
console.error(` Image not found: ${post.image}`);
continue;
}
opts.images = [
{
data: new Uint8Array(readFileSync(post.image)),
alt: post.image_alt ?? basename(post.image),
},
];
} else if (post.card) {
const {card} = post;
const cardPreview = {
uri: card.uri,
title: card.title,
description: card.description,
};
if (card.thumb_url) {
try {
const thumbRes = await fetch(card.thumb_url, {
signal: AbortSignal.timeout(10000),
});
cardPreview.thumb = new Uint8Array(await thumbRes.arrayBuffer());
} catch (err) {
console.error(` Warning: could not fetch thumbnail: ${err.message}`);
}
}
opts.cardPreview = cardPreview;
}
const results = await client.post(post.message, opts);
if (!post.posted) post.posted = {};
for (let i = 0; i < results.length; i++) {
const result = results[i];
if (result.ok) {
post.posted[strategies[i].id] = {url: result.url ?? null};
console.log(` ✓ ${result.name}: ${result.url ?? '(no url)'}`);
} else {
console.error(` ✗ ${result.name}: ${result.reason.message}`);
}
}
modified = true;
} catch (err) {
console.error(` Error: ${err.message}`);
}
}
if (modified) {
savePosts(posts);
console.log(`Updated ${postsPath}`);
}
}
// --- add command (TUI) ---
function statusLabel(post) {
if (!post.posted) return 'draft';
const services = Object.keys(post.posted);
return `posted → ${services.join(', ')}`;
}
function truncate(str, len = 60) {
const oneLine = str.replace(/\n/g, ' ');
return oneLine.length > len ? oneLine.slice(0, len) + '…' : oneLine;
}
function postLabel(post) {
let label = truncate(post.message);
if (post.image) label += ' 📎';
if (post.card) label += ' 🔗';
return label;
}
async function readMultiline(prompt) {
const rl = createInterface({
input: process.stdin,
output: process.stdout,
});
console.log(`${prompt} (two empty lines to finish)`);
const lines = [];
let consecutiveEmpty = 0;
while (true) {
const line = await rl.question(' │ ');
if (line === '') {
consecutiveEmpty++;
if (consecutiveEmpty >= 2) break;
} else {
consecutiveEmpty = 0;
}
lines.push(line);
}
rl.close();
return lines.join('\n').trim();
}
function unescapePath(str) {
// Remove shell line continuations (\<newline>) and any stray newlines/carriage returns,
// then unescape remaining backslash sequences (e.g. '\ ' -> ' ').
return str
.replace(/\\\r?\n/g, '')
.replace(/[\r\n]/g, '')
.replace(/\\(.)/g, '$1')
.trim();
}
async function promptImage() {
const addImage = await p.confirm({message: 'Attach an image?', initialValue: false});
if (p.isCancel(addImage) || !addImage) return null;
const rawImage = await p.text({
message: 'Image path (absolute)',
placeholder: '/path/to/photo.png',
validate(value) {
if (!value) return 'Path is required';
console.log('raw:', JSON.stringify(value));
const unescaped = unescapePath(value);
console.log('unescaped:', JSON.stringify(unescaped));
if (!existsSync(unescaped)) return `File not found: ${unescaped}`;
},
});
if (p.isCancel(rawImage)) return null;
const image = unescapePath(rawImage);
const imageAlt = await p.text({
message: 'Alt text',
placeholder: basename(image),
});
if (p.isCancel(imageAlt)) return null;
return {image, image_alt: imageAlt || basename(image)};
}
async function promptCard(message) {
const urls = extractUrls(message);
if (urls.length === 0) return null;
const options = [
{value: 'none', label: 'None'},
...urls.map(u => ({value: u, label: truncate(u, 70)})),
];
const chosen = await p.select({
message: 'Generate an embed card for which link?',
options,
});
if (p.isCancel(chosen) || chosen === 'none') return null;
const s = p.spinner();
s.start(`Fetching Open Graph data from ${chosen}`);
try {
const card = await fetchOgData(chosen);
s.stop(`Card: ${card.title || '(no title)'}`);
if (!card.title && !card.description) {
p.log.warn('No Open Graph metadata found for this URL.');
return null;
}
const thumbLine = card.thumb_url ? `\n🖼 ${card.thumb_url}` : '';
p.note(`${card.title}\n${card.description}${thumbLine}`, 'Embed card');
return card;
} catch (err) {
s.stop('Failed to fetch');
p.log.warn(`Could not fetch card data: ${err.message}`);
return null;
}
}
async function addPost(posts) {
const message = await readMultiline(p.log.step('Compose your post') ?? '');
if (!message) {
p.log.warn('Empty message, skipping.');
return;
}
const imageInfo = await promptImage();
const card = !imageInfo ? await promptCard(message) : null;
let preview = message;
if (imageInfo) preview += `\n\n📎 ${imageInfo.image} (${imageInfo.image_alt})`;
if (card) preview += `\n\n🔗 ${card.title}`;
p.note(preview, 'Preview');
const ok = await p.confirm({message: 'Add this post?'});
if (p.isCancel(ok) || !ok) {
p.log.info('Discarded.');
return;
}
const entry = {message};
if (imageInfo) {
entry.image = imageInfo.image;
entry.image_alt = imageInfo.image_alt;
}
if (card) {
entry.card = card;
}
posts.push(entry);
savePosts(posts);
p.log.success('Post added.');
}
async function listPosts(posts) {
if (posts.length === 0) {
p.log.info('No posts yet.');
return;
}
const summary = posts
.map((post, i) => {
const status = statusLabel(post);
return ` ${i + 1}. [${status}] ${postLabel(post)}`;
})
.join('\n');
p.note(summary, `${posts.length} post(s)`);
}
async function editPost(posts) {
if (posts.length === 0) {
p.log.info('No posts to edit.');
return;
}
const drafts = posts.map((post, i) => ({post, i})).filter(({post}) => !post.posted);
if (drafts.length === 0) {
p.log.info('No draft posts to edit.');
return;
}
const idx = await p.select({
message: 'Which post to edit?',
options: drafts.map(({post, i}) => ({
value: i,
label: postLabel(post),
})),
});
if (p.isCancel(idx)) return;
const message = await readMultiline(p.log.step('New message (replaces current)') ?? '');
if (!message) {
p.log.warn('Empty message, keeping original.');
return;
}
const imageInfo = await promptImage();
const card = !imageInfo ? await promptCard(message) : null;
let preview = message;
if (imageInfo) preview += `\n\n📎 ${imageInfo.image} (${imageInfo.image_alt})`;
if (card) preview += `\n\n🔗 ${card.title}`;
p.note(preview, 'Preview');
const ok = await p.confirm({message: 'Save changes?'});
if (p.isCancel(ok) || !ok) return;
posts[idx].message = message;
if (imageInfo) {
posts[idx].image = imageInfo.image;
posts[idx].image_alt = imageInfo.image_alt;
}
if (card) {
posts[idx].card = card;
}
savePosts(posts);
p.log.success('Post updated.');
}
async function deletePost(posts) {
if (posts.length === 0) {
p.log.info('No posts to delete.');
return;
}
const drafts = posts.map((post, i) => ({post, i})).filter(({post}) => !post.posted);
if (drafts.length === 0) {
p.log.info('No draft posts to delete.');
return;
}
const idx = await p.select({
message: 'Which post to delete?',
options: drafts.map(({post, i}) => ({
value: i,
label: postLabel(post),
})),
});
if (p.isCancel(idx)) return;
p.note(posts[idx].message, 'This post will be deleted');
const ok = await p.confirm({message: 'Are you sure?'});
if (p.isCancel(ok) || !ok) return;
posts.splice(idx, 1);
savePosts(posts);
p.log.success('Post deleted.');
}
async function runAdd() {
p.intro('crosspost — manage posts');
const posts = loadPosts();
while (true) {
const drafts = posts.filter(p => !p.posted).length;
const posted = posts.length - drafts;
const action = await p.select({
message: `${posts.length} post(s): ${drafts} draft, ${posted} posted`,
options: [
{value: 'add', label: 'Add a new post'},
{value: 'list', label: 'List all posts'},
{value: 'edit', label: 'Edit a draft'},
{value: 'delete', label: 'Delete a draft'},
{value: 'post', label: 'Post all drafts'},
{value: 'quit', label: 'Quit'},
],
});
if (p.isCancel(action) || action === 'quit') {
p.outro('Done.');
break;
}
if (action === 'add') await addPost(posts);
else if (action === 'list') await listPosts(posts);
else if (action === 'edit') await editPost(posts);
else if (action === 'delete') await deletePost(posts);
else if (action === 'post') await runPost();
}
}
// --- main ---
runAdd();