]> git.r.bdr.sh - rbdr/linkding-linkblog-updater/blame_incremental - index.js
Make some of the values more generic
[rbdr/linkding-linkblog-updater] / index.js
... / ...
CommitLineData
1const { exec } = require('child_process');
2const { resolve, join} = require('path');
3const { rm, writeFile } = require('fs/promises');
4const { promisify } = require('util');
5const Pinboard = require('node-pinboard').default;
6
7const internals = {
8 exec: promisify(exec),
9
10 apiToken: process.env.PINBOARD_TOKEN,
11 blogUrl: process.env.BLOG_URL,
12 archiveUrl: process.env.ARCHIVE_URL,
13 blogPublicUrl: process.env.BLOG_PUBLIC_URL,
14 archivePublicUrl: process.env.ARCHIVE_PUBLIC_URL,
15 tootToken: process.env.TOOT_TOKEN,
16 mastodonDomain: process.env.MASTODON_DOMAIN,
17
18 date: (new Date()).toISOString().split('T')[0],
19
20 generateGemtext(title, text) {
21
22 return `# ${title}\n\n${text}`;
23 },
24
25 getText(posts) {
26
27 return posts.map((post) => {
28
29 return `=> ${post.href} ${post.description}\n${post.extended}`;
30 }).join('\n\n');
31 },
32
33 getTitle(posts) {
34
35 if (posts.length === 1) {
36 return `Link: ${posts[0].description}`;
37 }
38 return `${posts.length} links for ${internals.date}`;
39 },
40
41 slugify(text) {
42
43 return text.toLowerCase().replace(/[^a-z0-9 ]/g, '').replace(/ +/g, '-')
44 },
45
46 async toot(title) {
47
48 const body = new FormData();
49 body.set(
50 'status',
51 `New post: ${title}\n\nAvailable on:\n\nā™Šļø the gemini archive ${internals.archivePublicUrl}\n\n or, the ephemeral blog šŸŒ: ${internals.blogPublicUrl}`
52 );
53 return fetch(`https://${internals.mastodonDomain}/api/v1/statuses`, {
54 method: 'post',
55 headers: {
56 Authorization: `Bearer ${internals.tootToken}`,
57 },
58 body
59 });
60 }
61
62};
63
64
65async function run() {
66 const pinboard = new Pinboard(internals.apiToken);
67 const getPins = promisify(pinboard.all);
68 const addPin = promisify(pinboard.add);
69
70 const pins = await getPins({ tag: 'linkblog' });
71
72 if (pins.length === 0) {
73 console.error('No links to post');
74 return;
75 }
76
77 const title = internals.getTitle(pins);
78 const text = internals.getText(pins);
79 const gemtext = internals.generateGemtext(title, text);
80 const filename = internals.slugify(title);
81
82 const gemfile = resolve(join(__dirname, `${filename}.gmi`));
83 await writeFile(gemfile, gemtext);
84 await internals.exec(`blog --add ${gemfile}`);
85 await internals.exec(`blog --publish ${internals.blogUrl}`);
86 await internals.exec(`blog --publish-archive ${internals.archiveUrl}`);
87 await rm(gemfile);
88
89 for (const pin of pins) {
90 await addPin({
91 url: pin.href,
92 description: pin.description,
93 extended: pin.extended,
94 dt: pin.time,
95 tags: pin.tags.replace('linkblog', 'linkblog-posted')
96 });
97 }
98
99 if (internals.tootToken) {
100 await internals.toot(title);
101 }
102}
103
104run()
105 .then(() => process.exit(0))
106 .catch((err) => {
107 console.error(err);
108 process.exit(1);
109 })