]> git.r.bdr.sh - rbdr/linkding-linkblog-updater/blob - index.js
Add publishing
[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 { promisify } = require('util');
5 const Pinboard = require('node-pinboard').default;
6
7 const 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
14 date: (new Date()).toISOString().split('T')[0],
15
16 generateGemtext(posts) {
17
18 const title = posts.length === 1 ? 'A link' : `${posts.length} links`;
19
20 const linkText = posts.map((pin) => {
21 return `=> ${pin.href} ${pin.description}\n${pin.extended}`;
22 }).join('\n\n');
23
24 return `# ${title} for ${internals.date}\n\n${linkText}`;
25 }
26 };
27
28
29 async function run() {
30 const pinboard = new Pinboard(internals.apiToken);
31 const getPins = promisify(pinboard.get);
32 const addPin = promisify(pinboard.add);
33
34 const pins = await getPins({ tag: 'linkblog' });
35
36 if (pins.posts.length === 0) {
37 console.error('No links to post');
38 return;
39 }
40
41 const gemtext = internals.generateGemtext(pins.posts);
42 const gemfile = resolve(join(__dirname, `linkblog-${internals.date}.gmi`));
43 await writeFile(gemfile, gemtext);
44 await internals.exec(`blog --add ${gemfile}`);
45 await internals.exec(`blog --publish ${internals.blogUrl}`);
46 await internals.exec(`blog --publish-archive ${internals.archiveUrl}`);
47 await rm(gemfile);
48
49 for (const pin of pins.posts) {
50 await addPin({
51 url: pin.href,
52 description: pin.description,
53 extended: pin.extended,
54 dt: pin.time,
55 tags: pin.tags.replace('linkblog', 'linkblog-posted')
56 });
57 }
58 }
59
60 run()
61 .then(() => process.exit(0))
62 .catch((err) => {
63 console.error(err);
64 process.exit(1);
65 })