]>
git.r.bdr.sh - rbdr/linkding-linkblog-updater/blob - index.js
78c3d267e72ad10776b3389122ed0c66149b73ae
1 const { exec
} = require('child_process');
2 const { resolve
, join
} = require('path');
3 const { rm
, writeFile
} = require('fs/promises');
4 const { debuglog
, promisify
} = require('util');
5 const Pinboard
= require('node-pinboard').default;
9 debuglog: debuglog('blog'),
10 apiToken: process
.env
.PINBOARD_TOKEN
,
11 date: (new Date()).toISOString().split('T')[0],
13 generateGemtext(posts
) {
15 const title
= posts
.length
=== 1 ? 'A link' : `${posts.length} links`;
17 const linkText
= posts
.map((pin
) => {
18 return `=> ${pin.href} ${pin.description}\n${pin.extended}`;
21 return `# ${title} for ${internals.date}\n\n${linkText}`;
26 async
function run() {
27 const pinboard
= new Pinboard(internals
.apiToken
);
28 const getPins
= promisify(pinboard
.get);
29 const addPin
= promisify(pinboard
.add
);
31 internals
.debuglog('Fetching Pins');
32 const pins
= await
getPins({ tag: 'linkblog' });
34 internals
.debuglog(`Found ${pins.posts.length} pins`);
35 if (pins
.posts
.length
=== 0) {
36 console
.error('No links to post');
40 internals
.debuglog('Generating gemtext');
41 const gemtext
= internals
.generateGemtext(pins
.posts
);
42 const gemfile
= resolve(join(__dirname
, `linkblog-${internals.date}.gmi`));
43 internals
.debuglog(`Writing to ${gemfile}`);
44 await
writeFile(gemfile
, gemtext
);
45 internals
.debuglog('Adding gemfile to log');
46 const output
= await internals
.exec(`blog --add ${gemfile}`);
47 internals
.debuglog(output
.stderr
);
48 internals
.debuglog(`Removing ${gemfile}`);
51 for (const pin
of pins
.posts
) {
52 internals
.debuglog(`Updating ${pin.href}`);
55 description: pin
.description
,
56 extended: pin
.extended
,
58 tags: pin
.tags
.replace('linkblog', 'linkblog-posted')
64 .then(() => process
.exit(0))