]>
git.r.bdr.sh - rbdr/linkding-linkblog-updater/blob - index.js
279594aa6403152f991cff9cbf09404ab7d625f4
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
);
68 const pins
= await pinboard
.all({ tag: 'linkblog' });
70 if (pins
.length
=== 0) {
71 console
.error('No links to post');
75 const title
= internals
.getTitle(pins
);
76 const text
= internals
.getText(pins
);
77 const gemtext
= internals
.generateGemtext(title
, text
);
78 const filename
= internals
.slugify(title
);
80 const gemfile
= resolve(join(__dirname
, `${filename}.gmi`));
81 await
writeFile(gemfile
, gemtext
);
82 await internals
.exec(`blog add ${gemfile}`);
83 await internals
.exec(`blog publish ${internals.blogUrl}`);
84 await internals
.exec(`blog publish-archive ${internals.archiveUrl}`);
87 for (const pin
of pins
) {
90 description: pin
.description
,
91 extended: pin
.extended
,
93 tags: pin
.tags
.replace('linkblog', 'linkblog-posted')
97 if (internals
.tootToken
) {
98 await internals
.toot(title
);
103 .then(() => process
.exit(0))