]> git.r.bdr.sh - rbdr/linkding-linkblog-updater/blob - index.js
Add the project
[rbdr/linkding-linkblog-updater] / index.js
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;
6
7 const internals = {
8 exec: promisify(exec),
9 debuglog: debuglog('blog'),
10 apiToken: process.env.PINBOARD_TOKEN,
11 date: (new Date()).toISOString().split('T')[0],
12
13 generateGemtext(posts) {
14
15 const title = posts.length === 1 ? 'A link' : `${posts.length} links`;
16
17 const linkText = posts.map((pin) => {
18 return `=> ${pin.href} ${pin.description}\n${pin.extended}`;
19 }).join('\n\n');
20
21 return `# ${title} for ${internals.date}\n\n${linkText}`;
22 }
23 };
24
25
26 async function run() {
27 const pinboard = new Pinboard(internals.apiToken);
28 const getPins = promisify(pinboard.get);
29 const addPin = promisify(pinboard.add);
30
31 internals.debuglog('Fetching Pins');
32 const pins = await getPins({ tag: 'linkblog' });
33
34 internals.debuglog(`Found ${pins.posts.length} pins`);
35 if (pins.posts.length === 0) {
36 console.error('No links to post');
37 return;
38 }
39
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}`);
49 await rm(gemfile);
50
51 for (const pin of pins.posts) {
52 internals.debuglog(`Updating ${pin.href}`);
53 await addPin({
54 url: pin.href,
55 description: pin.description,
56 extended: pin.extended,
57 dt: pin.time,
58 tags: pin.tags.replace('linkblog', 'linkblog-posted')
59 });
60 }
61 }
62
63 run()
64 .then(() => process.exit(0))
65 .catch((err) => {
66 console.error(err);
67 process.exit(1);
68 })