]> git.r.bdr.sh - rbdr/linkding-linkblog-updater/blame - index.js
Add publishing
[rbdr/linkding-linkblog-updater] / index.js
CommitLineData
89326417
RBR
1const { exec } = require('child_process');
2const { resolve, join} = require('path');
3const { rm, writeFile } = require('fs/promises');
2390d230 4const { promisify } = require('util');
89326417
RBR
5const Pinboard = require('node-pinboard').default;
6
7const internals = {
8 exec: promisify(exec),
2390d230 9
89326417 10 apiToken: process.env.PINBOARD_TOKEN,
2390d230
RBR
11 blogUrl: process.env.BLOG_URL,
12 archiveUrl: process.env.ARCHIVE_URL,
13
89326417
RBR
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
29async function run() {
30 const pinboard = new Pinboard(internals.apiToken);
31 const getPins = promisify(pinboard.get);
32 const addPin = promisify(pinboard.add);
33
89326417
RBR
34 const pins = await getPins({ tag: 'linkblog' });
35
89326417
RBR
36 if (pins.posts.length === 0) {
37 console.error('No links to post');
38 return;
39 }
40
89326417
RBR
41 const gemtext = internals.generateGemtext(pins.posts);
42 const gemfile = resolve(join(__dirname, `linkblog-${internals.date}.gmi`));
89326417 43 await writeFile(gemfile, gemtext);
2390d230
RBR
44 await internals.exec(`blog --add ${gemfile}`);
45 await internals.exec(`blog --publish ${internals.blogUrl}`);
46 await internals.exec(`blog --publish-archive ${internals.archiveUrl}`);
89326417
RBR
47 await rm(gemfile);
48
49 for (const pin of pins.posts) {
89326417
RBR
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
60run()
61 .then(() => process.exit(0))
62 .catch((err) => {
63 console.error(err);
64 process.exit(1);
65 })