]>
git.r.bdr.sh - rbdr/linkding-linkblog-updater/blob - index.js
518cc79b83c44610a609e824d7c7da1d9d48a30a
1 const { exec
} = require('child_process');
2 const { resolve
, join
} = require('path');
3 const { rm
, writeFile
} = require('fs/promises');
4 const { promisify
} = require('util');
5 const Pinboard
= require('node-pinboard').default;
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
,
18 date: (new Date()).toISOString().split('T')[0],
20 generateGemtext(title
, text
) {
22 return `# ${title}\n\n${text}`;
27 return posts
.map((post
) => {
29 return `=> ${post.href} ${post.description}\n${post.extended}`;
35 if (posts
.length
=== 1) {
36 return `Link: ${posts[0].description}`;
38 return `${posts.length} links for ${internals.date}`;
43 return text
.toLowerCase().replace(/[^a
-z0
-9 ]/g
, '').replace(/ +/g
, '-')
48 const body
= new FormData();
51 `New post: ${title}\n\nAvailable on:\n\n♊️ the gemini archive ${internals.archivePublicUrl}\n\n or, the ephemeral blog 🌐: ${internals.blogPublicUrl}`
53 return fetch(`https://${internals.mastodonDomain}/api/v1/statuses`, {
56 Authorization: `Bearer ${internals.tootToken}`,
65 async
function run() {
66 const pinboard
= new Pinboard(internals
.apiToken
);
67 const getPins
= promisify(pinboard
.all
);
68 const addPin
= promisify(pinboard
.add
);
70 const pins
= await
getPins({ tag: 'linkblog' });
72 if (pins
.length
=== 0) {
73 console
.error('No links to post');
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
);
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}`);
89 for (const pin
of pins
) {
92 description: pin
.description
,
93 extended: pin
.extended
,
95 tags: pin
.tags
.replace('linkblog', 'linkblog-posted')
99 if (internals
.tootToken
) {
100 await internals
.toot(title
);
105 .then(() => process
.exit(0))